Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - mftrace.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.11.0 lcov report (development 22860-5579deb0b) Lines: 7109 7275 97.7 %
Date: 2018-07-18 05:36:42 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       27405 : mkgNK(GEN N, GEN k, GEN CHI, GEN P) { return mkvec4(N, k, CHI, P); }
      77             : static GEN
      78       12257 : mkNK(long N, long k, GEN CHI) { return mkgNK(stoi(N), stoi(k), CHI, pol_x(1)); }
      79             : GEN
      80        6601 : MF_get_CHI(GEN mf) { return gmael(mf,1,3); }
      81             : GEN
      82       15988 : MF_get_gN(GEN mf) { return gmael(mf,1,1); }
      83             : long
      84       15302 : MF_get_N(GEN mf) { return itou(MF_get_gN(mf)); }
      85             : GEN
      86       11333 : MF_get_gk(GEN mf) { return gmael(mf,1,2); }
      87             : long
      88        5537 : MF_get_k(GEN mf)
      89             : {
      90        5537 :   GEN gk = MF_get_gk(mf);
      91        5537 :   if (typ(gk)!=t_INT) pari_err_IMPL("half-integral weight");
      92        5537 :   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       10661 : MF_get_space(GEN mf) { return itos(gmael(mf,1,4)); }
     103             : GEN
     104        3451 : MF_get_E(GEN mf) { return gel(mf,2); }
     105             : GEN
     106       16429 : 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        3409 : MF_get_dim(GEN mf)
     111             : {
     112        3409 :   switch(MF_get_space(mf))
     113             :   {
     114             :     case mf_FULL:
     115         553 :       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        2716 :       return lg(MF_get_S(mf)) - 1;
     120             :   }
     121             : }
     122             : GEN
     123        6447 : 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        5698 : MF_get_M(GEN mf) { return gmael(mf,5,3); }
     128             : GEN
     129        2247 : MF_get_Minv(GEN mf) { return gmael(mf,5,2); }
     130             : GEN
     131        7063 : 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        1288 : QabM_ker(GEN M, GEN P, long n)
     203             : {
     204             :   GEN B;
     205        1288 :   if (n <= 2)
     206         833 :     B = ZM_ker(Q_primpart(M));
     207             :   else
     208         455 :     B = ZabM_ker(Q_primpart(liftpol_shallow(M)), P, n);
     209        1288 :   return B;
     210             : }
     211             : /* pseudo-inverse of M */
     212             : static GEN
     213        1029 : QabM_pseudoinv(GEN M, GEN P, long n, GEN *pv, GEN *pden)
     214             : {
     215             :   GEN cM, Mi;
     216        1029 :   if (n <= 2)
     217             :   {
     218         826 :     M = Q_primitive_part(M, &cM);
     219         826 :     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        1029 :   *pden = mul_content(*pden, cM);
     228        1029 :   return Mi;
     229             : }
     230             : 
     231             : static GEN
     232        9331 : QabM_indexrank(GEN M, GEN P, long n)
     233             : {
     234             :   GEN z;
     235        9331 :   if (n <= 2)
     236             :   {
     237        8225 :     M = vec_Q_primpart(M);
     238        8225 :     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        9331 :   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             :   GEN fa;
     258      106106 :   if (n == 1) return 1;
     259       90874 :   av = avma; fa = myfactoru(n);
     260       90874 :   avma = av; return eulerphiu_fact(fa);
     261             : }
     262             : static long
     263       77476 : mymoebiusu(ulong n)
     264             : {
     265             :   pari_sp av;
     266             :   GEN fa;
     267       77476 :   if (n == 1) return 1;
     268       70182 :   av = avma; fa = myfactoru(n);
     269       70182 :   avma = av; return moebiusu_fact(fa);
     270             : }
     271             : 
     272             : static long
     273        2709 : mynumdivu(long N)
     274             : {
     275             :   pari_sp av;
     276             :   GEN fa;
     277        2709 :   if (N == 1) return 1;
     278        2604 :   av = avma; fa = myfactoru(N);
     279        2604 :   avma = av; return numdivu_fact(fa);
     280             : }
     281             : 
     282             : /* N\prod_{p|N} (1+1/p) */
     283             : static long
     284      264383 : mypsiu(ulong N)
     285             : {
     286      264383 :   pari_sp av = avma;
     287      264383 :   GEN P = gel(myfactoru(N), 1);
     288      264383 :   long j, l = lg(P), res = N;
     289      264383 :   for (j = 1; j < l; j++) res += res/P[j];
     290      264383 :   avma = av; return res;
     291             : }
     292             : /* write n = mf^2. Return m, set f. */
     293             : static ulong
     294         210 : mycore(ulong n, long *pf)
     295             : {
     296         210 :   pari_sp av = avma;
     297         210 :   GEN fa = myfactoru(n), P = gel(fa,1), E = gel(fa,2);
     298         210 :   long i, l = lg(P), m = 1, f = 1;
     299         850 :   for (i = 1; i < l; i++)
     300             :   {
     301         640 :     long j, p = P[i], e = E[i];
     302         640 :     if (e & 1) m *= p;
     303         640 :     for (j = 2; j <= e; j+=2) f *= p;
     304             :   }
     305         210 :   avma = av; *pf = f; return m;
     306             : }
     307             : 
     308             : /* fa = factorization of -D > 0, return -D0 > 0 (where D0 is fundamental) */
     309             : static long
     310     7553259 : corediscs_fact(GEN fa)
     311             : {
     312     7553259 :   GEN P = gel(fa,1), E = gel(fa,2);
     313     7553259 :   long i, l = lg(P), m = 1;
     314    25047428 :   for (i = 1; i < l; i++)
     315             :   {
     316    17494169 :     long p = P[i], e = E[i];
     317    17494169 :     if (e & 1) m *= p;
     318             :   }
     319     7553259 :   if ((m&3L) != 3) m <<= 2;
     320     7553259 :   return m;
     321             : }
     322             : static long
     323        6111 : mubeta(long n)
     324             : {
     325        6111 :   pari_sp av = avma;
     326        6111 :   GEN E = gel(myfactoru(n), 2);
     327        6111 :   long i, s = 1, l = lg(E);
     328       12684 :   for (i = 1; i < l; i++)
     329             :   {
     330        6573 :     long e = E[i];
     331        6573 :     if (e >= 3) { avma = av; return 0; }
     332        6573 :     if (e == 1) s *= -2;
     333             :   }
     334        6111 :   avma = av; return s;
     335             : }
     336             : 
     337             : /* n = n1*n2, n1 = ppo(n, m); return mubeta(n1)*moebiusu(n2).
     338             :  * N.B. If n from newt_params we, in fact, never return 0 */
     339             : static long
     340     4094608 : mubeta2(long n, long m)
     341             : {
     342     4094608 :   pari_sp av = avma;
     343     4094608 :   GEN fa = myfactoru(n), P = gel(fa,1), E = gel(fa,2);
     344     4094608 :   long i, s = 1, l = lg(P);
     345     8353408 :   for (i = 1; i < l; i++)
     346             :   {
     347     4258800 :     long p = P[i], e = E[i];
     348     4258800 :     if (m % p)
     349             :     { /* p^e in n1 */
     350     3392963 :       if (e >= 3) { avma = av; return 0; }
     351     3392963 :       if (e == 1) s *= -2;
     352             :     }
     353             :     else
     354             :     { /* in n2 */
     355      865837 :       if (e >= 2) { avma = av; return 0; }
     356      865837 :       s = -s;
     357             :     }
     358             :   }
     359     4094608 :   avma = av; return s;
     360             : }
     361             : 
     362             : /* write N = prod p^{ep} and n = df^2, d squarefree.
     363             :  * set g  = ppo(gcd(sqfpart(N), f), FC)
     364             :  *     N2 = prod p^if(e==1 || p|n, ep-1, ep-2) */
     365             : static void
     366      931721 : newt_params(long N, long n, long FC, long *pg, long *pN2)
     367             : {
     368      931721 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
     369      931721 :   long i, g = 1, N2 = 1, l = lg(P);
     370     2491629 :   for (i = 1; i < l; i++)
     371             :   {
     372     1559908 :     long p = P[i], e = E[i];
     373     1559908 :     if (e == 1)
     374     1319045 :     { if (FC % p && n % (p*p) == 0) g *= p; }
     375             :     else
     376      240863 :       N2 *= upowuu(p,(n % p)? e-2: e-1);
     377             :   }
     378      931721 :   *pg = g; *pN2 = N2;
     379      931721 : }
     380             : /* simplified version of newt_params for n = 1 (newdim) */
     381             : static void
     382       32725 : newd_params(long N, long *pN2)
     383             : {
     384       32725 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
     385       32725 :   long i, N2 = 1, l = lg(P);
     386       83034 :   for (i = 1; i < l; i++)
     387             :   {
     388       50309 :     long p = P[i], e = E[i];
     389       50309 :     if (e > 2) N2 *= upowuu(p, e-2);
     390             :   }
     391       32725 :   *pN2 = N2;
     392       32725 : }
     393             : 
     394             : static long
     395          21 : newd_params2(long N)
     396             : {
     397          21 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
     398          21 :   long i, N2 = 1, l = lg(P);
     399          56 :   for (i = 1; i < l; i++)
     400             :   {
     401          35 :     long p = P[i], e = E[i];
     402          35 :     if (e >= 2) N2 *= upowuu(p, e);
     403             :   }
     404          21 :   return N2;
     405             : }
     406             : 
     407             : /*******************************************************************/
     408             : /*   Relative trace between cyclotomic fields (TODO: export this)  */
     409             : /*******************************************************************/
     410             : /* g>=1; return g * prod_{p | g, (p,q) = 1} (1-1/p) */
     411             : static long
     412       42651 : phipart(long g, long q)
     413             : {
     414       42651 :   if (g > 1)
     415             :   {
     416       16625 :     GEN P = gel(myfactoru(g), 1);
     417       16625 :     long i, l = lg(P);
     418       16625 :     for (i = 1; i < l; i++) { long p = P[i]; if (q % p) g -= g / p; }
     419             :   }
     420       42651 :   return g;
     421             : }
     422             : /* Trace(zeta_n^k) from Q(\zeta_n) to Q(\zeta_m) with n = m*d; k > 0 */
     423             : static GEN
     424       77385 : tracerelz(long d, long m, long k, long vt)
     425             : {
     426       77385 :   long s, v, g = ugcd(k, d), q = d / g, muq = mymoebiusu(q);
     427       77385 :   if (!muq) return gen_0;
     428       50351 :   if (m == 1)
     429             :   {
     430       17843 :     s = phipart(g, q); if (muq < 0) s = -s;
     431       17843 :     return stoi(s);
     432             :   }
     433       32508 :   if (ugcd(q, m) > 1) return gen_0;
     434       24808 :   s = phipart(g, m*q); if (muq < 0) s = -s;
     435       24808 :   v = Fl_inv(q % m, m);
     436       24808 :   v = (v*(k/g)) % m;
     437       24808 :   return mygmodulo_lift(v, m, stoi(s), vt);
     438             : }
     439             : /* m | n, both not 2 mod 4. Pn = polcyclo(n) */
     440             : GEN
     441       17983 : Qab_trace_init(GEN Pn, long n, long m)
     442             : {
     443             :   GEN T, Pm;
     444             :   long a, i, d, vt;
     445       17983 :   if (m == n) return mkvec(Pn);
     446       12593 :   d = degpol(Pn);
     447       12593 :   vt = varn(Pn);
     448       12593 :   Pm = polcyclo(m, vt);
     449       12593 :   T = cgetg(d+1, t_VEC);
     450       12593 :   gel(T,1) = utoipos(d / degpol(Pm)); /* Tr 1 */
     451       12593 :   a = n / m;
     452       12593 :   for (i = 1; i < d; i++) gel(T,i+1) = tracerelz(a, m, i, vt);
     453       12593 :   return mkvec3(Pm, Pn, T);
     454             : }
     455             : /* x a t_POL modulo Phi_n; n, m not 2 mod 4, degrel != 1*/
     456             : static GEN
     457       48286 : tracerel_i(GEN T, GEN x)
     458             : {
     459       48286 :   long k, l = lg(x);
     460       48286 :   GEN S = gen_0;
     461       48286 :   for (k = 2; k < l; k++) S = gadd(S, gmul(gel(T,k-1), gel(x,k)));
     462       48286 :   return S;
     463             : }
     464             : /* v = Qab_trace_init(n,m); x is a t_VEC of polmodulo Phi_n
     465             :  * Tr_{Q(zeta_n)/Q(zeta_m)} (zeta_n^t * x) */
     466             : GEN
     467        4081 : QabV_tracerel(GEN v, long t, GEN x)
     468             : {
     469             :   long l, j, degrel;
     470             :   GEN y, z, Pm, Pn, T;
     471        4081 :   if (lg(v) != 4) return x;
     472        4081 :   y = cgetg_copy(x, &l);
     473        4081 :   Pm = gel(v,1);
     474        4081 :   Pn = gel(v,2);
     475        4081 :   T  = gel(v,3);
     476        4081 :   degrel = degpol(Pn) / degpol(Pm);
     477        4081 :   z = RgX_rem(pol_xn(t, varn(Pn)), Pn);
     478      100520 :   for (j = 1; j < l; j++)
     479             :   {
     480       96439 :     GEN a = liftpol_shallow(gel(x,j));
     481       96439 :     a = simplify_shallow( gmul(a, z) );
     482       96439 :     if (typ(a) == t_POL)
     483             :     {
     484       48286 :       a = gdivgs(tracerel_i(T, RgX_rem(a, Pn)), degrel);
     485       48286 :       if (typ(a) == t_POL) a = RgX_rem(a, Pm);
     486             :     }
     487       96439 :     gel(y,j) = a;
     488             :   }
     489        4081 :   return y;
     490             : }
     491             : 
     492             : /*              Operations on Dirichlet characters                       */
     493             : 
     494             : /* A Dirichlet character can be given in GP in different formats, but in this
     495             :  * package, it will be a vector CHI=[G,chi,ord], where G is the (Z/MZ)^* to
     496             :  * which the character belongs, chi is the character in Conrey format, ord is
     497             :  * the order */
     498             : 
     499             : static GEN
     500      913115 : gmfcharorder(GEN CHI) { return gel(CHI, 3); }
     501             : long
     502      882805 : mfcharorder(GEN CHI) { return itou(gmfcharorder(CHI)); }
     503             : static long
     504        8253 : mfcharistrivial(GEN CHI) { return !CHI || mfcharorder(CHI) == 1; }
     505             : static GEN
     506      591941 : gmfcharmodulus(GEN CHI) { return gmael3(CHI, 1, 1, 1); }
     507             : long
     508      591941 : mfcharmodulus(GEN CHI) { return itou(gmfcharmodulus(CHI)); }
     509             : GEN
     510      183505 : mfcharpol(GEN CHI) { return gel(CHI,4); }
     511             : static long
     512      448245 : ord_canon(long ord)
     513             : {
     514      448245 :   if ((ord & 3L) == 2) ord >>= 1;
     515      448245 :   return ord;
     516             : }
     517             : static long
     518       27608 : mfcharorder_canon(GEN CHI) { return ord_canon(mfcharorder(CHI)); }
     519             : 
     520             : /* t^k mod polcyclo(ord), ord = order(CHI) > 1 */
     521             : static GEN
     522        1113 : mygmodulo(GEN CHI, long k)
     523             : {
     524             :   GEN C, Pn;
     525             :   long ord;
     526        1113 :   if (!k) return gen_1;
     527         770 :   ord = mfcharorder(CHI);
     528         770 :   if ((k << 1) == ord) return gen_m1;
     529         434 :   Pn = mfcharpol(CHI);
     530         434 :   if ((ord&3L) != 2)
     531          84 :     C = gen_1;
     532             :   else
     533             :   {
     534         350 :     ord >>= 1;
     535         350 :     if (odd(k)) { C = gen_m1; k += ord; } else C = gen_1;
     536         350 :     k >>= 1;
     537             :   }
     538         434 :   return gmodulo(monomial(C, k, varn(Pn)), Pn);
     539             : }
     540             : /* C*zeta_ord^k */
     541             : static GEN
     542      613368 : mygmodulo_lift(long k, long ord, GEN C, long vt)
     543             : {
     544      613368 :   if (!k) return C;
     545      324940 :   if ((k << 1) == ord) return gneg(C);
     546      219751 :   if ((ord&3L) == 2)
     547             :   {
     548       87185 :     if (odd(k)) { C = gneg(C); k += ord >> 1; }
     549       87185 :     k >>= 1;
     550             :   }
     551      219751 :   return monomial(C, k, vt);
     552             : }
     553             : /* vz[i+1] = image of (zeta_ord)^i in Fp */
     554             : static ulong
     555      171437 : mygmodulo_Fl(long k, GEN vz, ulong C, ulong p)
     556             : {
     557             :   long ord;
     558      171437 :   if (!k) return C;
     559      111069 :   ord = lg(vz)-2;
     560      111069 :   if ((k << 1) == ord) return Fl_neg(C,p);
     561       89446 :   if ((ord&3L) == 2)
     562             :   {
     563       70140 :     if (odd(k)) { C = Fl_neg(C,p); k += ord >> 1; }
     564       70140 :     k >>= 1;
     565             :   }
     566       89446 :   return Fl_mul(C, vz[k+1], p);
     567             : }
     568             : 
     569             : static long
     570      450408 : znchareval_i(GEN CHI, long n, GEN ord)
     571      450408 : { return itos(znchareval(gel(CHI,1), gel(CHI,2), stoi(n), ord)); }
     572             : 
     573             : /* G a znstar, L a Conrey log: return a 'mfchar' */
     574             : static GEN
     575      368830 : mfcharGL(GEN G, GEN L)
     576             : {
     577      368830 :   GEN o = zncharorder(G,L);
     578      368830 :   long ord = ord_canon(itou(o)), vt = fetch_user_var("t");
     579      368830 :   return mkvec4(G, L, o, polcyclo(ord,vt));
     580             : }
     581             : static GEN
     582        3899 : mfchartrivial()
     583        3899 : { return mfcharGL(znstar0(gen_1,1), cgetg(1,t_COL)); }
     584             : /* convert a generic character into an 'mfchar' */
     585             : static GEN
     586        3850 : get_mfchar(GEN CHI)
     587             : {
     588             :   GEN G, L;
     589        3850 :   if (typ(CHI) != t_VEC) CHI = znchar(CHI);
     590             :   else
     591             :   {
     592         833 :     long l = lg(CHI);
     593         833 :     if ((l != 3 && l != 5) || !checkznstar_i(gel(CHI,1)))
     594           7 :       pari_err_TYPE("checkNF [chi]", CHI);
     595         826 :     if (l == 5) return CHI;
     596             :   }
     597        3815 :   G = gel(CHI,1);
     598        3815 :   L = gel(CHI,2); if (typ(L) != t_COL) L = znconreylog(G,L);
     599        3815 :   return mfcharGL(G, L);
     600             : }
     601             : 
     602             : /* parse [N], [N,k], [N,k,CHI]. If 'joker' is set, allow wildcard for CHI */
     603             : static GEN
     604        8925 : checkCHI(GEN NK, long N, int joker)
     605             : {
     606             :   GEN CHI;
     607        8925 :   if (lg(NK) == 3)
     608         595 :     CHI = mfchartrivial();
     609             :   else
     610             :   {
     611             :     long i, l;
     612        8330 :     CHI = gel(NK,3); l = lg(CHI);
     613        8330 :     if (isintzero(CHI) && joker)
     614        4095 :       CHI = NULL; /* all character orbits */
     615        4235 :     else if (isintm1(CHI) && joker > 1)
     616        2373 :       CHI = gen_m1; /* sum over all character orbits */
     617        1995 :     else if ((typ(CHI) == t_VEC &&
     618         189 :              (l == 1 || l != 3 || !checkznstar_i(gel(CHI,1)))) && joker)
     619             :     {
     620         133 :       CHI = shallowtrans(CHI); /* list of characters */
     621         133 :       for (i = 1; i < l; i++) gel(CHI,i) = get_mfchar(gel(CHI,i));
     622             :     }
     623             :     else
     624             :     {
     625        1729 :       CHI = get_mfchar(CHI); /* single char */
     626        1729 :       if (N % mfcharmodulus(CHI)) pari_err_TYPE("checkNF [chi]", NK);
     627             :     }
     628             :   }
     629        8911 :   return CHI;
     630             : }
     631             : /* support half-integral weight */
     632             : static void
     633        8932 : checkNK2(GEN NK, long *N, long *nk, long *dk, GEN *CHI, int joker)
     634             : {
     635        8932 :   long l = lg(NK);
     636             :   GEN T;
     637        8932 :   if (typ(NK) != t_VEC || l < 3 || l > 4) pari_err_TYPE("checkNK", NK);
     638        8932 :   T = gel(NK,1); if (typ(T) != t_INT) pari_err_TYPE("checkNF [N]", NK);
     639        8932 :   *N = itos(T); if (*N <= 0) pari_err_TYPE("checkNF [N <= 0]", NK);
     640        8932 :   T = gel(NK,2);
     641        8932 :   switch(typ(T))
     642             :   {
     643        5572 :     case t_INT:  *nk = itos(T); *dk = 1; break;
     644             :     case t_FRAC:
     645        3353 :       *nk = itos(gel(T,1));
     646        3353 :       *dk = itou(gel(T,2)); if (*dk == 2) break;
     647           7 :     default: pari_err_TYPE("checkNF [k]", NK);
     648             :   }
     649        8925 :   *CHI = checkCHI(NK, *N, joker);
     650        8911 : }
     651             : /* don't support half-integral weight */
     652             : static void
     653         126 : checkNK(GEN NK, long *N, long *k, GEN *CHI, int joker)
     654             : {
     655             :   long d;
     656         126 :   checkNK2(NK, N, k, &d, CHI, joker);
     657         126 :   if (d != 1) pari_err_TYPE("checkNF [k]", NK);
     658         126 : }
     659             : 
     660             : static GEN
     661        4851 : mfchargalois(long N, int odd, GEN flagorder)
     662             : {
     663        4851 :   GEN G = znstar0(utoi(N), 1), L = chargalois(G, flagorder);
     664        4851 :   long l = lg(L), i, j;
     665      112735 :   for (i = j = 1; i < l; i++)
     666             :   {
     667      107884 :     GEN chi = znconreyfromchar(G, gel(L,i));
     668      107884 :     if (zncharisodd(G,chi) == odd) gel(L,j++) = mfcharGL(G,chi);
     669             :   }
     670        4851 :   setlg(L, j); return L;
     671             : }
     672             : /* possible characters for non-trivial S_1(N, chi) */
     673             : static GEN
     674        1708 : mfwt1chars(long N, GEN vCHI)
     675             : {
     676        1708 :   if (vCHI) return vCHI; /*do not filter, user knows best*/
     677             :   /* Tate's theorem */
     678        1638 :   return mfchargalois(N, 1, uisprime(N)? mkvecsmall2(2,4): NULL);
     679             : }
     680             : static GEN
     681        3255 : mfchars(long N, long k, long dk, GEN vCHI)
     682        3255 : { return vCHI? vCHI: mfchargalois(N, (dk == 2)? 0: (k & 1), NULL); }
     683             : 
     684             : /* wrappers from mfchar to znchar */
     685             : static long
     686       65359 : mfcharparity(GEN CHI)
     687             : {
     688       65359 :   if (!CHI) return 1;
     689       65359 :   return zncharisodd(gel(CHI,1), gel(CHI,2)) ? -1 : 1;
     690             : }
     691             : /* if CHI is primitive, return CHI itself, not a copy */
     692             : static GEN
     693       63434 : mfchartoprimitive(GEN CHI, long *pF)
     694             : {
     695             :   pari_sp av;
     696             :   GEN chi, F;
     697       63434 :   if (!CHI) { if (pF) *pF = 1; return mfchartrivial(); }
     698       63434 :   av = avma; F = znconreyconductor(gel(CHI,1), gel(CHI,2), &chi);
     699       63434 :   if (typ(F) == t_INT) avma = av;
     700             :   else
     701             :   {
     702        7273 :     CHI = leafcopy(CHI);
     703        7273 :     gel(CHI,1) = znstar0(F, 1);
     704        7273 :     gel(CHI,2) = chi;
     705             :   }
     706       63434 :   if (pF) *pF = mfcharmodulus(CHI);
     707       63434 :   return CHI;
     708             : }
     709             : static long
     710      391279 : mfcharconductor(GEN CHI)
     711             : {
     712      391279 :   pari_sp ltop = avma;
     713      391279 :   GEN res = znconreyconductor(gel(CHI,1), gel(CHI,2), NULL);
     714             :   long FC;
     715      391279 :   if (typ(res) == t_VEC) res = gel(res, 1);
     716      391279 :   FC = itos(res); avma = ltop; return FC;
     717             : }
     718             : 
     719             : /* n coprime with the modulus of CHI */
     720             : static GEN
     721        5523 : mfchareval_i(GEN CHI, long n)
     722             : {
     723        5523 :   GEN ord = gmfcharorder(CHI);
     724        5523 :   if (equali1(ord)) return gen_1;
     725        1113 :   return mygmodulo(CHI, znchareval_i(CHI, n, ord));
     726             : }
     727             : /* d a multiple of ord(CHI); n coprime with char modulus;
     728             :  * return x s.t. CHI(n) = \zeta_d^x] */
     729             : static long
     730      781417 : mfcharevalord(GEN CHI, long n, long d)
     731             : {
     732      781417 :   if (mfcharorder(CHI) == 1) return 0;
     733      441777 :   return znchareval_i(CHI, n, utoi(d));
     734             : }
     735             : 
     736             : /*                      Operations on mf closures                    */
     737             : static GEN
     738       47572 : tagparams(long t, GEN NK) { return mkvec2(mkvecsmall(t), NK); }
     739             : static GEN
     740         812 : lfuntag(long t, GEN x) { return mkvec2(mkvecsmall(t), x); }
     741             : static GEN
     742          49 : tag0(long t, GEN NK) { retmkvec(tagparams(t,NK)); }
     743             : static GEN
     744        8638 : tag(long t, GEN NK, GEN x) { retmkvec2(tagparams(t,NK), x); }
     745             : static GEN
     746       27160 : tag2(long t, GEN NK, GEN x, GEN y) { retmkvec3(tagparams(t,NK), x,y); }
     747             : static GEN
     748       11613 : tag3(long t, GEN NK, GEN x,GEN y,GEN z) { retmkvec4(tagparams(t,NK), x,y,z); }
     749             : /* is F a "modular form" ? */
     750             : int
     751       14616 : checkmf_i(GEN F)
     752       14616 : { return typ(F) == t_VEC
     753       14126 :     && lg(F) > 1 && typ(gel(F,1)) == t_VEC
     754       10101 :     && lg(gel(F,1)) == 3
     755        9940 :     && typ(gmael(F,1,1)) == t_VECSMALL
     756       24556 :     && typ(gmael(F,1,2)) == t_VEC; }
     757      128471 : long mf_get_type(GEN F) { return gmael(F,1,1)[1]; }
     758       94213 : GEN mf_get_gN(GEN F) { return gmael3(F,1,2,1); }
     759       81592 : GEN mf_get_gk(GEN F) { return gmael3(F,1,2,2); }
     760             : /* k - 1/2, assume k in 1/2 + Z */
     761         266 : long mf_get_r(GEN F) { return itou(gel(mf_get_gk(F),1)) >> 1; }
     762       66549 : long mf_get_N(GEN F) { return itou(mf_get_gN(F)); }
     763       45710 : long mf_get_k(GEN F)
     764             : {
     765       45710 :   GEN gk = mf_get_gk(F);
     766       45710 :   if (typ(gk)!=t_INT) pari_err_IMPL("half-integral weight");
     767       45710 :   return itou(gk);
     768             : }
     769       29274 : GEN mf_get_CHI(GEN F) { return gmael3(F,1,2,3); }
     770       17346 : GEN mf_get_field(GEN F) { return gmael3(F,1,2,4); }
     771       15239 : GEN mf_get_NK(GEN F) { return gmael(F,1,2); }
     772             : static void
     773         308 : mf_setfield(GEN f, GEN P)
     774             : {
     775         308 :   gel(f,1) = leafcopy(gel(f,1));
     776         308 :   gmael(f,1,2) = leafcopy(gmael(f,1,2));
     777         308 :   gmael3(f,1,2,4) = P;
     778         308 : }
     779             : 
     780             : /* UTILITY FUNCTIONS */
     781             : GEN
     782        3969 : mftocol(GEN F, long lim, long d)
     783        3969 : { GEN c = mfcoefs_i(F, lim, d); settyp(c,t_COL); return c; }
     784             : GEN
     785        1064 : mfvectomat(GEN vF, long lim, long d)
     786             : {
     787        1064 :   long j, l = lg(vF);
     788        1064 :   GEN M = cgetg(l, t_MAT);
     789        1064 :   for (j = 1; j < l; j++) gel(M,j) = mftocol(gel(vF,j), lim, d);
     790        1064 :   return M;
     791             : }
     792             : 
     793             : static GEN
     794        3892 : RgV_to_ser_full(GEN x) { return RgV_to_ser(x, 0, lg(x)+1); }
     795             : /* TODO: delete */
     796             : static GEN
     797         819 : mfcoefsser(GEN F, long n) { return RgV_to_ser_full(mfcoefs_i(F,n,1)); }
     798             : static GEN
     799         224 : sertovecslice(GEN S, long n)
     800             : {
     801         224 :   GEN v = gtovec0(S, -(lg(S) - 2 + valp(S)));
     802         224 :   long l = lg(v), n2 = n + 2;
     803         224 :   if (l < n2) pari_err_BUG("sertovecslice [n too large]");
     804         224 :   return (l == n2)? v: vecslice(v, 1, n2-1);
     805             : }
     806             : 
     807             : /* a, b two RgV of the same length, multiply as truncated power series */
     808             : static GEN
     809        2954 : RgV_mul_RgXn(GEN a, GEN b)
     810             : {
     811        2954 :   long n = lg(a)-1;
     812             :   GEN c;
     813        2954 :   a = RgV_to_RgX(a,0);
     814        2954 :   b = RgV_to_RgX(b,0); c = RgXn_mul(a, b, n);
     815        2954 :   c = RgX_to_RgC(c,n); settyp(c,t_VEC); return c;
     816             : }
     817             : /* divide as truncated power series */
     818             : static GEN
     819         259 : RgV_div_RgXn(GEN a, GEN b)
     820             : {
     821         259 :   long n = lg(a)-1;
     822             :   GEN c;
     823         259 :   a = RgV_to_RgX(a,0);
     824         259 :   b = RgV_to_RgX(b,0); c = RgXn_mul(a, RgXn_inv(b,n), n);
     825         259 :   c = RgX_to_RgC(c,n); settyp(c,t_VEC); return c;
     826             : }
     827             : /* a^b */
     828             : static GEN
     829          77 : RgV_pows_RgXn(GEN a, long b)
     830             : {
     831          77 :   long n = lg(a)-1;
     832             :   GEN c;
     833          77 :   a = RgV_to_RgX(a,0);
     834          77 :   if (b < 0) { a = RgXn_inv(a, n); b = -b; }
     835          77 :   c = RgXn_powu_i(a,b,n);
     836          77 :   c = RgX_to_RgC(c,n); settyp(c,t_VEC); return c;
     837             : }
     838             : 
     839             : /* assume lg(V) >= n*d + 2 */
     840             : static GEN
     841        5775 : c_deflate(long n, long d, GEN v)
     842             : {
     843        5775 :   long i, id, l = n+2;
     844             :   GEN w;
     845        5775 :   if (d == 1) return lg(v) == l ? v: vecslice(v, 1, l-1);
     846         294 :   w = cgetg(l, typ(v));
     847         294 :   for (i = id = 1; i < l; i++, id += d) gel(w, i) = gel(v, id);
     848         294 :   return w;
     849             : }
     850             : static GEN
     851         518 : c_mul(long n, long d, GEN F, GEN G)
     852             : {
     853         518 :   pari_sp av = avma;
     854         518 :   long nd = n*d;
     855         518 :   GEN VF = mfcoefs_i(F, nd, 1);
     856         518 :   GEN VG = mfcoefs_i(G, nd, 1);
     857         518 :   return gerepilecopy(av, c_deflate(n, d, RgV_mul_RgXn(VF,VG)));
     858             : }
     859             : static GEN
     860          77 : c_pow(long n, long d, GEN F, GEN a)
     861             : {
     862          77 :   pari_sp av = avma;
     863          77 :   long nd = n*d;
     864          77 :   GEN f = RgV_pows_RgXn(mfcoefs_i(F,nd,1), itos(a));
     865          77 :   return gerepilecopy(av, c_deflate(n, d, f));
     866             : }
     867             : 
     868             : /* F * Theta */
     869             : static GEN
     870         336 : mfmultheta(GEN F)
     871             : {
     872         336 :   if (typ(mf_get_gk(F)) == t_FRAC && mf_get_type(F) == t_MF_DIV)
     873             :   {
     874         112 :     GEN T = gel(F,3); /* hopefully mfTheta() */
     875         112 :     if (mf_get_type(T) == t_MF_THETA && mf_get_N(T) == 4) return gel(F,2);
     876             :   }
     877         224 :   return mfmul(F, mfTheta(NULL));
     878             : }
     879             : 
     880             : static GEN
     881          21 : c_bracket(long n, long d, GEN F, GEN G, GEN gm)
     882             : {
     883          21 :   pari_sp av = avma;
     884          21 :   long i, nd = n*d;
     885          21 :   GEN VF = mfcoefs_i(F, nd, 1), tF = cgetg(nd+2, t_VEC);
     886          21 :   GEN VG = mfcoefs_i(G, nd, 1), tG = cgetg(nd+2, t_VEC);
     887          21 :   GEN C, mpow, res = NULL, gk = mf_get_gk(F), gl = mf_get_gk(G);
     888          21 :   ulong j, m = itou(gm);
     889             :   /* pow[i,j+1] = i^j */
     890          21 :   mpow = cgetg(m+2, t_MAT);
     891          21 :   gel(mpow,1) = const_col(nd, gen_1);
     892          49 :   for (j = 1; j <= m; j++)
     893             :   {
     894          28 :     GEN c = cgetg(nd+1, t_COL);
     895          28 :     gel(mpow,j+1) = c;
     896          28 :     for (i = 1; i <= nd; i++) gel(c,i) = muliu(gcoeff(mpow,i,j), i);
     897             :   }
     898          21 :   C = binomial(gaddgs(gk, m-1), m);
     899          70 :   for (j = 0; j <= m; j++)
     900             :   { /* C = (-1)^j binom(m+l-1, j) binom(m+k-1,m-j) */
     901             :     GEN c;
     902          49 :     gel(tF,1) = j == 0? gel(VF,1): gen_0;
     903          49 :     gel(tG,1) = j == m? gel(VG,1): gen_0;
     904         462 :     for (i = 1; i <= nd; i++)
     905             :     {
     906         413 :       gel(tF, i+1) = gmul(gcoeff(mpow,i,j+1),   gel(VF, i+1));
     907         413 :       gel(tG, i+1) = gmul(gcoeff(mpow,i,m-j+1), gel(VG, i+1));
     908             :     }
     909          49 :     c = gmul(C, c_deflate(n, d, RgV_mul_RgXn(tF, tG)));
     910          49 :     res = res? gadd(res, c): c;
     911          49 :     if (j < m)
     912          56 :       C = gdiv(gmul(C, gmulsg(m-j, gaddgs(gl,m-j-1))),
     913          28 :                gmulsg(-(j+1), gaddgs(gk,j)));
     914             :   }
     915          21 :   return gerepileupto(av, res);
     916             : }
     917             : /* linear combination \sum L[j] vecF[j] */
     918             : static GEN
     919        2387 : c_linear(long n, long d, GEN F, GEN L, GEN dL)
     920             : {
     921        2387 :   pari_sp av = avma;
     922        2387 :   long j, l = lg(L);
     923        2387 :   GEN S = NULL;
     924        7546 :   for (j = 1; j < l; j++)
     925             :   {
     926        5159 :     GEN c = gel(L,j);
     927        5159 :     if (gequal0(c)) continue;
     928        4578 :     c = gmul(c, mfcoefs_i(gel(F,j), n, d));
     929        4578 :     S = S? gadd(S,c): c;
     930             :   }
     931        2387 :   if (!S) return zerovec(n+1);
     932        2387 :   if (!is_pm1(dL)) S = gdiv(S, dL);
     933        2387 :   return gerepileupto(av, S);
     934             : }
     935             : 
     936             : /* B_d(T_j Trace^new) as t_MF_BD(t_MF_HECKE(t_MF_NEWTRACE)) or
     937             :  * t_MF_HECKE(t_MF_NEWTRACE)
     938             :  * or t_MF_NEWTRACE in level N. Set d and j, return t_MF_NEWTRACE component*/
     939             : static GEN
     940       52017 : bhn_parse(GEN f, long *d, long *j)
     941             : {
     942       52017 :   long t = mf_get_type(f);
     943       52017 :   *d = *j = 1;
     944       52017 :   if (t == t_MF_BD) { *d = itos(gel(f,3)); f = gel(f,2); t = mf_get_type(f); }
     945       52017 :   if (t == t_MF_HECKE) { *j = gel(f,2)[1]; f = gel(f,3); }
     946       52017 :   return f;
     947             : }
     948             : /* f as above, return the t_MF_NEWTRACE component */
     949             : static GEN
     950       13608 : bhn_newtrace(GEN f)
     951             : {
     952       13608 :   long t = mf_get_type(f);
     953       13608 :   if (t == t_MF_BD) { f = gel(f,2); t = mf_get_type(f); }
     954       13608 :   if (t == t_MF_HECKE) f = gel(f,3);
     955       13608 :   return f;
     956             : }
     957             : static int
     958        2436 : ok_bhn_linear(GEN vf)
     959             : {
     960        2436 :   long i, N0 = 0, l = lg(vf);
     961             :   GEN CHI, gk;
     962        2436 :   if (l == 1) return 1;
     963        2436 :   gk = mf_get_gk(gel(vf,1));
     964        2436 :   CHI = mf_get_CHI(gel(vf,1));
     965        9534 :   for (i = 1; i < l; i++)
     966             :   {
     967        8568 :     GEN f = bhn_newtrace(gel(vf,i));
     968        8568 :     long N = mf_get_N(f);
     969        8568 :     if (mf_get_type(f) != t_MF_NEWTRACE) return 0;
     970        7098 :     if (N < N0) return 0; /* largest level must come last */
     971        7098 :     N0 = N;
     972        7098 :     if (!gequal(gk,mf_get_gk(f))) return 0; /* same k */
     973        7098 :     if (!gequal(gel(mf_get_CHI(f),2), gel(CHI,2))) return 0; /* same CHI */
     974             :   }
     975         966 :   return 1;
     976             : }
     977             : 
     978             : /* vF not empty, same hypotheses as bhnmat_extend */
     979             : static GEN
     980        5117 : bhnmat_extend_nocache(GEN M, long N, long n, long d, GEN vF)
     981             : {
     982             :   cachenew_t cache;
     983        5117 :   long l = lg(vF);
     984             :   GEN f;
     985        5117 :   if (l == 1) return M? M: cgetg(1, t_MAT);
     986        5040 :   f = bhn_newtrace(gel(vF,1)); /* N.B. mf_get_N(f) divides N */
     987        5040 :   init_cachenew(&cache, n*d, N, f);
     988        5040 :   M = bhnmat_extend(M, n, d, vF, &cache);
     989        5040 :   dbg_cachenew(&cache); return M;
     990             : }
     991             : /* c_linear of "bhn" mf closures, same hypotheses as bhnmat_extend */
     992             : static GEN
     993        1001 : c_linear_bhn(long n, long d, GEN F)
     994             : {
     995             :   pari_sp av;
     996        1001 :   GEN M, v, vF = gel(F,2), L = gel(F,3), dL = gel(F,4);
     997        1001 :   if (lg(L) == 1) return zerovec(n+1);
     998        1001 :   av = avma;
     999        1001 :   M = bhnmat_extend_nocache(NULL, mf_get_N(F), n, d, vF);
    1000        1001 :   v = RgM_RgC_mul(M,L); settyp(v, t_VEC);
    1001        1001 :   if (!is_pm1(dL)) v = gdiv(v, dL);
    1002        1001 :   return gerepileupto(av, v);
    1003             : }
    1004             : 
    1005             : /* c in K, K := Q[X]/(T) vz = vector of consecutive powers of root z of T
    1006             :  * attached to an embedding s: K -> C. Return s(c) in C */
    1007             : static GEN
    1008       62006 : Rg_embed1(GEN c, GEN vz)
    1009             : {
    1010       62006 :   long t = typ(c);
    1011       62006 :   if (t == t_POLMOD) { c = gel(c,2); t = typ(c); }
    1012       62006 :   if (t == t_POL) c = RgX_RgV_eval(c, vz);
    1013       62006 :   return c;
    1014             : }
    1015             : /* return s(P) in C[X] */
    1016             : static GEN
    1017         861 : RgX_embed1(GEN P, GEN vz)
    1018             : {
    1019             :   long i, l;
    1020         861 :   GEN Q = cgetg_copy(P, &l);
    1021         861 :   Q[1] = P[1];
    1022         861 :   for (i = 2; i < l; i++) gel(Q,i) = Rg_embed1(gel(P,i), vz);
    1023         861 :   return normalizepol_lg(Q,l); /* normally a no-op */
    1024             : }
    1025             : /* return s(P) in C^n */
    1026             : static GEN
    1027         434 : vecembed1(GEN P, GEN vz)
    1028             : {
    1029             :   long i, l;
    1030         434 :   GEN Q = cgetg_copy(P, &l);
    1031         434 :   for (i = 1; i < l; i++) gel(Q,i) = Rg_embed1(gel(P,i), vz);
    1032         434 :   return Q;
    1033             : }
    1034             : /* P in L = K[X]/(U), K = Q[t]/T; s an embedding of K -> C attached
    1035             :  * to a root of T, extended to an embedding of L -> C attached to a root
    1036             :  * of s(U); vT powers of the root of T, vU powers of the root of s(U).
    1037             :  * Return s(P) in C^n */
    1038             : static GEN
    1039       13314 : Rg_embed2(GEN P, long vt, GEN vT, GEN vU)
    1040             : {
    1041             :   long i, l;
    1042             :   GEN Q;
    1043       13314 :   P = liftpol_shallow(P);
    1044       13314 :   if (typ(P) != t_POL) return P;
    1045       13300 :   if (varn(P) == vt) return Rg_embed1(P, vT);
    1046             :   /* varn(P) == vx */
    1047       13293 :   Q = cgetg_copy(P, &l); Q[1] = P[1];
    1048       13293 :   for (i = 2; i < l; i++) gel(Q,i) = Rg_embed1(gel(P,i), vT);
    1049       13293 :   return Rg_embed1(Q, vU);
    1050             : }
    1051             : static GEN
    1052          42 : vecembed2(GEN P, long vt, GEN vT, GEN vU)
    1053             : {
    1054             :   long i, l;
    1055          42 :   GEN Q = cgetg_copy(P, &l);
    1056          42 :   for (i = 1; i < l; i++) gel(Q,i) = Rg_embed2(gel(P,i), vt, vT, vU);
    1057          42 :   return Q;
    1058             : }
    1059             : static GEN
    1060         532 : RgX_embed2(GEN P, long vt, GEN vT, GEN vU)
    1061             : {
    1062             :   long i, l;
    1063         532 :   GEN Q = cgetg_copy(P, &l);
    1064         532 :   for (i = 2; i < l; i++) gel(Q,i) = Rg_embed2(gel(P,i), vt, vT, vU);
    1065         532 :   Q[1] = P[1]; return normalizepol_lg(Q,l);
    1066             : }
    1067             : /* embed polynomial f in variable vx [ may be a scalar ], E from getembed */
    1068             : static GEN
    1069        1575 : RgX_embed(GEN f, long vx, GEN E)
    1070             : {
    1071             :   GEN vT;
    1072        1575 :   if (typ(f) != t_POL || varn(f) != vx) return mfembed(E, f);
    1073        1554 :   if (lg(E) == 1) return f;
    1074        1358 :   vT = gel(E,2);
    1075        1358 :   if (lg(E) == 3)
    1076         826 :     f = RgX_embed1(f, vT);
    1077             :   else
    1078         532 :     f = RgX_embed2(f, varn(gel(E,1)), vT, gel(E,3));
    1079        1358 :   return f;
    1080             : }
    1081             : /* embed vector, E from getembed */
    1082             : GEN
    1083        1274 : mfvecembed(GEN E, GEN v)
    1084             : {
    1085             :   GEN vT;
    1086        1274 :   if (lg(E) == 1) return v;
    1087         476 :   vT = gel(E,2);
    1088         476 :   if (lg(E) == 3)
    1089         434 :     v = vecembed1(v, vT);
    1090             :   else
    1091          42 :     v = vecembed2(v, varn(gel(E,1)), vT, gel(E,3));
    1092         476 :   return v;
    1093             : }
    1094             : GEN
    1095           7 : mfmatembed(GEN E, GEN f)
    1096             : {
    1097             :   long i, l;
    1098             :   GEN g;
    1099           7 :   if (lg(E) == 1) return f;
    1100           7 :   g = cgetg_copy(f, &l);
    1101           7 :   for (i = 1; i < l; i++) gel(g,i) = mfvecembed(E, gel(f,i));
    1102           7 :   return g;
    1103             : }
    1104             : /* embed vector of polynomials in var vx */
    1105             : static GEN
    1106          98 : RgXV_embed(GEN f, long vx, GEN E)
    1107             : {
    1108             :   long i, l;
    1109             :   GEN v;
    1110          98 :   if (lg(E) == 1) return f;
    1111          70 :   v = cgetg_copy(f, &l);
    1112          70 :   for (i = 1; i < l; i++) gel(v,i) = RgX_embed(gel(f,i), vx, E);
    1113          70 :   return v;
    1114             : }
    1115             : 
    1116             : /* embed scalar */
    1117             : GEN
    1118       95794 : mfembed(GEN E, GEN f)
    1119             : {
    1120             :   GEN vT;
    1121       95794 :   if (lg(E) == 1) return f;
    1122       13335 :   vT = gel(E,2);
    1123       13335 :   if (lg(E) == 3)
    1124        4221 :     f = Rg_embed1(f, vT);
    1125             :   else
    1126        9114 :     f = Rg_embed2(f, varn(gel(E,1)), vT, gel(E,3));
    1127       13335 :   return f;
    1128             : }
    1129             : /* vector of the sigma(f), sigma in vE */
    1130             : static GEN
    1131         273 : RgX_embedall(GEN f, long vx, GEN vE)
    1132             : {
    1133         273 :   long i, l = lg(vE);
    1134         273 :   GEN v = cgetg(l, t_VEC);
    1135         273 :   for (i = 1; i < l; i++) gel(v,i) = RgX_embed(f, vx, gel(vE,i));
    1136         273 :   return l == 2? gel(v,1): v;
    1137             : }
    1138             : /* matrix whose colums are the sigma(v), sigma in vE */
    1139             : static GEN
    1140         329 : RgC_embedall(GEN v, GEN vE)
    1141             : {
    1142         329 :   long j, l = lg(vE);
    1143         329 :   GEN M = cgetg(l, t_MAT);
    1144         329 :   for (j = 1; j < l; j++) gel(M,j) = mfvecembed(gel(vE,j), v);
    1145         329 :   return M;
    1146             : }
    1147             : /* vector of the sigma(v), sigma in vE */
    1148             : static GEN
    1149        4907 : Rg_embedall_i(GEN v, GEN vE)
    1150             : {
    1151        4907 :   long j, l = lg(vE);
    1152        4907 :   GEN M = cgetg(l, t_VEC);
    1153        4907 :   for (j = 1; j < l; j++) gel(M,j) = mfembed(gel(vE,j), v);
    1154        4907 :   return M;
    1155             : }
    1156             : /* vector of the sigma(v), sigma in vE; if #vE == 1, return v */
    1157             : static GEN
    1158       90446 : Rg_embedall(GEN v, GEN vE)
    1159       90446 : { return (lg(vE) == 2)? mfembed(gel(vE,1), v): Rg_embedall_i(v, vE); }
    1160             : 
    1161             : static GEN
    1162         224 : c_div_i(long n, GEN F, GEN G)
    1163             : {
    1164             :   GEN VF, VG, a0, a0i, H;
    1165         224 :   VF = mfcoefsser(F, n); VG = mfcoefsser(G, n);
    1166         224 :   a0 = polcoef_i(VG, 0, -1);
    1167         224 :   if (gequal0(a0) || gequal1(a0)) a0 = a0i = NULL;
    1168             :   else
    1169             :   {
    1170          49 :     a0i = ginv(a0);
    1171          49 :     VG = gmul(ser_unscale(VG,a0), a0i);
    1172          49 :     VF = gmul(ser_unscale(VF,a0), a0i);
    1173             :   }
    1174         224 :   H = gdiv(VF, VG);
    1175         224 :   if (a0) H = ser_unscale(H,a0i);
    1176         224 :   return sertovecslice(H, n);
    1177             : }
    1178             : static GEN
    1179         224 : c_div(long n, long d, GEN F, GEN G)
    1180             : {
    1181         224 :   pari_sp av = avma;
    1182         224 :   GEN D = (d==1)? c_div_i(n, F,G): c_deflate(n, d, c_div_i(n*d, F,G));
    1183         224 :   return gerepilecopy(av, D);
    1184             : }
    1185             : 
    1186             : static GEN
    1187          35 : c_shift(long n, long d, GEN F, GEN gsh)
    1188             : {
    1189          35 :   pari_sp av = avma;
    1190             :   GEN vF;
    1191          35 :   long sh = itos(gsh), n1 = n*d + sh;
    1192          35 :   if (n1 < 0) return zerovec(n+1);
    1193          35 :   vF = mfcoefs_i(F, n1, 1);
    1194          35 :   if (sh < 0) vF = shallowconcat(zerovec(-sh), vF);
    1195          35 :   else vF = vecslice(vF, sh+1, n1+1);
    1196          35 :   return gerepilecopy(av, c_deflate(n, d, vF));
    1197             : }
    1198             : 
    1199             : static GEN
    1200          21 : c_deriv(long n, long d, GEN F, GEN gm)
    1201             : {
    1202          21 :   pari_sp av = avma;
    1203          21 :   GEN V = mfcoefs_i(F, n, d), res;
    1204          21 :   long i, m = itos(gm);
    1205          21 :   if (!m) return V;
    1206          21 :   res = cgetg(n+2, t_VEC); gel(res,1) = gen_0;
    1207          21 :   if (m < 0)
    1208           7 :   { for (i=1; i <= n; i++) gel(res, i+1) = gdiv(gel(V, i+1), powuu(i,-m)); }
    1209             :   else
    1210          14 :   { for (i=1; i <= n; i++) gel(res, i+1) = gmul(gel(V,i+1), powuu(i,m)); }
    1211          21 :   return gerepileupto(av, res);
    1212             : }
    1213             : 
    1214             : static GEN
    1215          14 : c_derivE2(long n, long d, GEN F, GEN gm)
    1216             : {
    1217          14 :   pari_sp av = avma;
    1218             :   GEN VF, VE, res, tmp, gk;
    1219          14 :   long i, m = itos(gm), nd;
    1220          14 :   if (m == 0) return mfcoefs_i(F, n, d);
    1221          14 :   nd = n*d;
    1222          14 :   VF = mfcoefs_i(F, nd, 1); VE = mfcoefs_i(mfEk(2), nd, 1);
    1223          14 :   gk = mf_get_gk(F);
    1224          14 :   if (m == 1)
    1225             :   {
    1226           7 :     res = cgetg(n+2, t_VEC);
    1227           7 :     for (i = 0; i <= n; i++) gel(res, i+1) = gmulsg(i, gel(VF, i*d+1));
    1228           7 :     tmp = c_deflate(n, d, RgV_mul_RgXn(VF, VE));
    1229           7 :     return gerepileupto(av, gsub(res, gmul(gdivgs(gk, 12), tmp)));
    1230             :   }
    1231             :   else
    1232             :   {
    1233             :     long j;
    1234          35 :     for (j = 1; j <= m; j++)
    1235             :     {
    1236          28 :       tmp = RgV_mul_RgXn(VF, VE);
    1237          28 :       for (i = 0; i <= nd; i++) gel(VF, i+1) = gmulsg(i, gel(VF, i+1));
    1238          28 :       VF = gsub(VF, gmul(gdivgs(gaddgs(gk, 2*(j-1)), 12), tmp));
    1239             :     }
    1240           7 :     return gerepilecopy(av, c_deflate(n, d, VF));
    1241             :   }
    1242             : }
    1243             : 
    1244             : /* Twist by the character (D/.) */
    1245             : static GEN
    1246           7 : c_twist(long n, long d, GEN F, GEN D)
    1247             : {
    1248           7 :   pari_sp av = avma;
    1249           7 :   GEN V = mfcoefs_i(F, n, d), res = cgetg(n+2, t_VEC);
    1250             :   long i;
    1251         119 :   for (i = 0; i <= n; i++)
    1252         112 :     gel(res, i + 1) = gmulsg(krois(D, i), gel(V, i+1));
    1253           7 :   return gerepileupto(av, res);
    1254             : }
    1255             : 
    1256             : /* form F given by closure, compute T(n)(F) as closure */
    1257             : static GEN
    1258         434 : c_hecke(long m, long l, GEN DATA, GEN F)
    1259             : {
    1260         434 :   pari_sp av = avma;
    1261         434 :   return gerepilecopy(av, hecke_i(m, l, NULL, F, DATA));
    1262             : }
    1263             : static GEN
    1264         140 : c_const(long n, long d, GEN C)
    1265             : {
    1266         140 :   GEN V = zerovec(n+1);
    1267         140 :   long i, j, l = lg(C);
    1268         140 :   if (l > d*n+2) l = d*n+2;
    1269         140 :   for (i = j = 1; i < l; i+=d, j++) gel(V, j) = gcopy(gel(C,i));
    1270         140 :   return V;
    1271             : }
    1272             : 
    1273             : static GEN
    1274         455 : eta3_ZXn(long m)
    1275             : {
    1276         455 :   long l = m+2, n, k;
    1277         455 :   GEN P = cgetg(l,t_POL);
    1278         455 :   P[1] = evalsigne(1)|evalvarn(0);
    1279         455 :   for (n = 2; n < l; n++) gel(P,n) = gen_0;
    1280        2471 :   for (n = k = 0;; n++)
    1281             :   {
    1282        4487 :     if (k + n >= m) { setlg(P, k+3); return P; }
    1283        2016 :     k += n;
    1284             :     /* now k = n(n+1) / 2 */
    1285        2016 :     gel(P, k+2) = odd(n)? utoineg(2*n+1): utoipos(2*n+1);
    1286             :   }
    1287             : }
    1288             : 
    1289             : static GEN
    1290         455 : c_delta(long n, long d)
    1291             : {
    1292         455 :   pari_sp ltop = avma;
    1293         455 :   long N = n*d;
    1294         455 :   GEN e = eta3_ZXn(N);
    1295         455 :   e = ZXn_sqr(e,N);
    1296         455 :   e = ZXn_sqr(e,N);
    1297         455 :   e = ZXn_sqr(e,N); /* eta(x)^24 */
    1298         455 :   settyp(e, t_VEC);
    1299         455 :   gel(e,1) = gen_0; /* Delta(x) = x*eta(x)^24 as a t_VEC */
    1300         455 :   return gerepilecopy(ltop, c_deflate(n, d, e));
    1301             : }
    1302             : 
    1303             : /* return s(d) such that s|f <=> d | f^2 */
    1304             : static long
    1305          35 : mysqrtu(ulong d)
    1306             : {
    1307          35 :   GEN fa = myfactoru(d), P = gel(fa,1), E = gel(fa,2);
    1308          35 :   long l = lg(P), i, s = 1;
    1309          35 :   for (i = 1; i < l; i++) s *= upowuu(P[i], (E[i]+1)>>1);
    1310          35 :   return s;
    1311             : }
    1312             : static GEN
    1313        1428 : c_theta(long n, long d, GEN psi)
    1314             : {
    1315        1428 :   long lim = usqrt(n*d), F = mfcharmodulus(psi), par = mfcharparity(psi);
    1316        1428 :   long f, d2 = d == 1? 1: mysqrtu(d);
    1317        1428 :   GEN V = zerovec(n + 1);
    1318        5558 :   for (f = d2; f <= lim; f += d2)
    1319        4130 :     if (ugcd(F, f) == 1)
    1320             :     {
    1321        4123 :       pari_sp av = avma;
    1322        4123 :       GEN c = mfchareval_i(psi, f);
    1323        4123 :       gel(V, f*f/d + 1) = gerepileupto(av, par < 0 ? gmulgs(c,2*f) : gmul2n(c,1));
    1324             :     }
    1325        1428 :   if (F == 1) gel(V, 1) = gen_1;
    1326        1428 :   return V; /* no gerepile needed */
    1327             : }
    1328             : 
    1329             : static GEN
    1330         133 : c_etaquo(long n, long d, GEN eta, GEN gs)
    1331             : {
    1332         133 :   pari_sp av = avma;
    1333         133 :   long s = itos(gs), nd = n*d, nds = nd - s + 1;
    1334             :   GEN c;
    1335         133 :   if (nds <= 0) return zerovec(n+1);
    1336         112 :   c = RgX_to_RgC(eta_product_ZXn(eta, nds), nds); settyp(c, t_VEC);
    1337         112 :   if (s > 0) c = shallowconcat(zerovec(s), c);
    1338         112 :   return gerepilecopy(av, c_deflate(n, d, c));
    1339             : }
    1340             : 
    1341             : static GEN
    1342          63 : c_ell(long n, long d, GEN E)
    1343             : {
    1344          63 :   pari_sp av = avma;
    1345             :   GEN v;
    1346          63 :   if (d == 1) return concat(gen_0, anell(E, n));
    1347           7 :   v = shallowconcat(gen_0, anell(E, n*d));
    1348           7 :   return gerepilecopy(av, c_deflate(n, d, v));
    1349             : }
    1350             : 
    1351             : static GEN
    1352          21 : c_cusptrace(long n, long d, GEN F)
    1353             : {
    1354          21 :   pari_sp av = avma;
    1355          21 :   GEN D = gel(F,2), res = cgetg(n+2, t_VEC);
    1356          21 :   long i, N = mf_get_N(F), k = mf_get_k(F);
    1357          21 :   gel(res, 1) = gen_0;
    1358         140 :   for (i = 1; i <= n; i++)
    1359         119 :     gel(res, i+1) = mfcusptrace_i(N, k, i*d, mydivisorsu(i*d), D);
    1360          21 :   return gerepilecopy(av, res);
    1361             : }
    1362             : 
    1363             : static GEN
    1364         749 : c_newtrace(long n, long d, GEN F)
    1365             : {
    1366         749 :   pari_sp av = avma;
    1367             :   cachenew_t cache;
    1368         749 :   long N = mf_get_N(F);
    1369             :   GEN v;
    1370         749 :   init_cachenew(&cache, n*d, N, F);
    1371         749 :   v = colnewtrace(0, n, d, N, mf_get_k(F), &cache);
    1372         749 :   settyp(v, t_VEC); return gerepilecopy(av, v);
    1373             : }
    1374             : 
    1375             : static GEN
    1376        3780 : c_Bd(long n, long d, GEN F, GEN A)
    1377             : {
    1378        3780 :   pari_sp av = avma;
    1379        3780 :   long a = itou(A), ad = ugcd(a,d), aad = a/ad, i, j;
    1380        3780 :   GEN w, v = mfcoefs_i(F, n/aad, d/ad);
    1381        3780 :   if (a == 1) return v;
    1382        3780 :   n++; w = zerovec(n);
    1383        3780 :   for (i = j = 1; j <= n; i++, j += aad) gel(w,j) = gcopy(gel(v,i));
    1384        3780 :   return gerepileupto(av, w);
    1385             : }
    1386             : 
    1387             : static GEN
    1388        3591 : c_dihedral(long n, long d, GEN bnr, GEN w, GEN k0j)
    1389             : {
    1390        3591 :   pari_sp av = avma;
    1391        3591 :   GEN V = dihan(bnr, w, k0j, n*d);
    1392        3591 :   GEN Tinit = gel(w,3), Pm = gel(Tinit,1);
    1393        3591 :   GEN A = c_deflate(n, d, V);
    1394        3591 :   if (degpol(Pm) == 1 || RgV_is_ZV(A)) return gerepilecopy(av, A);
    1395         763 :   return gerepileupto(av, gmodulo(A, Pm));
    1396             : }
    1397             : 
    1398             : static GEN
    1399         140 : c_mfEH(long n, long d, GEN F)
    1400             : {
    1401         140 :   pari_sp av = avma;
    1402             :   GEN v, M, A;
    1403         140 :   long i, r = mf_get_r(F);
    1404         140 :   if (n == 1)
    1405          14 :     return gerepilecopy(av, mkvec2(mfEHcoef(r,0),mfEHcoef(r,d)));
    1406             :   /* speedup mfcoef */
    1407         126 :   if (r == 1)
    1408             :   {
    1409          70 :     v = cgetg(n+2, t_VEC);
    1410          70 :     gel(v,1) = sstoQ(-1,12);
    1411       83258 :     for (i = 1; i <= n; i++)
    1412             :     {
    1413       83188 :       long id = i*d, a = id & 3;
    1414       83188 :       gel(v,i+1) = (a==1 || a==2)? gen_0: sstoQ(hclassno6u(id), 6);
    1415             :     }
    1416          70 :     return v; /* no gerepile needed */
    1417             :   }
    1418          56 :   M = mfEHmat(n*d+1,r);
    1419          56 :   if (d > 1)
    1420             :   {
    1421           7 :     long l = lg(M);
    1422           7 :     for (i = 1; i < l; i++) gel(M,i) = c_deflate(n, d, gel(M,i));
    1423             :   }
    1424          56 :   A = gel(F,2); /* [num(B), den(B)] */
    1425          56 :   v = RgC_Rg_div(RgM_RgC_mul(M, gel(A,1)), gel(A,2));
    1426          56 :   settyp(v,t_VEC); return gerepileupto(av, v);
    1427             : }
    1428             : 
    1429             : static GEN
    1430        5733 : c_mfeisen(long n, long d, GEN F)
    1431             : {
    1432        5733 :   pari_sp av = avma;
    1433        5733 :   GEN v, vchi, E0, P, T, CHI, gk = mf_get_gk(F);
    1434             :   long i, k;
    1435        5733 :   if (typ(gk) != t_INT) return c_mfEH(n, d, F);
    1436        5593 :   k = itou(gk);
    1437        5593 :   vchi = gel(F,2);
    1438        5593 :   E0 = gel(vchi,1);
    1439        5593 :   T = gel(vchi,2);
    1440        5593 :   P = gel(T,1);
    1441        5593 :   CHI = gel(vchi,3);
    1442        5593 :   v = cgetg(n+2, t_VEC);
    1443        5593 :   gel(v, 1) = gcopy(E0); /* E(0) */
    1444        5593 :   if (lg(vchi) == 5)
    1445             :   { /* E_k(chi1,chi2) */
    1446        3794 :     GEN CHI2 = gel(vchi,4), F3 = gel(F,3);
    1447        3794 :     long ord = F3[1], j = F3[2];
    1448        3794 :     for (i = 1; i <= n; i++) gel(v, i+1) = sigchi2(k, CHI, CHI2, i*d, ord);
    1449        3794 :     if (lg(T) == 4) v = QabV_tracerel(T, j, v);
    1450             :   }
    1451             :   else
    1452             :   { /* E_k(chi) */
    1453        1799 :     for (i = 1; i <= n; i++) gel(v, i+1) = sigchi(k, CHI, i*d);
    1454             :   }
    1455        5593 :   if (degpol(P) != 1 && !RgV_is_QV(v)) return gerepileupto(av, gmodulo(v, P));
    1456        4942 :   return gerepilecopy(av, v);
    1457             : }
    1458             : 
    1459             : /* L(chi_D, 1-k) */
    1460             : static GEN
    1461          28 : lfunquadneg(long D, long k)
    1462             : {
    1463          28 :   GEN B, dS, S = gen_0;
    1464          28 :   long r, N = labs(D);
    1465             :   pari_sp av;
    1466          28 :   if (k == 1 && N == 1) return gneg(ghalf);
    1467             :   /* B = N^k * denom(B) * B(x/N) */
    1468          28 :   B = ZX_rescale(Q_remove_denom(bernpol(k, 0), &dS), utoi(N));
    1469          28 :   dS = mul_denom(dS, stoi(-N*k));
    1470          28 :   av = avma;
    1471        7175 :   for (r = 0; r < N; r++)
    1472             :   {
    1473        7147 :     long c = kross(D, r);
    1474        7147 :     if (c)
    1475             :     {
    1476        5152 :       GEN tmp = poleval(B, utoi(r));
    1477        5152 :       S = c > 0 ? addii(S, tmp) : subii(S, tmp);
    1478        5152 :       S = gerepileuptoint(av, S);
    1479             :     }
    1480             :   }
    1481          28 :   return gdiv(S, dS);
    1482             : }
    1483             : 
    1484             : /* Returns vector of coeffs from F[0], F[d], ..., F[d*n] */
    1485             : static GEN
    1486       21427 : mfcoefs_i(GEN F, long n, long d)
    1487             : {
    1488       21427 :   if (n < 0) return gen_0;
    1489       21427 :   switch(mf_get_type(F))
    1490             :   {
    1491         140 :     case t_MF_CONST: return c_const(n, d, gel(F,2));
    1492        5733 :     case t_MF_EISEN: return c_mfeisen(n, d, F);
    1493         658 :     case t_MF_Ek: return c_Ek(n, d, F);
    1494         455 :     case t_MF_DELTA: return c_delta(n, d);
    1495        1365 :     case t_MF_THETA: return c_theta(n, d, gel(F,2));
    1496         133 :     case t_MF_ETAQUO: return c_etaquo(n, d, gel(F,2), gel(F,3));
    1497          63 :     case t_MF_ELL: return c_ell(n, d, gel(F,2));
    1498         518 :     case t_MF_MUL: return c_mul(n, d, gel(F,2), gel(F,3));
    1499          77 :     case t_MF_POW: return c_pow(n, d, gel(F,2), gel(F,3));
    1500          21 :     case t_MF_BRACKET: return c_bracket(n, d, gel(F,2), gel(F,3), gel(F,4));
    1501        2387 :     case t_MF_LINEAR: return c_linear(n, d, gel(F,2), gel(F,3), gel(F,4));
    1502        1001 :     case t_MF_LINEAR_BHN: return c_linear_bhn(n, d, F);
    1503         224 :     case t_MF_DIV: return c_div(n, d, gel(F,2), gel(F,3));
    1504          35 :     case t_MF_SHIFT: return c_shift(n, d, gel(F,2), gel(F,3));
    1505          21 :     case t_MF_DERIV: return c_deriv(n, d, gel(F,2), gel(F,3));
    1506          14 :     case t_MF_DERIVE2: return c_derivE2(n, d, gel(F,2), gel(F,3));
    1507           7 :     case t_MF_TWIST: return c_twist(n, d, gel(F,2), gel(F,3));
    1508         434 :     case t_MF_HECKE: return c_hecke(n, d, gel(F,2), gel(F,3));
    1509        3780 :     case t_MF_BD: return c_Bd(n, d, gel(F,2), gel(F,3));
    1510          21 :     case t_MF_TRACE: return c_cusptrace(n, d, F);
    1511         749 :     case t_MF_NEWTRACE: return c_newtrace(n, d, F);
    1512        3591 :     case t_MF_DIHEDRAL: return c_dihedral(n, d, gel(F,2), gel(F,3), gel(F,4));
    1513             :     default: pari_err_TYPE("mfcoefs",F); return NULL;/*LCOV_EXCL_LINE*/
    1514             :   }
    1515             : }
    1516             : 
    1517             : static GEN
    1518         147 : matdeflate(long n, long d, GEN M)
    1519             : {
    1520             :   long i, l;
    1521             :   GEN A;
    1522             :   /*  if (d == 1) return M; */
    1523         147 :   A = cgetg_copy(M,&l);
    1524         147 :   for (i = 1; i < l; i++) gel(A,i) = c_deflate(n,d,gel(M,i));
    1525         147 :   return A;
    1526             : }
    1527             : static int
    1528        5194 : space_is_cusp(long space) { return space != mf_FULL && space != mf_EISEN; }
    1529             : /* safe with flraw mf */
    1530             : static GEN
    1531        1988 : mfcoefs_mf(GEN mf, long n, long d)
    1532             : {
    1533        1988 :   GEN MS, ME, E = MF_get_E(mf), S = MF_get_S(mf), M = MF_get_M(mf);
    1534        1988 :   long lE = lg(E), lS = lg(S), l = lE+lS-1;
    1535             : 
    1536        1988 :   if (l == 1) return cgetg(1, t_MAT);
    1537        1876 :   if (typ(M) == t_MAT && lg(M) != 1 && (n+1)*d < nbrows(M))
    1538          21 :     return matdeflate(n, d, M); /*cached; lg = 1 is possible from mfinit */
    1539        1855 :   ME = (lE == 1)? cgetg(1, t_MAT): mfvectomat(E, n, d);
    1540        1855 :   if (lS == 1)
    1541         357 :     MS = cgetg(1, t_MAT);
    1542        1498 :   else if (mf_get_type(gel(S,1)) == t_MF_DIV) /*k 1/2-integer or k=1 (exotic)*/
    1543         126 :     MS = matdeflate(n,d, mflineardivtomat(MF_get_N(mf), S, n*d));
    1544        1372 :   else if (MF_get_k(mf) == 1) /* k = 1 (dihedral) */
    1545             :   {
    1546         140 :     GEN M = mfvectomat(gmael(S,1,2), n, d);
    1547             :     long i;
    1548         140 :     MS = cgetg(lS, t_MAT);
    1549         448 :     for (i = 1; i < lS; i++)
    1550             :     {
    1551         308 :       GEN f = gel(S,i), dc = gel(f,4), c = RgM_RgC_mul(M, gel(f,3));
    1552         308 :       if (!equali1(dc)) c = RgC_Rg_div(c,dc);
    1553         308 :       gel(MS,i) = c;
    1554             :     }
    1555             :   }
    1556             :   else /* k >= 2 integer */
    1557        1232 :     MS = bhnmat_extend_nocache(NULL, MF_get_N(mf), n, d, S);
    1558        1855 :   return shallowconcat(ME,MS);
    1559             : }
    1560             : GEN
    1561        3248 : mfcoefs(GEN F, long n, long d)
    1562             : {
    1563        3248 :   if (!checkmf_i(F))
    1564             :   {
    1565          42 :     pari_sp av = avma;
    1566          42 :     GEN mf = checkMF_i(F); if (!mf) pari_err_TYPE("mfcoefs", F);
    1567          42 :     return gerepilecopy(av, mfcoefs_mf(mf,n,d));
    1568             :   }
    1569        3206 :   if (d <= 0) pari_err_DOMAIN("mfcoefs", "d", "<=", gen_0, stoi(d));
    1570        3206 :   if (n < 0) return cgetg(1, t_VEC);
    1571        3206 :   return mfcoefs_i(F, n, d);
    1572             : }
    1573             : 
    1574             : /* assume k >= 0 */
    1575             : static GEN
    1576         210 : mfak_i(GEN F, long k)
    1577             : {
    1578         210 :   if (!k) return gel(mfcoefs_i(F,0,1), 1);
    1579         154 :   return gel(mfcoefs_i(F,1,k), 2);
    1580             : }
    1581             : GEN
    1582          70 : mfcoef(GEN F, long n)
    1583             : {
    1584          70 :   pari_sp av = avma;
    1585          70 :   if (!checkmf_i(F)) pari_err_TYPE("mfcoef",F);
    1586          70 :   return n < 0? gen_0: gerepilecopy(av, mfak_i(F, n));
    1587             : }
    1588             : 
    1589             : static GEN
    1590         112 : paramconst() { return tagparams(t_MF_CONST, mkNK(1,0,mfchartrivial())); }
    1591             : static GEN
    1592          70 : mftrivial(void) { retmkvec2(paramconst(), cgetg(1,t_VEC)); }
    1593             : static GEN
    1594          42 : mf1(void) { retmkvec2(paramconst(), mkvec(gen_1)); }
    1595             : 
    1596             : /* induce mfchar CHI to G */
    1597             : static GEN
    1598      306593 : induce(GEN G, GEN CHI)
    1599             : {
    1600             :   GEN o, chi;
    1601      306593 :   if (typ(CHI) == t_INT) /* Kronecker */
    1602             :   {
    1603      300860 :     chi = znchar_quad(G, CHI);
    1604      300860 :     o = ZV_equal0(chi)? gen_1: gen_2;
    1605      300860 :     CHI = mkvec4(G,chi,o,cgetg(1,t_VEC));
    1606             :   }
    1607             :   else
    1608             :   {
    1609        5733 :     if (mfcharmodulus(CHI) == itos(znstar_get_N(G))) return CHI;
    1610        5334 :     CHI = leafcopy(CHI);
    1611        5334 :     chi = zncharinduce(gel(CHI,1), gel(CHI,2), G);
    1612        5334 :     gel(CHI,1) = G;
    1613        5334 :     gel(CHI,2) = chi;
    1614             :   }
    1615      306194 :   return CHI;
    1616             : }
    1617             : /* induce mfchar CHI to znstar(G) */
    1618             : static GEN
    1619       42441 : induceN(long N, GEN CHI)
    1620             : {
    1621       42441 :   if (mfcharmodulus(CHI) != N) CHI = induce(znstar0(utoipos(N),1), CHI);
    1622       42441 :   return CHI;
    1623             : }
    1624             : /* *pCHI1 and *pCHI2 are mfchar, induce to common modulus */
    1625             : static void
    1626        5215 : char2(GEN *pCHI1, GEN *pCHI2)
    1627             : {
    1628        5215 :   GEN CHI1 = *pCHI1, G1 = gel(CHI1,1), N1 = znstar_get_N(G1);
    1629        5215 :   GEN CHI2 = *pCHI2, G2 = gel(CHI2,1), N2 = znstar_get_N(G2);
    1630        5215 :   if (!equalii(N1,N2))
    1631             :   {
    1632        3822 :     GEN G, d = gcdii(N1,N2);
    1633        3822 :     if      (equalii(N2,d)) *pCHI2 = induce(G1, CHI2);
    1634        1253 :     else if (equalii(N1,d)) *pCHI1 = induce(G2, CHI1);
    1635             :     else
    1636             :     {
    1637         154 :       if (!equali1(d)) N2 = diviiexact(N2,d);
    1638         154 :       G = znstar0(mulii(N1,N2), 1);
    1639         154 :       *pCHI1 = induce(G, CHI1);
    1640         154 :       *pCHI2 = induce(G, CHI2);
    1641             :     }
    1642             :   }
    1643        5215 : }
    1644             : /* mfchar or charinit wrt same modulus; outputs a mfchar */
    1645             : static GEN
    1646      301854 : mfcharmul_i(GEN CHI1, GEN CHI2)
    1647             : {
    1648      301854 :   GEN G = gel(CHI1,1), chi3 = zncharmul(G, gel(CHI1,2), gel(CHI2,2));
    1649      301854 :   return mfcharGL(G, chi3);
    1650             : }
    1651             : /* mfchar or charinit; outputs a mfchar */
    1652             : static GEN
    1653        1001 : mfcharmul(GEN CHI1, GEN CHI2)
    1654             : {
    1655        1001 :   char2(&CHI1, &CHI2); return mfcharmul_i(CHI1,CHI2);
    1656             : }
    1657             : /* mfchar or charinit; outputs a mfchar */
    1658             : static GEN
    1659         105 : mfcharpow(GEN CHI, GEN n)
    1660             : {
    1661             :   GEN G, chi;
    1662         105 :   G = gel(CHI,1); chi = zncharpow(G, gel(CHI,2), n);
    1663         105 :   return mfcharGL(G, chi);
    1664             : }
    1665             : /* mfchar or charinit wrt same modulus; outputs a mfchar */
    1666             : static GEN
    1667        4214 : mfchardiv_i(GEN CHI1, GEN CHI2)
    1668             : {
    1669        4214 :   GEN G = gel(CHI1,1), chi3 = znchardiv(G, gel(CHI1,2), gel(CHI2,2));
    1670        4214 :   return mfcharGL(G, chi3);
    1671             : }
    1672             : /* mfchar or charinit; outputs a mfchar */
    1673             : static GEN
    1674        4214 : mfchardiv(GEN CHI1, GEN CHI2)
    1675             : {
    1676        4214 :   char2(&CHI1, &CHI2); return mfchardiv_i(CHI1,CHI2);
    1677             : }
    1678             : static GEN
    1679          28 : mfcharconj(GEN CHI)
    1680             : {
    1681          28 :   CHI = leafcopy(CHI);
    1682          28 :   gel(CHI,2) = zncharconj(gel(CHI,1), gel(CHI,2));
    1683          28 :   return CHI;
    1684             : }
    1685             : 
    1686             : /* CHI mfchar, assume 4 | N. Multiply CHI by \chi_{-4} */
    1687             : static GEN
    1688        1071 : mfchilift(GEN CHI, long N)
    1689             : {
    1690        1071 :   CHI = induceN(N, CHI);
    1691        1071 :   return mfcharmul_i(CHI, induce(gel(CHI,1), stoi(-4)));
    1692             : }
    1693             : /* CHI defined mod N, N4 = N/4;
    1694             :  * if CHI is defined mod N4 return CHI;
    1695             :  * else if CHI' = CHI*(-4,.) is defined mod N4, return CHI' (primitive)
    1696             :  * else return NULL */
    1697             : static GEN
    1698          70 : mfcharchiliftprim(GEN CHI, long N4)
    1699             : {
    1700          70 :   long FC = mfcharconductor(CHI);
    1701          70 :   if (N4 % FC == 0) return CHI;
    1702          14 :   CHI = mfchilift(CHI, N4 << 2);
    1703          14 :   CHI = mfchartoprimitive(CHI, &FC);
    1704          14 :   return (N4 % FC == 0)? CHI: NULL;
    1705             : }
    1706             : static GEN
    1707        2198 : mfchiadjust(GEN CHI, GEN gk, long N)
    1708             : {
    1709        2198 :   long par = mfcharparity(CHI);
    1710        2198 :   if (typ(gk) == t_INT &&  mpodd(gk)) par = -par;
    1711        2198 :   return par == 1 ? CHI : mfchilift(CHI, N);
    1712             : }
    1713             : 
    1714             : static GEN
    1715        3003 : mfsamefield(GEN P, GEN Q)
    1716             : {
    1717        3003 :   if (degpol(P) == 1) return Q;
    1718         455 :   if (degpol(Q) == 1) return P;
    1719         427 :   if (!gequal(P,Q)) pari_err_TYPE("mfsamefield [different fields]",mkvec2(P,Q));
    1720         420 :   return P;
    1721             : }
    1722             : 
    1723             : GEN
    1724         336 : mfmul(GEN f, GEN g)
    1725             : {
    1726         336 :   pari_sp av = avma;
    1727             :   GEN N, K, NK, CHI;
    1728         336 :   if (!checkmf_i(f)) pari_err_TYPE("mfmul",f);
    1729         336 :   if (!checkmf_i(g)) pari_err_TYPE("mfmul",g);
    1730         336 :   N = lcmii(mf_get_gN(f), mf_get_gN(g));
    1731         336 :   K = gadd(mf_get_gk(f), mf_get_gk(g));
    1732         336 :   CHI = mfcharmul(mf_get_CHI(f), mf_get_CHI(g));
    1733         336 :   CHI = mfchiadjust(CHI, K, itos(N));
    1734         336 :   NK = mkgNK(N, K, CHI, mfsamefield(mf_get_field(f), mf_get_field(g)));
    1735         329 :   return gerepilecopy(av, tag2(t_MF_MUL, NK, f, g));
    1736             : }
    1737             : GEN
    1738          49 : mfpow(GEN f, long n)
    1739             : {
    1740          49 :   pari_sp av = avma;
    1741             :   GEN KK, NK, gn, CHI;
    1742          49 :   if (!checkmf_i(f)) pari_err_TYPE("mfpow",f);
    1743          49 :   if (!n) return mf1();
    1744          49 :   if (n == 1) return gcopy(f);
    1745          49 :   KK = gmulsg(n,mf_get_gk(f));
    1746          49 :   gn = stoi(n);
    1747          49 :   CHI = mfcharpow(mf_get_CHI(f), gn);
    1748          49 :   CHI = mfchiadjust(CHI, KK, mf_get_N(f));
    1749          49 :   NK = mkgNK(mf_get_gN(f), KK, CHI, mf_get_field(f));
    1750          49 :   return gerepilecopy(av, tag2(t_MF_POW, NK, f, gn));
    1751             : }
    1752             : GEN
    1753          21 : mfbracket(GEN f, GEN g, long m)
    1754             : {
    1755          21 :   pari_sp av = avma;
    1756             :   GEN N, K, NK, CHI;
    1757          21 :   if (!checkmf_i(f)) pari_err_TYPE("mfbracket",f);
    1758          21 :   if (!checkmf_i(g)) pari_err_TYPE("mfbracket",g);
    1759          21 :   if (m < 0) pari_err_TYPE("mfbracket [m<0]",stoi(m));
    1760          21 :   K = gaddgs(gadd(mf_get_gk(f), mf_get_gk(g)), 2*m);
    1761          21 :   if (gsigne(K) < 0) pari_err_IMPL("mfbracket for this form");
    1762          21 :   N = lcmii(mf_get_gN(f), mf_get_gN(g));
    1763          21 :   CHI = mfcharmul(mf_get_CHI(f), mf_get_CHI(g));
    1764          21 :   CHI = mfchiadjust(CHI, K, itou(N));
    1765          21 :   NK = mkgNK(N, K, CHI, mfsamefield(mf_get_field(f), mf_get_field(g)));
    1766          21 :   return gerepilecopy(av, tag3(t_MF_BRACKET, NK, f, g, utoi(m)));
    1767             : }
    1768             : 
    1769             : /* remove 0 entries in L */
    1770             : static int
    1771        1078 : mflinear_strip(GEN *pF, GEN *pL)
    1772             : {
    1773        1078 :   pari_sp av = avma;
    1774        1078 :   GEN F = *pF, L = *pL;
    1775        1078 :   long i, j, l = lg(L);
    1776        1078 :   GEN F2 = cgetg(l, t_VEC), L2 = cgetg(l, t_VEC);
    1777        6545 :   for (i = j = 1; i < l; i++)
    1778             :   {
    1779        5467 :     if (gequal0(gel(L,i))) continue;
    1780        3031 :     gel(F2,j) = gel(F,i);
    1781        3031 :     gel(L2,j) = gel(L,i); j++;
    1782             :   }
    1783        1078 :   if (j == l) avma = av;
    1784             :   else
    1785             :   {
    1786         280 :     setlg(F2,j); *pF = F2;
    1787         280 :     setlg(L2,j); *pL = L2;
    1788             :   }
    1789        1078 :   return (j > 1);
    1790             : }
    1791             : static GEN
    1792        4102 : taglinear_i(long t, GEN NK, GEN F, GEN L)
    1793             : {
    1794             :   GEN dL;
    1795        4102 :   L = Q_remove_denom(L, &dL); if (!dL) dL = gen_1;
    1796        4102 :   return tag3(t, NK, F, L, dL);
    1797             : }
    1798             : static GEN
    1799        1442 : taglinear(GEN NK, GEN F, GEN L)
    1800             : {
    1801        1442 :   long t = ok_bhn_linear(F)? t_MF_LINEAR_BHN: t_MF_LINEAR;
    1802        1442 :    return taglinear_i(t, NK, F, L);
    1803             : }
    1804             : /* assume F has parameters NK = [N,K,CHI] */
    1805             : static GEN
    1806         301 : mflinear_i(GEN NK, GEN F, GEN L)
    1807             : {
    1808         301 :   if (!mflinear_strip(&F,&L)) return mftrivial();
    1809         301 :   return taglinear(NK, F,L);
    1810             : }
    1811             : static GEN
    1812         462 : mflinear_bhn(GEN mf, GEN L)
    1813             : {
    1814             :   long i, l;
    1815         462 :   GEN P, NK, F = MF_get_S(mf);
    1816         462 :   if (!mflinear_strip(&F,&L)) return mftrivial();
    1817         455 :   l = lg(L); P = pol_x(1);
    1818        2520 :   for (i = 1; i < l; i++)
    1819             :   {
    1820        2065 :     GEN c = gel(L,i);
    1821        2065 :     if (typ(c) == t_POLMOD && varn(gel(c,1)) == 1) P = mfsamefield(P,gel(c,1));
    1822             :   }
    1823         455 :   NK = mkgNK(MF_get_gN(mf), MF_get_gk(mf), MF_get_CHI(mf), P);
    1824         455 :   return taglinear_i(t_MF_LINEAR_BHN,  NK, F,L);
    1825             : }
    1826             : 
    1827             : /* F vector of forms with same weight and character but varying level, return
    1828             :  * global [N,k,chi,P] */
    1829             : static GEN
    1830        1974 : vecmfNK(GEN F)
    1831             : {
    1832        1974 :   long i, l = lg(F);
    1833             :   GEN N, f;
    1834        1974 :   if (l == 1) return mkNK(1, 0, mfchartrivial());
    1835        1974 :   f = gel(F,1); N = mf_get_gN(f);
    1836        1974 :   for (i = 2; i < l; i++) N = lcmii(N, mf_get_gN(gel(F,i)));
    1837        1974 :   return mkgNK(N, mf_get_gk(f), mf_get_CHI(f), mf_get_field(f));
    1838             : }
    1839             : /* do not use mflinear: mflineardivtomat rely on F being constant across the
    1840             :  * basis where mflinear strips the ones matched by 0 coeffs. Assume k and CHI
    1841             :  * constant, N is allowed to vary. */
    1842             : static GEN
    1843         994 : vecmflinear(GEN F, GEN C)
    1844             : {
    1845         994 :   long i, t, l = lg(C);
    1846         994 :   GEN NK, v = cgetg(l, t_VEC);
    1847         994 :   if (l == 1) return v;
    1848         994 :   t = ok_bhn_linear(F)? t_MF_LINEAR_BHN: t_MF_LINEAR;
    1849         994 :   NK = vecmfNK(F);
    1850         994 :   for (i = 1; i < l; i++) gel(v,i) = taglinear_i(t, NK, F, gel(C,i));
    1851         994 :   return v;
    1852             : }
    1853             : /* vecmflinear(F,C), then divide everything by E, which has valuation 0 */
    1854             : static GEN
    1855         266 : vecmflineardiv0(GEN F, GEN C, GEN E)
    1856             : {
    1857         266 :   GEN v = vecmflinear(F, C);
    1858         266 :   long i, l = lg(v);
    1859         266 :   for (i = 1; i < l; i++) gel(v,i) = mfdiv_val(gel(v,i), E, 0);
    1860         266 :   return v;
    1861             : }
    1862             : 
    1863             : /* Non empty linear combination of linear combinations of same
    1864             :  * F_j=\sum_i \mu_{i,j}G_i so R = \sum_i (\sum_j(\la_j\mu_{i,j})) G_i */
    1865             : static GEN
    1866         980 : mflinear_linear(GEN F, GEN L, int strip)
    1867             : {
    1868         980 :   long l = lg(F), j;
    1869         980 :   GEN vF, M = cgetg(l, t_MAT);
    1870        6839 :   for (j = 1; j < l; j++)
    1871             :   {
    1872        5859 :     GEN f = gel(F,j), c = gel(f,3), d = gel(f,4);
    1873        5859 :     if (typ(c) == t_VEC) c = shallowtrans(c);
    1874        5859 :     if (!isint1(d)) c = RgC_Rg_div(c, d);
    1875        5859 :     gel(M,j) = c;
    1876             :   }
    1877         980 :   vF = gmael(F,1,2);
    1878         980 :   L = RgM_RgC_mul(M,L);
    1879         980 :   if (strip && !mflinear_strip(&vF,&L)) return mftrivial();
    1880         980 :   return taglinear(vecmfNK(vF), vF, L);
    1881             : }
    1882             : /* F non-empty vector of forms of the form mfdiv(mflinear(B,v), E) where E
    1883             :  * does not vanish at oo, or mflinear(B,v). Apply mflinear(F, L) */
    1884             : static GEN
    1885         980 : mflineardiv_linear(GEN F, GEN L, int strip)
    1886             : {
    1887         980 :   long l = lg(F), j;
    1888             :   GEN v, E, f;
    1889         980 :   if (lg(L) != l) pari_err_DIM("mflineardiv_linear");
    1890         980 :   f = gel(F,1); /* l > 1 */
    1891         980 :   if (mf_get_type(f) != t_MF_DIV) return mflinear_linear(F,L,strip);
    1892         812 :   E = gel(f,3);
    1893         812 :   v = cgetg(l, t_VEC);
    1894         812 :   for (j = 1; j < l; j++) { GEN f = gel(F,j); gel(v,j) = gel(f,2); }
    1895         812 :   return mfdiv_val(mflinear_linear(v,L,strip), E, 0);
    1896             : }
    1897             : static GEN
    1898         259 : vecmflineardiv_linear(GEN F, GEN M)
    1899             : {
    1900         259 :   long i, l = lg(M);
    1901         259 :   GEN v = cgetg(l, t_VEC);
    1902         259 :   for (i = 1; i < l; i++) gel(v,i) = mflineardiv_linear(F, gel(M,i), 0);
    1903         259 :   return v;
    1904             : }
    1905             : 
    1906             : static GEN
    1907         476 : tobasis(GEN mf, GEN F, GEN L)
    1908             : {
    1909         476 :   if (checkmf_i(L) && mf) return mftobasis(mf, L, 0);
    1910         469 :   if (typ(F) != t_VEC) pari_err_TYPE("mflinear",F);
    1911         469 :   if (!is_vec_t(typ(L))) pari_err_TYPE("mflinear",L);
    1912         469 :   if (lg(L) != lg(F)) pari_err_DIM("mflinear");
    1913         469 :   return L;
    1914             : }
    1915             : GEN
    1916         504 : mflinear(GEN F, GEN L)
    1917             : {
    1918         504 :   pari_sp av = avma;
    1919         504 :   GEN G, NK, P, mf = checkMF_i(F), N = NULL, K = NULL, CHI = NULL;
    1920             :   long i, l;
    1921         504 :   if (mf)
    1922             :   {
    1923         378 :     GEN gk = MF_get_gk(mf);
    1924         378 :     F = MF_get_basis(F);
    1925         378 :     if (typ(gk) != t_INT)
    1926          28 :       return gerepilecopy(av, mflineardiv_linear(F, L, 1));
    1927         350 :     if (itou(gk) > 1 && space_is_cusp(MF_get_space(mf)))
    1928             :     {
    1929         238 :       L = tobasis(mf, F, L);
    1930         238 :       return gerepilecopy(av, mflinear_bhn(mf, L));
    1931             :     }
    1932             :   }
    1933         238 :   L = tobasis(mf, F, L);
    1934         238 :   if (!mflinear_strip(&F,&L)) return mftrivial();
    1935             : 
    1936         231 :   l = lg(F);
    1937         231 :   if (l == 2 && gequal1(gel(L,1))) return gerepilecopy(av, gel(F,1));
    1938         175 :   P = pol_x(1);
    1939         581 :   for (i = 1; i < l; i++)
    1940             :   {
    1941         413 :     GEN f = gel(F,i), c = gel(L,i), Ni, Ki;
    1942         413 :     if (!checkmf_i(f)) pari_err_TYPE("mflinear", f);
    1943         413 :     Ni = mf_get_gN(f); N = N? lcmii(N, Ni): Ni;
    1944         413 :     Ki = mf_get_gk(f);
    1945         413 :     if (!K) K = Ki;
    1946         238 :     else if (!gequal(K, Ki))
    1947           7 :       pari_err_TYPE("mflinear [different weights]", mkvec2(K,Ki));
    1948         406 :     P = mfsamefield(P, mf_get_field(f));
    1949         406 :     if (typ(c) == t_POLMOD && varn(gel(c,1)) == 1) P = mfsamefield(P, gel(c,1));
    1950             :   }
    1951         168 :   G = znstar0(N,1);
    1952         560 :   for (i = 1; i < l; i++)
    1953             :   {
    1954         399 :     GEN CHI2 = mf_get_CHI(gel(F,i));
    1955         399 :     CHI2 = induce(G, CHI2);
    1956         399 :     if (!CHI) CHI = CHI2;
    1957         231 :     else if (!gequal(CHI, CHI2))
    1958           7 :       pari_err_TYPE("mflinear [different characters]", mkvec2(CHI,CHI2));
    1959             :   }
    1960         161 :   NK = mkgNK(N, K, CHI, P);
    1961         161 :   return gerepilecopy(av, taglinear(NK,F,L));
    1962             : }
    1963             : 
    1964             : GEN
    1965          42 : mfshift(GEN F, long sh)
    1966             : {
    1967          42 :   pari_sp av = avma;
    1968          42 :   if (!checkmf_i(F)) pari_err_TYPE("mfshift",F);
    1969          42 :   return gerepilecopy(av, tag2(t_MF_SHIFT, mf_get_NK(F), F, stoi(sh)));
    1970             : }
    1971             : static long
    1972          42 : mfval(GEN F)
    1973             : {
    1974          42 :   pari_sp av = avma;
    1975          42 :   long i = 0, n, sb;
    1976             :   GEN gk, gN;
    1977          42 :   if (!checkmf_i(F)) pari_err_TYPE("mfval", F);
    1978          42 :   gN = mf_get_gN(F);
    1979          42 :   gk = mf_get_gk(F);
    1980          42 :   sb = mfsturmNgk(itou(gN), gk);
    1981         105 :   for (n = 1; n <= sb;)
    1982             :   {
    1983             :     GEN v;
    1984          56 :     if (n > 0.5*sb) n = sb+1;
    1985          56 :     v = mfcoefs_i(F, n, 1);
    1986         112 :     for (; i <= n; i++)
    1987          91 :       if (!gequal0(gel(v, i+1))) { avma = av; return i; }
    1988          21 :     n <<= 1;
    1989             :   }
    1990           7 :   avma = av; return -1;
    1991             : }
    1992             : 
    1993             : GEN
    1994        1722 : mfdiv_val(GEN f, GEN g, long vg)
    1995             : {
    1996             :   GEN N, K, NK, CHI;
    1997        1722 :   if (vg) { f = mfshift(f,vg); g = mfshift(g,vg); }
    1998        1722 :   N = lcmii(mf_get_gN(f), mf_get_gN(g));
    1999        1722 :   K = gsub(mf_get_gk(f), mf_get_gk(g));
    2000        1722 :   CHI = mfchardiv(mf_get_CHI(f), mf_get_CHI(g));
    2001        1722 :   CHI = mfchiadjust(CHI, K, itos(N));
    2002        1722 :   NK = mkgNK(N, K, CHI, mfsamefield(mf_get_field(f), mf_get_field(g)));
    2003        1722 :   return tag2(t_MF_DIV, NK, f, g);
    2004             : }
    2005             : GEN
    2006          42 : mfdiv(GEN F, GEN G)
    2007             : {
    2008          42 :   pari_sp av = avma;
    2009          42 :   long v = mfval(G);
    2010          42 :   if (!checkmf_i(F)) pari_err_TYPE("mfdiv", F);
    2011          35 :   if (v < 0 || (v && !gequal0(mfcoefs(F, v-1, 1))))
    2012          14 :     pari_err_DOMAIN("mfdiv", "ord(G)", ">", strtoGENstr("ord(F)"),
    2013             :                     mkvec2(F, G));
    2014          21 :   return gerepilecopy(av, mfdiv_val(F, G, v));
    2015             : }
    2016             : GEN
    2017          28 : mfderiv(GEN F, long m)
    2018             : {
    2019          28 :   pari_sp av = avma;
    2020             :   GEN NK, gk;
    2021          28 :   if (!checkmf_i(F)) pari_err_TYPE("mfderiv",F);
    2022          28 :   gk = gaddgs(mf_get_gk(F), 2*m);
    2023          28 :   NK = mkgNK(mf_get_gN(F), gk, mf_get_CHI(F), mf_get_field(F));
    2024          28 :   return gerepilecopy(av, tag2(t_MF_DERIV, NK, F, stoi(m)));
    2025             : }
    2026             : GEN
    2027          21 : mfderivE2(GEN F, long m)
    2028             : {
    2029          21 :   pari_sp av = avma;
    2030             :   GEN NK, gk;
    2031          21 :   if (!checkmf_i(F)) pari_err_TYPE("mfderivE2",F);
    2032          21 :   if (m < 0) pari_err_DOMAIN("mfderivE2","m","<",gen_0,stoi(m));
    2033          21 :   gk = gaddgs(mf_get_gk(F), 2*m);
    2034          21 :   NK = mkgNK(mf_get_gN(F), gk, mf_get_CHI(F), mf_get_field(F));
    2035          21 :   return gerepilecopy(av, tag2(t_MF_DERIVE2, NK, F, stoi(m)));
    2036             : }
    2037             : 
    2038             : GEN
    2039          14 : mftwist(GEN F, GEN D)
    2040             : {
    2041          14 :   pari_sp av = avma;
    2042             :   GEN NK, CHI, NT, Da;
    2043             :   long q;
    2044          14 :   if (!checkmf_i(F)) pari_err_TYPE("mftwist", F);
    2045          14 :   if (typ(D) != t_INT) pari_err_TYPE("mftwist", D);
    2046          14 :   Da = mpabs_shallow(D);
    2047          14 :   CHI = mf_get_CHI(F); q = mfcharconductor(CHI);
    2048          14 :   NT = glcm(glcm(mf_get_gN(F), mulsi(q, Da)), sqri(Da));
    2049          14 :   NK = mkgNK(NT, mf_get_gk(F), CHI, mf_get_field(F));
    2050          14 :   return gerepilecopy(av, tag2(t_MF_TWIST, NK, F, D));
    2051             : }
    2052             : 
    2053             : /***************************************************************/
    2054             : /*                 Generic cache handling                      */
    2055             : /***************************************************************/
    2056             : enum { cache_FACT, cache_DIV, cache_H, cache_D, cache_DIH };
    2057             : typedef struct {
    2058             :   const char *name;
    2059             :   GEN cache;
    2060             :   ulong minself;
    2061             :   ulong maxself;
    2062             :   void (*init)(long);
    2063             :   ulong miss;
    2064             :   ulong maxmiss;
    2065             : } cache;
    2066             : 
    2067             : static void constfact(long lim);
    2068             : static void constdiv(long lim);
    2069             : static void consttabh(long lim);
    2070             : static void consttabdihedral(long lim);
    2071             : static void constcoredisc(long lim);
    2072             : static THREAD cache caches[] = {
    2073             : { "Factors",  NULL,  50000,    50000, &constfact, 0, 0 },
    2074             : { "Divisors", NULL,  50000,    50000, &constdiv, 0, 0 },
    2075             : { "H",        NULL, 100000, 10000000, &consttabh, 0, 0 },
    2076             : { "CorediscF",NULL, 100000, 10000000, &constcoredisc, 0, 0 },
    2077             : { "Dihedral", NULL,   1000,     3000, &consttabdihedral, 0, 0 },
    2078             : };
    2079             : 
    2080             : static void
    2081         315 : cache_reset(long id) { caches[id].miss = caches[id].maxmiss = 0; }
    2082             : static void
    2083        6380 : cache_delete(long id) { if (caches[id].cache) gunclone(caches[id].cache); }
    2084             : static void
    2085         322 : cache_set(long id, GEN S)
    2086             : {
    2087         322 :   GEN old = caches[id].cache;
    2088         322 :   caches[id].cache = gclone(S);
    2089         322 :   if (old) gunclone(old);
    2090         322 : }
    2091             : 
    2092             : /* handle a cache miss: store stats, possibly reset table; return value
    2093             :  * if (now) cached; return NULL on failure. HACK: some caches contain an
    2094             :  * ulong where the 0 value is impossible, and return it (typecase to GEN) */
    2095             : static GEN
    2096   140728464 : cache_get(long id, ulong D)
    2097             : {
    2098   140728464 :   cache *S = &caches[id];
    2099             :   /* cache_H is compressed: D=0,1 mod 4 */
    2100   140728464 :   const ulong d = (id == cache_H)? D>>1: D;
    2101             :   ulong max, l;
    2102             : 
    2103   140728464 :   if (!S->cache)
    2104             :   {
    2105         182 :     max = maxuu(minuu(D, S->maxself), S->minself);
    2106         182 :     S->init(max);
    2107         182 :     l = lg(S->cache);
    2108             :   }
    2109             :   else
    2110             :   {
    2111   140728282 :     l = lg(S->cache);
    2112   140728282 :     if (l <= d)
    2113             :     {
    2114         987 :       if (D > S->maxmiss) S->maxmiss = D;
    2115         987 :       if (DEBUGLEVEL >= 3)
    2116           0 :         err_printf("miss in cache %s: %lu, max = %lu\n",
    2117             :                    S->name, D, S->maxmiss);
    2118         987 :       if (S->miss++ >= 5 && D < S->maxself)
    2119             :       {
    2120          84 :         max = minuu(S->maxself, (long)(S->maxmiss * 1.2));
    2121          84 :         if (max <= S->maxself)
    2122             :         {
    2123          84 :           if (DEBUGLEVEL >= 3)
    2124           0 :             err_printf("resetting cache %s to %lu\n", S->name, max);
    2125          84 :           S->init(max); l = lg(S->cache);
    2126             :         }
    2127             :       }
    2128             :     }
    2129             :   }
    2130   140728464 :   return (l <= d)? NULL: gel(S->cache, d);
    2131             : }
    2132             : static GEN
    2133          70 : cache_report(long id)
    2134             : {
    2135          70 :   cache *S = &caches[id];
    2136          70 :   GEN v = zerocol(5);
    2137          70 :   gel(v,1) = strtoGENstr(S->name);
    2138          70 :   if (S->cache)
    2139             :   {
    2140          35 :     gel(v,2) = utoi(lg(S->cache)-1);
    2141          35 :     gel(v,3) = utoi(S->miss);
    2142          35 :     gel(v,4) = utoi(S->maxmiss);
    2143          35 :     gel(v,5) = utoi(gsizebyte(S->cache));
    2144             :   }
    2145          70 :   return v;
    2146             : }
    2147             : GEN
    2148          14 : getcache(void)
    2149             : {
    2150          14 :   pari_sp av = avma;
    2151          14 :   GEN M = cgetg(6, t_MAT);
    2152          14 :   gel(M,1) = cache_report(cache_FACT);
    2153          14 :   gel(M,2) = cache_report(cache_DIV);
    2154          14 :   gel(M,3) = cache_report(cache_H);
    2155          14 :   gel(M,4) = cache_report(cache_D);
    2156          14 :   gel(M,5) = cache_report(cache_DIH);
    2157          14 :   return gerepilecopy(av, shallowtrans(M));
    2158             : }
    2159             : 
    2160             : void
    2161        1595 : pari_close_mf(void)
    2162             : {
    2163        1595 :   cache_delete(cache_DIH);
    2164        1595 :   cache_delete(cache_DIV);
    2165        1595 :   cache_delete(cache_FACT);
    2166        1595 :   cache_delete(cache_H);
    2167        1595 : }
    2168             : 
    2169             : /*************************************************************************/
    2170             : /* a odd, update local cache (recycle memory) */
    2171             : static GEN
    2172        1885 : update_factor_cache(long a, long lim, long *pb)
    2173             : {
    2174        1885 :   const long step = 16000; /* even; don't increase this: RAM cache thrashing */
    2175        1885 :   if (a + 2*step > lim)
    2176         189 :     *pb = lim; /* fuse last 2 chunks */
    2177             :   else
    2178        1696 :     *pb = a + step;
    2179        1885 :   return vecfactoroddu_i(a, *pb);
    2180             : }
    2181             : /* assume lim < MAX_LONG/8 */
    2182             : static void
    2183          63 : constcoredisc(long lim)
    2184             : {
    2185          63 :   pari_sp av2, av = avma;
    2186          63 :   GEN D = caches[cache_D].cache, CACHE = NULL;
    2187          63 :   long cachea, cacheb, N, LIM = !D ? 4 : lg(D)-1;
    2188          63 :   if (lim <= 0) lim = 5;
    2189          63 :   if (lim <= LIM) return;
    2190          63 :   cache_reset(cache_D);
    2191          63 :   D = zero_zv(lim);
    2192          63 :   av2 = avma;
    2193          63 :   cachea = cacheb = 0;
    2194     7553322 :   for (N = 1; N <= lim; N+=2)
    2195             :   { /* N odd */
    2196             :     long i, d, d2;
    2197             :     GEN F;
    2198     7553259 :     if (N > cacheb)
    2199             :     {
    2200         924 :       avma = av2; cachea = N;
    2201         924 :       CACHE = update_factor_cache(N, lim, &cacheb);
    2202             :     }
    2203     7553259 :     F = gel(CACHE, ((N-cachea)>>1)+1); /* factoru(N) */
    2204     7553259 :     D[N] = d = corediscs_fact(F); /* = 3 mod 4 or 4 mod 16 */
    2205     7553259 :     d2 = odd(d)? d<<3: d<<1;
    2206     7553259 :     for (i = 1;;)
    2207             :     {
    2208    12588751 :       if ((N << i) > lim) break;
    2209     5035484 :       D[N<<i] = d2; i++;
    2210     5035484 :       if ((N << i) > lim) break;
    2211     2517746 :       D[N<<i] = d; i++;
    2212             :     }
    2213             :   }
    2214          63 :   cache_set(cache_D, D);
    2215          63 :   avma = av;
    2216             : }
    2217             : 
    2218             : static void
    2219          70 : constfact(long lim)
    2220             : {
    2221             :   pari_sp av;
    2222          70 :   GEN VFACT = caches[cache_FACT].cache;
    2223          70 :   long LIM = VFACT? lg(VFACT)-1: 4;
    2224          70 :   if (lim <= 0) lim = 5;
    2225          70 :   if (lim <= LIM) return;
    2226          63 :   cache_reset(cache_FACT); av = avma;
    2227          63 :   cache_set(cache_FACT, vecfactoru_i(1,lim)); avma = av;
    2228             : }
    2229             : static void
    2230          63 : constdiv(long lim)
    2231             : {
    2232             :   pari_sp av;
    2233          63 :   GEN VFACT, VDIV = caches[cache_DIV].cache;
    2234          63 :   long N, LIM = VDIV? lg(VDIV)-1: 4;
    2235          63 :   if (lim <= 0) lim = 5;
    2236          63 :   if (lim <= LIM) return;
    2237          63 :   constfact(lim);
    2238          63 :   VFACT = caches[cache_FACT].cache;
    2239          63 :   cache_reset(cache_DIV); av = avma;
    2240          63 :   VDIV  = cgetg(lim+1, t_VEC);
    2241          63 :   for (N = 1; N <= lim; N++) gel(VDIV,N) = divisorsu_fact(gel(VFACT,N));
    2242          63 :   cache_set(cache_DIV, VDIV); avma = av;
    2243             : }
    2244             : 
    2245             : /* n > 1, D = divisors(n); sets L = 2*lambda(n), S = sigma(n) */
    2246             : static void
    2247     9517628 : lamsig(GEN D, long *pL, long *pS)
    2248             : {
    2249     9517628 :   pari_sp av = avma;
    2250     9517628 :   long i, l = lg(D), L = 1, S = D[l-1]+1;
    2251    34382155 :   for (i = 2; i < l; i++) /* skip d = 1 */
    2252             :   {
    2253    34382155 :     long d = D[i], nd = D[l-i]; /* nd = n/d */
    2254    34382155 :     if (d < nd) { L += d; S += d + nd; }
    2255             :     else
    2256             :     {
    2257     9517628 :       L <<= 1; if (d == nd) { L += d; S += d; }
    2258     9517628 :       break;
    2259             :     }
    2260             :   }
    2261     9517628 :   avma = av; *pL = L; *pS = S;
    2262     9517628 : }
    2263             : /* table of 6 * Hurwitz class numbers D <= lim */
    2264             : static void
    2265         126 : consttabh(long lim)
    2266             : {
    2267         126 :   pari_sp av = avma, av2;
    2268         126 :   GEN VHDH0, VDIV, CACHE = NULL;
    2269         126 :   GEN VHDH = caches[cache_H].cache;
    2270         126 :   long r, N, cachea, cacheb, lim0 = VHDH? lg(VHDH)-1: 2, LIM = lim0 << 1;
    2271             : 
    2272         126 :   if (lim <= 0) lim = 5;
    2273         126 :   if (lim <= LIM) return;
    2274         126 :   cache_reset(cache_H);
    2275         126 :   r = lim&3L; if (r) lim += 4-r;
    2276         126 :   cache_get(cache_DIV, lim);
    2277         126 :   VDIV = caches[cache_DIV].cache;
    2278         126 :   VHDH0 = cgetg(lim/2 + 1, t_VECSMALL);
    2279         126 :   VHDH0[1] = 2;
    2280         126 :   VHDH0[2] = 3;
    2281         126 :   for (N = 3; N <= lim0; N++) VHDH0[N] = VHDH[N];
    2282         126 :   av2 = avma;
    2283         126 :   cachea = cacheb = 0;
    2284     4758940 :   for (N = LIM + 3; N <= lim; N += 4)
    2285             :   {
    2286     4758814 :     long s = 0, limt = usqrt(N>>2), flsq = 0, ind, t, L, S;
    2287             :     GEN DN, DN2;
    2288     4758814 :     if (N + 2 >= lg(VDIV))
    2289             :     { /* use local cache */
    2290             :       GEN F;
    2291     3971440 :       if (N + 2 > cacheb)
    2292             :       {
    2293         961 :         avma = av2; cachea = N;
    2294         961 :         CACHE = update_factor_cache(N, lim+2, &cacheb);
    2295             :       }
    2296     3971440 :       F = gel(CACHE, ((N-cachea)>>1)+1); /* factoru(N) */
    2297     3971440 :       DN = divisorsu_fact(F);
    2298     3971440 :       F = gel(CACHE, ((N-cachea)>>1)+2); /* factoru(N+2) */
    2299     3971440 :       DN2 = divisorsu_fact(F);
    2300             :     }
    2301             :     else
    2302             :     { /* use global cache */
    2303      787374 :       DN = gel(VDIV,N);
    2304      787374 :       DN2 = gel(VDIV,N+2);
    2305             :     }
    2306     4758814 :     ind = N >> 1;
    2307  1059809401 :     for (t = 1; t <= limt; t++)
    2308             :     {
    2309  1055050587 :       ind -= (t<<2)-2; /* N/2 - 2t^2 */
    2310  1055050587 :       if (ind) s += VHDH0[ind]; else flsq = 1;
    2311             :     }
    2312     4758814 :     lamsig(DN, &L,&S);
    2313     4758814 :     VHDH0[N >> 1] = 2*S - 3*L - 2*s + flsq;
    2314     4758814 :     s = 0; flsq = 0; limt = (usqrt(N+2) - 1) >> 1;
    2315     4758814 :     ind = (N+1) >> 1;
    2316  1057451335 :     for (t = 1; t <= limt; t++)
    2317             :     {
    2318  1052692521 :       ind -= t<<2; /* (N+1)/2 - 2t(t+1) */
    2319  1052692521 :       if (ind) s += VHDH0[ind]; else flsq = 1;
    2320             :     }
    2321     4758814 :     lamsig(DN2, &L,&S);
    2322     4758814 :     VHDH0[(N+1) >> 1] = S - 3*(L >> 1) - s - flsq;
    2323             :   }
    2324         126 :   cache_set(cache_H, VHDH0); avma = av;
    2325             : }
    2326             : 
    2327             : /*************************************************************************/
    2328             : /* Core functions using factorizations, divisors of class numbers caches */
    2329             : /* TODO: myfactoru and factorization cache should be exported */
    2330             : static GEN
    2331    15330301 : myfactoru(long N)
    2332             : {
    2333    15330301 :   GEN z = cache_get(cache_FACT, N);
    2334    15330301 :   return z? gcopy(z): factoru(N);
    2335             : }
    2336             : static GEN
    2337    37652433 : mydivisorsu(long N)
    2338             : {
    2339    37652433 :   GEN z = cache_get(cache_DIV, N);
    2340    37652433 :   return z? leafcopy(z): divisorsu(N);
    2341             : }
    2342             : /* write -n = Df^2, D < 0 fundamental discriminant. Return D, set f. */
    2343             : static long
    2344    47230911 : mycoredisc2neg(ulong n, long *pf)
    2345             : {
    2346    47230911 :   ulong m, D = (ulong)cache_get(cache_D, n);
    2347    47230911 :   if (D) { *pf = usqrt(n/D); return -(long)D; }
    2348         196 :   m = mycore(n, pf);
    2349         196 :   if ((m&3) != 3) { m <<= 2; *pf >>= 1; }
    2350         196 :   return (long)-m;
    2351             : }
    2352             : /* write n = Df^2, D > 0 fundamental discriminant. Return D, set f. */
    2353             : static long
    2354          14 : mycoredisc2pos(ulong n, long *pf)
    2355             : {
    2356          14 :   ulong m = mycore(n, pf);
    2357          14 :   if ((m&3) != 1) { m <<= 2; *pf >>= 1; }
    2358          14 :   return (long)m;
    2359             : }
    2360             : 
    2361             : /* 1+p+...+p^e, e >= 1 */
    2362             : static ulong
    2363          49 : usumpow(ulong p, long e)
    2364             : {
    2365          49 :   ulong q = 1+p;
    2366             :   long i;
    2367          49 :   for (i = 1; i < e; i++) q = p*q + 1;
    2368          49 :   return q;
    2369             : }
    2370             : /* Hurwitz(D0 F^2)/ Hurwitz(D0)
    2371             :  * = \sum_{f|F}  f \prod_{p|f} (1-kro(D0/p)/p)
    2372             :  * = \prod_{p^e || F} (1 + (p^e-1) / (p-1) * (p-kro(D0/p))) */
    2373             : static long
    2374         294 : get_sh(long F, long D0)
    2375             : {
    2376         294 :   GEN fa = myfactoru(F), P = gel(fa,1), E = gel(fa,2);
    2377         294 :   long i, l = lg(P), t = 1;
    2378         794 :   for (i = 1; i < l; i++)
    2379             :   {
    2380         500 :     long p = P[i], e = E[i], s = kross(D0,p);
    2381         500 :     if (e == 1) { t *= 1 + p - s; continue; }
    2382         153 :     if (s == 1) { t *= upowuu(p,e); continue; }
    2383          49 :     t *= 1 + usumpow(p,e-1)*(p-s);
    2384             :   }
    2385         294 :   return t;
    2386             : }
    2387             : /* d > 0, d = 0,3 (mod 4). Return 6*hclassno(d); -d must be fundamental
    2388             :  * Faster than quadclassunit up to 5*10^5 or so */
    2389             : static ulong
    2390          42 : hclassno6u_count(ulong d)
    2391             : {
    2392          42 :   ulong a, b, b2, h = 0;
    2393          42 :   int f = 0;
    2394             : 
    2395          42 :   if (d > 500000)
    2396           7 :     return 6 * itou(gel(quadclassunit0(utoineg(d), 0, NULL, 0), 1));
    2397             : 
    2398             :   /* this part would work with -d non fundamental */
    2399          35 :   b = d&1; b2 = (1+d)>>2;
    2400          35 :   if (!b)
    2401             :   {
    2402           0 :     for (a=1; a*a<b2; a++)
    2403           0 :       if (b2%a == 0) h++;
    2404           0 :     f = (a*a==b2); b=2; b2=(4+d)>>2;
    2405             :   }
    2406        7168 :   while (b2*3 < d)
    2407             :   {
    2408        7098 :     if (b2%b == 0) h++;
    2409     1188551 :     for (a=b+1; a*a < b2; a++)
    2410     1181453 :       if (b2%a == 0) h += 2;
    2411        7098 :     if (a*a == b2) h++;
    2412        7098 :     b += 2; b2 = (b*b+d)>>2;
    2413             :   }
    2414          35 :   if (b2*3 == d) return 6*h+2;
    2415          35 :   if (f) return 6*h+3;
    2416          35 :   return 6*h;
    2417             : }
    2418             : /* D > 0; 6 * hclassno(D), using D = D0*F^2 */
    2419             : static long
    2420         336 : hclassno6u_2(ulong D, long D0, long F)
    2421             : {
    2422             :   long h;
    2423         336 :   if (F == 1) h = hclassno6u_count(D);
    2424             :   else
    2425             :   { /* second chance */
    2426         294 :     h = (ulong)cache_get(cache_H, -D0);
    2427         294 :     if (!h) h = hclassno6u_count(-D0);
    2428         294 :     h *= get_sh(F,D0);
    2429             :   }
    2430         336 :   return h;
    2431             : }
    2432             : /* D > 0; 6 * hclassno(D) (6*Hurwitz). Beware, cached value for D (=0,3 mod 4)
    2433             :  * is stored at D>>1 */
    2434             : ulong
    2435      155745 : hclassno6u(ulong D)
    2436             : {
    2437      155745 :   ulong z = (ulong)cache_get(cache_H, D);
    2438             :   long D0, F;
    2439      155745 :   if (z) return z;
    2440         336 :   D0 = mycoredisc2neg(D, &F);
    2441         336 :   return hclassno6u_2(D,D0,F);
    2442             : }
    2443             : /* same, where the decomposition D = D0*F^2 is already known */
    2444             : static ulong
    2445    35546532 : hclassno6u_i(ulong D, long D0, long F)
    2446             : {
    2447    35546532 :   ulong z = (ulong)cache_get(cache_H, D);
    2448    35546532 :   if (z) return z;
    2449           0 :   return hclassno6u_2(D,D0,F);
    2450             : }
    2451             : 
    2452             : #if 0
    2453             : /* D > 0, return h(-D) [ordinary class number].
    2454             :  * Assume consttabh(D or more) was previously called */
    2455             : static long
    2456             : hfromH(long D)
    2457             : {
    2458             :   pari_sp ltop = avma;
    2459             :   GEN m, d, fa = myfactoru(D), P = gel(fa,1), E = gel(fa,2);
    2460             :   GEN VH = caches[cache_H].cache;
    2461             :   long i, nd, S, l = lg(P);
    2462             : 
    2463             :   /* n = d[i] loops through squarefree divisors of f, where f^2 = largest square
    2464             :    * divisor of N = |D|; m[i] = moebius(n) */
    2465             :   nd = 1 << (l-1);
    2466             :   d = cgetg(nd+1, t_VECSMALL);
    2467             :   m = cgetg(nd+1, t_VECSMALL);
    2468             :   d[1] = 1; S = VH[D >> 1]; /* 6 hclassno(-D) */
    2469             :   m[1] = 1; nd = 1;
    2470             :   i = 1;
    2471             :   if (P[1] == 2 && E[1] <= 3) /* need D/n^2 to be a discriminant */
    2472             :   { if (odd(E[1]) || (E[1] == 2 && (D & 15) == 4)) i = 2; }
    2473             :   for (; i<l; i++)
    2474             :   {
    2475             :     long j, p = P[i];
    2476             :     if (E[i] == 1) continue;
    2477             :     for (j=1; j<=nd; j++)
    2478             :     {
    2479             :       long n, s, hn;
    2480             :       d[nd+j] = n = d[j] * p;
    2481             :       m[nd+j] = s = - m[j]; /* moebius(n) */
    2482             :       hn = VH[(D/(n*n)) >> 1]; /* 6 hclassno(-D/n^2) */
    2483             :       if (s > 0) S += hn; else S -= hn;
    2484             :     }
    2485             :     nd <<= 1;
    2486             :   }
    2487             :   avma = ltop; return S/6;
    2488             : }
    2489             : #endif
    2490             : /* D < -4 fundamental, h(D), ordinary class number */
    2491             : static long
    2492     4788693 : myh(long D)
    2493             : {
    2494     4788693 :   ulong z = (ulong)cache_get(cache_H, -D);
    2495     4788693 :   if (z) return z/6; /* should be hfromH(-D) if D non-fundamental */
    2496           0 :   return itou(quadclassno(stoi(D)));
    2497             : }
    2498             : 
    2499             : /*************************************************************************/
    2500             : /*                          TRACE FORMULAS                               */
    2501             : /* CHIP primitive, initialize for t_POLMOD output */
    2502             : static GEN
    2503       24892 : mfcharinit(GEN CHIP)
    2504             : {
    2505       24892 :   long n, o, l, vt, N = mfcharmodulus(CHIP);
    2506             :   GEN c, v, V, G, Pn;
    2507       24892 :   if (N == 1) return mkvec2(mkvec(gen_1), pol_x(0));
    2508        3997 :   G = gel(CHIP,1);
    2509        3997 :   v = ncharvecexpo(G, znconrey_normalized(G, gel(CHIP,2)));
    2510        3997 :   l = lg(v); V = cgetg(l, t_VEC);
    2511        3997 :   o = mfcharorder(CHIP);
    2512        3997 :   Pn = mfcharpol(CHIP); vt = varn(Pn);
    2513        3997 :   if (o <= 2)
    2514             :   {
    2515       29176 :     for (n = 1; n < l; n++)
    2516             :     {
    2517       26103 :       if (v[n] < 0) c = gen_0; else c = v[n]? gen_m1: gen_1;
    2518       26103 :       gel(V,n) = c;
    2519             :     }
    2520             :   }
    2521             :   else
    2522             :   {
    2523       16639 :     for (n = 1; n < l; n++)
    2524             :     {
    2525       15715 :       if (v[n] < 0) c = gen_0;
    2526             :       else
    2527             :       {
    2528        8750 :         c = mygmodulo_lift(v[n], o, gen_1, vt);
    2529        8750 :         if (typ(c) == t_POL && lg(c) >= lg(Pn)) c = RgX_rem(c, Pn);
    2530             :       }
    2531       15715 :       gel(V,n) = c;
    2532             :     }
    2533             :   }
    2534        3997 :   return mkvec2(V, Pn);
    2535             : }
    2536             : static GEN
    2537      332388 : vchip_lift(GEN VCHI, long x, GEN C)
    2538             : {
    2539      332388 :   GEN V = gel(VCHI,1);
    2540      332388 :   long F = lg(V)-1;
    2541      332388 :   if (F == 1) return C;
    2542       26068 :   x %= F;
    2543       26068 :   if (!x) return C;
    2544       26068 :   if (x <= 0) x += F;
    2545       26068 :   return gmul(C, gel(V, x));
    2546             : }
    2547             : static long
    2548    78766114 : vchip_FC(GEN VCHI) { return lg(gel(VCHI,1))-1; }
    2549             : static GEN
    2550     3225670 : vchip_mod(GEN VCHI, GEN S)
    2551     3225670 : { return (typ(S) == t_POL)? RgX_rem(S, gel(VCHI,2)): S; }
    2552             : static GEN
    2553      964537 : vchip_polmod(GEN VCHI, GEN S)
    2554      964537 : { return (typ(S) == t_POL)? mkpolmod(S, gel(VCHI,2)): S; }
    2555             : 
    2556             : /* ceil(m/d) */
    2557             : static long
    2558      104517 : ceildiv(long m, long d)
    2559             : {
    2560             :   long q;
    2561      104517 :   if (!m) return 0;
    2562       37345 :   q = m/d; return m%d? q+1: q;
    2563             : }
    2564             : 
    2565             : /* contribution of scalar matrices in dimension formula */
    2566             : static GEN
    2567      225764 : A1(long N, long k)
    2568      225764 : { return sstoQ(mypsiu(N)*(k-1), 12); }
    2569             : static long
    2570        7224 : ceilA1(long N, long k)
    2571        7224 : { return ceildiv(mypsiu(N) * (k-1), 12); }
    2572             : 
    2573             : /* sturm bound, slightly larger than dimension */
    2574             : long
    2575       28903 : mfsturmNk(long N, long k) { return 1 + (mypsiu(N)*k)/12; }
    2576             : long
    2577        1925 : mfsturmNgk(long N, GEN k)
    2578             : {
    2579        1925 :   long n,d; Qtoss(k,&n,&d);
    2580        1925 :   return (d == 1)? mfsturmNk(N,n): 1 + (mypsiu(N)*n)/24;
    2581             : }
    2582             : 
    2583             : /* List of all solutions of x^2 + x + 1 = 0 modulo N, x modulo N */
    2584             : static GEN
    2585         504 : sqrtm3modN(long N)
    2586             : {
    2587             :   pari_sp av;
    2588             :   GEN fa, P, E, B, mB, A, Q, T, R, v, gen_m3;
    2589         504 :   long l, i, n, ct, fl3 = 0, Ninit;
    2590         504 :   if (!odd(N) || (N%9) == 0) return cgetg(1,t_VECSMALL);
    2591         476 :   Ninit = N;
    2592         476 :   if ((N%3) == 0) { N /= 3; fl3 = 1; }
    2593         476 :   fa = myfactoru(N); P = gel(fa, 1); E = gel(fa, 2);
    2594         476 :   l = lg(P);
    2595         665 :   for (i = 1; i < l; i++)
    2596         483 :     if ((P[i]%3) == 2) return cgetg(1,t_VECSMALL);
    2597         182 :   A = cgetg(l, t_VECSMALL);
    2598         182 :   B = cgetg(l, t_VECSMALL);
    2599         182 :   mB= cgetg(l, t_VECSMALL);
    2600         182 :   Q = cgetg(l, t_VECSMALL); gen_m3 = utoineg(3);
    2601         371 :   for (i = 1; i < l; i++)
    2602             :   {
    2603         189 :     long p = P[i], e = E[i];
    2604         189 :     Q[i] = upowuu(p,e);
    2605         189 :     B[i] = itou( Zp_sqrt(gen_m3, utoipos(p), e) );
    2606         189 :     mB[i]= Q[i] - B[i];
    2607             :   }
    2608         182 :   ct = 1 << (l-1);
    2609         182 :   T = ZV_producttree(Q);
    2610         182 :   R = ZV_chinesetree(Q,T);
    2611         182 :   v = cgetg(ct+1, t_VECSMALL);
    2612         182 :   av = avma;
    2613         560 :   for (n = 1; n <= ct; n++)
    2614             :   {
    2615         378 :     long m = n-1, r;
    2616         784 :     for (i = 1; i < l; i++)
    2617             :     {
    2618         406 :       A[i] = (m&1L)? mB[i]: B[i];
    2619         406 :       m >>= 1;
    2620             :     }
    2621         378 :     r = itou( ZV_chinese_tree(A, Q, T, R) );
    2622         378 :     if (fl3) while (r%3) r += N;
    2623         378 :     avma = av; v[n] = odd(r) ? (r-1) >> 1 : (r+Ninit-1) >> 1;
    2624             :   }
    2625         182 :   return v;
    2626             : }
    2627             : 
    2628             : /* number of elliptic points of order 3 in X0(N) */
    2629             : static long
    2630        9233 : nu3(long N)
    2631             : {
    2632             :   long i, l;
    2633             :   GEN P;
    2634        9233 :   if (!odd(N) || (N%9) == 0) return 0;
    2635        8239 :   if ((N%3) == 0) N /= 3;
    2636        8239 :   P = gel(myfactoru(N), 1); l = lg(P);
    2637        8239 :   for (i = 1; i < l; i++) if ((P[i]%3) == 2) return 0;
    2638        3619 :   return 1L<<(l-1);
    2639             : }
    2640             : /* number of elliptic points of order 2 in X0(N) */
    2641             : static long
    2642       16058 : nu2(long N)
    2643             : {
    2644             :   long i, l;
    2645             :   GEN P;
    2646       16058 :   if ((N&3L) == 0) return 0;
    2647       16058 :   if (!odd(N)) N >>= 1;
    2648       16058 :   P = gel(myfactoru(N), 1); l = lg(P);
    2649       16058 :   for (i = 1; i < l; i++) if ((P[i]&3L) == 3) return 0;
    2650        3654 :   return 1L<<(l-1);
    2651             : }
    2652             : 
    2653             : /* contribution of elliptic matrices of order 3 in dimension formula
    2654             :  * Only depends on CHIP the primitive char attached to CHI */
    2655             : static GEN
    2656       39599 : A21(long N, long k, GEN CHI)
    2657             : {
    2658             :   GEN res, G, chi, o;
    2659             :   long a21, i, limx, S;
    2660       39599 :   if ((N&1L) == 0) return gen_0;
    2661       19250 :   a21 = k%3 - 1;
    2662       19250 :   if (!a21) return gen_0;
    2663       18606 :   if (N <= 3) return sstoQ(a21, 3);
    2664        9737 :   if (!CHI) return sstoQ(nu3(N) * a21, 3);
    2665         504 :   res = sqrtm3modN(N); limx = (N - 1) >> 1;
    2666         504 :   G = gel(CHI,1); chi = gel(CHI,2);
    2667         504 :   o = gmfcharorder(CHI);
    2668         882 :   for (S = 0, i = 1; i < lg(res); i++)
    2669             :   { /* (x,N) = 1; S += chi(x) + chi(x^2) */
    2670         378 :     long x = res[i];
    2671         378 :     if (x <= limx)
    2672             :     { /* CHI(x)=e(c/o), 3rd-root of 1 */
    2673         189 :       GEN c = znchareval(G, chi, utoi(x), o);
    2674         189 :       if (!signe(c)) S += 2; else S--;
    2675             :     }
    2676             :   }
    2677         504 :   return sstoQ(a21 * S, 3);
    2678             : }
    2679             : 
    2680             : /* List of all square roots of -1 modulo N */
    2681             : static GEN
    2682         567 : sqrtm1modN(long N)
    2683             : {
    2684             :   pari_sp av;
    2685             :   GEN fa, P, E, B, mB, A, Q, T, R, v;
    2686         567 :   long l, i, n, ct, fleven = 0;
    2687         567 :   if ((N&3L) == 0) return cgetg(1,t_VECSMALL);
    2688         567 :   if ((N&1L) == 0) { N >>= 1; fleven = 1; }
    2689         567 :   fa = myfactoru(N); P = gel(fa,1); E = gel(fa,2);
    2690         567 :   l = lg(P);
    2691         917 :   for (i = 1; i < l; i++)
    2692         637 :     if ((P[i]&3L) == 3) return cgetg(1,t_VECSMALL);
    2693         280 :   A = cgetg(l, t_VECSMALL);
    2694         280 :   B = cgetg(l, t_VECSMALL);
    2695         280 :   mB= cgetg(l, t_VECSMALL);
    2696         280 :   Q = cgetg(l, t_VECSMALL);
    2697         574 :   for (i = 1; i < l; i++)
    2698             :   {
    2699         294 :     long p = P[i], e = E[i];
    2700         294 :     Q[i] = upowuu(p,e);
    2701         294 :     B[i] = itou( Zp_sqrt(gen_m1, utoipos(p), e) );
    2702         294 :     mB[i]= Q[i] - B[i];
    2703             :   }
    2704         280 :   ct = 1 << (l-1);
    2705         280 :   T = ZV_producttree(Q);
    2706         280 :   R = ZV_chinesetree(Q,T);
    2707         280 :   v = cgetg(ct+1, t_VECSMALL);
    2708         280 :   av = avma;
    2709         868 :   for (n = 1; n <= ct; n++)
    2710             :   {
    2711         588 :     long m = n-1, r;
    2712        1232 :     for (i = 1; i < l; i++)
    2713             :     {
    2714         644 :       A[i] = (m&1L)? mB[i]: B[i];
    2715         644 :       m >>= 1;
    2716             :     }
    2717         588 :     r = itou( ZV_chinese_tree(A, Q, T, R) );
    2718         588 :     if (fleven && !odd(r)) r += N;
    2719         588 :     avma = av; v[n] = r;
    2720             :   }
    2721         280 :   return v;
    2722             : }
    2723             : 
    2724             : /* contribution of elliptic matrices of order 4 in dimension formula.
    2725             :  * Only depends on CHIP the primitive char attached to CHI */
    2726             : static GEN
    2727       39599 : A22(long N, long k, GEN CHI)
    2728             : {
    2729             :   GEN G, chi, o, res;
    2730             :   long S, a22, i, limx, o2;
    2731       39599 :   if ((N&3L) == 0) return gen_0;
    2732       27636 :   a22 = (k & 3L) - 1; /* (k % 4) - 1 */
    2733       27636 :   if (!a22) return gen_0;
    2734       27636 :   if (N <= 2) return sstoQ(a22, 4);
    2735       16828 :   if (!CHI) return sstoQ(nu2(N)*a22, 4);
    2736         770 :   if (mfcharparity(CHI) == -1) return gen_0;
    2737         567 :   res = sqrtm1modN(N); limx = (N - 1) >> 1;
    2738         567 :   G = gel(CHI,1); chi = gel(CHI,2);
    2739         567 :   o = gmfcharorder(CHI);
    2740         567 :   o2 = itou(o)>>1;
    2741        1155 :   for (S = 0, i = 1; i < lg(res); i++)
    2742             :   { /* (x,N) = 1, S += real(chi(x)) */
    2743         588 :     long x = res[i];
    2744         588 :     if (x <= limx)
    2745             :     { /* CHI(x)=e(c/o), 4th-root of 1 */
    2746         294 :       long c = itou( znchareval(G, chi, utoi(x), o) );
    2747         294 :       if (!c) S++; else if (c == o2) S--;
    2748             :     }
    2749             :   }
    2750         567 :   return sstoQ(a22 * S, 2);
    2751             : }
    2752             : 
    2753             : /* sumdiv(N,d,eulerphi(gcd(d,N/d))) */
    2754             : static long
    2755       35658 : nuinf(long N)
    2756             : {
    2757       35658 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
    2758       35658 :   long i, t = 1, l = lg(P);
    2759       75852 :   for (i=1; i<l; i++)
    2760             :   {
    2761       40194 :     long p = P[i], e = E[i];
    2762       40194 :     if (odd(e))
    2763       32242 :       t *= upowuu(p,e>>1) << 1;
    2764             :     else
    2765        7952 :       t *= upowuu(p,(e>>1)-1) * (p+1);
    2766             :   }
    2767       35658 :   return t;
    2768             : }
    2769             : 
    2770             : /* contribution of hyperbolic matrices in dimension formula */
    2771             : static GEN
    2772       39998 : A3(long N, long FC)
    2773             : {
    2774             :   long i, S, NF, l;
    2775             :   GEN D;
    2776       39998 :   if (FC == 1) return sstoQ(nuinf(N),2);
    2777        4340 :   D = mydivisorsu(N); l = lg(D);
    2778        4340 :   S = 0; NF = N/FC;
    2779       33173 :   for (i = 1; i < l; i++)
    2780             :   {
    2781       28833 :     long g = ugcd(D[i], D[l-i]);
    2782       28833 :     if (NF%g == 0) S += myeulerphiu(g);
    2783             :   }
    2784        4340 :   return sstoQ(S, 2);
    2785             : }
    2786             : 
    2787             : /* special contribution in weight 2 in dimension formula */
    2788             : static long
    2789       39263 : A4(long k, long FC)
    2790       39263 : { return (k==2 && FC==1)? 1: 0; }
    2791             : /* gcd(x,N) */
    2792             : static long
    2793    93268364 : myugcd(GEN GCD, ulong x)
    2794             : {
    2795    93268364 :   ulong N = lg(GCD)-1;
    2796    93268364 :   if (x >= N) x %= N;
    2797    93268364 :   return GCD[x+1];
    2798             : }
    2799             : /* 1_{gcd(x,N) = 1} * chi(x), return NULL if 0 */
    2800             : static GEN
    2801   126055307 : mychicgcd(GEN GCD, GEN VCHI, long x)
    2802             : {
    2803   126055307 :   long N = lg(GCD)-1;
    2804   126055307 :   if (N == 1) return gen_1;
    2805   109261474 :   x = smodss(x, N);
    2806   109261474 :   if (GCD[x+1] != 1) return NULL;
    2807    75394011 :   x %= vchip_FC(VCHI); if (!x) return gen_1;
    2808     6634362 :   return gel(gel(VCHI,1), x);
    2809             : }
    2810             : 
    2811             : /* contribution of scalar matrices to trace formula */
    2812             : static GEN
    2813     3062101 : TA1(long N, long k, GEN VCHI, GEN GCD, long n)
    2814             : {
    2815             :   GEN S;
    2816             :   ulong m;
    2817     3062101 :   if (!uissquareall(n, &m)) return gen_0;
    2818      242123 :   if (m == 1) return A1(N,k); /* common */
    2819      212261 :   S = mychicgcd(GCD, VCHI, m);
    2820      212261 :   return S? gmul(gmul(powuu(m, k-2), A1(N,k)), S): gen_0;
    2821             : }
    2822             : 
    2823             : /* All square roots modulo 4N, x modulo 2N, precomputed to accelerate TA2 */
    2824             : static GEN
    2825       98763 : mksqr(long N)
    2826             : {
    2827       98763 :   pari_sp av = avma;
    2828       98763 :   long x, N2 = N << 1, N4 = N << 2;
    2829       98763 :   GEN v = const_vec(N2, cgetg(1, t_VECSMALL));
    2830       98763 :   gel(v, N2) = mkvecsmall(0); /* x = 0 */
    2831     2271276 :   for (x = 1; x <= N; x++)
    2832             :   {
    2833     2172513 :     long r = (((x*x - 1)%N4) >> 1) + 1;
    2834     2172513 :     gel(v,r) = vecsmall_append(gel(v,r), x);
    2835             :   }
    2836       98763 :   return gerepilecopy(av, v);
    2837             : }
    2838             : 
    2839             : static GEN
    2840       98763 : mkgcd(long N)
    2841             : {
    2842             :   GEN GCD, d;
    2843             :   long i, N2;
    2844       98763 :   if (N == 1) return mkvecsmall(N);
    2845       81711 :   GCD = cgetg(N + 1, t_VECSMALL);
    2846       81711 :   d = GCD+1; /* GCD[i+1] = d[i] = gcd(i,N) = gcd(N-i,N), i = 0..N-1 */
    2847       81711 :   d[0] = N; d[1] = d[N-1] = 1; N2 = N>>1;
    2848       81711 :   for (i = 2; i <= N2; i++) d[i] = d[N-i] = ugcd(N, i);
    2849       81711 :   return GCD;
    2850             : }
    2851             : 
    2852             : /* Table of \sum_{x^2-tx+n=0 mod Ng}chi(x) for all g dividing gcd(N,F),
    2853             :  * F^2 largest such that (t^2-4n)/F^2=0 or 1 mod 4; t >= 0 */
    2854             : static GEN
    2855     9652818 : mutglistall(long t, long N, long NF, GEN VCHI, long n, GEN MUP, GEN li, GEN GCD)
    2856             : {
    2857     9652818 :   long i, lx = lg(li);
    2858     9652818 :   GEN DNF = mydivisorsu(NF), v = zerovec(NF);
    2859     9652818 :   long j, g, lDNF = lg(DNF);
    2860    26467364 :   for (i = 1; i < lx; i++)
    2861             :   {
    2862    16814546 :     long x = (li[i] + t) >> 1, y, lD;
    2863    16814546 :     GEN D, c = mychicgcd(GCD, VCHI, x);
    2864    16814546 :     if (li[i] && li[i] != N)
    2865             :     {
    2866    11106011 :       GEN c2 = mychicgcd(GCD, VCHI, t - x);
    2867    11106011 :       if (c2) c = c? gadd(c, c2): c2;
    2868             :     }
    2869    16814546 :     if (!c) continue;
    2870     9819026 :     y = (x*(x - t) + n) / N; /* exact division */
    2871     9819026 :     D = mydivisorsu(ugcd(labs(y), NF)); lD = lg(D);
    2872     9819026 :     for (j=1; j < lD; j++) { g = D[j]; gel(v,g) = gadd(gel(v,g), c); }
    2873             :   }
    2874             :   /* j = 1 corresponds to g = 1, and MUP[1] = 1 */
    2875     9652818 :   for (j=2; j < lDNF; j++) { g = DNF[j]; gel(v,g) = gmulsg(MUP[g], gel(v,g)); }
    2876     9652818 :   return v;
    2877             : }
    2878             : 
    2879             : /* special case (N,F) = 1: easier */
    2880             : static GEN
    2881    37577743 : mutg1(long t, long N, GEN VCHI, GEN li, GEN GCD)
    2882             : { /* (N,F) = 1 */
    2883    37577743 :   GEN S = NULL;
    2884    37577743 :   long i, lx = lg(li);
    2885    79827489 :   for (i = 1; i < lx; i++)
    2886             :   {
    2887    42249746 :     long x = (li[i] + t) >> 1;
    2888    42249746 :     GEN c = mychicgcd(GCD, VCHI, x);
    2889    42249746 :     if (c) S = S? gadd(S, c): c;
    2890    42249746 :     if (li[i] && li[i] != N)
    2891             :     {
    2892    24755626 :       c = mychicgcd(GCD, VCHI, t - x);
    2893    24755626 :       if (c) S = S? gadd(S, c): c;
    2894             :     }
    2895    42249746 :     if (S && !signe(S)) S = NULL; /* strive hard to add gen_0 */
    2896             :   }
    2897    37577743 :   return S; /* single value */
    2898             : }
    2899             : 
    2900             : /* Gegenbauer pol; n > 2, P = \sum_{0<=j<=n/2} (-1)^j (n-j)!/j!(n-2*j)! X^j */
    2901             : static GEN
    2902      305445 : mfrhopol(long n)
    2903             : {
    2904             : #ifdef LONG_IS_64BIT
    2905      261810 :   const long M = 2642249;
    2906             : #else
    2907       43635 :   const long M = 1629;
    2908             : #endif
    2909      305445 :   long j, d = n >> 1; /* >= 1 */
    2910      305445 :   GEN P = cgetg(d + 3, t_POL);
    2911             : 
    2912      305445 :   if (n > M) pari_err_IMPL("mfrhopol for large weight"); /* avoid overflow */
    2913      305445 :   P[1] = evalvarn(0)|evalsigne(1);
    2914      305445 :   gel(P,2) = gen_1;
    2915      305445 :   gel(P,3) = utoineg(n-1); /* j = 1 */
    2916      305445 :   if (d > 1) gel(P,4) = utoipos(((n-3)*(n-2)) >> 1); /* j = 2 */
    2917      305445 :   if (d > 2) gel(P,5) = utoineg(((n-5)*(n-4)*(n-3)) / 6); /* j = 3 */
    2918      321902 :   for (j = 4; j <= d; j++)
    2919       16457 :     gel(P,j+2) = divis(mulis(gel(P,j+1), (n-2*j+1)*(n-2*j+2)), (n-j+1)*(-j));
    2920      305445 :   return P;
    2921             : }
    2922             : 
    2923             : /* polrecip(Q)(t2), assume Q(0) = 1 */
    2924             : static GEN
    2925     1917552 : ZXrecip_u_eval(GEN Q, ulong t2)
    2926             : {
    2927     1917552 :   GEN T = addiu(gel(Q,3), t2);
    2928     1917552 :   long l = lg(Q), j;
    2929     1917552 :   for (j = 4; j < l; j++) T = addii(gel(Q,j), mului(t2, T));
    2930     1917552 :   return T;
    2931             : }
    2932             : /* return sh * sqrt(n)^nu * G_nu(t/(2*sqrt(n))) for t != 0
    2933             :  * else (sh/2) * sqrt(n)^nu * G_nu(0) [ implies nu is even ]
    2934             :  * G_nu(z) = \sum_{0<=j<=nu/2} (-1)^j (nu-j)!/j!(nu-2*j)! * (2z)^(nu-2*j)) */
    2935             : static GEN
    2936    40851048 : mfrhopowsimp(GEN Q, GEN sh, long nu, long t, long t2, long n)
    2937             : {
    2938             :   GEN T;
    2939    40851048 :   switch (nu)
    2940             :   {
    2941    34062581 :     case 0: return t? sh: gmul2n(sh,-1);
    2942     3462326 :     case 1: return gmulsg(t, sh);
    2943     1394197 :     case 2: return t? gmulsg(t2 - n, sh): gmul(gmul2n(stoi(-n), -1), sh);
    2944         469 :     case 3: return gmul(mulss(t, t2 - 2*n), sh);
    2945             :     default:
    2946     1931475 :       if (!t) return gmul(gmul2n(gel(Q, lg(Q) - 1), -1), sh);
    2947     1917552 :       T = ZXrecip_u_eval(Q, t2); if (odd(nu)) T = mulsi(t, T);
    2948     1917552 :       return gmul(T, sh);
    2949             :   }
    2950             : }
    2951             : 
    2952             : /* contribution of elliptic matrices to trace formula */
    2953             : static GEN
    2954     3062101 : TA2(long N, long k, GEN VCHI, long n, GEN SQRTS, GEN MUP, GEN GCD)
    2955             : {
    2956     3062101 :   const long n4 = n << 2, N4 = N << 2, nu = k - 2;
    2957     3062101 :   const long st = (!odd(N) && odd(n)) ? 2 : 1;
    2958             :   long limt, t;
    2959             :   GEN S, Q;
    2960             : 
    2961     3062101 :   limt = usqrt(n4);
    2962     3062101 :   if (limt*limt == n4) limt--;
    2963     3062101 :   Q = nu > 3 ? ZX_z_unscale(mfrhopol(nu), n) : NULL;
    2964     3062101 :   S = gen_0;
    2965    84675304 :   for (t = odd(k)? st: 0; t <= limt; t += st) /* t^2 < 4n */
    2966             :   {
    2967    81613203 :     pari_sp av = avma;
    2968    81613203 :     long t2 = t*t, D = n4 - t2, F, D0, NF;
    2969             :     GEN sh, li;
    2970             : 
    2971    81613203 :     li = gel(SQRTS, (smodss(-D - 1, N4) >> 1) + 1);
    2972   122375358 :     if (lg(li) == 1) continue;
    2973    47230561 :     D0 = mycoredisc2neg(D, &F);
    2974    47230561 :     NF = myugcd(GCD, F);
    2975    47230561 :     if (NF == 1)
    2976             :     { /* (N,F) = 1 => single value in mutglistall */
    2977    37577743 :       GEN mut = mutg1(t, N, VCHI, li, GCD);
    2978    37577743 :       if (!mut) { avma = av; continue; }
    2979    35546532 :       sh = gmul(sstoQ(hclassno6u_i(D,D0,F),6), mut);
    2980             :     }
    2981             :     else
    2982             :     {
    2983     9652818 :       GEN v = mutglistall(t, N, NF, VCHI, n, MUP, li, GCD);
    2984     9652818 :       GEN DF = mydivisorsu(F);
    2985     9652818 :       long i, lDF = lg(DF);
    2986     9652818 :       sh = gen_0;
    2987    37321823 :       for (i = 1; i < lDF; i++)
    2988             :       {
    2989    27669005 :         long Ff, f = DF[i], g = myugcd(GCD, f);
    2990    27669005 :         GEN mut = gel(v, g);
    2991    27669005 :         if (gequal0(mut)) continue;
    2992    12802692 :         Ff = DF[lDF-i]; /* F/f */
    2993    12802692 :         if (Ff == 1) sh = gadd(sh, mut);
    2994             :         else
    2995             :         {
    2996     9181697 :           GEN P = gel(myfactoru(Ff), 1);
    2997     9181697 :           long j, lP = lg(P);
    2998     9181697 :           for (j = 1; j < lP; j++) { long p = P[j]; Ff -= kross(D0, p)*Ff/p; }
    2999     9181697 :           sh = gadd(sh, gmulsg(Ff, mut));
    3000             :         }
    3001             :       }
    3002     9652818 :       if (gequal0(sh)) { avma = av; continue; }
    3003     5304516 :       if (D0 == -3) sh = gdivgs(sh, 3);
    3004     5047035 :       else if (D0 == -4) sh = gdivgs(sh, 2);
    3005     4788693 :       else sh = gmulgs(sh, myh(D0));
    3006             :     }
    3007    40851048 :     S = gerepileupto(av, gadd(S, mfrhopowsimp(Q,sh,nu,t,t2,n)));
    3008             :   }
    3009     3062101 :   return S;
    3010             : }
    3011             : 
    3012             : /* compute global auxiliary data for TA3 */
    3013             : static GEN
    3014       98763 : mkbez(long N, long FC)
    3015             : {
    3016       98763 :   long ct, i, NF = N/FC;
    3017       98763 :   GEN w, D = mydivisorsu(N);
    3018       98763 :   long l = lg(D);
    3019             : 
    3020       98763 :   w = cgetg(l, t_VEC);
    3021      285082 :   for (i = ct = 1; i < l; i++)
    3022             :   {
    3023      268030 :     long u, v, h, c = D[i], Nc = D[l-i];
    3024      268030 :     if (c > Nc) break;
    3025      186319 :     h = cbezout(c, Nc, &u, &v);
    3026      186319 :     if (h == 1) /* shortcut */
    3027      136248 :       gel(w, ct++) = mkvecsmall4(1,u*c,1,i);
    3028       50071 :     else if (!(NF%h))
    3029       44415 :       gel(w, ct++) = mkvecsmall4(h,u*(c/h),myeulerphiu(h),i);
    3030             :   }
    3031       98763 :   setlg(w,ct); stackdummy((pari_sp)(w+ct),(pari_sp)(w+l));
    3032       98763 :   return w;
    3033             : }
    3034             : 
    3035             : /* contribution of hyperbolic matrices to trace formula, d * nd = n,
    3036             :  * DN = divisorsu(N) */
    3037             : static GEN
    3038    12033756 : auxsum(GEN VCHI, GEN GCD, long d, long nd, GEN DN, GEN BEZ)
    3039             : {
    3040    12033756 :   GEN S = gen_0;
    3041    12033756 :   long ct, g = nd - d, lDN = lg(DN), lBEZ = lg(BEZ);
    3042    33897605 :   for (ct = 1; ct < lBEZ; ct++)
    3043             :   {
    3044    21863849 :     GEN y, B = gel(BEZ, ct);
    3045    21863849 :     long ic, c, Nc, uch, h = B[1];
    3046    21863849 :     if (g%h) continue;
    3047    21395563 :     uch = B[2];
    3048    21395563 :     ic  = B[4];
    3049    21395563 :     c = DN[ic];
    3050    21395563 :     Nc= DN[lDN - ic]; /* Nc = N/c */
    3051    21395563 :     if (ugcd(Nc, nd) == 1)
    3052    15854664 :       y = mychicgcd(GCD, VCHI, d + uch*g); /* 0 if (c,d) > 1 */
    3053             :     else
    3054     5540899 :       y = NULL;
    3055    21395563 :     if (c != Nc && ugcd(Nc, d) == 1)
    3056             :     {
    3057    15062453 :       GEN y2 = mychicgcd(GCD, VCHI, nd - uch*g); /* 0 if (c,nd) > 1 */
    3058    15062453 :       if (y2) y = y? gadd(y, y2): y2;
    3059             :     }
    3060    21395563 :     if (y) S = gadd(S, gmulsg(B[3], y));
    3061             :   }
    3062    12033756 :   return S;
    3063             : }
    3064             : 
    3065             : static GEN
    3066     3062101 : TA3(long N, long k, GEN VCHI, GEN GCD, GEN Dn, GEN BEZ)
    3067             : {
    3068     3062101 :   GEN S = gen_0, DN = mydivisorsu(N);
    3069     3062101 :   long i, l = lg(Dn);
    3070    15095857 :   for (i = 1; i < l; i++)
    3071             :   {
    3072    15065995 :     long d = Dn[i], nd = Dn[l-i]; /* = n/d */
    3073             :     GEN t, u;
    3074    15065995 :     if (d > nd) break;
    3075    12033756 :     t = auxsum(VCHI, GCD, d, nd, DN, BEZ);
    3076    12033756 :     if (isintzero(t)) continue;
    3077    10943247 :     u = powuu(d,k-1); if (d == nd) u = gmul2n(u,-1);
    3078    10943247 :     S = gadd(S, gmul(u,t));
    3079             :   }
    3080     3062101 :   return S;
    3081             : }
    3082             : 
    3083             : /* special contribution in weight 2 in trace formula */
    3084             : static long
    3085     3062101 : TA4(long k, GEN VCHIP, GEN Dn, GEN GCD)
    3086             : {
    3087             :   long i, l, S;
    3088     3062101 :   if (k != 2 || vchip_FC(VCHIP) != 1) return 0;
    3089     2421440 :   l = lg(Dn); S = 0;
    3090    20790238 :   for (i = 1; i < l; i++)
    3091             :   {
    3092    18368798 :     long d = Dn[i]; /* gcd(N,n/d) == 1? */
    3093    18368798 :     if (myugcd(GCD, Dn[l-i]) == 1) S += d;
    3094             :   }
    3095     2421440 :   return S;
    3096             : }
    3097             : 
    3098             : /* precomputation of products occurring im mutg, again to accelerate TA2 */
    3099             : static GEN
    3100       98763 : mkmup(long N)
    3101             : {
    3102       98763 :   GEN fa = myfactoru(N), P = gel(fa,1), D = divisorsu_fact(fa);
    3103       98763 :   long i, lP = lg(P), lD = lg(D);
    3104       98763 :   GEN MUP = zero_zv(N);
    3105       98763 :   MUP[1] = 1;
    3106      343875 :   for (i = 2; i < lD; i++)
    3107             :   {
    3108      245112 :     long j, g = D[i], Ng = D[lD-i]; /*  N/g */
    3109      245112 :     for (j = 1; j < lP; j++) { long p = P[j]; if (Ng%p) g += g/p; }
    3110      245112 :     MUP[D[i]] = g;
    3111             :   }
    3112       98763 :   return MUP;
    3113             : }
    3114             : 
    3115             : /* quadratic non-residues mod p; p odd prime, p^2 fits in a long */
    3116             : static GEN
    3117        1400 : non_residues(long p)
    3118             : {
    3119        1400 :   long i, j, p2 = p >> 1;
    3120        1400 :   GEN v = cgetg(p2+1, t_VECSMALL), w = const_vecsmall(p-1, 1);
    3121        1400 :   for (i = 2; i <= p2; i++) w[(i*i) % p] = 0; /* no need to check 1 */
    3122        1400 :   for (i = 2, j = 1; i < p; i++) if (w[i]) v[j++] = i;
    3123        1400 :   return v;
    3124             : }
    3125             : 
    3126             : /* CHIP primitive. Return t_VECSMALL v of length q such that
    3127             :  * Tr^new_{N,CHIP}(n) = 0 whenever v[(n%q) + 1] is non-zero */
    3128             : static GEN
    3129       24920 : mfnewzerodata(long N, GEN CHIP)
    3130             : {
    3131       24920 :   GEN V, M, L, faN = myfactoru(N), PN = gel(faN,1), EN = gel(faN,2);
    3132       24920 :   GEN G = gel(CHIP,1), chi = gel(CHIP,2);
    3133       24920 :   GEN fa = znstar_get_faN(G), P = ZV_to_zv(gel(fa,1)), E = gel(fa,2);
    3134       24920 :   long i, mod, j = 1, l = lg(PN);
    3135             : 
    3136       24920 :   M = cgetg(l, t_VECSMALL); M[1] = 0;
    3137       24920 :   V = cgetg(l, t_VEC);
    3138             :   /* Tr^new(n) = 0 if (n mod M[i]) in V[i]  */
    3139       24920 :   if ((N & 3) == 0)
    3140             :   {
    3141        9541 :     long e = EN[1];
    3142        9541 :     long c = (lg(P) > 1 && P[1] == 2)? E[1]: 0; /* c = v_2(FC) */
    3143             :     /* e >= 2 */
    3144        9541 :     if (c == e-1) return NULL; /* Tr^new = 0 */
    3145        9506 :     if (c == e)
    3146             :     {
    3147        2422 :       if (e == 2)
    3148             :       { /* sc: -4 */
    3149        1652 :         gel(V,1) = mkvecsmall(3);
    3150        1652 :         M[1] = 4;
    3151             :       }
    3152         770 :       else if (e == 3)
    3153             :       { /* sc: -8 (CHI_2(-1)=-1<=>chi[1]=1) and 8 (CHI_2(-1)=1 <=> chi[1]=0) */
    3154         770 :         long t = signe(gel(chi,1))? 7: 3;
    3155         770 :         gel(V,1) = mkvecsmall2(5, t);
    3156         770 :         M[1] = 8;
    3157             :       }
    3158             :     }
    3159        7084 :     else if (e == 5 && c == 3)
    3160         154 :     { /* sc: -8 (CHI_2(-1)=-1<=>chi[1]=1) and 8 (CHI_2(-1)=1 <=> chi[1]=0) */
    3161         154 :       long t = signe(gel(chi,1))? 7: 3;
    3162         154 :       gel(V,1) = mkvecsmalln(6, 2L,4L,5L,6L,8L,t);
    3163         154 :       M[1] = 8;
    3164             :     }
    3165        6930 :     else if ((e == 4 && c == 2) || (e == 5 && c <= 2) || (e == 6 && c <= 2)
    3166        5719 :          || (e >= 7 && c == e - 3))
    3167             :     { /* sc: 4 */
    3168        1211 :       gel(V,1) = mkvecsmall3(0,2,3);
    3169        1211 :       M[1] = 4;
    3170             :     }
    3171        5719 :     else if ((e <= 4 && c == 0) || (e >= 5 && c == e - 2))
    3172             :     { /* sc: 2 */
    3173        5453 :       gel(V,1) = mkvecsmall(0);
    3174        5453 :       M[1] = 2;
    3175             :     }
    3176         266 :     else if ((e == 6 && c == 3) || (e >= 7 && c <= e - 4))
    3177             :     { /* sc: -2 */
    3178         266 :       gel(V,1) = mkvecsmalln(7, 0L,2L,3L,4L,5L,6L,7L);
    3179         266 :       M[1] = 8;
    3180             :     }
    3181             :   }
    3182       24885 :   j = M[1]? 2: 1;
    3183       53431 :   for (i = odd(N)? 1: 2; i < l; i++) /* skip p=2, done above */
    3184             :   {
    3185       28546 :     long p = PN[i], e = EN[i];
    3186       28546 :     long z = zv_search(P, p), c = z? E[z]: 0; /* c = v_p(FC) */
    3187       28546 :     if ((e <= 2 && c == 1 && itos(gel(chi,z)) == (p>>1)) /* ord(CHI_p)=2 */
    3188       27608 :         || (e >= 3 && c <= e - 2))
    3189        1400 :     { /* sc: -p */
    3190        1400 :       GEN v = non_residues(p);
    3191        1400 :       if (e != 1) v = vecsmall_prepend(v, 0);
    3192        1400 :       gel(V,j) = v;
    3193        1400 :       M[j] = p; j++;
    3194             :     }
    3195       27146 :     else if (e >= 2 && c < e)
    3196             :     { /* sc: p */
    3197        1827 :       gel(V,j) = mkvecsmall(0);
    3198        1827 :       M[j] = p; j++;
    3199             :     }
    3200             :   }
    3201       24885 :   if (j == 1) return cgetg(1, t_VECSMALL);
    3202       11207 :   setlg(V,j); setlg(M,j); mod = zv_prod(M);
    3203       11207 :   L = zero_zv(mod);
    3204       23940 :   for (i = 1; i < j; i++)
    3205             :   {
    3206       12733 :     GEN v = gel(V,i);
    3207       12733 :     long s, m = M[i], lv = lg(v);
    3208       33152 :     for (s = 1; s < lv; s++)
    3209             :     {
    3210       20419 :       long a = v[s] + 1;
    3211       30205 :       do { L[a] = 1; a += m; } while (a <= mod);
    3212             :     }
    3213             :   }
    3214       11207 :   return L;
    3215             : }
    3216             : /* v=mfnewzerodata(N,CHI); returns TRUE if newtrace(n) must be zero,
    3217             :  * (but newtrace(n) may still be zero if we return FALSE) */
    3218             : static long
    3219     1277668 : mfnewchkzero(GEN v, long n) { long q = lg(v)-1; return q && v[(n%q) + 1]; }
    3220             : 
    3221             : /* if (!VCHIP): from mftraceform_cusp;
    3222             :  * else from initnewtrace and CHI is known to be primitive */
    3223             : static GEN
    3224       98763 : inittrace(long N, GEN CHI, GEN VCHIP)
    3225             : {
    3226             :   long FC;
    3227       98763 :   if (VCHIP)
    3228       98756 :     FC = mfcharmodulus(CHI);
    3229             :   else
    3230           7 :     VCHIP = mfcharinit(mfchartoprimitive(CHI, &FC));
    3231       98763 :   return mkvecn(5, mksqr(N), mkmup(N), mkgcd(N), VCHIP, mkbez(N, FC));
    3232             : }
    3233             : 
    3234             : /* p > 2 prime; return a sorted t_VECSMALL of primes s.t Tr^new(p) = 0 for all
    3235             :  * weights > 2 */
    3236             : static GEN
    3237       24885 : inittrconj(long N, long FC)
    3238             : {
    3239             :   GEN fa, P, E, v;
    3240             :   long i, k, l;
    3241             : 
    3242       24885 :   if (FC != 1) return cgetg(1,t_VECSMALL);
    3243             : 
    3244       20888 :   fa = myfactoru(N >> vals(N));
    3245       20888 :   P = gel(fa,1); l = lg(P);
    3246       20888 :   E = gel(fa,2);
    3247       20888 :   v = cgetg(l, t_VECSMALL);
    3248       45773 :   for (i = k = 1; i < l; i++)
    3249             :   {
    3250       24885 :     long j, p = P[i]; /* > 2 */
    3251       60508 :     for (j = 1; j < l; j++)
    3252       35623 :       if (j != i && E[j] == 1 && kross(-p, P[j]) == 1) v[k++] = p;
    3253             :   }
    3254       20888 :   setlg(v,k); return v;
    3255             : }
    3256             : 
    3257             : /* assume CHIP primitive, f(CHIP) | N; NZ = mfnewzerodata(N,CHIP) */
    3258             : static GEN
    3259       24885 : initnewtrace_i(long N, GEN CHIP, GEN NZ)
    3260             : {
    3261       24885 :   GEN T = const_vec(N, cgetg(1,t_VEC)), D, VCHIP;
    3262       24885 :   long FC = mfcharmodulus(CHIP), N1, N2, i, l;
    3263             : 
    3264       24885 :   if (!NZ) NZ = mkvecsmall(1); /*Tr^new = 0; initialize data nevertheless*/
    3265       24885 :   VCHIP = mfcharinit(CHIP);
    3266       24885 :   N1 = N/FC; newd_params(N1, &N2);
    3267       24885 :   D = mydivisorsu(N1/N2); l = lg(D);
    3268       24885 :   N2 *= FC;
    3269      123641 :   for (i = 1; i < l; i++)
    3270             :   {
    3271       98756 :     long M = D[i]*N2;
    3272       98756 :     gel(T,M) = inittrace(M, CHIP, VCHIP);
    3273             :   }
    3274       24885 :   gel(T,N) = shallowconcat(gel(T,N), mkvec2(NZ, inittrconj(N,FC)));
    3275       24885 :   return T;
    3276             : }
    3277             : /* don't initialize if Tr^new = 0, return NULL */
    3278             : static GEN
    3279       24920 : initnewtrace(long N, GEN CHI)
    3280             : {
    3281       24920 :   GEN CHIP = mfchartoprimitive(CHI, NULL), NZ = mfnewzerodata(N,CHIP);
    3282       24920 :   return NZ? initnewtrace_i(N, CHIP, NZ): NULL;
    3283             : }
    3284             : 
    3285             : /* (-1)^k */
    3286             : static long
    3287        6888 : m1pk(long k) { return odd(k)? -1 : 1; }
    3288             : static long
    3289        6643 : badchar(long N, long k, GEN CHI)
    3290        6643 : { return mfcharparity(CHI) != m1pk(k) || (CHI && N % mfcharconductor(CHI)); }
    3291             : 
    3292             : /* dimension of space of cusp forms S_k(\G_0(N),CHI)
    3293             :  * Only depends on CHIP the primitive char attached to CHI */
    3294             : long
    3295       39214 : mfcuspdim(long N, long k, GEN CHI)
    3296             : {
    3297       39214 :   pari_sp av = avma;
    3298             :   long FC;
    3299             :   GEN s;
    3300       39214 :   if (k <= 0) return 0;
    3301       39214 :   if (k == 1) return mfwt1cuspdim(N, CHI);
    3302       39081 :   FC = CHI? mfcharconductor(CHI): 1;
    3303       39081 :   if (FC == 1) CHI = NULL;
    3304       39081 :   s = gsub(A1(N, k), gadd(A21(N, k, CHI), A22(N, k, CHI)));
    3305       39081 :   s = gadd(s, gsubsg(A4(k, FC), A3(N, FC)));
    3306       39081 :   avma = av; return itos(s);
    3307             : }
    3308             : 
    3309             : /* dimension of whole space M_k(\G_0(N),CHI)
    3310             :  * Only depends on CHIP the primitive char attached to CHI; assumes !badchar */
    3311             : long
    3312         686 : mffulldim(long N, long k, GEN CHI)
    3313             : {
    3314         686 :   pari_sp av = avma;
    3315         686 :   long FC = CHI? mfcharconductor(CHI): 1;
    3316             :   GEN s;
    3317         686 :   if (k <= 0) return (k == 0 && FC == 1)? 1: 0;
    3318         686 :   if (k == 1)
    3319             :   {
    3320         168 :     long dim = itos(A3(N, FC));
    3321         168 :     avma = av; return dim + mfwt1cuspdim(N, CHI);
    3322             :   }
    3323         518 :   if (FC == 1) CHI = NULL;
    3324         518 :   s = gsub(A1(N, k), gadd(A21(N, k, CHI), A22(N, k, CHI)));
    3325         518 :   s = gadd(s, A3(N, FC));
    3326         518 :   avma = av; return itos(s);
    3327             : }
    3328             : 
    3329             : /* Dimension of the space of Eisenstein series */
    3330             : long
    3331         231 : mfeisensteindim(long N, long k, GEN CHI)
    3332             : {
    3333         231 :   pari_sp av = avma;
    3334         231 :   long s, FC = CHI? mfcharconductor(CHI): 1;
    3335         231 :   if (k <= 0) return (k == 0 && FC == 1)? 1: 0;
    3336         231 :   s = itos(gmul2n(A3(N, FC), 1));
    3337         231 :   if (k > 1) s -= A4(k, FC);
    3338          49 :   else s >>= 1;
    3339         231 :   avma = av; return s;
    3340             : }
    3341             : 
    3342             : enum { _SQRTS = 1, _MUP, _GCD, _VCHIP, _BEZ, _NEWLZ, _TRCONJ };
    3343             : /* Trace of T(n) on space of cuspforms; only depends on CHIP the primitive char
    3344             :  * attached to CHI */
    3345             : static GEN
    3346     3062101 : mfcusptrace_i(long N, long k, long n, GEN Dn, GEN S)
    3347             : {
    3348     3062101 :   pari_sp av = avma;
    3349             :   GEN a, b, VCHIP, GCD;
    3350             :   long t;
    3351     3062101 :   if (!n) return gen_0;
    3352     3062101 :   VCHIP = gel(S,_VCHIP);
    3353     3062101 :   GCD = gel(S,_GCD);
    3354     3062101 :   t = TA4(k, VCHIP, Dn, GCD);
    3355     3062101 :   a = TA1(N, k, VCHIP, GCD, n); if (t) a = gaddgs(a,t);
    3356     3062101 :   b = TA2(N, k, VCHIP, n, gel(S,_SQRTS), gel(S,_MUP), GCD);
    3357     3062101 :   b = gadd(b, TA3(N, k, VCHIP, GCD, Dn, gel(S,_BEZ)));
    3358     3062101 :   b = gsub(a,b);
    3359     3062101 :   if (typ(b) != t_POL) return gerepileupto(av, b);
    3360       32816 :   return gerepilecopy(av, vchip_polmod(VCHIP, b));
    3361             : }
    3362             : 
    3363             : static GEN
    3364     4094608 : mfcusptracecache(long N, long k, long n, GEN Dn, GEN S, cachenew_t *cache)
    3365             : {
    3366     4094608 :   GEN C = NULL, T = gel(cache->vfull,N);
    3367     4094608 :   long lcache = lg(T);
    3368     4094608 :   if (n < lcache) C = gel(T, n);
    3369     4094608 :   if (C) cache->cuspHIT++; else C = mfcusptrace_i(N, k, n, Dn, S);
    3370     4094608 :   cache->cuspTOTAL++;
    3371     4094608 :   if (n < lcache) gel(T,n) = C;
    3372     4094608 :   return C;
    3373             : }
    3374             : 
    3375             : /* return the divisors of n, known to be among the elements of D */
    3376             : static GEN
    3377      287112 : div_restrict(GEN D, ulong n)
    3378             : {
    3379             :   long i, j, l;
    3380      287112 :   GEN v, VDIV = caches[cache_DIV].cache;
    3381      287112 :   if (lg(VDIV) > n) return gel(VDIV,n);
    3382           0 :   l = lg(D);
    3383           0 :   v = cgetg(l, t_VECSMALL);
    3384           0 :   for (i = j = 1; i < l; i++)
    3385             :   {
    3386           0 :     ulong d = D[i];
    3387           0 :     if (n % d == 0) v[j++] = d;
    3388             :   }
    3389           0 :   setlg(v,j); return v;
    3390             : }
    3391             : 
    3392             : /* for some prime divisors of N, Tr^new(p) = 0 */
    3393             : static int
    3394      201579 : trconj(GEN T, long N, long n)
    3395      201579 : { return (lg(T) > 1 && N % n == 0 && zv_search(T, n)); }
    3396             : 
    3397             : /* n > 0; trace formula on new space */
    3398             : static GEN
    3399     1277668 : mfnewtrace_i(long N, long k, long n, cachenew_t *cache)
    3400             : {
    3401     1277668 :   GEN VCHIP, s, Dn, DN1, SN, S = cache->DATA;
    3402             :   long FC, N1, N2, N1N2, g, i, j, lDN1;
    3403             : 
    3404     1277668 :   if (!S) return gen_0;
    3405     1277668 :   SN = gel(S,N);
    3406     1277668 :   if (mfnewchkzero(gel(SN,_NEWLZ), n)) return gen_0;
    3407      931756 :   if (k > 2 && trconj(gel(SN,_TRCONJ), N, n)) return gen_0;
    3408      931721 :   VCHIP = gel(SN, _VCHIP); FC = vchip_FC(VCHIP);
    3409      931721 :   N1 = N/FC; newt_params(N1, n, FC, &g, &N2);
    3410      931721 :   N1N2 = N1/N2;
    3411      931721 :   DN1 = mydivisorsu(N1N2); lDN1 = lg(DN1);
    3412      931721 :   N2 *= FC;
    3413      931721 :   Dn = mydivisorsu(n); /* this one is probably out of cache */
    3414      931721 :   s = gmulsg(mubeta2(N1N2,n), mfcusptracecache(N2, k, n, Dn, gel(S,N2), cache));
    3415     3807496 :   for (i = 2; i < lDN1; i++)
    3416             :   { /* skip M1 = 1, done above */
    3417     2875775 :     long M1 = DN1[i], N1M1 = DN1[lDN1-i];
    3418     2875775 :     GEN Dg = mydivisorsu(ugcd(M1, g));
    3419     2875775 :     M1 *= N2;
    3420     2875775 :     s = gadd(s, gmulsg(mubeta2(N1M1,n),
    3421     2875775 :                        mfcusptracecache(M1, k, n, Dn, gel(S,M1), cache)));
    3422     3162887 :     for (j = 2; j < lg(Dg); j++) /* skip d = 1, done above */
    3423             :     {
    3424      287112 :       long d = Dg[j], ndd = n/(d*d), M = M1/d;
    3425      287112 :       GEN z = mulsi(mubeta2(N1M1,ndd), powuu(d,k-1)), C = vchip_lift(VCHIP,d,z);
    3426      287112 :       GEN Dndd = div_restrict(Dn, ndd);
    3427      287112 :       s = gadd(s, gmul(C, mfcusptracecache(M, k, ndd, Dndd, gel(S,M), cache)));
    3428             :     }
    3429     2875775 :     s = vchip_mod(VCHIP, s);
    3430             :   }
    3431      931721 :   return vchip_polmod(VCHIP, s);
    3432             : }
    3433             : 
    3434             : /* mfcuspdim(N,k,CHI) - mfnewdim(N,k,CHI); CHIP primitive (for efficiency) */
    3435             : static long
    3436        7371 : mfolddim_i(long N, long k, GEN CHIP)
    3437             : {
    3438        7371 :   long S, i, l, FC = mfcharmodulus(CHIP), N1 = N/FC, N2;
    3439             :   GEN D;
    3440        7371 :   newd_params(N1, &N2); /* will ensure mubeta != 0 */
    3441        7371 :   D = mydivisorsu(N1/N2); l = lg(D);
    3442        7371 :   N2 *= FC; S = 0;
    3443       29141 :   for (i = 2; i < l; i++)
    3444             :   {
    3445       21770 :     long M = D[l-i]*N2, d = mfcuspdim(M, k, CHIP);
    3446       21770 :     if (d) S -= mubeta(D[i]) * d;
    3447             :   }
    3448        7371 :   return S;
    3449             : }
    3450             : long
    3451         322 : mfolddim(long N, long k, GEN CHI)
    3452             : {
    3453         322 :   pari_sp av = avma;
    3454         322 :   GEN CHIP = mfchartoprimitive(CHI, NULL);
    3455         322 :   long S = mfolddim_i(N, k, CHIP);
    3456         322 :   avma = av; return S;
    3457             : }
    3458             : /* Only depends on CHIP the primitive char attached to CHI; assumes !badchar */
    3459             : long
    3460       14378 : mfnewdim(long N, long k, GEN CHI)
    3461             : {
    3462       14378 :   pari_sp av = avma;
    3463             :   long S;
    3464       14378 :   GEN CHIP = mfchartoprimitive(CHI, NULL);
    3465       14378 :   S = mfcuspdim(N, k, CHIP); if (!S) return 0;
    3466        7035 :   S -= mfolddim_i(N, k, CHIP);
    3467        7035 :   avma = av; return S;
    3468             : }
    3469             : 
    3470             : /* trace form, given as closure */
    3471             : static GEN
    3472         882 : mftraceform_new(long N, long k, GEN CHI)
    3473             : {
    3474             :   GEN T;
    3475         882 :   if (k == 1) return initwt1newtrace(mfinit_Nkchi(N, 1, CHI, mf_CUSP, 0));
    3476         861 :   T = initnewtrace(N,CHI); if (!T) return mftrivial();
    3477         861 :   return tag(t_MF_NEWTRACE, mkNK(N,k,CHI), T);
    3478             : }
    3479             : static GEN
    3480          14 : mftraceform_cusp(long N, long k, GEN CHI)
    3481             : {
    3482          14 :   if (k == 1) return initwt1trace(mfinit_Nkchi(N, 1, CHI, mf_CUSP, 0));
    3483           7 :   return tag(t_MF_TRACE, mkNK(N,k,CHI), inittrace(N,CHI,NULL));
    3484             : }
    3485             : static GEN
    3486          91 : mftraceform_i(GEN NK, long space)
    3487             : {
    3488             :   GEN CHI;
    3489             :   long N, k;
    3490          91 :   checkNK(NK, &N, &k, &CHI, 0);
    3491          91 :   if (!mfdim_Nkchi(N, k, CHI, space)) return mftrivial();
    3492          70 :   switch(space)
    3493             :   {
    3494          49 :     case mf_NEW: return mftraceform_new(N, k, CHI);
    3495          14 :     case mf_CUSP:return mftraceform_cusp(N, k, CHI);
    3496             :   }
    3497           7 :   pari_err_DOMAIN("mftraceform", "space", "=", utoi(space), NK);
    3498             :   return NULL;/*LCOV_EXCL_LINE*/
    3499             : }
    3500             : GEN
    3501          91 : mftraceform(GEN NK, long space)
    3502          91 : { pari_sp av = avma; return gerepilecopy(av, mftraceform_i(NK,space)); }
    3503             : 
    3504             : static GEN
    3505       13944 : hecke_data(long N, long n)
    3506       13944 : { return mkvecsmall3(n, u_ppo(n, N), N); }
    3507             : /* 1/2-integral weight */
    3508             : static GEN
    3509          84 : heckef2_data(long N, long n)
    3510             : {
    3511             :   ulong f, fN, fN2;
    3512          84 :   if (!uissquareall(n, &f)) return NULL;
    3513          77 :   fN = u_ppo(f, N); fN2 = fN*fN;
    3514          77 :   return mkvec2(myfactoru(fN), mkvecsmall4(n, N, fN2, n/fN2));
    3515             : }
    3516             : /* N = mf_get_N(F) or a multiple */
    3517             : static GEN
    3518       20503 : mfhecke_i(long n, long N, GEN F)
    3519             : {
    3520       20503 :   if (n == 1) return F;
    3521       13818 :   return tag2(t_MF_HECKE, mf_get_NK(F), hecke_data(N,n), F);
    3522             : }
    3523             : 
    3524             : GEN
    3525         105 : mfhecke(GEN mf, GEN F, long n)
    3526             : {
    3527         105 :   pari_sp av = avma;
    3528             :   GEN NK, CHI, gk, DATA;
    3529             :   long N, nk, dk;
    3530         105 :   mf = checkMF(mf);
    3531         105 :   if (!checkmf_i(F)) pari_err_TYPE("mfhecke",F);
    3532         105 :   if (n <= 0) pari_err_TYPE("mfhecke [n <= 0]", stoi(n));
    3533         105 :   if (n == 1) return gcopy(F);
    3534         105 :   gk = mf_get_gk(F);
    3535         105 :   Qtoss(gk,&nk,&dk);
    3536         105 :   CHI = mf_get_CHI(F);
    3537         105 :   N = MF_get_N(mf);
    3538         105 :   if (dk == 2)
    3539             :   {
    3540          77 :     DATA = heckef2_data(N,n);
    3541          77 :     if (!DATA) return mftrivial();
    3542             :   }
    3543             :   else
    3544          28 :     DATA = hecke_data(N,n);
    3545          98 :   NK = mkgNK(lcmii(stoi(N), mf_get_gN(F)), gk, CHI, mf_get_field(F));
    3546          98 :   return gerepilecopy(av, tag2(t_MF_HECKE, NK, DATA, F));
    3547             : }
    3548             : 
    3549             : /* form F given by closure, compute B(d)(F) as closure (q -> q^d) */
    3550             : static GEN
    3551       26810 : mfbd_i(GEN F, long d)
    3552             : {
    3553             :   GEN D, NK, gk, CHI;
    3554       26810 :   if (d == 1) return F;
    3555        9590 :   if (d <= 0) pari_err_TYPE("mfbd [d <= 0]", stoi(d));
    3556        9590 :   if (mf_get_type(F) != t_MF_BD) D = utoi(d);
    3557           7 :   else { D = mului(d, gel(F,3)); F = gel(F,2); }
    3558        9590 :   gk = mf_get_gk(F); CHI = mf_get_CHI(F);
    3559        9590 :   if (typ(gk) != t_INT) CHI = mfcharmul(CHI, get_mfchar(utoi(d << 2)));
    3560        9590 :   NK = mkgNK(muliu(mf_get_gN(F), d), gk, CHI, mf_get_field(F));
    3561        9590 :   return tag2(t_MF_BD, NK, F, D);
    3562             : }
    3563             : GEN
    3564          35 : mfbd(GEN F, long d)
    3565             : {
    3566          35 :   pari_sp av = avma;
    3567          35 :   if (!checkmf_i(F)) pari_err_TYPE("mfbd",F);
    3568          35 :   return gerepilecopy(av, mfbd_i(F, d));
    3569             : }
    3570             : 
    3571             : /* CHI is a character defined modulo N4 */
    3572             : static GEN
    3573          98 : RgV_shimura(GEN V, long n, long D, long N4, long r, GEN CHI)
    3574             : {
    3575          98 :   GEN R, a0, Pn = mfcharpol(CHI);
    3576          98 :   long m, Da, ND, ord = mfcharorder(CHI), vt = varn(Pn), d4 = D & 3L;
    3577             : 
    3578          98 :   if (d4 == 2 || d4 == 3) D *= 4;
    3579          98 :   Da = labs(D); ND = N4*Da;
    3580          98 :   R = cgetg(n + 2, t_VEC);
    3581          98 :   a0 = gel(V, 1);
    3582          98 :   if (!gequal0(a0))
    3583             :   {
    3584           7 :     long D4 = D << 2;
    3585           7 :     GEN CHID = induceN(ulcm(mfcharmodulus(CHI), labs(D4)), CHI);
    3586           7 :     CHID = mfcharmul_i(CHID, induce(gel(CHID,1), stoi(D4)));
    3587           7 :     a0 = gmul(a0, charLFwtk(r, CHID, mfcharorder(CHID)));
    3588             :   }
    3589          98 :   if (odd(ND) && !odd(mfcharmodulus(CHI))) ND <<= 1;
    3590          98 :   gel(R, 1) = a0;
    3591         567 :   for (m = 1; m <= n; m++)
    3592             :   {
    3593         469 :     GEN Dm = mydivisorsu(u_ppo(m, ND)), S = gel(V, m*m + 1);
    3594         469 :     long i, l = lg(Dm);
    3595         770 :     for (i = 2; i < l; i++)
    3596             :     { /* (e,ND) = 1; skip i = 1: e = 1, done above */
    3597         301 :       long e = Dm[i], me = m / e;
    3598         301 :       long a = mfcharevalord(CHI, e, ord);
    3599         301 :       GEN c, C = powuu(e, r - 1);
    3600         301 :       if (kross(D, e) == -1) C = negi(C);
    3601         301 :       c = mygmodulo_lift(a, ord, C, vt);
    3602         301 :       S = gadd(S, gmul(c, gel(V, me*me + 1)));
    3603             :     }
    3604         469 :     gel(R, m+1) = S;
    3605             :   }
    3606          98 :   return degpol(Pn) > 1? gmodulo(R, Pn): R;
    3607             : }
    3608             : static GEN
    3609          28 : c_shimura(long n, GEN F, long D, GEN CHI)
    3610             : {
    3611          28 :   GEN v = mfcoefs_i(F, n*n, labs(D));
    3612          28 :   return RgV_shimura(v, n, D, mf_get_N(F)>>2, mf_get_r(F), CHI);
    3613             : }
    3614             : 
    3615             : static long
    3616          14 : mfisinkohnen(GEN mf, GEN F)
    3617             : {
    3618          14 :   GEN v, gk = MF_get_gk(mf), CHI = MF_get_CHI(mf);
    3619          14 :   long i, sb, eps, N4 = MF_get_N(mf) >> 2, r = MF_get_r(mf);
    3620          14 :   sb = mfsturmNgk(N4 << 4, gk) + 1;
    3621          14 :   eps = N4 % mfcharconductor(CHI)? -1 : 1;
    3622          14 :   if (odd(r)) eps = -eps;
    3623          14 :   v = mfcoefs(F, sb, 1);
    3624         896 :   for (i = 0; i <= sb; i++)
    3625             :   {
    3626         882 :     long j = i & 3L;
    3627         882 :     if ((j == 2 || j == 2 + eps) && !gequal0(gel(v,i+1))) return 0;
    3628             :   }
    3629          14 :   return 1;
    3630             : }
    3631             : 
    3632             : static long
    3633          35 : mfshimura_space_cusp(GEN mf)
    3634             : {
    3635          35 :   long fl = 1, r = MF_get_r(mf), M = MF_get_N(mf) >> 2;
    3636          35 :   if (r == 1 && M >= 4)
    3637             :   {
    3638          14 :     GEN E = gel(myfactoru(M), 2);
    3639          14 :     long ma = vecsmall_max(E);
    3640          14 :     if (ma > 2 || (ma == 2 && !mfcharistrivial(MF_get_CHI(mf)))) fl = 0;
    3641             :   }
    3642          35 :   return fl;
    3643             : }
    3644             : 
    3645             : /* D is either a discriminant (not necessarily fundamental) with
    3646             :    sign(D)=(-1)^{k-1/2}*eps, or a positive squarefree integer t, which is then
    3647             :    transformed into a fundamental discriminant of the correct sign. */
    3648             : GEN
    3649          35 : mfshimura(GEN mf, GEN F, long D)
    3650             : {
    3651          35 :   pari_sp av = avma;
    3652             :   GEN gk, G, res, mf2, CHI, CHIP;
    3653          35 :   long M, r, space, cusp, N4, flagdisc = 0;
    3654          35 :   if (!checkmf_i(F)) pari_err_TYPE("mfshimura",F);
    3655          35 :   gk = mf_get_gk(F);
    3656          35 :   if (typ(gk) != t_FRAC) pari_err_TYPE("mfshimura [integral weight]", F);
    3657          35 :   r = MF_get_r(mf);
    3658          35 :   if (r <= 0) pari_err_DOMAIN("mfshimura", "weight", "<=", ghalf, gk);
    3659          35 :   N4 = MF_get_N(mf) >> 2; CHI = MF_get_CHI(mf);
    3660          35 :   CHIP = mfcharchiliftprim(CHI, N4);
    3661          35 :   if (!CHIP) CHIP = CHI;
    3662             :   else
    3663             :   {
    3664          35 :     long epsD = CHI == CHIP? D: -D, rd = D & 3L;
    3665          35 :     if (odd(r)) epsD = -epsD;
    3666          35 :     if (epsD > 0 && (rd == 0 || rd == 1)) flagdisc = 1;
    3667             :     else
    3668             :     {
    3669          14 :       if (D < 0 || !uissquarefree(D))
    3670           7 :         pari_err_TYPE("shimura [incorrect D]", stoi(D));
    3671           7 :       D = epsD;
    3672             :     }
    3673             :   }
    3674          28 :   M = N4;
    3675          28 :   cusp = mfiscuspidal(mf,F);
    3676          28 :   space = cusp && mfshimura_space_cusp(mf)? mf_CUSP : mf_FULL;
    3677          28 :   if (!cusp || !flagdisc || !mfisinkohnen(mf,F)) M <<= 1;
    3678          28 :   mf2 = mfinit_Nkchi(M, r << 1, mfcharpow(CHI, gen_2), space, 0);
    3679          28 :   G = c_shimura(mfsturm(mf2), F, D, CHIP);
    3680          28 :   res = mftobasis_i(mf2, G);
    3681             :   /* not mflinear(mf2,): we want lowest possible level */
    3682          28 :   G = mflinear(MF_get_basis(mf2), res);
    3683          28 :   return gerepilecopy(av, mkvec3(mf2, G, res));
    3684             : }
    3685             : 
    3686             : /* W ZabM (ZM if n = 1), a t_INT or NULL, b t_INT, ZXQ mod P or NULL.
    3687             :  * Write a/b = A/d with d t_INT and A Zab return [W,d,A,P] */
    3688             : static GEN
    3689        6580 : mkMinv(GEN W, GEN a, GEN b, GEN P)
    3690             : {
    3691        6580 :   GEN A = (b && typ(b) == t_POL)? Q_remove_denom(QXQ_inv(b,P), &b): NULL;
    3692        6580 :   if (a && b)
    3693             :   {
    3694         938 :     a = Qdivii(a,b);
    3695         938 :     if (typ(a) == t_INT) b = gen_1; else { b = gel(a,2); a = gel(a,1); }
    3696         938 :     if (is_pm1(a)) a = NULL;
    3697             :   }
    3698        6580 :   if (a) A = A? ZX_Z_mul(A,a): a; else if (!A) A = gen_1;
    3699        6580 :   if (!b) b = gen_1;
    3700        6580 :   if (!P) P = gen_0;
    3701        6580 :   return mkvec4(W,b,A,P);
    3702             : }
    3703             : /* M square invertible QabM, return [M',d], M*M' = d*Id */
    3704             : static GEN
    3705         441 : QabM_Minv(GEN M, GEN P, long n)
    3706             : {
    3707             :   GEN dW, W, dM;
    3708         441 :   M = Q_remove_denom(M, &dM);
    3709         441 :   W = P? ZabM_inv(liftpol_shallow(M), P, n, &dW): ZM_inv(M, &dW);
    3710         441 :   return mkMinv(W, dM, dW, P);
    3711             : }
    3712             : /* Simplified form of mfclean, after a QabM_indexrank: M a ZabM with full
    3713             :  * column rank and z = indexrank(M) is known */
    3714             : static GEN
    3715         798 : mfclean2(GEN M, GEN z, GEN P, long n)
    3716             : {
    3717         798 :   GEN d, Minv, y = gel(z,1), W = rowpermute(M, y);
    3718         798 :   W = P? ZabM_inv(liftpol_shallow(W), P, n, &d): ZM_inv(W, &d);
    3719         798 :   M = rowslice(M, 1, y[lg(y)-1]);
    3720         798 :   Minv = mkMinv(W, NULL, d, P);
    3721         798 :   return mkvec3(y, Minv, M);
    3722             : }
    3723             : /* M QabM, lg(M)>1 and [y,z] its rank profile. Let Minv be the inverse of the
    3724             :  * invertible square matrix in mkMinv format. Return [y,Minv, M[..y[#y],]]
    3725             :  * P cyclotomic polynomial of order n != 2 mod 4 or NULL */
    3726             : static GEN
    3727        4200 : mfclean(GEN M, GEN P, long n, int ratlift)
    3728             : {
    3729        4200 :   GEN W, v, y, z, d, Minv, dM, MdM = Q_remove_denom(M, &dM);
    3730        4200 :   if (n == 1)
    3731        3479 :     W = ZM_pseudoinv(MdM, &v, &d);
    3732             :   else
    3733         721 :     W = ZabM_pseudoinv_i(liftpol_shallow(MdM), P, n, &v, &d, ratlift);
    3734        4200 :   y = gel(v,1);
    3735        4200 :   z = gel(v,2);
    3736        4200 :   if (lg(z) != lg(MdM)) M = vecpermute(M,z);
    3737        4200 :   M = rowslice(M, 1, y[lg(y)-1]);
    3738        4200 :   Minv = mkMinv(W, dM, d, P);
    3739        4200 :   return mkvec3(y, Minv, M);
    3740             : }
    3741             : /* call mfclean using only CHI */
    3742             : static GEN
    3743        3409 : mfcleanCHI(GEN M, GEN CHI, int ratlift)
    3744             : {
    3745        3409 :   long n = mfcharorder_canon(CHI);
    3746        3409 :   GEN P = (n == 1)? NULL: mfcharpol(CHI);
    3747        3409 :   return mfclean(M, P, n, ratlift);
    3748             : }
    3749             : 
    3750             : /* DATA component of a t_MF_NEWTRACE. Was it stripped to save memory ? */
    3751             : static int
    3752       25767 : newtrace_stripped(GEN DATA)
    3753       25767 : { return DATA && (lg(DATA) == 5 && typ(gel(DATA,3)) == t_INT); }
    3754             : /* f a t_MF_NEWTRACE */
    3755             : static GEN
    3756       25767 : newtrace_DATA(long N, GEN f)
    3757             : {
    3758       25767 :   GEN DATA = gel(f,2);
    3759       25767 :   return newtrace_stripped(DATA)? initnewtrace(N, DATA): DATA;
    3760             : }
    3761             : /* reset cachenew for new level incorporating new DATA, tf a t_MF_NEWTRACE
    3762             :  * (+ possibly initialize 'full' for new allowed levels) */
    3763             : static void
    3764       25767 : reset_cachenew(cachenew_t *cache, long N, GEN tf)
    3765             : {
    3766             :   long i, n, l;
    3767       25767 :   GEN v, DATA = newtrace_DATA(N,tf);
    3768       25767 :   cache->DATA = DATA;
    3769       25767 :   if (!DATA) return;
    3770       25732 :   n = cache->n;
    3771       25732 :   v = cache->vfull; l = N+1; /* = lg(DATA) */
    3772     1384901 :   for (i = 1; i < l; i++)
    3773     1359169 :     if (typ(gel(v,i)) == t_INT && lg(gel(DATA,i)) != 1)
    3774       40649 :       gel(v,i) = const_vec(n, NULL);
    3775       25732 :   cache->VCHIP = gel(gel(DATA,N),_VCHIP);
    3776             : }
    3777             : /* initialize a cache of newtrace / cusptrace up to index n and level | N;
    3778             :  * DATA may be NULL (<=> Tr^new = 0). tf a t_MF_NEWTRACE */
    3779             : static void
    3780        9814 : init_cachenew(cachenew_t *cache, long n, long N, GEN tf)
    3781             : {
    3782        9814 :   long i, l = N+1; /* = lg(tf.DATA) when DATA != NULL */
    3783             :   GEN v;
    3784        9814 :   cache->n = n;
    3785        9814 :   cache->vnew = v = cgetg(l, t_VEC);
    3786        9814 :   for (i = 1; i < l; i++) gel(v,i) = (N % i)? gen_0: const_vec(n, NULL);
    3787        9814 :   cache->newHIT = cache->newTOTAL = cache->cuspHIT = cache->cuspTOTAL = 0;
    3788        9814 :   cache->vfull = v = zerovec(N);
    3789        9814 :   reset_cachenew(cache, N, tf);
    3790        9814 : }
    3791             : static void
    3792       14546 : dbg_cachenew(cachenew_t *C)
    3793             : {
    3794       14546 :   if (DEBUGLEVEL >= 2 && C)
    3795           0 :     err_printf("newtrace cache hits: new = %ld/%ld, cusp = %ld/%ld\n",
    3796             :                     C->newHIT, C->newTOTAL, C->cuspHIT, C->cuspTOTAL);
    3797       14546 : }
    3798             : 
    3799             : /* newtrace_{N,k}(d*i), i = n0, ..., n */
    3800             : static GEN
    3801      107912 : colnewtrace(long n0, long n, long d, long N, long k, cachenew_t *cache)
    3802             : {
    3803      107912 :   GEN v = cgetg(n-n0+2, t_COL);
    3804             :   long i;
    3805      107912 :   for (i = n0; i <= n; i++) gel(v, i-n0+1) = mfnewtracecache(N, k, i*d, cache);
    3806      107912 :   return v;
    3807             : }
    3808             : /* T_n(l*m0, l*(m0+1), ..., l*m) F, F = t_MF_NEWTRACE [N,k],DATA, cache
    3809             :  * contains DATA != NULL as well as cached values of F */
    3810             : static GEN
    3811       61887 : heckenewtrace(long m0, long m, long l, long N, long NBIG, long k, long n, cachenew_t *cache)
    3812             : {
    3813       61887 :   long lD, a, k1, nl = n*l;
    3814       61887 :   GEN D, V, v = colnewtrace(m0, m, nl, N, k, cache); /* d=1 */
    3815             :   GEN VCHIP;
    3816       61887 :   if (n == 1) return v;
    3817       39347 :   VCHIP = cache->VCHIP;
    3818       39347 :   D = mydivisorsu(u_ppo(n, NBIG)); lD = lg(D);
    3819       39347 :   k1 = k - 1;
    3820       84623 :   for (a = 2; a < lD; a++)
    3821             :   { /* d > 1, (d,NBIG) = 1 */
    3822       45276 :     long i, j, d = D[a], c = ugcd(l, d), dl = d/c, m0d = ceildiv(m0, dl);
    3823       45276 :     GEN C = vchip_lift(VCHIP, d, powuu(d, k1));
    3824             :     /* m0=0: i = 1 => skip F(0) = 0 */
    3825       45276 :     if (!m0) { i = 1; j = dl; } else { i = 0; j = m0d*dl; }
    3826       45276 :     V = colnewtrace(m0d, m/dl, nl/(d*c), N, k, cache);
    3827             :     /* C = chi(d) d^(k-1) */
    3828      395171 :     for (; j <= m; i++, j += dl)
    3829      349895 :       gel(v,j-m0+1) = gadd(gel(v,j-m0+1), vchip_mod(VCHIP, gmul(C,gel(V,i+1))));
    3830             :   }
    3831       39347 :   return v;
    3832             : }
    3833             : 
    3834             : /* Given v = an[i], return an[d*i] */
    3835             : static GEN
    3836         406 : anextract(GEN v, long n, long d)
    3837             : {
    3838         406 :   GEN w = cgetg(n+2, t_VEC);
    3839             :   long i;
    3840         406 :   for (i = 0; i <= n; i++) gel(w, i+1) = gel(v, i*d+1);
    3841         406 :   return w;
    3842             : }
    3843             : /* T_n(F)(0, l, ..., l*m) */
    3844             : static GEN
    3845         700 : hecke_i(long m, long l, GEN V, GEN F, GEN DATA)
    3846             : {
    3847             :   long k, n, nNBIG, NBIG, lD, M, a, t, nl;
    3848             :   GEN D, v, CHI;
    3849         700 :   if (typ(DATA) == t_VEC)
    3850             :   { /* 1/2-integral k */
    3851          98 :     if (!V) { GEN S = gel(DATA,2); V = mfcoefs_i(F, m*l*S[3], S[4]); }
    3852          98 :     return RgV_heckef2(m, l, V, F, DATA);
    3853             :   }
    3854         602 :   k = mf_get_k(F);
    3855         602 :   n = DATA[1]; nl = n*l;
    3856         602 :   nNBIG = DATA[2];
    3857         602 :   NBIG = DATA[3];
    3858         602 :   if (nNBIG == 1) return V? V: mfcoefs_i(F,m,nl);
    3859         413 :   if (!V && mf_get_type(F) == t_MF_NEWTRACE)
    3860             :   { /* inline F to allow cache, T_n at level NBIG acting on Tr^new(N,k,CHI) */
    3861             :     cachenew_t cache;
    3862         210 :     long N = mf_get_N(F);
    3863         210 :     init_cachenew(&cache, m*nl, N, F);
    3864         210 :     v = heckenewtrace(0, m, l, N, NBIG, k, n, &cache);
    3865         210 :     dbg_cachenew(&cache);
    3866         210 :     settyp(v, t_VEC); return v;
    3867             :   }
    3868         203 :   CHI = mf_get_CHI(F);
    3869         203 :   D = mydivisorsu(nNBIG); lD = lg(D);
    3870         203 :   M = m + 1;
    3871         203 :   t = nNBIG * ugcd(nNBIG, l);
    3872         203 :   if (!V) V = mfcoefs_i(F, m * t, nl / t); /* usually nl = t */
    3873         203 :   v = anextract(V, m, t); /* mfcoefs(F, m, nl); d = 1 */
    3874         406 :   for (a = 2; a < lD; a++)
    3875             :   { /* d > 1, (d, NBIG) = 1 */
    3876         203 :     long d = D[a], c = ugcd(l, d), dl = d/c, i, idl;
    3877         203 :     GEN C = gmul(mfchareval_i(CHI, d), powuu(d, k-1));
    3878         203 :     GEN w = anextract(V, m/dl, t/(d*c)); /* mfcoefs(F, m/dl, nl/(d*c)) */
    3879         644 :     for (i = idl = 1; idl <= M; i++, idl += dl)
    3880         441 :       gel(v,idl) = gadd(gel(v,idl), gmul(C, gel(w,i)));
    3881             :   }
    3882         203 :   return v;
    3883             : }
    3884             : 
    3885             : static GEN
    3886       11018 : mkmf(GEN x1, GEN x2, GEN x3, GEN x4, GEN x5)
    3887             : {
    3888       11018 :   GEN MF = obj_init(5, MF_SPLITN);
    3889       11018 :   gel(MF,1) = x1;
    3890       11018 :   gel(MF,2) = x2;
    3891       11018 :   gel(MF,3) = x3;
    3892       11018 :   gel(MF,4) = x4;
    3893       11018 :   gel(MF,5) = x5; return MF;
    3894             : }
    3895             : 
    3896             : /* return an integer b such that p | b => T_p^k Tr^new = 0, for all k > 0 */
    3897             : static long
    3898        6839 : get_badj(long N, long FC)
    3899             : {
    3900        6839 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
    3901        6839 :   long i, b = 1, l = lg(P);
    3902       18207 :   for (i = 1; i < l; i++)
    3903       11368 :     if (E[i] > 1 && u_lval(FC, P[i]) < E[i]) b *= P[i];
    3904        6839 :   return b;
    3905             : }
    3906             : /* in place, assume perm strictly increasing */
    3907             : static void
    3908        1106 : vecpermute_inplace(GEN v, GEN perm)
    3909             : {
    3910        1106 :   long i, l = lg(perm);
    3911        1106 :   for (i = 1; i < l; i++) gel(v,i) = gel(v,perm[i]);
    3912        1106 : }
    3913             : 
    3914             : /* Find basis of newspace using closures; assume k >= 2 and !badchar.
    3915             :  * Return NULL if space is empty, else
    3916             :  * [mf1, list of closures T(j)traceform, list of corresponding j, matrix] */
    3917             : static GEN
    3918       14140 : mfnewinit(long N, long k, GEN CHI, cachenew_t *cache, long init)
    3919             : {
    3920             :   GEN S, vj, M, CHIP, mf1, listj, P, tf;
    3921             :   long j, ct, ctlj, dim, jin, SB, sb, two, ord, FC, badj;
    3922             : 
    3923       14140 :   dim = mfnewdim(N, k, CHI);
    3924       14140 :   if (!dim && !init) return NULL;
    3925        6839 :   sb = mfsturmNk(N, k);
    3926        6839 :   CHIP = mfchartoprimitive(CHI, &FC);
    3927             :   /* remove newtrace data from S to save space in output: negligible slowdown */
    3928        6839 :   tf = tag(t_MF_NEWTRACE, mkNK(N,k,CHIP), CHIP);
    3929        6839 :   badj = get_badj(N, FC);
    3930             :   /* try sbsmall first: Sturm bound not sharp for new space */
    3931        6839 :   SB = ceilA1(N, k);
    3932        6839 :   listj = cgetg(2*sb + 3, t_VECSMALL);
    3933      314706 :   for (j = ctlj = 1; ctlj < 2*sb + 3; j++)
    3934      307867 :     if (ugcd(j, badj) == 1) listj[ctlj++] = j;
    3935        6839 :   if (init)
    3936             :   {
    3937        3815 :     init_cachenew(cache, (SB+1)*listj[dim+1], N, tf);
    3938        3815 :     if (init == -1 || !dim) return NULL; /* old space or dim = 0 */
    3939             :   }
    3940             :   else
    3941        3024 :     reset_cachenew(cache, N, tf);
    3942             :   /* cache.DATA is not NULL */
    3943        6412 :   ord = mfcharorder_canon(CHIP);
    3944        6412 :   P = ord == 1? NULL: mfcharpol(CHIP);
    3945        6412 :   vj = cgetg(dim+1, t_VECSMALL);
    3946        6412 :   M = cgetg(dim+1, t_MAT);
    3947        6419 :   for (two = 1, ct = 0, jin = 1; two <= 2; two++)
    3948             :   {
    3949        6419 :     long a, jlim = jin + sb;
    3950       18123 :     for (a = jin; a <= jlim; a++)
    3951             :     {
    3952             :       GEN z, vecz;
    3953       18116 :       ct++; vj[ct] = listj[a];
    3954       18116 :       gel(M, ct) = heckenewtrace(0, SB, 1, N, N, k, vj[ct], cache);
    3955       18116 :       if (ct < dim) continue;
    3956             : 
    3957        6965 :       z = QabM_indexrank(M, P, ord);
    3958        6965 :       vecz = gel(z, 2); ct = lg(vecz) - 1;
    3959        6965 :       if (ct == dim) { M = mkvec3(z, gen_0, M); break; } /*maximal rank, done*/
    3960         553 :       vecpermute_inplace(M, vecz);
    3961         553 :       vecpermute_inplace(vj, vecz);
    3962             :     }
    3963        6419 :     if (a <= jlim) break;
    3964             :     /* sbsmall was not sufficient, use Sturm bound: must extend M */
    3965          70 :     for (j = 1; j <= ct; j++)
    3966             :     {
    3967          63 :       GEN t = heckenewtrace(SB + 1, sb, 1, N, N, k, vj[j], cache);
    3968          63 :       gel(M,j) = shallowconcat(gel(M, j), t);
    3969             :     }
    3970           7 :     jin = jlim + 1; SB = sb;
    3971             :   }
    3972        6412 :   S = cgetg(dim + 1, t_VEC);
    3973        6412 :   for (j = 1; j <= dim; j++) gel(S, j) = mfhecke_i(vj[j], N, tf);
    3974        6412 :   dbg_cachenew(cache);
    3975        6412 :   mf1 = mkvec4(utoipos(N), utoipos(k), CHI, utoi(mf_NEW));
    3976        6412 :   return mkmf(mf1, cgetg(1,t_VEC), S, vj, M);
    3977             : }
    3978             : /* k > 1 integral, mf space is mf_CUSP or mf_FULL */
    3979             : static GEN
    3980          28 : mfinittonew(GEN mf)
    3981             : {
    3982          28 :   GEN CHI = MF_get_CHI(mf), S = MF_get_S(mf), vMjd = MFcusp_get_vMjd(mf);
    3983          28 :   GEN M = MF_get_M(mf), vj, mf1;
    3984          28 :   long i, j, l, l0 = lg(S), N0 = MF_get_N(mf);
    3985         161 :   for (i = l0-1; i > 0; i--)
    3986             :   {
    3987         161 :     long N = gel(vMjd,i)[1];
    3988         161 :     if (N != N0) break;
    3989             :   }
    3990          28 :   if (i == l0-1) return NULL;
    3991          28 :   S = vecslice(S, i+1, l0-1); /* forms of conductor N0 */
    3992          28 :   l = lg(S); vj = cgetg(l, t_VECSMALL);
    3993          28 :   for (j = 1; j < l; j++) vj[j] = gel(vMjd,j+i)[2];
    3994          28 :   M = vecslice(M, lg(M)-lg(S)+1, lg(M)-1); /* their coefficients */
    3995          28 :   M = mfcleanCHI(M, CHI, 0);
    3996          28 :   mf1 = mkvec4(utoipos(N0), MF_get_gk(mf), CHI, utoi(mf_NEW));
    3997          28 :   return mkmf(mf1, cgetg(1,t_VEC), S, vj, M);
    3998             : }
    3999             : 
    4000             : /* Bd(f)[m0..m], v = f[ceil(m0/d)..floor(m/d)], m0d = ceil(m0/d) */
    4001             : static GEN
    4002       52017 : RgC_Bd_expand(long m0, long m, GEN v, long d, long m0d)
    4003             : {
    4004             :   long i, j;
    4005             :   GEN w;
    4006       52017 :   if (d == 1) return v;
    4007       14413 :   w = zerocol(m-m0+1);
    4008       14413 :   if (!m0) { i = 1; j = d; } else { i = 0; j = m0d*d; }
    4009       14413 :   for (; j <= m; i++, j += d) gel(w,j-m0+1) = gel(v,i+1);
    4010       14413 :   return w;
    4011             : }
    4012             : /* S a non-empty vector of t_MF_BD(t_MF_HECKE(t_MF_NEWTRACE)); M the matrix
    4013             :  * of their coefficients r*0, r*1, ..., r*m0 (~ mfvectomat) or NULL (empty),
    4014             :  * extend it to coeffs up to m > m0. The forms B_d(T_j(tf_N))in S should be
    4015             :  * sorted by level N, then j, then increasing d. No reordering here. */
    4016             : static GEN
    4017        7098 : bhnmat_extend(GEN M, long m, long r, GEN S, cachenew_t *cache)
    4018             : {
    4019        7098 :   long i, mr, m0, m0r, Nold = 0, jold = 0, l = lg(S);
    4020        7098 :   GEN MAT = cgetg(l, t_MAT), v = NULL;
    4021        7098 :   if (M) { m0 = nbrows(M); m0r = m0 * r; } else m0 = m0r = 0;
    4022        7098 :   mr = m*r;
    4023       59115 :   for (i = 1; i < l; i++)
    4024             :   {
    4025             :     long d, j, md, N;
    4026       52017 :     GEN c, f = bhn_parse(gel(S,i), &d,&j); /* t_MF_NEWTRACE */
    4027       52017 :     N = mf_get_N(f);
    4028       52017 :     md = ceildiv(m0r,d);
    4029       52017 :     if (N != Nold) { reset_cachenew(cache, N, f); Nold = N; jold = 0; }
    4030       52017 :     if (!cache->DATA) { gel(MAT,i) = zerocol(m+1); continue; }
    4031       52017 :     if (j != jold || md)
    4032       43498 :     { v = heckenewtrace(md, mr/d, 1, N, N, mf_get_k(f), j,cache); jold=j; }
    4033       52017 :     c = RgC_Bd_expand(m0r, mr, v, d, md);
    4034       52017 :     if (r > 1) c = c_deflate(m-m0, r, c);
    4035       52017 :     if (M) c = shallowconcat(gel(M,i), c);
    4036       52017 :     gel(MAT,i) = c;
    4037             :   }
    4038        7098 :   return MAT;
    4039             : }
    4040             : 
    4041             : static GEN
    4042        2884 : mfinitcusp(long N, long k, GEN CHI, cachenew_t *cache, long space)
    4043             : {
    4044             :   long L, l, lDN1, FC, N1, d1, i, init;
    4045        2884 :   GEN vS, vMjd, DN1, vmf, CHIP = mfchartoprimitive(CHI, &FC);
    4046             : 
    4047        2884 :   d1 = (space == mf_OLD)? mfolddim_i(N, k, CHIP): mfcuspdim(N, k, CHIP);
    4048        2884 :   if (!d1) return NULL;
    4049        2646 :   N1 = N/FC; DN1 = mydivisorsu(N1); lDN1 = lg(DN1);
    4050        2646 :   init = (space == mf_OLD)? -1: 1;
    4051        2646 :   vmf = cgetg(lDN1, t_VEC);
    4052       15617 :   for (i = lDN1 - 1, l = 1; i; i--)
    4053             :   { /* by decreasing level to allow cache */
    4054       12971 :     GEN mf = mfnewinit(FC*DN1[i], k, CHIP, cache, init);
    4055       12971 :     if (mf) gel(vmf, l++) = mf;
    4056       12971 :     init = 0;
    4057             :   }
    4058        2646 :   setlg(vmf,l); vmf = vecreverse(vmf); /* reorder by increasing level */
    4059             : 
    4060        2646 :   L = mfsturmNk(N, k)+1;
    4061        2646 :   vS = vectrunc_init(L);
    4062        2646 :   vMjd = vectrunc_init(L);
    4063        8260 :   for (i = 1; i < l; i++)
    4064             :   {
    4065        5614 :     GEN DNM, mf = gel(vmf,i), S = MF_get_S(mf), vj = MFnew_get_vj(mf);
    4066        5614 :     long a, lDNM, lS = lg(S), M = MF_get_N(mf);
    4067        5614 :     DNM = mydivisorsu(N / M); lDNM = lg(DNM);
    4068       21315 :     for (a = 1; a < lS; a++)
    4069             :     {
    4070       15701 :       GEN tf = gel(S,a);
    4071       15701 :       long b, j = vj[a];
    4072       38696 :       for (b = 1; b < lDNM; b++)
    4073             :       {
    4074       22995 :         long d = DNM[b];
    4075       22995 :         vectrunc_append(vS, mfbd_i(tf, d));
    4076       22995 :         vectrunc_append(vMjd, mkvecsmall3(M, j, d));
    4077             :       }
    4078             :     }
    4079             :   }
    4080        2646 :   return mkmf(NULL, cgetg(1, t_VEC), vS, vMjd, NULL);
    4081             : }
    4082             : 
    4083             : long
    4084        2961 : mfsturm_mf(GEN mf)
    4085             : {
    4086        2961 :   GEN Mindex = MF_get_Mindex(mf);
    4087        2961 :   long n = lg(Mindex)-1;
    4088        2961 :   return n? Mindex[n]: 0;
    4089             : }
    4090             : 
    4091             : long
    4092         532 : mfsturm(GEN T)
    4093             : {
    4094             :   long N, nk, dk;
    4095         532 :   GEN CHI, mf = checkMF_i(T);
    4096         532 :   if (mf) return mfsturm_mf(mf);
    4097           7 :   checkNK2(T, &N, &nk, &dk, &CHI, 0);
    4098           7 :   return dk == 1 ? mfsturmNk(N, nk) : mfsturmNk(N, (nk + 1) >> 1);
    4099             : }
    4100             : 
    4101             : long
    4102           7 : mfisequal(GEN F, GEN G, long lim)
    4103             : {
    4104           7 :   pari_sp av = avma;
    4105             :   long t, sb;
    4106           7 :   if (!checkmf_i(F)) pari_err_TYPE("mfisequal",F);
    4107           7 :   if (!checkmf_i(G)) pari_err_TYPE("mfisequal",G);
    4108           7 :   if (lim) sb = lim;
    4109             :   else
    4110             :   {
    4111             :     GEN gN, gk;
    4112           7 :     gN = mf_get_gN(F); gk = mf_get_gk(F);
    4113           7 :     sb = mfsturmNgk(itou(gN), gk);
    4114           7 :     gN = mf_get_gN(G); gk = mf_get_gk(G);
    4115           7 :     sb = maxss(sb, mfsturmNgk(itou(gN), gk));
    4116             :   }
    4117           7 :   t = gequal(mfcoefs_i(F, sb+1, 1), mfcoefs_i(G, sb+1, 1));
    4118           7 :   avma = av; return t;
    4119             : }
    4120             : 
    4121             : GEN
    4122          35 : mffields(GEN mf)
    4123             : {
    4124          35 :   if (checkmf_i(mf)) return gcopy(mf_get_field(mf));
    4125          35 :   mf = checkMF(mf); return gcopy(MF_get_fields(mf));
    4126             : }
    4127             : 
    4128             : GEN
    4129         154 : mfeigenbasis(GEN mf)
    4130             : {
    4131         154 :   pari_sp ltop = avma;
    4132             :   GEN F, S, v, vP;
    4133             :   long i, l, k, dS;
    4134             : 
    4135         154 :   mf = checkMF(mf);
    4136         154 :   k = MF_get_k(mf);
    4137         154 :   S = MF_get_S(mf); dS = lg(S)-1;
    4138         154 :   if (!dS) return cgetg(1, t_VEC);
    4139         147 :   F = MF_get_newforms(mf);
    4140         147 :   vP = MF_get_fields(mf);
    4141         147 :   if (k == 1)
    4142             :   {
    4143          49 :     if (MF_get_space(mf) == mf_FULL)
    4144             :     {
    4145           7 :       long dE = lg(MF_get_E(mf)) - 1;
    4146           7 :       if (dE) F = rowslice(F, dE+1, dE+dS);
    4147             :     }
    4148          49 :     v = vecmflineardiv_linear(S, F);
    4149          49 :     l = lg(v);
    4150             :   }
    4151             :   else
    4152             :   {
    4153          98 :     GEN (*L)(GEN, GEN) = (MF_get_space(mf) == mf_FULL)? mflinear: mflinear_bhn;
    4154          98 :     l = lg(F); v = cgetg(l, t_VEC);
    4155          98 :     for (i = 1; i < l; i++) gel(v,i) = L(mf, gel(F,i));
    4156             :   }
    4157         147 :   for (i = 1; i < l; i++) mf_setfield(gel(v,i), gel(vP,i));
    4158         147 :   return gerepilecopy(ltop, v);
    4159             : }
    4160             : 
    4161             : /* Minv = [M, d, A], v a t_COL; A a Zab, d a t_INT; return (A/d) * M*v */
    4162             : static GEN
    4163        5138 : Minv_RgC_mul(GEN Minv, GEN v)
    4164             : {
    4165        5138 :   GEN M = gel(Minv,1), d = gel(Minv,2), A = gel(Minv,3);
    4166        5138 :   v = RgM_RgC_mul(M, v);
    4167        5138 :   if (!equali1(A))
    4168             :   {
    4169        1225 :     if (typ(A) == t_POL && degpol(A) > 0) A = mkpolmod(A, gel(Minv,4));
    4170        1225 :     v = RgC_Rg_mul(v, A);
    4171             :   }
    4172        5138 :   if (!equali1(d)) v = RgC_Rg_div(v, d);
    4173        5138 :   return v;
    4174             : }
    4175             : static GEN
    4176         973 : Minv_RgM_mul(GEN Minv, GEN B)
    4177             : {
    4178         973 :   long j, l = lg(B);
    4179         973 :   GEN M = cgetg(l, t_MAT);
    4180         973 :   for (j = 1; j < l; j++) gel(M,j) = Minv_RgC_mul(Minv, gel(B,j));
    4181         973 :   return M;
    4182             : }
    4183             : /* B * Minv; allow B = NULL for Id */
    4184             : static GEN
    4185        2002 : RgM_Minv_mul(GEN B, GEN Minv)
    4186             : {
    4187        2002 :   GEN M = gel(Minv,1), d = gel(Minv,2), A = gel(Minv,3);
    4188        2002 :   if (B) M = RgM_mul(B, M);
    4189        2002 :   if (!equali1(A))
    4190             :   {
    4191         714 :     if (typ(A) == t_POL) A = mkpolmod(A, gel(Minv,4));
    4192         714 :     M = RgM_Rg_mul(M, A);
    4193             :   }
    4194        2002 :   if (!equali1(d)) M = RgM_Rg_div(M,d);
    4195        2002 :   return M;
    4196             : }
    4197             : 
    4198             : /* perm vector of strictly increasing indices, v a vector or arbitrary length;
    4199             :  * the last r entries of perm fall beyond v.
    4200             :  * Return v o perm[1..(-r)], discarding the last r entries of v */
    4201             : static GEN
    4202        1043 : vecpermute_partial(GEN v, GEN perm, long *r)
    4203             : {
    4204        1043 :   long i, n = lg(v)-1, l = lg(perm);
    4205             :   GEN w;
    4206        1043 :   if (perm[l-1] <= n) { *r = 0; return vecpermute(v,perm); }
    4207          63 :   for (i = 1; i < l; i++)
    4208          63 :     if (perm[i] > n) break;
    4209          21 :   *r = l - i; l = i;
    4210          21 :   w = cgetg(l, typ(v));
    4211          21 :   for (i = 1; i < l; i++) gel(w,i) = gel(v,perm[i]);
    4212          21 :   return w;
    4213             : }
    4214             : 
    4215             : /* given form F, find coeffs of F on mfbasis(mf). If power series, not
    4216             :  * guaranteed correct if precision less than Sturm bound */
    4217             : static GEN
    4218        1050 : mftobasis_i(GEN mf, GEN F)
    4219             : {
    4220             :   GEN v, Mindex, Minv;
    4221        1050 :   if (!MF_get_dim(mf)) return cgetg(1, t_COL);
    4222        1050 :   Mindex = MF_get_Mindex(mf);
    4223        1050 :   Minv = MF_get_Minv(mf);
    4224        1050 :   if (checkmf_i(F))
    4225             :   {
    4226         154 :     long n = Mindex[lg(Mindex)-1];
    4227         154 :     v = vecpermute(mfcoefs_i(F, n, 1), Mindex);
    4228         154 :     return Minv_RgC_mul(Minv, v);
    4229             :   }
    4230             :   else
    4231             :   {
    4232         896 :     GEN A = gel(Minv,1), d = gel(Minv,2);
    4233             :     long r;
    4234         896 :     v = F;
    4235         896 :     switch(typ(F))
    4236             :     {
    4237           0 :       case t_SER: v = sertocol(v);
    4238         896 :       case t_VEC: case t_COL: break;
    4239           0 :       default: pari_err_TYPE("mftobasis", F);
    4240             :     }
    4241         896 :     if (lg(v) == 1) pari_err_TYPE("mftobasis",v);
    4242         896 :     v = vecpermute_partial(v, Mindex, &r);
    4243         896 :     if (!r) return Minv_RgC_mul(Minv, v); /* single solution */
    4244             :     /* affine space of dimension r */
    4245          21 :     v = RgM_RgC_mul(vecslice(A, 1, lg(v)-1), v);
    4246          21 :     if (!equali1(d)) v = RgC_Rg_div(v,d);
    4247          21 :     return mkvec2(v, vecslice(A, lg(A)-r, lg(A)-1));
    4248             :   }
    4249             : }
    4250             : 
    4251             : static GEN
    4252         560 : const_mat(long n, GEN x)
    4253             : {
    4254         560 :   long j, l = n+1;
    4255         560 :   GEN A = cgetg(l,t_MAT);
    4256         560 :   for (j = 1; j < l; j++) gel(A,j) = const_col(n, x);
    4257         560 :   return A;
    4258             : }
    4259             : 
    4260             : /* L is the mftobasis of a form on CUSP space. We allow mf_FULL or mf_CUSP */
    4261             : static GEN
    4262         280 : mftonew_i(GEN mf, GEN L, long *plevel)
    4263             : {
    4264             :   GEN S, listMjd, CHI, res, Aclos, Acoef, D, perm;
    4265         280 :   long N1, LC, lD, i, l, t, level, N = MF_get_N(mf);
    4266             : 
    4267         280 :   if (MF_get_k(mf) == 1) pari_err_IMPL("mftonew in weight 1");
    4268         280 :   listMjd = MFcusp_get_vMjd(mf);
    4269         280 :   CHI = MF_get_CHI(mf); LC = mfcharconductor(CHI);
    4270         280 :   S = MF_get_S(mf);
    4271             : 
    4272         280 :   N1 = N/LC;
    4273         280 :   D = mydivisorsu(N1); lD = lg(D);
    4274         280 :   perm = cgetg(N1+1, t_VECSMALL);
    4275         280 :   for (i = 1; i < lD; i++) perm[D[i]] = i;
    4276         280 :   Aclos = const_mat(lD-1, cgetg(1,t_VEC));
    4277         280 :   Acoef = const_mat(lD-1, cgetg(1,t_VEC));
    4278         280 :   l = lg(listMjd);
    4279        2877 :   for (i = 1; i < l; i++)
    4280             :   {
    4281             :     long M, d;
    4282             :     GEN v;
    4283        2597 :     if (gequal0(gel(L,i))) continue;
    4284         273 :     v = gel(listMjd, i);
    4285         273 :     M = perm[ v[1]/LC ];
    4286         273 :     d = perm[ v[3] ];
    4287         273 :     gcoeff(Aclos,M,d) = vec_append(gcoeff(Aclos,M,d), gel(S,i));
    4288         273 :     gcoeff(Acoef,M,d) = shallowconcat(gcoeff(Acoef,M,d), gel(L,i));
    4289             :   }
    4290         280 :   res = cgetg(l, t_VEC); level = 1;
    4291        2009 :   for (i = t = 1; i < lD; i++)
    4292             :   {
    4293        1729 :     long j, M = D[i]*LC;
    4294        1729 :     GEN gM = utoipos(M);
    4295       15134 :     for (j = 1; j < lD; j++)
    4296             :     {
    4297       13405 :       GEN f = gcoeff(Aclos,i,j), C, NK;
    4298             :       long d;
    4299       13405 :       if (lg(f) == 1) continue;
    4300         245 :       NK = mf_get_NK(gel(f,1));
    4301         245 :       d = D[j];
    4302         245 :       C = gcoeff(Acoef,i,j);
    4303         245 :       level = ulcm(level, M*d);
    4304         245 :       gel(res,t++) = mkvec3(gM, utoipos(d), mflinear_i(NK,f,C));
    4305             :     }
    4306             :   }
    4307         280 :   if (plevel) *plevel = level;
    4308         280 :   setlg(res, t); return res;
    4309             : }
    4310             : GEN
    4311          35 : mftonew(GEN mf, GEN F)
    4312             : {
    4313          35 :   pari_sp av = avma;
    4314             :   GEN ES;
    4315             :   long s;
    4316          35 :   mf = checkMF(mf);
    4317          35 :   s = MF_get_space(mf);
    4318          35 :   if (s != mf_FULL && s != mf_CUSP)
    4319           7 :     pari_err_TYPE("mftonew [not a full or cuspidal space]", mf);
    4320          28 :   ES = mftobasisES(mf,F);
    4321          21 :   if (!gequal0(gel(ES,1)))
    4322           0 :     pari_err_TYPE("mftonew [not a cuspidal form]", F);
    4323          21 :   F = gel(ES,2);
    4324          21 :   return gerepilecopy(av, mftonew_i(mf,F, NULL));
    4325             : }
    4326             : 
    4327             : static GEN mfeisenstein_i(long k, GEN CHI1, GEN CHI2);
    4328             : 
    4329             : /* mfinit(F * Theta) */
    4330             : static GEN
    4331          70 : mf2init(GEN mf)
    4332             : {
    4333          70 :   GEN CHI = MF_get_CHI(mf), gk = gadd(MF_get_gk(mf), ghalf);
    4334          70 :   long N = MF_get_N(mf);
    4335          70 :   return mfinit_Nkchi(N, itou(gk), mfchiadjust(CHI, gk, N), mf_FULL, 0);
    4336             : }
    4337             : 
    4338             : static long
    4339         329 : mfvec_first_cusp(GEN v)
    4340             : {
    4341         329 :   long i, l = lg(v);
    4342         798 :   for (i = 1; i < l; i++)
    4343             :   {
    4344         721 :     GEN F = gel(v,i);
    4345         721 :     long t = mf_get_type(F);
    4346         721 :     if (t == t_MF_BD) { F = gel(F,2); t = mf_get_type(F); }
    4347         721 :     if (t == t_MF_HECKE) { F = gel(F,3); t = mf_get_type(F); }
    4348         721 :     if (t == t_MF_NEWTRACE) break;
    4349             :   }
    4350         329 :   return i;
    4351             : }
    4352             : /* vF a vector of mf F of type DIV(LINEAR(BAS,L), f) in (lcm) level N,
    4353             :  * F[2]=LINEAR(BAS,L), F[2][2]=BAS=fixed basis (Eisentstein or bhn type),
    4354             :  * F[2][3]=L, F[3]=f; mfvectomat(vF, n) */
    4355             : static GEN
    4356         336 : mflineardivtomat(long N, GEN vF, long n)
    4357             : {
    4358             :   GEN F, M, f, fc, V, ME, B, a0;
    4359         336 :   long lM, lF = lg(vF), i, j;
    4360             : 
    4361         336 :   if (lF == 1) return cgetg(1,t_MAT);
    4362         329 :   F = gel(vF,1);
    4363         329 :   M = gmael(F,2,2); /* BAS */
    4364         329 :   lM = lg(M);
    4365         329 :   i = mfvec_first_cusp(M);
    4366         329 :   if (i == 1) ME = NULL;
    4367             :   else
    4368             :   { /* BAS starts by Eisenstein */
    4369         105 :     ME = mfvectomat(vecslice(M,1,i-1), n, 1);
    4370         105 :     M = vecslice(M, i,lM-1);
    4371             :   }
    4372         329 :   M = bhnmat_extend_nocache(NULL, N, n, 1, M);
    4373         329 :   if (ME) M = shallowconcat(ME,M);
    4374             :   /* M = mfcoefs of BAS */
    4375         329 :   f = mfcoefsser(gel(F,3),n);
    4376         329 :   a0 = polcoef_i(f, 0, -1);
    4377         329 :   if (gequal0(a0) || gequal1(a0))
    4378         266 :     a0 = NULL;
    4379             :   else
    4380          63 :     f = gdiv(ser_unscale(f, a0), a0);
    4381         329 :   fc = ginv(f);
    4382         329 :   V = cgetg(lM, t_VEC);
    4383        3402 :   for (i = 1; i < lM; i++)
    4384             :   {
    4385        3073 :     pari_sp av = avma;
    4386        3073 :     GEN LISer = RgV_to_ser_full(gel(M,i)), f;
    4387        3073 :     if (a0) LISer = gdiv(ser_unscale(LISer, a0), a0);
    4388        3073 :     f = gmul(LISer, fc);
    4389        3073 :     if (a0) f = ser_unscale(f, ginv(a0));
    4390        3073 :     f = sertocol(f); setlg(f, n+2);
    4391        3073 :     gel(V,i) = gerepileupto(av,f);
    4392             :   }
    4393         329 :   B = cgetg(lF, t_MAT);
    4394        1568 :   for (j = 1; j < lF; j++)
    4395             :   {
    4396        1239 :     pari_sp av = avma;
    4397        1239 :     GEN S = gen_0, coe;
    4398        1239 :     F = gel(vF, j); /* t_MF_DIV */
    4399        1239 :     coe = gdiv(gmael(F,2,3), gmael(F,2,4));
    4400       17108 :     for (i = 1; i < lM; i++)
    4401             :     {
    4402       15869 :       GEN co = gel(coe, i);
    4403       15869 :       if (!gequal0(co)) S = gadd(S, gmul(co, gel(V, i)));
    4404             :     }
    4405        1239 :     gel(B,j) = gerepileupto(av, S);
    4406             :   }
    4407         329 :   return B;
    4408             : }
    4409             : 
    4410             : static GEN
    4411         105 : mfheckemat_mfcoefs(GEN mf, GEN B, GEN DATA)
    4412             : {
    4413         105 :   GEN Mindex = MF_get_Mindex(mf), Minv = MF_get_Minv(mf);
    4414         105 :   long j, l = lg(B), sb = mfsturm_mf(mf)-1;
    4415         105 :   GEN b = MF_get_basis(mf), Q = cgetg(l, t_VEC);
    4416         371 :   for (j = 1; j < l; j++)
    4417             :   {
    4418         266 :     GEN v = hecke_i(sb, 1, gel(B,j), gel(b,j), DATA); /* Tn b[j] */
    4419         266 :     settyp(v,t_COL); gel(Q,j) = vecpermute(v, Mindex);
    4420             :   }
    4421         105 :   return Minv_RgM_mul(Minv,Q);
    4422             : }
    4423             : /* T_p^2, p prime, 1/2-integral weight; B = mfcoefs(mf,sb*p^2,1) or (mf,sb,p^2)
    4424             :  * if p|N */
    4425             : static GEN
    4426           7 : mfheckemat_mfcoefs_p2(GEN mf, long p, GEN B)
    4427             : {
    4428           7 :   pari_sp av = avma;
    4429           7 :   GEN DATA = heckef2_data(MF_get_N(mf), p*p);
    4430           7 :   return gerepileupto(av, mfheckemat_mfcoefs(mf, B, DATA));
    4431             : }
    4432             : /* convert Mindex from row-index to mfcoef indexation: a(n) is stored in
    4433             :  * mfcoefs()[n+1], so subtract 1 from all indices */
    4434             : static GEN
    4435          49 : Mindex_as_coef(GEN mf)
    4436             : {
    4437          49 :   GEN v, Mindex = MF_get_Mindex(mf);
    4438          49 :   long i, l = lg(Mindex);
    4439          49 :   v = cgetg(l, t_VECSMALL);
    4440          49 :   for (i = 1; i < l; i++) v[i] = Mindex[i]-1;
    4441          49 :   return v;
    4442             : }
    4443             : /* T_p, p prime; B = mfcoefs(mf,sb*p,1) or (mf,sb,p) if p|N; integral weight */
    4444             : static GEN
    4445          35 : mfheckemat_mfcoefs_p(GEN mf, long p, GEN B)
    4446             : {
    4447          35 :   pari_sp av = avma;
    4448          35 :   GEN vm, Q, C, Minv = MF_get_Minv(mf);
    4449          35 :   long lm, k, i, j, l = lg(B), N = MF_get_N(mf);
    4450             : 
    4451          35 :   if (N % p == 0) return Minv_RgM_mul(Minv, rowpermute(B, MF_get_Mindex(mf)));
    4452          21 :   k = MF_get_k(mf);
    4453          21 :   C = gmul(mfchareval_i(MF_get_CHI(mf), p), powuu(p, k-1));
    4454          21 :   vm = Mindex_as_coef(mf); lm = lg(vm);
    4455          21 :   Q = cgetg(l, t_MAT);
    4456          21 :   for (j = 1; j < l; j++) gel(Q,j) = cgetg(lm, t_COL);
    4457         147 :   for (i = 1; i < lm; i++)
    4458             :   {
    4459         126 :     long m = vm[i], mp = m*p;
    4460         126 :     GEN Cm = (m % p) == 0? C : NULL;
    4461        1260 :     for (j = 1; j < l; j++)
    4462             :     {
    4463        1134 :       GEN S = gel(B,j), s = gel(S, mp + 1);
    4464        1134 :       if (Cm) s = gadd(s, gmul(C, gel(S, m/p + 1)));
    4465        1134 :       gcoeff(Q, i, j) = s;
    4466             :     }
    4467             :   }
    4468          21 :   return gerepileupto(av, Minv_RgM_mul(Minv,Q));
    4469             : }
    4470             : /* Matrix of T(p), p prime, dim(mf) > 0 and integral weight */
    4471             : static GEN
    4472          98 : mfheckemat_p(GEN mf, long p)
    4473             : {
    4474          98 :   pari_sp av = avma;
    4475          98 :   long N = MF_get_N(mf), sb = mfsturm_mf(mf)-1;
    4476          98 :   GEN B = (N % p)? mfcoefs_mf(mf, sb * p, 1): mfcoefs_mf(mf, sb, p);
    4477          98 :   return gerepileupto(av, mfheckemat_mfcoefs(mf, B, hecke_data(N,p)));
    4478             : }
    4479             : 
    4480             : /* mf_NEW != (0), weight > 1, p prime. Use
    4481             :  * T(p) T(j) = T(j*p) + p^{k-1} \chi(p) 1_{p | j, p \nmid N} T(j/p) */
    4482             : static GEN
    4483         833 : mfnewmathecke_p(GEN mf, long p)
    4484             : {
    4485         833 :   pari_sp av = avma;
    4486         833 :   GEN tf, vj = MFnew_get_vj(mf), CHI = MF_get_CHI(mf);
    4487         833 :   GEN Mindex = MF_get_Mindex(mf), Minv = MF_get_Minv(mf);
    4488         833 :   long N = MF_get_N(mf), k = MF_get_k(mf);
    4489         833 :   long i, j, lvj = lg(vj), lim = vj[lvj-1] * p;
    4490         833 :   GEN M, perm, V, need = zero_zv(lim);
    4491         833 :   GEN C = (N % p)? gmul(mfchareval_i(CHI,p), powuu(p,k-1)): NULL;
    4492         833 :   tf = mftraceform_new(N, k, CHI);
    4493        3423 :   for (i = 1; i < lvj; i++)
    4494             :   {
    4495        2590 :     j = vj[i]; need[j*p] = 1;
    4496        2590 :     if (N % p && j % p == 0) need[j/p] = 1;
    4497             :   }
    4498         833 :   perm = zero_zv(lim);
    4499         833 :   V = cgetg(lim+1, t_VEC);
    4500       11389 :   for (i = j = 1; i <= lim; i++)
    4501       10556 :     if (need[i]) { gel(V,j) = mfhecke_i(i, N, tf); perm[i] = j; j++; }
    4502         833 :   setlg(V, j);
    4503         833 :   V = bhnmat_extend_nocache(NULL, N, mfsturm_mf(mf)-1, 1, V);
    4504         833 :   V = rowpermute(V, Mindex); /* V[perm[i]] = coeffs(T_i newtrace) */
    4505         833 :   M = cgetg(lvj, t_MAT);
    4506        3423 :   for (i = 1; i < lvj; i++)
    4507             :   {
    4508             :     GEN t;
    4509        2590 :     j = vj[i]; t = gel(V, perm[j*p]);
    4510        2590 :     if (C && j % p == 0) t = RgC_add(t, RgC_Rg_mul(gel(V, perm[j/p]),C));
    4511        2590 :     gel(M,i) = t;
    4512             :   }
    4513         833 :   return gerepileupto(av, Minv_RgM_mul(Minv, M));
    4514             : }
    4515             : 
    4516             : GEN
    4517          77 : mfheckemat(GEN mf, GEN vn)
    4518             : {
    4519          77 :   pari_sp av = avma;
    4520          77 :   long lv, lvP, i, N, dim, nk, dk, p, sb, flint = (typ(vn)==t_INT);
    4521             :   GEN CHI, res, vT, FA, B, vP;
    4522             : 
    4523          77 :   mf = checkMF(mf);
    4524          77 :   if (typ(vn) != t_VECSMALL) vn = gtovecsmall(vn);
    4525          77 :   N = MF_get_N(mf); CHI = MF_get_CHI(mf); Qtoss(MF_get_gk(mf), &nk, &dk);
    4526          77 :   dim = MF_get_dim(mf);
    4527          77 :   lv = lg(vn);
    4528          77 :   res = cgetg(lv, t_VEC);
    4529          77 :   FA = cgetg(lv, t_VEC);
    4530          77 :   vP = cgetg(lv, t_VEC);
    4531          77 :   vT = const_vec(vecsmall_max(vn), NULL);
    4532         182 :   for (i = 1; i < lv; i++)
    4533             :   {
    4534         105 :     ulong n = (ulong)labs(vn[i]);
    4535             :     GEN fa;
    4536         105 :     if (!n) pari_err_TYPE("mfheckemat", vn);
    4537         105 :     if (dk == 1 || uissquareall(n, &n)) fa = myfactoru(n);
    4538           0 :     else { n = 0; fa = myfactoru(1); } /* dummy: T_{vn[i]} = 0 */
    4539         105 :     vn[i] = n;
    4540         105 :     gel(FA,i) = fa;
    4541         105 :     gel(vP,i) = gel(fa,1);
    4542             :   }
    4543          77 :   vP = shallowconcat1(vP); vecsmall_sort(vP);
    4544          77 :   vP = vecsmall_uniq_sorted(vP); /* all primes occurring in vn */
    4545          77 :   lvP = lg(vP); if (lvP == 1) goto END;
    4546          56 :   p = vP[lvP-1];
    4547          56 :   sb = mfsturm_mf(mf)-1;
    4548          56 :   if (dk == 1 && nk != 1 && MF_get_space(mf) == mf_NEW)
    4549          21 :     B = NULL; /* special purpose mfnewmathecke_p is faster */
    4550          35 :   else if (lvP == 2 && N % p == 0)
    4551          21 :     B = mfcoefs_mf(mf, sb, dk==2? p*p: p); /* single prime | N, can optimize */
    4552             :   else
    4553          14 :     B = mfcoefs_mf(mf, sb * (dk==2? p*p: p), 1); /* general initialization */
    4554         126 :   for (i = 1; i < lvP; i++)
    4555             :   {
    4556          70 :     long j, l, q, e = 1;
    4557             :     GEN C, Tp, u1, u0;
    4558          70 :     p = vP[i];
    4559          70 :     for (j = 1; j < lv; j++) e = maxss(e, z_lval(vn[j], p));
    4560          70 :     if (!B)
    4561          28 :       Tp = mfnewmathecke_p(mf, p);
    4562          42 :     else if (dk == 2)
    4563           7 :       Tp = mfheckemat_mfcoefs_p2(mf,p, (lvP==2||N%p)? B: matdeflate(sb,p*p,B));
    4564             :     else
    4565          35 :       Tp = mfheckemat_mfcoefs_p(mf, p, (lvP==2||N%p)? B: matdeflate(sb,p,B));
    4566          70 :     gel(vT, p) = Tp;
    4567          70 :     if (e == 1) continue;
    4568          14 :     u0 = gen_1;
    4569          14 :     if (dk == 2)
    4570             :     {
    4571           0 :       C = N % p? gmul(mfchareval_i(CHI,p*p), powuu(p, nk-2)): NULL;
    4572           0 :       if (e == 2) u0 = sstoQ(p+1,p); /* special case T_{p^4} */
    4573             :     }
    4574             :     else
    4575          14 :       C = N % p? gmul(mfchareval_i(CHI,p),   powuu(p, nk-1)): NULL;
    4576          28 :     for (u1=Tp, q=p, l=2; l <= e; l++)
    4577             :     { /* u0 = T_{p^{l-2}}, u1 = T_{p^{l-1}} for l > 2 */
    4578          14 :       GEN v = gmul(Tp, u1);
    4579          14 :       if (C) v = gsub(v, gmul(C, u0));
    4580             :       /* q = p^l, vT[q] = T_q for k integer else T_{q^2} */
    4581          14 :       q *= p; u0 = u1; gel(vT, q) = u1 = v;
    4582             :     }
    4583             :   }
    4584             : END:
    4585             :   /* vT[p^e] = T_{p^e} for all p^e occurring below */
    4586         182 :   for (i = 1; i < lv; i++)
    4587             :   {
    4588         105 :     long n = vn[i], j, lP;
    4589             :     GEN fa, P, E, M;
    4590         105 :     if (n == 0) { gel(res,i) = zeromat(dim,dim); continue; }
    4591         105 :     if (n == 1) { gel(res,i) = matid(dim); continue; }
    4592          77 :     fa = gel(FA,i);
    4593          77 :     P = gel(fa,1); lP = lg(P);
    4594          77 :     E = gel(fa,2); M = gel(vT, upowuu(P[1], E[1]));
    4595          77 :     for (j = 2; j < lP; j++) M = RgM_mul(M, gel(vT, upowuu(P[j], E[j])));
    4596          77 :     gel(res,i) = M;
    4597             :   }
    4598          77 :   if (flint) res = gel(res,1);
    4599          77 :   return gerepilecopy(av, res);
    4600             : }
    4601             : 
    4602             : 
    4603             : /* f = \sum_i v[i] T_listj[i] (Trace Form) attached to v; replace by f/a_1(f) */
    4604             : static GEN
    4605        1162 : mf_normalize(GEN mf, GEN v)
    4606             : {
    4607        1162 :   GEN c, dc = NULL, M = MF_get_M(mf), Mindex = MF_get_Mindex(mf);
    4608        1162 :   v = Q_primpart(v);
    4609        1162 :   c = RgMrow_RgC_mul(M, v, 2); /* a_1(f) */
    4610        1162 :   if (gequal1(c)) return v;
    4611         707 :   if (typ(c) == t_POL) c = gmodulo(c, mfcharpol(MF_get_CHI(mf)));
    4612         707 :   if (typ(c) == t_POLMOD && varn(gel(c,1)) == 1 && degpol(gel(c,1)) >= 40
    4613           7 :                          && Mindex[1] == 2
    4614           7 :                          && mfcharorder(MF_get_CHI(mf)) <= 2)
    4615           7 :   { /* normalize using expansion at infinity (small coefficients) */
    4616           7 :     GEN w, P = gel(c,1), a1 = gel(c,2);
    4617           7 :     long i, l = lg(Mindex);
    4618           7 :     w = cgetg(l, t_COL);
    4619           7 :     gel(w,1) = gen_1;
    4620         280 :     for (i = 2; i < l; i++)
    4621             :     {
    4622         273 :       c = liftpol_shallow(RgMrow_RgC_mul(M, v, Mindex[i]));
    4623         273 :       gel(w,i) = QXQ_div_ratlift(c, a1, P);
    4624             :     }
    4625             :     /* w = expansion at oo of normalized form */
    4626           7 :     v = Minv_RgC_mul(MF_get_Minv(mf), Q_remove_denom(w, &dc));
    4627           7 :     v = gmodulo(v, P); /* back to mfbasis coefficients */
    4628             :   }
    4629             :   else
    4630             :   {
    4631         700 :     c = ginv(c);
    4632         700 :     if (typ(c) == t_POLMOD) c = Q_remove_denom(c, &dc);
    4633         700 :     v = RgC_Rg_mul(v, c);
    4634             :   }
    4635         707 :   if (dc) v = RgC_Rg_div(v, dc);
    4636         707 :   return v;
    4637             : }
    4638             : static void
    4639         238 : pol_red(GEN NF, GEN *pP, GEN *pa, long flag)
    4640             : {
    4641         238 :   GEN dP, a, P = *pP;
    4642         238 :   long d = degpol(P);
    4643             : 
    4644         238 :   *pa = a = pol_x(varn(P));
    4645         238 :   if (d > 30) return;
    4646             : 
    4647         231 :   dP = RgX_disc(P);
    4648         231 :   if (typ(dP) != t_INT)
    4649          35 :   { dP = gnorm(dP); if (typ(dP) != t_INT) pari_err_BUG("mfnewsplit"); }
    4650         231 :   if (d == 2 || expi(dP) < 62)
    4651             :   {
    4652         217 :     if (expi(dP) < 31)
    4653         217 :       P = NF? rnfpolredabs(NF, P,flag): polredabs0(P,flag);
    4654             :     else
    4655           0 :       P = NF? rnfpolredbest(NF,P,flag): polredbest(P,flag);
    4656         217 :     if (flag)
    4657             :     {
    4658         203 :       a = gel(P,2); if (typ(a) == t_POLMOD) a = gel(a,2);
    4659         203 :       P = gel(P,1);
    4660             :     }
    4661             :   }
    4662         231 :   *pP = P;
    4663         231 :   *pa = a;
    4664             : }
    4665             : 
    4666             : /* Diagonalize and normalize. See mfsplit for meaning of flag. */
    4667             : static GEN
    4668         847 : mfspclean(GEN mf, GEN mf0, GEN NF, long ord, GEN simplesp, long flag)
    4669             : {
    4670         847 :   const long vz = 1;
    4671         847 :   long i, l = lg(simplesp);
    4672         847 :   GEN res = cgetg(l, t_MAT), pols = cgetg(l, t_VEC);
    4673         847 :   GEN zeros = (mf == mf0)? NULL: zerocol(MF_get_dim(mf) - MF_get_dim(mf0));
    4674        2023 :   for (i = 1; i < l; i++)
    4675             :   {
    4676        1176 :     GEN ATP = gel(simplesp, i), A = gel(ATP,1), P = gel(ATP,3);
    4677        1176 :     long d = degpol(P);
    4678        1176 :     GEN a, v = (flag && d > flag)? NULL: gel(A,1);
    4679        1176 :     if (d == 1) P = pol_x(vz);
    4680             :     else
    4681             :     {
    4682         238 :       pol_red(NF, &P, &a, !!v);
    4683         238 :       if (v)
    4684             :       { /* Mod(a,P) root of charpoly(T), K*gpowers(a) = eigenvector of T */
    4685         224 :         GEN K, den, M = cgetg(d+1, t_MAT), T = gel(ATP,2);
    4686             :         long j;
    4687         224 :         T = shallowtrans(T);
    4688         224 :         gel(M,1) = vec_ei(d,1); /* basis of cyclic vectors */
    4689         224 :         for (j = 2; j <= d; j++) gel(M,j) = RgM_RgC_mul(T, gel(M,j-1));
    4690         224 :         M = Q_primpart(M);
    4691         273 :         K = NF? ZabM_inv(liftpol_shallow(M), nf_get_pol(NF), ord, &den)
    4692         273 :               : ZM_inv(M,&den);
    4693         224 :         K = shallowtrans(K);
    4694         224 :         v = gequalX(a)? pol_x_powers(d, vz): RgXQ_powers(a, d-1, P);
    4695         224 :         v = gmodulo(RgM_RgC_mul(A, RgM_RgC_mul(K,v)), P);
    4696             :       }
    4697             :     }
    4698        1176 :     if (v)
    4699             :     {
    4700        1162 :       v = mf_normalize(mf0, v); if (zeros) v = shallowconcat(zeros,v);
    4701        1162 :       gel(res,i) = v; if (flag) setlg(res,i+1);
    4702             :     }
    4703        1176 :     gel(pols,i) = P;
    4704             :   }
    4705         847 :   return mkvec2(res, pols);
    4706             : }
    4707             : 
    4708             : /* return v = v_{X-r}(P), and set Z = P / (X-r)^v */
    4709             : static long
    4710          63 : RgX_valrem_root(GEN P, GEN r, GEN *Z)
    4711             : {
    4712             :   long v;
    4713         126 :   for (v = 0; degpol(P); v++)
    4714             :   {
    4715         126 :     GEN t, Q = RgX_div_by_X_x(P, r, &t);
    4716         126 :     if (!gequal0(t)) break;
    4717          63 :     P = Q;
    4718             :   }
    4719          63 :   *Z = P; return v;
    4720             : }
    4721             : static GEN
    4722         945 : mynffactor(GEN NF, GEN P, long dimlim)
    4723             : {
    4724             :   long i, l, v;
    4725             :   GEN R, E;
    4726         945 :   if (dimlim != 1)
    4727             :   {
    4728         392 :     R = NF? nffactor(NF, P): QX_factor(P);
    4729         392 :     if (!dimlim) return R;
    4730          21 :     E = gel(R,2);
    4731          21 :     R = gel(R,1); l = lg(R);
    4732          98 :     for (i = 1; i < l; i++)
    4733          91 :       if (degpol(gel(R,i)) > dimlim) break;
    4734          21 :     if (i == 1) return NULL;
    4735          21 :     setlg(E,i);
    4736          21 :     setlg(R,i); return mkmat2(R, E);
    4737             :   }
    4738             :   /* dimlim = 1 */
    4739         553 :   R = nfroots(NF, P); l = lg(R);
    4740         553 :   if (l == 1) return NULL;
    4741         490 :   v = varn(P);
    4742         490 :   settyp(R, t_COL);
    4743         490 :   if (degpol(P) == l-1)
    4744         441 :     E = const_col(l-1, gen_1);
    4745             :   else
    4746             :   {
    4747          49 :     E = cgetg(l, t_COL);
    4748          49 :     for (i = 1; i < l; i++) gel(E,i) = utoi(RgX_valrem_root(P, gel(R,i), &P));
    4749             :   }
    4750         490 :   R = deg1_from_roots(R, v);
    4751         490 :   return mkmat2(R, E);
    4752             : }
    4753             : 
    4754             : /* Let K be a number field attached to NF (Q if NF = NULL). A K-vector
    4755             :  * space of dimension d > 0 is given by a t_MAT A (n x d, full column rank)
    4756             :  * giving a K-basis, X a section (d x n: left pseudo-inverse of A). Return a
    4757             :  * pair (T, fa), where T is an element of the Hecke algebra (a sum of Tp taken
    4758             :  * from vector vTp) acting on A (a d x d t_MAT) and fa is the factorization of
    4759             :  * its characteristic polynomial, limited to factors of degree <= dimlim if
    4760             :  * dimlim != 0 (return NULL if there are no factors of degree <= dimlim) */
    4761             : static GEN
    4762         938 : findbestsplit(GEN NF, GEN vTp, GEN A, GEN X, long dimlim, long vz)
    4763             : {
    4764         938 :   GEN T = NULL, Tkeep = NULL, fakeep = NULL;
    4765         938 :   long lmax = 0, i, lT = lg(vTp);
    4766        2030 :   for (i = 1; i < lT; i++)
    4767             :   {
    4768        1015 :     GEN D, P, E, fa, TpA = gel(vTp,i);
    4769             :     long l;
    4770        1890 :     if (typ(TpA) == t_INT) break;
    4771         945 :     if (lg(TpA) > lg(A)) TpA = RgM_mul(X, RgM_mul(TpA, A)); /* Tp | A */
    4772         945 :     T = T ? RgM_add(T, TpA) : TpA;
    4773         945 :     if (!NF) { P = QM_charpoly_ZX(T); setvarn(P, vz); }
    4774             :     else
    4775             :     {
    4776         112 :       P = charpoly(Q_remove_denom(T, &D), vz);
    4777         112 :       if (D) P = gdiv(RgX_unscale(P, D), powiu(D, degpol(P)));
    4778             :     }
    4779         945 :     fa = mynffactor(NF, P, dimlim);
    4780         945 :     if (!fa) return NULL;
    4781         882 :     E = gel(fa, 2);
    4782             :     /* characteristic polynomial is separable ? */
    4783         882 :     if (isint1(vecmax(E))) { Tkeep = T; fakeep = fa; break; }
    4784          77 :     l = lg(E);
    4785             :     /* characteristic polynomial has more factors than before ? */
    4786          77 :     if (l > lmax) { lmax = l; Tkeep = T; fakeep = fa; }
    4787             :   }
    4788         875 :   return mkvec2(Tkeep, fakeep);
    4789             : }
    4790             : 
    4791             : static GEN
    4792         147 : nfcontent(GEN nf, GEN v)
    4793             : {
    4794         147 :   long i, l = lg(v);
    4795         147 :   GEN c = gel(v,1);
    4796         147 :   for (i = 2; i < l; i++) c = idealadd(nf, c, gel(v,i));
    4797         147 :   if (typ(c) == t_MAT && gequal1(gcoeff(c,1,1))) c = gen_1;
    4798         147 :   return c;
    4799             : }
    4800             : static GEN
    4801         224 : nf_primpart(GEN nf, GEN B)
    4802             : {
    4803         224 :   switch(typ(B))
    4804             :   {
    4805             :     case t_COL:
    4806             :     {
    4807         147 :       GEN A = matalgtobasis(nf, B), c = nfcontent(nf, A);
    4808         147 :       if (typ(c) == t_INT) return B;
    4809          14 :       c = idealred_elt(nf,c);
    4810          14 :       A = Q_primpart( nfC_nf_mul(nf, A, Q_primpart(nfinv(nf,c))) );
    4811          14 :       A = liftpol_shallow( matbasistoalg(nf, A) );
    4812          14 :       if (gexpo(A) > gexpo(B)) A = B;
    4813          14 :       return A;
    4814             :     }
    4815             :     case t_MAT:
    4816             :     {
    4817             :       long i, l;
    4818          77 :       GEN A = cgetg_copy(B, &l);
    4819          77 :       for (i = 1; i < l; i++) gel(A,i) = nf_primpart(nf, gel(B,i));
    4820          77 :       return A;
    4821             :     }
    4822             :     default:
    4823           0 :       pari_err_TYPE("nf_primpart", B);
    4824             :       return NULL; /*LCOV_EXCL_LINE*/
    4825             :   }
    4826             : }
    4827             : 
    4828             : /* rotate entries of v to accomodate new entry 'x' (push out oldest entry) */
    4829             : static void
    4830         903 : vecpush(GEN v, GEN x)
    4831             : {
    4832             :   long i;
    4833         903 :   for (i = lg(v)-1; i > 1; i--) gel(v,i) = gel(v,i-1);
    4834         903 :   gel(v,1) = x;
    4835         903 : }
    4836             : 
    4837             : /* sort t_VEC of vector spaces by increasing dimension */
    4838             : static GEN
    4839         847 : sort_by_dim(GEN v)
    4840             : {
    4841         847 :   long i, l = lg(v);
    4842         847 :   GEN D = cgetg(l, t_VECSMALL);
    4843         847 :   for (i = 1; i < l; i++) D[i] = lg(gmael(v,i,2));
    4844         847 :   return vecpermute(v , vecsmall_indexsort(D));
    4845             : }
    4846             : static GEN
    4847         847 : split_starting_space(GEN mf)
    4848             : {
    4849         847 :   long d = MF_get_dim(mf), d2;
    4850         847 :   GEN id = matid(d);
    4851         847 :   switch(MF_get_space(mf))
    4852             :   {
    4853             :     case mf_NEW:
    4854         840 :     case mf_CUSP: return mkvec2(id, id);
    4855             :   }
    4856           7 :   d2 = lg(MF_get_S(mf))-1;
    4857           7 :   return mkvec2(vecslice(id, d-d2+1,d),
    4858             :                 shallowconcat(zeromat(d2,d-d2),matid(d2)));
    4859             : }
    4860             : /* If dimlim > 0, keep only the dimension <= dimlim eigenspaces.
    4861             :  * See mfsplit for the meaning of flag. */
    4862             : static GEN
    4863        1239 : split_ii(GEN mf, long dimlim, long flag, long *pnewd)
    4864             : {
    4865             :   forprime_t iter;
    4866        1239 :   GEN CHI = MF_get_CHI(mf), empty = cgetg(1, t_VEC), mf0 = mf;
    4867             :   GEN NF, POLCYC, todosp, Tpbigvec, simplesp;
    4868        1239 :   long N = MF_get_N(mf), k = MF_get_k(mf);
    4869        1239 :   long ord, FC, NEWT, dimsimple = 0, newd = -1;
    4870        1239 :   const long NBH = 5, vz = 1;
    4871             :   ulong p;
    4872             : 
    4873        1239 :   switch(MF_get_space(mf))
    4874             :   {
    4875        1127 :     case mf_NEW: break;
    4876             :     case mf_CUSP:
    4877             :     case mf_FULL:
    4878         105 :       if (k > 1) { mf0 = mfinittonew(mf); break; }
    4879          98 :       newd = lg(MF_get_S(mf))-1 - mfolddim(N, k, CHI);
    4880          98 :       break;
    4881           7 :     default: pari_err_TYPE("mfsplit [space does not contain newspace]", mf);
    4882             :       return NULL; /*LCOV_EXCL_LINE*/
    4883             :   }
    4884        1232 :   if (newd < 0) newd = mf0? MF_get_dim(mf0): 0;
    4885        1232 :   *pnewd = newd;
    4886        1232 :   if (!newd) return mkvec2(cgetg(1, t_MAT), empty);
    4887             : 
    4888         847 :   NEWT = (k > 1 && MF_get_space(mf0) == mf_NEW);
    4889         847 :   todosp = mkvec( split_starting_space(mf0) );
    4890         847 :   simplesp = empty;
    4891         847 :   FC = mfcharconductor(CHI);
    4892         847 :   ord = mfcharorder_canon(CHI);
    4893         847 :   if (ord == 1) NF = POLCYC = NULL;
    4894             :   else
    4895             :   {
    4896          77 :     POLCYC = mfcharpol(CHI);
    4897          77 :     NF = nfinit(POLCYC,DEFAULTPREC);
    4898             :   }
    4899         847 :   Tpbigvec = zerovec(NBH);
    4900         847 :   u_forprime_init(&iter, 2, ULONG_MAX);
    4901         847 :   while (dimsimple < newd && (p = u_forprime_next(&iter)))
    4902             :   {
    4903             :     GEN nextsp;
    4904             :     long ind;
    4905        1127 :     if (N % (p*p) == 0 && N/p % FC == 0) continue; /* T_p = 0 in this case */
    4906         903 :     vecpush(Tpbigvec, NEWT? mfnewmathecke_p(mf0,p): mfheckemat_p(mf0,p));
    4907         903 :     nextsp = empty;
    4908        1841 :     for (ind = 1; ind < lg(todosp); ind++)
    4909             :     {
    4910         938 :       GEN tmp = gel(todosp, ind), fa, P, E, D, Tp, DTp;
    4911         938 :       GEN A = gel(tmp, 1);
    4912         938 :       GEN X = gel(tmp, 2);
    4913             :       long lP, i;
    4914         938 :       tmp = findbestsplit(NF, Tpbigvec, A, X, dimlim, vz);
    4915        1575 :       if (!tmp) continue; /* nothing there */
    4916         875 :       Tp = gel(tmp, 1);
    4917         875 :       fa = gel(tmp, 2);
    4918         875 :       P = gel(fa, 1);
    4919         875 :       E = gel(fa, 2); lP = lg(P);
    4920             :       /* lP > 1 */
    4921         875 :       if (DEBUGLEVEL) err_printf("Exponents = %Ps\n", E);
    4922         875 :       if (lP == 2)
    4923             :       {
    4924         616 :         GEN P1 = gel(P,1);
    4925         616 :         long e1 = itos(gel(E,1)), d1 = degpol(P1);
    4926         616 :         if (e1 * d1 == lg(Tp)-1)
    4927             :         {
    4928         574 :           if (e1 > 1) nextsp = vec_append(nextsp, mkvec2(A,X));
    4929             :           else
    4930             :           { /* simple module */
    4931         567 :             simplesp = vec_append(simplesp, mkvec3(A,Tp,P1));
    4932         567 :             dimsimple += d1;
    4933             :           }
    4934         574 :           continue;
    4935             :         }
    4936             :       }
    4937             :       /* Found splitting */
    4938         301 :       DTp = Q_remove_denom(Tp, &D);
    4939        1008 :       for (i = 1; i < lP; i++)
    4940             :       {
    4941         707 :         GEN Ai, Xi, dXi, AAi, v, y, Pi = gel(P,i);
    4942         707 :         Ai = RgX_RgM_eval(D? RgX_rescale(Pi,D): Pi, DTp);
    4943         707 :         Ai = QabM_ker(Ai, POLCYC, ord);
    4944         707 :         if (NF) Ai = nf_primpart(NF, Ai);
    4945             : 
    4946         707 :         AAi = RgM_mul(A, Ai);
    4947             :         /* gives section, works on nonsquare matrices */
    4948         707 :         Xi = QabM_pseudoinv(Ai, POLCYC, ord, &v, &dXi);
    4949         707 :         Xi = RgM_Rg_div(Xi, dXi);
    4950         707 :         y = gel(v,1);
    4951         707 :         if (isint1(gel(E,i)))
    4952             :         {
    4953         609 :           GEN Tpi = RgM_mul(Xi, RgM_mul(rowpermute(Tp,y), Ai));
    4954         609 :           simplesp = vec_append(simplesp, mkvec3(AAi, Tpi, Pi));
    4955         609 :           dimsimple += degpol(Pi);
    4956             :         }
    4957             :         else
    4958             :         {
    4959          98 :           Xi = RgM_mul(Xi, rowpermute(X,y));
    4960          98 :           nextsp = vec_append(nextsp, mkvec2(AAi, Xi));
    4961             :         }
    4962             :       }
    4963             :     }
    4964         903 :     todosp = nextsp; if (lg(todosp) == 1) break;
    4965             :   }
    4966         847 :   if (DEBUGLEVEL) err_printf("end split, need to clean\n");
    4967         847 :   return mfspclean(mf, mf0, NF, ord, sort_by_dim(simplesp), flag);
    4968             : }
    4969             : static GEN
    4970          14 : dim_filter(GEN v, long dim)
    4971             : {
    4972          14 :   GEN P = gel(v,2);
    4973          14 :   long j, l = lg(P);
    4974         112 :   for (j = 1; j < l; j++)
    4975         105 :     if (degpol(gel(P,j)) > dim)
    4976             :     {
    4977           7 :       v = mkvec2(vecslice(gel(v,1),1,j-1), vecslice(P,1,j-1));
    4978           7 :       break;
    4979             :     }
    4980          14 :   return v;
    4981             : }
    4982             : static long
    4983         119 : dim_sum(GEN v)
    4984             : {
    4985         119 :   GEN P = gel(v,2);
    4986         119 :   long j, l = lg(P), d = 0;
    4987         119 :   for (j = 1; j < l; j++) d += degpol(gel(P,j));
    4988         119 :   return d;
    4989             : }
    4990             : static GEN
    4991        1183 : split_i(GEN mf, long dimlim, long flag)
    4992        1183 : { long junk; return split_ii(mf, dimlim, flag, &junk); }
    4993             : /* mf is either already split or output by mfinit. Splitting is done only for
    4994             :  * newspace except in weight 1. If flag = 0 (default) split completely.
    4995             :  * If flag = d > 0, only give the Galois polynomials in degree > d
    4996             :  * Flag is ignored if dimlim = 1. */
    4997             : GEN
    4998          70 : mfsplit(GEN mf0, long dimlim, long flag)
    4999             : {
    5000          70 :   pari_sp av = avma;
    5001          70 :   GEN v, mf = checkMF_i(mf0);
    5002          70 :   if (!mf) pari_err_TYPE("mfsplit", mf0);
    5003          70 :   if ((v = obj_check(mf, MF_SPLIT)))
    5004          14 :   { if (dimlim) v = dim_filter(v, dimlim); }
    5005          56 :   else if (dimlim && (v = obj_check(mf, MF_SPLITN)))
    5006          21 :   { v = (itos(gel(v,1)) >= dimlim)? dim_filter(gel(v,2), dimlim): NULL; }
    5007          70 :   if (!v)
    5008             :   {
    5009             :     long newd;
    5010          56 :     v = split_ii(mf, dimlim, flag, &newd);
    5011          56 :     if (lg(v) == 1) obj_insert(mf, MF_SPLITN, mkvec2(utoi(dimlim), v));
    5012          56 :     else if (!flag)
    5013             :     {
    5014          42 :       if (dim_sum(v) == newd) obj_insert(mf, MF_SPLIT,v);
    5015          21 :       else obj_insert(mf, MF_SPLITN, mkvec2(utoi(dimlim), v));
    5016             :     }
    5017             :   }
    5018          70 :   return gerepilecopy(av, v);
    5019             : }
    5020             : static GEN
    5021         280 : split(GEN mf) { return split_i(mf,0,0); }
    5022             : GEN
    5023         406 : MF_get_newforms(GEN mf) { return gel(obj_checkbuild(mf,MF_SPLIT,&split),1); }
    5024             : GEN
    5025         385 : MF_get_fields(GEN mf) { return gel(obj_checkbuild(mf,MF_SPLIT,&split),2); }
    5026             : 
    5027             : /*************************************************************************/
    5028             : /*                     Modular forms of Weight 1                         */
    5029             : /*************************************************************************/
    5030             : /* S_1(G_0(N)), small N. Return 1 if definitely empty; return 0 if maybe
    5031             :  * non-empty  */
    5032             : static int
    5033       15932 : wt1empty(long N)
    5034             : {
    5035       15932 :   if (N <= 100) switch (N)
    5036             :   { /* non-empty [32/100] */
    5037             :     case 23: case 31: case 39: case 44: case 46:
    5038             :     case 47: case 52: case 55: case 56: case 57:
    5039             :     case 59: case 62: case 63: case 68: case 69:
    5040             :     case 71: case 72: case 76: case 77: case 78:
    5041             :     case 79: case 80: case 83: case 84: case 87:
    5042             :     case 88: case 92: case 93: case 94: case 95:
    5043        5425 :     case 99: case 100: return 0;
    5044        3437 :     default: return 1;
    5045             :   }
    5046        7070 :   if (N <= 600) switch(N)
    5047             :   { /* empty [111/500] */
    5048             :     case 101: case 102: case 105: case 106: case 109:
    5049             :     case 113: case 121: case 122: case 123: case 125:
    5050             :     case 130: case 134: case 137: case 146: case 149:
    5051             :     case 150: case 153: case 157: case 162: case 163:
    5052             :     case 169: case 170: case 173: case 178: case 181:
    5053             :     case 182: case 185: case 187: case 193: case 194:
    5054             :     case 197: case 202: case 205: case 210: case 218:
    5055             :     case 221: case 226: case 233: case 241: case 242:
    5056             :     case 245: case 246: case 250: case 257: case 265:
    5057             :     case 267: case 269: case 274: case 277: case 281:
    5058             :     case 289: case 293: case 298: case 305: case 306:
    5059             :     case 313: case 314: case 317: case 326: case 337:
    5060             :     case 338: case 346: case 349: case 353: case 361:
    5061             :     case 362: case 365: case 369: case 370: case 373:
    5062             :     case 374: case 377: case 386: case 389: case 394:
    5063             :     case 397: case 401: case 409: case 410: case 421:
    5064             :     case 425: case 427: case 433: case 442: case 449:
    5065             :     case 457: case 461: case 466: case 481: case 482:
    5066             :     case 485: case 490: case 493: case 509: case 514:
    5067             :     case 521: case 530: case 533: case 534: case 538:
    5068             :     case 541: case 545: case 554: case 557: case 562:
    5069             :     case 565: case 569: case 577: case 578: case 586:
    5070         336 :     case 593: return 1;
    5071        6720 :     default: return 0;
    5072             :   }
    5073          14 :   return 0;
    5074             : }
    5075             : 
    5076             : static GEN
    5077          28 : initwt1trace(GEN mf)
    5078             : {
    5079          28 :   GEN S = MF_get_S(mf), v, H;
    5080             :   long l, i;
    5081          28 :   if (lg(S) == 1) return mftrivial();
    5082          28 :   H = mfheckemat(mf, Mindex_as_coef(mf));
    5083          28 :   l = lg(H); v = cgetg(l, t_VEC);
    5084          28 :   for (i = 1; i < l; i++) gel(v,i) = gtrace(gel(H,i));
    5085          28 :   v = Minv_RgC_mul(MF_get_Minv(mf), v);
    5086          28 :   return mflineardiv_linear(S, v, 1);
    5087             : }
    5088             : static GEN
    5089          21 : initwt1newtrace(GEN mf)
    5090             : {
    5091          21 :   GEN v, D, S, Mindex, CHI = MF_get_CHI(mf);
    5092          21 :   long FC, lD, i, sb, N1, N2, lM, N = MF_get_N(mf);
    5093          21 :   CHI = mfchartoprimitive(CHI, &FC);
    5094          21 :   if (N % FC || mfcharparity(CHI) == 1) return mftrivial();
    5095          21 :   D = mydivisorsu(N/FC); lD = lg(D);
    5096          21 :   S = MF_get_S(mf);
    5097          21 :   if (lg(S) == 1) return mftrivial();
    5098          21 :   N2 = newd_params2(N);
    5099          21 :   N1 = N / N2;
    5100          21 :   Mindex = MF_get_Mindex(mf);
    5101          21 :   lM = lg(Mindex);
    5102          21 :   sb = Mindex[lM-1];
    5103          21 :   v = zerovec(sb+1);
    5104          42 :   for (i = 1; i < lD; i++)
    5105             :   {
    5106          21 :     long M = FC*D[i], j;
    5107          21 :     GEN tf = initwt1trace(M == N? mf: mfinit_Nkchi(M, 1, CHI, mf_CUSP, 0));
    5108             :     GEN listd, w;
    5109          21 :     if (mf_get_type(tf) == t_MF_CONST) continue;
    5110          21 :     w = mfcoefs_i(tf, sb, 1);
    5111          21 :     if (M == N) { v = gadd(v, w); continue; }
    5112           0 :     listd = mydivisorsu(u_ppo(ugcd(N/M, N1), FC));
    5113           0 :     for (j = 1; j < lg(listd); j++)
    5114             :     {
    5115           0 :       long d = listd[j], d2 = d*d; /* coprime to FC */
    5116           0 :       GEN dk = mfchareval_i(CHI, d);
    5117           0 :       long NMd = N/(M*d), m;
    5118           0 :       for (m = 1; m <= sb/d2; m++)
    5119             :       {
    5120           0 :         long be = mubeta2(NMd, m);
    5121           0 :         if (be)
    5122             :         {
    5123           0 :           GEN c = gmul(dk, gmulsg(be, gel(w, m+1)));
    5124           0 :           long n = m*d2;
    5125           0 :           gel(v, n+1) = gadd(gel(v, n+1), c);
    5126             :         }
    5127             :       }
    5128             :     }
    5129             :   }
    5130          21 :   if (gequal0(gel(v,2))) return mftrivial();
    5131          21 :   v = vecpermute(v,Mindex);
    5132          21 :   v = Minv_RgC_mul(MF_get_Minv(mf), v);
    5133          21 :   return mflineardiv_linear(S, v, 1);
    5134             : }
    5135             : 
    5136             : /* Matrix of T(p), p \nmid N */
    5137             : static GEN
    5138         119 : Tpmat(long p, long lim, GEN CHI)
    5139             : {
    5140         119 :   GEN M = zeromatcopy(lim, p*lim), chip = mfchareval_i(CHI, p); /* != 0 */
    5141             :   long i, j, pi, pj;
    5142         119 :   gcoeff(M, 1, 1) = gaddsg(1, chip);
    5143         119 :   for (i = 1, pi = p; i < lim; i++,  pi += p) gcoeff(M, i+1, pi+1) = gen_1;
    5144         119 :   for (j = 1, pj = p; pj < lim; j++, pj += p) gcoeff(M, pj+1, j+1) = chip;
    5145         119 :   return M;
    5146             : }
    5147             : 
    5148             : /* assume !wt1empty(N), in particular N>25 */
    5149             : /* Returns [[lim,p], mf (weight 2), p*lim x dim matrix] */
    5150             : static GEN
    5151        1722 : mfwt1_pre(long N)
    5152             : {
    5153        1722 :   GEN M, mf = mfinit_Nkchi(N, 2, mfchartrivial(), mf_CUSP, 0);
    5154             :   /*not empty for N>25*/
    5155             :   long p, lim;
    5156        1722 :   if (uisprime(N))
    5157             :   {
    5158         385 :     p = 2; /*N>25 is not 2 */
    5159         385 :     lim = ceilA1(N, 3);
    5160             :   }
    5161             :   else
    5162             :   {
    5163             :     forprime_t S;
    5164        1337 :     u_forprime_init(&S, 2, N);
    5165        1337 :     while ((p = u_forprime_next(&S)))
    5166        2422 :       if (N % p) break;
    5167        1337 :     lim = mfsturm_mf(mf) + 1;
    5168             :   }
    5169             :   /* p = smalllest prime not dividing N */
    5170        1722 :   M = bhnmat_extend_nocache(MF_get_M(mf), N, p*lim-1, 1, MF_get_S(mf));
    5171        1722 :   return mkvec3(mkvecsmall2(lim, p), mf, M);
    5172             : }
    5173             : 
    5174             : /* lg(A) > 1, E a t_POL */
    5175             : static GEN
    5176         833 : mfmatsermul(GEN A, GEN E)
    5177             : {
    5178         833 :   long j, l = lg(A), r = nbrows(A);
    5179         833 :   GEN M = cgetg(l, t_MAT);
    5180         833 :   E = RgXn_red_shallow(E, r+1);
    5181        8925 :   for (j = 1; j < l; j++)
    5182             :   {
    5183        8092 :     GEN c = RgV_to_RgX(gel(A,j), 0);
    5184        8092 :     gel(M, j) = RgX_to_RgC(RgXn_mul(c, E, r+1), r);
    5185             :   }
    5186         833 :   return M;
    5187             : }
    5188             : /* lg(Ap) > 1, Ep an Flxn */
    5189             : static GEN
    5190         441 : mfmatsermul_Fl(GEN Ap, GEN Ep, ulong p)
    5191             : {
    5192         441 :   long j, l = lg(Ap), r = nbrows(Ap);
    5193         441 :   GEN M = cgetg(l, t_MAT);
    5194        6258 :   for (j = 1; j < l; j++)
    5195             :   {
    5196        5817 :     GEN c = Flv_to_Flx(gel(Ap,j), 0);
    5197        5817 :     gel(M,j) = Flx_to_Flv(Flxn_mul(c, Ep, r+1, p), r);
    5198             :   }
    5199         441 :   return M;
    5200             : }
    5201             : 
    5202             : /* CHI mod F | N, return mfchar of modulus N.
    5203             :  * FIXME: wasteful, G should be precomputed  */
    5204             : static GEN
    5205       16212 : mfcharinduce(GEN CHI, long N)
    5206             : {
    5207             :   GEN G, chi;
    5208       16212 :   if (mfcharmodulus(CHI) == N) return CHI;
    5209        2877 :   G = znstar0(utoipos(N), 1);
    5210        2877 :   chi = zncharinduce(gel(CHI,1), gel(CHI,2), G);
    5211        2877 :   CHI = leafcopy(CHI);
    5212        2877 :   gel(CHI,1) = G;
    5213        2877 :   gel(CHI,2) = chi; return CHI;
    5214             : }
    5215             : 
    5216             : static GEN
    5217        3983 : gmfcharno(GEN CHI)
    5218             : {
    5219        3983 :   GEN G = gel(CHI,1), chi = gel(CHI,2);
    5220        3983 :   return mkintmod(znconreyexp(G, chi), znstar_get_N(G));
    5221             : }
    5222             : static long
    5223       12453 : mfcharno(GEN CHI)
    5224             : {
    5225       12453 :   GEN n = znconreyexp(gel(CHI,1), gel(CHI,2));
    5226       12453 :   return itou(n);
    5227             : }
    5228             : 
    5229             : /* return k such that minimal mfcharacter in Galois orbit of CHI is CHI^k */
    5230             : static long
    5231       11151 : mfconreyminimize(GEN CHI)
    5232             : {
    5233       11151 :   GEN G = gel(CHI,1), cyc, chi;
    5234       11151 :   cyc = ZV_to_zv(znstar_get_cyc(G));
    5235       11151 :   chi = ZV_to_zv(znconreychar(G, gel(CHI,2)));
    5236       11151 :   return zv_cyc_minimize(cyc, chi, coprimes_zv(mfcharorder(CHI)));
    5237             : }
    5238             : 
    5239             : /* find scalar c such that first non-0 entry of c*v is 1; return c*v
    5240             :  * (set c = NULL for 1) */
    5241             : static GEN
    5242        1561 : RgV_normalize(GEN v, GEN *pc)
    5243             : {
    5244        1561 :   long i, l = lg(v);
    5245        1561 :   *pc = NULL;
    5246        3668 :   for (i = 1; i < l; i++)
    5247             :   {
    5248        3668 :     GEN c = gel(v,i);
    5249        3668 :     if (!gequal0(c))
    5250             :     {
    5251        1561 :       if (gequal1(c)) { *pc = gen_1; return v; }
    5252         455 :       *pc = ginv(c); return RgV_Rg_mul(v, *pc);
    5253             :     }
    5254             :   }
    5255           0 :   return v;
    5256             : }
    5257             : /* ordchi != 2 mod 4 */
    5258             : static GEN
    5259        2282 : mftreatdihedral(GEN DIH, GEN POLCYC, long ordchi, long biglim, GEN *pS)
    5260             : {
    5261             :   GEN M, Minv, C;
    5262             :   long l, i;
    5263        2282 :   l = lg(DIH); if (l == 1) return NULL;
    5264        2282 :   if (!pS) return DIH;
    5265         728 :   C = cgetg(l, t_VEC);
    5266         728 :   M = cgetg(l, t_MAT);
    5267        2044 :   for (i = 1; i < l; i++)
    5268             :   {
    5269        1316 :     GEN c, v = mfcoefs_i(gel(DIH,i), biglim, 1);
    5270        1316 :     gel(M,i) = RgV_normalize(v, &c);
    5271        1316 :     gel(C,i) = Rg_col_ei(c, l-1, i);
    5272             :   }
    5273         728 :   Minv = gel(mfclean(M,POLCYC,ordchi,0),2);
    5274         728 :   M = RgM_Minv_mul(M, Minv);
    5275         728 :   C = RgM_Minv_mul(C, Minv);
    5276         728 :   *pS = vecmflinear(DIH, C);
    5277         728 :   return M;
    5278             : }
    5279             : 
    5280             : static GEN
    5281         112 : mfstabiter(GEN M, GEN A2, GEN E1inv, long lim, GEN P, long ordchi)
    5282             : {
    5283             :   GEN A, VC, con;
    5284         112 :   E1inv = primitive_part(E1inv, &con);
    5285         112 :   VC = con? ginv(con): gen_1;
    5286         112 :   A = mfmatsermul(A2, E1inv);
    5287             :   while(1)
    5288          56 :   {
    5289         168 :     GEN R = shallowconcat(RgM_mul(M,A), rowslice(A,1,lim));
    5290         168 :     GEN B = QabM_ker(R, P, ordchi);
    5291         168 :     long lA = lg(A), lB = lg(B);
    5292         168 :     if (lB == 1) return NULL;
    5293         168 :     if (lB == lA) return mkvec2(A, VC);
    5294          56 :     B = rowslice(B, 1, lA-1);
    5295          56 :     if (ordchi != 1) B = gmodulo(B, P);
    5296          56 :     A = Q_primitive_part(RgM_mul(A,B), &con);
    5297          56 :     VC = gmul(VC,B); /* first VC is a scalar, then a RgM */
    5298          56 :     if (con) VC = RgM_Rg_div(VC, con);
    5299             :   }
    5300             : }
    5301             : static long
    5302         112 : mfstabitermodp(GEN Mp, GEN Ap, long p, long lim)
    5303             : {
    5304         112 :   GEN VC = NULL;
    5305             :   while (1)
    5306           7 :   {
    5307         119 :     GEN Rp = shallowconcat(Flm_mul(Mp,Ap,p), rowslice(Ap,1,lim));
    5308         119 :     GEN Bp = Flm_ker(Rp, p);
    5309         119 :     long lA = lg(Ap), lB = lg(Bp);
    5310         119 :     if (lB == 1) return 0;
    5311         119 :     if (lB == lA) return lA-1;
    5312           7 :     Bp = rowslice(Bp, 1, lA-1);
    5313           7 :     Ap = Flm_mul(Ap, Bp, p);
    5314           7 :     VC = VC? Flm_mul(VC, Bp, p): Bp;
    5315             :   }
    5316             : }
    5317             : 
    5318             : static GEN
    5319         210 : mfintereis(GEN A, GEN M2, GEN y, GEN den, GEN E2, GEN P, long ordchi)
    5320             : {
    5321         210 :   GEN z, M1 = mfmatsermul(A,E2), M1den = is_pm1(den)? M1: RgM_Rg_mul(M1,den);
    5322         210 :   M2 = RgM_mul(M2, rowpermute(M1, y));
    5323         210 :   z = QabM_ker(RgM_sub(M2,M1den), P, ordchi);
    5324         210 :   if (ordchi != 1) z = gmodulo(z, P);
    5325         210 :   return mkvec2(RgM_mul(A,z), z);
    5326             : }
    5327             : static GEN
    5328         217 : mfintereismodp(GEN A, GEN M2, GEN E2, ulong p)
    5329             : {
    5330         217 :   GEN M1 = mfmatsermul_Fl(A, E2, p), z;
    5331         217 :   long j, lx = lg(A);
    5332         217 :   z = Flm_ker(shallowconcat(M1, M2), p);
    5333         217 :   for (j = lg(z) - 1; j; j--) setlg(z[j], lx);
    5334         217 :   return mkvec2(Flm_mul(A,z,p), z);
    5335             : }
    5336             : 
    5337             : static GEN
    5338         119 : mfcharinv_i(GEN CHI)
    5339             : {
    5340         119 :   GEN G = gel(CHI,1);
    5341         119 :   CHI = leafcopy(CHI); gel(CHI,2) =  zncharconj(G, gel(CHI,2)); return CHI;
    5342             : }
    5343             : 
    5344             : /* upper bound dim S_1(Gamma_0(N),chi) performing the linear algebra mod p */
    5345             : static long
    5346         119 : mfwt1dimmodp(GEN A, GEN ES, GEN M, long ordchi, long dih, long lim)
    5347             : {
    5348             :   GEN Ap, ApF, ES1p, VC;
    5349         119 :   ulong p, r = QabM_init(ordchi, &p);
    5350             : 
    5351         119 :   ApF = Ap = QabM_to_Flm(A, r, p);
    5352         119 :   VC = NULL;
    5353         119 :   ES1p = QabX_to_Flx(gel(ES,1), r, p);
    5354         119 :   if (lg(ES) >= 3)
    5355             :   {
    5356         112 :     GEN M2 = mfmatsermul_Fl(ApF, ES1p, p);
    5357         112 :     pari_sp av = avma;
    5358             :     long i;
    5359         322 :     for (i = 2; i < lg(ES); i++)
    5360             :     {
    5361         217 :       GEN ESip = QabX_to_Flx(gel(ES,i), r, p);
    5362         217 :       GEN C, ApC = mfintereismodp(Ap, M2, ESip, p);
    5363         217 :       Ap = gel(ApC,1);
    5364         217 :       if (lg(Ap)-1 == dih) return dih;
    5365         210 :       C = gel(ApC,2); VC = VC? Flm_mul(VC, C, p): C;
    5366         210 :       gerepileall(av, 2, &Ap,&VC);
    5367             :     }
    5368             :   }
    5369             :   /* intersection of Eisenstein series quotients non empty: use Schaeffer */
    5370         112 :   Ap = mfmatsermul_Fl(Ap, Flxn_inv(ES1p,nbrows(Ap),p), p);
    5371         112 :   return mfstabitermodp(QabM_to_Flm(M,r,p), Ap, p, lim);
    5372             : }
    5373             : 
    5374             : /* Compute the full S_1(\G_0(N),\chi). If pS is NULL, only the dimension
    5375             :  * dim, in the form of a vector having dim components. Otherwise output
    5376             :  * a basis: ptvf contains a pointer to the vector of forms, and the
    5377             :  * program returns the corresponding matrix of Fourier expansions.
    5378             :  * ptdimdih gives the dimension of the subspace generated by dihedral forms;
    5379             :  * TMP is from mfwt1_pre or NULL. */
    5380             : static GEN
    5381       10605 : mfwt1basis(long N, GEN CHI, GEN TMP, GEN *pS, long *ptdimdih)
    5382             : {
    5383             :   GEN ES, mf, A, M, Tp, tmp1, tmp2, den;
    5384             :   GEN S, ESA, VC, C, POLCYC, ES1, ES1INV, DIH, a0, a0i;
    5385             :   long plim, lim, biglim, i, p, dA, dimp, ordchi, dih;
    5386             : 
    5387       10605 :   if (ptdimdih) *ptdimdih = 0;
    5388       10605 :   if (pS) *pS = NULL;
    5389       10605 :   if (wt1empty(N) || mfcharparity(CHI) != -1) return NULL;
    5390       10423 :   ordchi = mfcharorder_canon(CHI);
    5391       10423 :   if (uisprime(N) && ordchi > 4) return NULL;
    5392       10395 :   if (!pS)
    5393             :   {
    5394        7014 :     dih = mfdihedralcuspdim(N, CHI);
    5395        7014 :     DIH = zerovec(dih);
    5396             :   }
    5397             :   else
    5398             :   {
    5399        3381 :     DIH = mfdihedralcusp(N, CHI);
    5400        3381 :     dih = lg(DIH) - 1;
    5401             :   }
    5402       10395 :   POLCYC = (ordchi == 1)? NULL: mfcharpol(CHI);
    5403       10395 :   if (ptdimdih) *ptdimdih = dih;
    5404       10395 :   biglim = mfsturmNk(N, 2);
    5405       10395 :   if (N <= 600) switch(N)
    5406             :   {
    5407             :     long m;
    5408             :     case 219: case 273: case 283: case 331: case 333: case 344: case 416:
    5409             :     case 438: case 468: case 491: case 504: case 546: case 553: case 563:
    5410             :     case 566: case 581: case 592:
    5411          14 :       break; /* one chi with both exotic and dihedral forms */
    5412             :     default: /* only dihedral forms */
    5413        9408 :       if (!dih) return NULL;
    5414             :       /* fall through */
    5415             :     case 124: case 133: case 148: case 171: case 201: case 209: case 224:
    5416             :     case 229: case 248: case 261: case 266: case 288: case 296: case 301:
    5417             :     case 309: case 325: case 342: case 371: case 372: case 380: case 399:
    5418             :     case 402: case 403: case 404: case 408: case 418: case 432: case 444:
    5419             :     case 448: case 451: case 453: case 458: case 496: case 497: case 513:
    5420             :     case 522: case 527: case 532: case 576: case 579:
    5421             :       /* no chi with both exotic and dihedral; one chi with exotic forms */
    5422        3108 :       if (dih) return mftreatdihedral(DIH, POLCYC, ordchi, biglim, pS);
    5423         833 :       CHI = mfcharinduce(CHI,N);
    5424         833 :       m = mfcharno(CHI);
    5425         833 :       if (N == 124 && (m != 67 && m != 87)) return NULL;
    5426         707 :       if (N == 133 && (m != 83 && m !=125)) return NULL;
    5427         413 :       if (N == 148 && (m !=105 && m !=117)) return NULL;
    5428         287 :       if (N == 171 && (m != 94 && m !=151)) return NULL;
    5429         287 :       if (N == 201 && (m != 29 && m !=104)) return NULL;
    5430         287 :       if (N == 209 && (m != 87 && m !=197)) return NULL;
    5431         287 :       if (N == 224 && (m != 95 && m !=191)) return NULL;
    5432         287 :       if (N == 229 && (m !=107 && m !=122)) return NULL;
    5433         287 :       if (N == 248 && (m != 87 && m !=191)) return NULL;
    5434         196 :       if (N == 261 && (m != 46 && m !=244)) return NULL;
    5435         196 :       if (N == 266 && (m != 83 && m !=125)) return NULL;
    5436         196 :       if (N == 288 && (m != 31 && m !=223)) return NULL;
    5437         196 :       if (N == 296 && (m !=105 && m !=265)) return NULL;
    5438             :   }
    5439         119 :   if (!TMP) TMP = mfwt1_pre(N);
    5440         119 :   tmp1= gel(TMP,1); lim = tmp1[1]; p = tmp1[2]; plim = p*lim;
    5441         119 :   mf  = gel(TMP,2);
    5442         119 :   A   = gel(TMP,3); /* p*lim x dim matrix */
    5443         119 :   S = MF_get_S(mf);
    5444         119 :   ESA = mfeisensteinbasis(N, 1, mfcharinv_i(CHI));
    5445         119 :   ES = RgM_to_RgXV(mfvectomat(ESA, plim+1, 1), 0);
    5446         119 :   ES1 = gel(ES,1); /* does not vanish at oo */
    5447         119 :   Tp = Tpmat(p, lim, CHI);
    5448         119 :   dimp = mfwt1dimmodp(A, ES, Tp, ordchi, dih, lim);
    5449         119 :   if (!dimp) return NULL;
    5450         119 :   if (dimp == dih) return mftreatdihedral(DIH, POLCYC, ordchi, biglim, pS);
    5451         112 :   VC = gen_1;
    5452         112 :   if (lg(ES) >= 3)
    5453             :   {
    5454             :     pari_sp btop;
    5455         105 :     long lim2 = (3*lim)/2 + 1;
    5456         105 :     GEN Ash = rowslice(A, 1, lim2), M2 = mfmatsermul(Ash, ES1);
    5457             :     GEN v, y, M2M2I, M2I;
    5458         105 :     M2I = QabM_pseudoinv(M2, POLCYC, ordchi, &v, &den);
    5459         105 :     y = gel(v,1);
    5460         105 :     M2M2I = RgM_mul(M2,M2I);
    5461         105 :     btop = avma;
    5462         315 :     for (i = 2; i < lg(ES); i++)
    5463             :     {
    5464         210 :       GEN APC = mfintereis(Ash, M2M2I, y, den, gel(ES,i), POLCYC,ordchi);
    5465         210 :       Ash = gel(APC,1); if (lg(Ash) == 1) return NULL;
    5466         210 :       VC = gmul(VC, gel(APC,2));
    5467         210 :       if (gc_needed(btop, 1))
    5468             :       {
    5469           6 :         if (DEBUGMEM > 1) pari_warn(warnmem,"mfwt1basis i = %ld", i);
    5470           6 :         gerepileall(btop, 2, &Ash, &VC);
    5471             :       }
    5472             :     }
    5473         105 :     A = RgM_mul(A, vecslice(VC,1, lg(Ash)-1));
    5474             :   }
    5475         112 :   a0 = gel(ES1,2); /* non-zero */
    5476         112 :   if (gequal1(a0)) a0 = a0i = NULL;
    5477             :   else
    5478             :   {
    5479         112 :     a0i = ginv(a0);
    5480         112 :     ES1 = RgX_Rg_mul(RgX_unscale(ES1,a0), a0i);
    5481             :   }
    5482         112 :   ES1INV = RgXn_inv(ES1, plim-1);
    5483         112 :   if (a0) ES1INV = RgX_Rg_mul(RgX_unscale(ES1INV, a0i), a0i);
    5484         112 :   tmp2 = mfstabiter(Tp, A, ES1INV, lim, POLCYC, ordchi);
    5485         112 :   if (!tmp2) return NULL;
    5486         112 :   A = gel(tmp2,1); dA = lg(A);
    5487         112 :   VC = gmul(VC, gel(tmp2,2));
    5488         112 :   C = cgetg(dA, t_VEC);
    5489         112 :   M = cgetg(dA, t_MAT);
    5490         357 :   for (i = 1; i < dA; i++)
    5491             :   {
    5492         245 :     GEN c, v = gel(A,i);
    5493         245 :     gel(M,i) = RgV_normalize(v, &c);
    5494         245 :     gel(C,i) = RgC_Rg_mul(gel(VC,i), c);
    5495             :   }
    5496         112 :   if (pS)
    5497             :   {
    5498          63 :     GEN Minv = gel(mfclean(M, POLCYC, ordchi, 0), 2);
    5499          63 :     M = RgM_Minv_mul(M, Minv);
    5500          63 :     C = RgM_Minv_mul(C, Minv);
    5501          63 :     *pS = vecmflineardiv0(S, C, gel(ESA,1));
    5502             :   }
    5503         112 :   return M;
    5504             : }
    5505             : 
    5506             : static void
    5507         245 : MF_set_space(GEN mf, long x) { gmael(mf,1,4) = utoi(x); }
    5508             : static GEN
    5509          91 : mfwt1_cusptonew(GEN mf)
    5510             : {
    5511          91 :   const long vy = 1;
    5512          91 :   GEN vP, F, S, Snew, vF, v = split(mf);
    5513             :   long i, lP, dSnew, ct;
    5514             : 
    5515          91 :   F = gel(v,1);
    5516          91 :   vP= gel(v,2); lP = lg(vP);
    5517          91 :   if (lP == 1) { obj_insert(mf, MF_SPLIT, v); return NULL; }
    5518          77 :   MF_set_space(mf, mf_NEW);
    5519          77 :   S = MF_get_S(mf);
    5520          77 :   dSnew = dim_sum(v);
    5521          77 :   Snew = cgetg(dSnew + 1, t_VEC); ct = 0;
    5522          77 :   vF = cgetg(lP, t_MAT);
    5523         168 :   for (i = 1; i < lP; i++)
    5524             :   {
    5525          91 :     GEN V, P = gel(vP,i), f = liftpol_shallow(gel(F,i));
    5526          91 :     long j, d = degpol(P);
    5527          91 :     gel(vF,i) = V = zerocol(dSnew);
    5528          91 :     if (d == 1)
    5529             :     {
    5530          56 :       gel(Snew, ct+1) = mflineardiv_linear(S, f, 0);
    5531          56 :       gel(V, ct+1) = gen_1;
    5532             :     }
    5533             :     else
    5534             :     {
    5535          35 :       f = RgXV_to_RgM(f,d);
    5536         112 :       for (j = 1; j <= d; j++)
    5537             :       {
    5538          77 :         gel(Snew, ct+j) = mflineardiv_linear(S, row(f,j), 0);
    5539          77 :         gel(V, ct+j) = mkpolmod(pol_xn(j-1,vy), P);
    5540             :       }
    5541             :     }
    5542          91 :     ct += d;
    5543             :   }
    5544          77 :   obj_insert(mf, MF_SPLIT, mkvec2(vF, vP));
    5545          77 :   gel(mf,3) = Snew; return mf;
    5546             : }
    5547             : static GEN
    5548        3465 : mfwt1init(long N, GEN CHI, GEN TMP, long space, long flraw)
    5549             : {
    5550        3465 :   GEN mf, mf1, S, M = mfwt1basis(N, CHI, TMP, &S, NULL);
    5551        3465 :   if (!M) return NULL;
    5552         791 :   mf1 = mkvec4(stoi(N), gen_1, CHI, utoi(mf_CUSP));
    5553         791 :   mf = mkmf(mf1, cgetg(1,t_VEC), S, gen_0, NULL);
    5554         791 :   if (space == mf_NEW)
    5555             :   {
    5556          91 :     gel(mf,5) = mfcleanCHI(M,CHI, 0);
    5557          91 :     mf = mfwt1_cusptonew(mf); if (!mf) return NULL;
    5558          77 :     if (!flraw) M = mfcoefs_mf(mf, mfsturmNk(N,1)+1, 1);
    5559             :   }
    5560         777 :   gel(mf,5) = flraw? zerovec(3): mfcleanCHI(M, CHI, 0);
    5561         777 :   return mf;
    5562             : }
    5563             : 
    5564             : static GEN
    5565         931 : mfEMPTY(GEN mf1)
    5566             : {
    5567         931 :   GEN Minv = mkMinv(cgetg(1,t_MAT), NULL,NULL,NULL);
    5568         931 :   GEN M = mkvec3(cgetg(1,t_VECSMALL), Minv, cgetg(1,t_MAT));
    5569         931 :   return mkmf(mf1, cgetg(1,t_VEC), cgetg(1,t_VEC), cgetg(1,t_VEC), M);
    5570             : }
    5571             : static GEN
    5572         616 : mfEMPTYall(long N, GEN gk, GEN vCHI, long space)
    5573             : {
    5574             :   long i, l;
    5575             :   GEN v, gN, gs;
    5576         616 :   if (!vCHI) return cgetg(1, t_VEC);
    5577          14 :   gN = utoipos(N); gs = utoi(space);
    5578          14 :   l = lg(vCHI); v = cgetg(l, t_VEC);
    5579          14 :   for (i = 1; i < l; i++) gel(v,i) = mfEMPTY(mkvec4(gN,gk,gel(vCHI,i),gs));
    5580          14 :   return v;
    5581             : }
    5582             : 
    5583             : static GEN
    5584        3983 : fmt_dim(GEN CHI, long d, long dih)
    5585        3983 : { return mkvec4(gmfcharorder(CHI), gmfcharno(CHI), utoi(d), stoi(dih)); }
    5586             : /* merge two vector of fmt_dim's for the same vector of characters. If CHI
    5587             :  * is not NULL, remove dim-0 spaces and add character from CHI */
    5588             : static GEN
    5589           7 : merge_dims(GEN V, GEN W, GEN CHI)
    5590             : {
    5591           7 :   long i, j, id, l = lg(V);
    5592           7 :   GEN A = cgetg(l, t_VEC);
    5593           7 :   if (l == 1) return A;
    5594           7 :   id = CHI? 1: 3;
    5595          21 :   for (i = j = 1; i < l; i++)
    5596             :   {
    5597          14 :     GEN v = gel(V,i), w = gel(W,i);
    5598          14 :     long dv = itou(gel(v,id)), dvh = itou(gel(v,id+1)), d;
    5599          14 :     long dw = itou(gel(w,id)), dwh = itou(gel(w,id+1));
    5600          14 :     d = dv + dw;
    5601          14 :     if (d || CHI)
    5602          42 :       gel(A,j++) = CHI? fmt_dim(gel(CHI,i),d, dvh+dwh)
    5603          28 :                       : mkvec2s(d,dvh+dwh);
    5604             :   }
    5605           7 :   setlg(A, j); return A;
    5606             : }
    5607             : static GEN
    5608        3010 : mfdim0all(GEN w)
    5609             : {
    5610        3010 :   if (w) retconst_vec(lg(w)-1, zerovec(2));
    5611        3003 :   return cgetg(1,t_VEC);
    5612             : }
    5613             : static long
    5614        7140 : mfwt1cuspdim_i(long N, GEN CHI, GEN TMP, long *dih)
    5615             : {
    5616        7140 :   pari_sp av = avma;
    5617        7140 :   GEN b = mfwt1basis(N, CHI, TMP, NULL, dih);
    5618        7140 :   avma = av; return b? lg(b)-1: 0;
    5619             : }
    5620             : static long
    5621         301 : mfwt1cuspdim(long N, GEN CHI) { return mfwt1cuspdim_i(N, CHI, NULL, NULL); }
    5622             : static GEN
    5623        4144 : mfwt1cuspdimall(long N, GEN vCHI)
    5624             : {
    5625             :   GEN z, TMP, w;
    5626             :   long i, j, l;
    5627        4144 :   if (wt1empty(N)) return mfdim0all(vCHI);
    5628        1141 :   w = mfwt1chars(N,vCHI);
    5629        1141 :   l = lg(w); if (l == 1) return cgetg(1,t_VEC);
    5630        1141 :   z = cgetg(l, t_VEC);
    5631        1141 :   TMP = mfwt1_pre(N);
    5632        7861 :   for (i = j = 1; i < l; i++)
    5633             :   {
    5634        6720 :     GEN CHI = gel(w,i);
    5635        6720 :     long dih, d = mfwt1cuspdim_i(N, CHI, TMP, &dih);
    5636        6720 :     if (vCHI)
    5637          42 :       gel(z,j++) = mkvec2s(d, dih);
    5638        6678 :     else if (d)
    5639        1428 :       gel(z,j++) = fmt_dim(CHI, d, dih);
    5640             :   }
    5641        1141 :   setlg(z,j); return z;
    5642             : }
    5643             : 
    5644             : /* dimension of S_1(Gamma_1(N)) */
    5645             : static long
    5646        4123 : mfwt1cuspdimsum(long N)
    5647             : {
    5648        4123 :   pari_sp av = avma;
    5649        4123 :   GEN v = mfwt1cuspdimall(N, NULL);
    5650        4123 :   long i, ct = 0, l = lg(v);
    5651        5544 :   for (i = 1; i < l; i++)
    5652             :   {
    5653        1421 :     GEN w = gel(v,i); /* [ord(CHI),*,dim,*] */
    5654        1421 :     ct += itou(gel(w,3))*myeulerphiu(itou(gel(w,1)));
    5655             :   }
    5656        4123 :   avma = av; return ct;
    5657             : }
    5658             : 
    5659             : static GEN
    5660          56 : mfwt1newdimall(long N, GEN vCHI)
    5661             : {
    5662             :   GEN z, w, vTMP, fa, P, E;
    5663             :   long i, c, l, lw, P1;
    5664          56 :   if (wt1empty(N)) return mfdim0all(vCHI);
    5665          56 :   w = mfwt1chars(N,vCHI);
    5666          56 :   lw = lg(w); if (lw == 1) return cgetg(1,t_VEC);
    5667          56 :   vTMP = const_vec(N, NULL);
    5668          56 :   gel(vTMP,N) = mfwt1_pre(N);
    5669             :   /* if p || N and p \nmid F(CHI), S_1^new(G0(N),chi) = 0 */
    5670          56 :   fa = znstar_get_faN(gmael(w,1,1));
    5671          56 :   P = gel(fa,1); l = lg(P);
    5672          56 :   E = gel(fa,2);
    5673         154 :   for (i = P1 = 1; i < l; i++)
    5674          98 :     if (E[i] == 1) P1 *= itou(gel(P,i));
    5675             :   /* P1 = \prod_{v_p(N) = 1} p */
    5676          56 :   z = cgetg(lw, t_VEC);
    5677         182 :   for (i = c = 1; i < lw; i++)
    5678             :   {
    5679             :     long S, j, l, F, dihnew;
    5680         126 :     GEN D, CHI = gel(w,i), CHIP = mfchartoprimitive(CHI,&F);
    5681             : 
    5682         126 :     S = F % P1? 0: mfwt1cuspdim_i(N, CHI, gel(vTMP,N), &dihnew);
    5683         126 :     if (!S)
    5684             :     {
    5685          56 :       if (vCHI) gel(z, c++) = zerovec(2);
    5686          56 :       continue;
    5687             :     }
    5688          70 :     D = mydivisorsu(N/F); l = lg(D);
    5689          77 :     for (j = l-2; j > 0; j--) /* skip last M = N */
    5690             :     {
    5691           7 :       long M = D[j]*F, m, s, dih;
    5692           7 :       GEN TMP = gel(vTMP,M);
    5693           7 :       if (wt1empty(M) || !(m = mubeta(D[l-j]))) continue; /*m = mubeta(N/M)*/
    5694           7 :       if (!TMP) gel(vTMP,M) = TMP = mfwt1_pre(M);
    5695           7 :       s = mfwt1cuspdim_i(M, CHIP, TMP, &dih);
    5696           7 :       if (s) { S += m * s; dihnew += m * dih; }
    5697             :     }
    5698          70 :     if (vCHI)
    5699          63 :       gel(z,c++) = mkvec2s(S, dihnew);
    5700           7 :     else if (S)
    5701           7 :       gel(z, c++) = fmt_dim(CHI, S, dihnew);
    5702             :   }
    5703          56 :   setlg(z,c); return z;
    5704             : }
    5705             : 
    5706             : static GEN
    5707          28 : mfwt1olddimall(long N, GEN vCHI)
    5708             : {
    5709             :   long i, j, l;
    5710             :   GEN z, w;
    5711          28 :   if (wt1empty(N)) return mfdim0all(vCHI);
    5712          28 :   w = mfwt1chars(N,vCHI);
    5713          28 :   l = lg(w); z = cgetg(l, t_VEC);
    5714          84 :   for (i = j = 1; i < l; i++)
    5715             :   {
    5716          56 :     GEN CHI = gel(w,i);
    5717          56 :     long d = mfolddim(N, 1, CHI);
    5718          56 :     if (vCHI)
    5719          28 :       gel(z,j++) = mkvec2s(d,d?-1:0);
    5720          28 :     else if (d)
    5721           7 :       gel(z, j++) = fmt_dim(CHI, d, -1);
    5722             :   }
    5723          28 :   setlg(z,j); return z;
    5724             : }
    5725             : 
    5726             : static long
    5727         469 : mfwt1olddimsum(long N)
    5728             : {
    5729             :   GEN D;
    5730         469 :   long N2, i, l, S = 0;
    5731         469 :   newd_params(N, &N2); /* will ensure mubeta != 0 */
    5732         469 :   D = mydivisorsu(N/N2); l = lg(D);
    5733        2485 :   for (i = 2; i < l; i++)
    5734             :   {
    5735        2016 :     long M = D[l-i]*N2, d = mfwt1cuspdimsum(M);
    5736        2016 :     if (d) S -= mubeta(D[i]) * d;
    5737             :   }
    5738         469 :   return S;
    5739             : }
    5740             : static long
    5741        1050 : mfwt1newdimsum(long N)
    5742             : {
    5743        1050 :   long S = mfwt1cuspdimsum(N);
    5744        1050 :   return S? S - mfwt1olddimsum(N): 0;
    5745             : }
    5746             : 
    5747             : /* Guess Galois type of wt1 eigenforms. */
    5748             : /* NK can be mf or [N,1,CHI] */
    5749             : static long
    5750          49 : mfisdihedral(GEN F, GEN DIH)
    5751             : {
    5752          49 :   GEN vG = gel(DIH,1), M = gel(DIH,2), v;
    5753             :   long i, l;
    5754          49 :   if (lg(M) == 1) return 0;
    5755          28 :   v = RgM_RgC_invimage(M, mftocol(F, nbrows(M)-1, 1));
    5756          28 :   if (!v) return 0;
    5757          28 :   l = lg(v);
    5758          28 :   for (i = 1; i < l; i++)
    5759          28 :     if (!gequal0(gel(v,i)))
    5760             :     {
    5761          28 :       GEN G = gel(vG,i), bnr = gel(G,2), w = gel(G,3);
    5762          28 :       GEN gen, cyc = bnr_get_cyc(bnr), D = gel(cyc,1);
    5763          28 :       GEN f = bnr_get_mod(bnr), nf = bnr_get_nf(bnr);
    5764          28 :       GEN con = gel(galoisconj(nf,gen_1), 2);
    5765          28 :       GEN f0 = gel(f,1), f0b = galoisapply(nf, con, f0);
    5766          28 :       GEN xin = zv_to_ZV(gel(w,2)); /* xi(bnr.gen[i]) = e(xin[i] / D) */
    5767             :       long e, j, L, n;
    5768          28 :       if (!gequal(f0,f0b))
    5769             :       { /* finite part of conductor not ambiguous */
    5770          14 :         GEN a = idealmul(nf, f0, idealdivexact(nf, f0b, idealadd(nf, f0, f0b)));
    5771          14 :         GEN bnr0 = bnr;
    5772          14 :         bnr = bnrinit0(bnr_get_bnf(bnr), mkvec2(a, gel(f,2)), 1);
    5773          14 :         xin = RgV_RgM_mul(xin, bnrsurjection(bnr, bnr0));
    5774             :         /* still xi(gen[i]) = e(xin[i] / D), for the new generators */
    5775             :       }
    5776          28 :       gen = bnr_get_gen(bnr); L = lg(gen);
    5777          42 :       for (j = 1, e = itou(D); j < L; j++)
    5778             :       {
    5779          35 :         GEN Ng = idealnorm(nf, gel(gen,j));
    5780          35 :         GEN a = shifti(gel(xin,j), 1); /* xi(g_j^2) = e(a/D) */
    5781          35 :         GEN b = FpV_dotproduct(xin, isprincipalray(bnr,Ng), D);
    5782          35 :         GEN m = Fp_sub(a, b, D); /* xi(g_j/\bar{g_j}) = e(m/D) */
    5783          35 :         e = ugcd(e, itou(m)); if (e == 1) break;
    5784             :       }
    5785          28 :       n = itou(D) / e;
    5786          28 :       return n == 1? 4: 2*n;
    5787             :     }
    5788           0 :   return 0;
    5789             : }
    5790             : 
    5791             : static ulong
    5792          21 : radical_u(ulong n)
    5793          21 : { return zv_prod(gel(myfactoru(n),1)); }
    5794             : 
    5795             : /* list of fundamental discriminants unramified outside N, with sign s
    5796             :  * [s = 0 => no sign condition] */
    5797             : static GEN
    5798          21 : mfunram(long N, long s)
    5799             : {
    5800          21 :   long cN = radical_u(N >> vals(N)), p = 1, m = 1, l, c, i;
    5801          21 :   GEN D = mydivisorsu(cN), res;
    5802          21 :   l = lg(D);
    5803          21 :   if (s == 1) m = 0; else if (s == -1) p = 0;
    5804          21 :   res = cgetg(6*l - 5, t_VECSMALL);
    5805          21 :   c = 1;
    5806          21 :   if (!odd(N))
    5807             :   { /* d = 1 */
    5808          14 :     if (p) res[c++] = 8;
    5809          14 :     if (m) { res[c++] =-8; res[c++] =-4; }
    5810             :   }
    5811          56 :   for (i = 2; i < l; i++)
    5812             :   { /* skip d = 1, done above */
    5813          35 :     long d = D[i], d4 = d & 3L; /* d odd, squarefree, d4 = 1 or 3 */
    5814          35 :     if (d4 == 1) { if (p) res[c++] = d; }
    5815          28 :     else         { if (m) res[c++] =-d; }
    5816          35 :     if (!odd(N))
    5817             :     {
    5818          14 :       if (p) { res[c++] = 8*d; if (d4 == 3) res[c++] = 4*d; }
    5819          14 :       if (m) { res[c++] =-8*d; if (d4 == 1) res[c++] =-4*d; }
    5820             :     }
    5821             :   }
    5822          21 :   setlg(res, c); return res;
    5823             : }
    5824             : 
    5825             : /* Return 1 if F is definitely not S4 type; return 0 on failure. */
    5826             : static long
    5827           7 : mfisnotS4(long N, GEN w)
    5828             : {
    5829           7 :   GEN D = mfunram(N, 0);
    5830           7 :   long i, lD = lg(D), lw = lg(w);
    5831          56 :   for (i = 1; i < lD; i++)
    5832             :   {
    5833          49 :     long p, d = D[i], ok = 0;
    5834         154 :     for (p = 2; p < lw; p++)
    5835         154 :       if (w[p] && kross(d,p) == -1) { ok = 1; break; }
    5836          49 :     if (!ok) return 0;
    5837             :   }
    5838           7 :   return 1;
    5839             : }
    5840             : 
    5841             : /* Return 1 if Q(sqrt(5)) \not\subset Q(F), i.e. F is definitely not A5 type;
    5842             :  * return 0 on failure. */
    5843             : static long
    5844           7 : mfisnotA5(GEN F)
    5845             : {
    5846           7 :   GEN CHI = mf_get_CHI(F), P = mfcharpol(CHI), T, Q;
    5847             : 
    5848           7 :   if (mfcharorder(CHI) % 5 == 0) return 0;
    5849           7 :   T = mf_get_field(F); if (degpol(T) == 1) return 1;
    5850           7 :   if (degpol(P) > 1) T = rnfequation(P,T);
    5851           7 :   Q = gsubgs(pol_xn(2,varn(T)), 5);
    5852           7 :   return (typ(nfisincl(Q, T)) == t_INT);
    5853             : }
    5854             : 
    5855             : /* Given x = z + 1/z with z prim. root of unity of order n, find n */
    5856             : static long
    5857         357 : mffindrootof1(GEN u1)
    5858             : {
    5859         357 :   pari_sp av = avma;
    5860         357 :   GEN u0 = gen_2, u1k = u1, u2;
    5861         357 :   long c = 1;
    5862        1379 :   while (!gequalsg(2, liftpol_shallow(u1))) /* u1 = z^c + z^-c */
    5863             :   {
    5864         665 :     u2 = gsub(gmul(u1k, u1), u0);
    5865         665 :     u0 = u1; u1 = u2; c++;
    5866             :   }
    5867         357 :   avma = av; return c;
    5868             : }
    5869             : 
    5870             : /* we known that F is not dihedral */
    5871             : static long
    5872          21 : mfgaloistype_i(long N, GEN CHI, GEN F, long lim)
    5873             : {
    5874             :   forprime_t iter;
    5875          21 :   GEN v = mfcoefs_i(F,lim,1), w = zero_zv(lim);
    5876             :   ulong p;
    5877          21 :   u_forprime_init(&iter, 2, lim);
    5878         406 :   while((p = u_forprime_next(&iter)))
    5879             :   {
    5880             :     GEN u;
    5881             :     long n;
    5882         378 :     if (!(N%p)) continue;
    5883         357 :     u = gdiv(gsqr(gel(v, p+1)), mfchareval_i(CHI, p));
    5884         357 :     n = mffindrootof1(gsubgs(u,2));
    5885         357 :     if (n == 3) w[p] = 1;
    5886         357 :     if (n == 4) return -24; /* S4 */
    5887         350 :     if (n == 5) return -60; /* A5 */
    5888         350 :     if (n > 5) pari_err_DOMAIN("mfgaloistype", "form", "not a",
    5889             :                                strtoGENstr("cuspidal eigenform"), F);
    5890             :   }
    5891           7 :   if (mfisnotS4(N,w) && mfisnotA5(F)) return -12; /* A4 */
    5892           0 :   return 0; /* FAILURE */
    5893             : }
    5894             : 
    5895             : static GEN
    5896          49 : mfgaloistype0(long N, GEN CHI, GEN F, GEN DIH, long lim)
    5897             : {
    5898          49 :   pari_sp av = avma;
    5899          49 :   long t = mfisdihedral(F, DIH);
    5900          49 :   avma = av;
    5901          49 :   if (t) return stoi(t);
    5902             :   for(;;)
    5903             :   {
    5904          21 :     t = mfgaloistype_i(N, CHI, F, lim);
    5905          14 :     avma = av; if (t) return stoi(t);
    5906           0 :     lim += lim >> 1;
    5907             :   }
    5908             : }
    5909             : 
    5910             : /* If f is NULL, give all the galoistypes, otherwise just for f */
    5911             : GEN
    5912          63 : mfgaloistype(GEN NK, GEN f)
    5913             : {
    5914          63 :   pari_sp av = avma;
    5915          63 :   GEN CHI, T, F, DIH, mf = checkMF_i(NK);
    5916             :   long N, k, lL, i, lim, SB;
    5917             : 
    5918          63 :   if (f && !checkmf_i(f)) pari_err_TYPE("mfgaloistype", f);
    5919          56 :   if (mf)
    5920             :   {
    5921          21 :     N = MF_get_N(mf);
    5922          21 :     k = MF_get_k(mf);
    5923          21 :     CHI = MF_get_CHI(mf);
    5924             :   }
    5925             :   else
    5926             :   {
    5927          35 :     checkNK(NK, &N, &k, &CHI, 0);
    5928          35 :     mf = f? NULL: mfinit_i(NK, mf_NEW);
    5929             :   }
    5930          56 :   if (k != 1) pari_err_DOMAIN("mfgaloistype", "k", "!=", gen_1, stoi(k));
    5931          56 :   SB = mfsturmNk(N,1) + 1;
    5932          56 :   lim = maxss(200, 3*SB);
    5933          56 :   DIH = mfdihedralnew(N,CHI);
    5934          56 :   DIH = mkvec2(DIH, mfvectomat(DIH,SB,1));
    5935          56 :   if (f) return gerepileuptoint(av, mfgaloistype0(N,CHI, f, DIH, lim));
    5936          49 :   F = mfeigenbasis(mf); lL = lg(F);
    5937          49 :   T = cgetg(lL, t_VEC);
    5938          49 :   for (i=1; i < lL; i++) gel(T,i) = mfgaloistype0(N,CHI, gel(F,i), DIH, lim);
    5939          49 :   return gerepileupto(av, T);
    5940             : }
    5941             : 
    5942             : /******************************************************************/
    5943             : /*                   Find all dihedral forms.                     */
    5944             : /******************************************************************/
    5945             : /* lim >= 2 */
    5946             : static void
    5947           7 : consttabdihedral(long lim)
    5948           7 : { cache_set(cache_DIH, mfdihedralall(mkvecsmall2(1,lim))); }
    5949             : 
    5950             : /* a ideal coprime to bnr modulus */
    5951             : static long
    5952       77385 : mfdiheval(GEN bnr, GEN w, GEN a)
    5953             : {
    5954       77385 :   GEN L, cycn = gel(w,1), chin = gel(w,2);
    5955       77385 :   long ordmax = cycn[1];
    5956       77385 :   L = ZV_to_Flv(isprincipalray(bnr,a), ordmax);
    5957       77385 :   return Flv_dotproduct(chin, L, ordmax);
    5958             : }
    5959             : 
    5960             : /* A(x^k) mod T */
    5961             : static GEN
    5962       25711 : Galois(GEN A, long k, GEN T)
    5963             : {
    5964       25711 :   if (typ(A) != t_POL) return A;
    5965        9730 :   return gmod(RgX_inflate(A, k), T);
    5966             : }
    5967             : static GEN
    5968         609 : vecGalois(GEN v, long k, GEN T)
    5969             : {
    5970             :   long i, l;
    5971         609 :   GEN w = cgetg_copy(v,&l);
    5972         609 :   for (i = 1; i < l; i++) gel(w,i) = Galois(gel(v,i), k, T);
    5973         609 :   return w;
    5974             : }
    5975             : 
    5976             : static GEN
    5977      154392 : fix_pol(GEN S, GEN Pn, int *trace)
    5978             : {
    5979      154392 :   if (typ(S) != t_POL) return S;
    5980      107884 :   S = RgX_rem(S, Pn);
    5981      107884 :   if (typ(S) == t_POL)
    5982             :   {
    5983      107884 :     switch(lg(S))
    5984             :     {
    5985       37919 :       case 2: return gen_0;
    5986       17094 :       case 3: return gel(S,2);
    5987             :     }
    5988       52871 :     *trace = 1;
    5989             :   }
    5990       52871 :   return S;
    5991             : }
    5992             : 
    5993             : static GEN
    5994       10465 : dihan(GEN bnr, GEN w, GEN k0j, ulong lim)
    5995             : {
    5996       10465 :   GEN nf = bnr_get_nf(bnr), f = bid_get_ideal(bnr_get_bid(bnr));
    5997       10465 :   GEN v = zerovec(lim+1), cycn = gel(w,1), Tinit = gel(w,3);
    5998       10465 :   GEN Pn = gel(Tinit,lg(Tinit)==4? 2: 1);
    5999       10465 :   long j, ordmax = cycn[1], k0 = k0j[1], jdeg = k0j[2];
    6000       10465 :   long D = itos(nf_get_disc(nf)), vt = varn(Pn);
    6001       10465 :   int trace = 0;
    6002             :   ulong p, n;
    6003             :   forprime_t T;
    6004             : 
    6005       10465 :   if (!lim) return v;
    6006       10465 :   gel(v,2) = gen_1;
    6007       10465 :   u_forprime_init(&T, 2, lim);
    6008             :   /* fill in prime powers first */
    6009       10465 :   while ((p = u_forprime_next(&T)))
    6010             :   {
    6011             :     GEN vP, vchiP, S;
    6012             :     long k, lP;
    6013             :     ulong q, qk;
    6014       70980 :     if (kross(D,p) >= 0) q = p;
    6015       29232 :     else if (!(q = umuluu_le(p,p,lim))) continue;
    6016             :     /* q = Norm P */
    6017       47390 :     vP = idealprimedec(nf, utoipos(p));
    6018       47390 :     lP = lg(vP);
    6019       47390 :     vchiP = cgetg(lP, t_VECSMALL);
    6020      128674 :     for (j = k = 1; j < lP; j++)
    6021             :     {
    6022       81284 :       GEN P = gel(vP,j);
    6023       81284 :       if (!idealval(nf, f, P)) vchiP[k++] = mfdiheval(bnr,w,P);
    6024             :     }
    6025       47390 :     if (k == 1) continue;
    6026       45689 :     setlg(vchiP, k); lP = k;
    6027       45689 :     if (lP == 2)
    6028             :     { /* one prime above p not dividing f */
    6029       13993 :       long s, s0 = vchiP[1];
    6030       24738 :       for (qk=q, s = s0;; s = Fl_add(s,s0,ordmax))
    6031             :       {
    6032       35483 :         S = mygmodulo_lift(s, ordmax, gen_1, vt);
    6033       24738 :         gel(v, qk+1) = fix_pol(S, Pn, &trace);
    6034       24738 :         if (!(qk = umuluu_le(qk,q,lim))) break;
    6035             :       }
    6036             :     }
    6037             :     else /* two primes above p not dividing f */
    6038             :     {
    6039       31696 :       long s, s0 = vchiP[1], s1 = vchiP[2];
    6040       46928 :       for (qk=q, k = 1;; k++)
    6041       15232 :       { /* sum over a,b s.t. Norm( P1^a P2^b ) = q^k, i.e. a+b = k */
    6042             :         long a;
    6043       46928 :         GEN S = gen_0;
    6044      163723 :         for (a = 0; a <= k; a++)
    6045             :         {
    6046      116795 :           s = Fl_add(Fl_mul(a, s0, ordmax), Fl_mul(k-a, s1, ordmax), ordmax);
    6047      116795 :           S = gadd(S, mygmodulo_lift(s, ordmax, gen_1, vt));
    6048             :         }
    6049       46928 :         gel(v, qk+1) = fix_pol(S, Pn, &trace);
    6050       46928 :         if (!(qk = umuluu_le(qk,q,lim))) break;
    6051             :       }
    6052             :     }
    6053             :   }
    6054             :   /* complete with non-prime powers */
    6055      200116 :   for (n = 2; n <= lim; n++)
    6056             :   {
    6057      189651 :     GEN S, fa = myfactoru(n), P = gel(fa, 1), E = gel(fa, 2);
    6058             :     long q;
    6059      189651 :     if (lg(P) == 2) continue;
    6060             :     /* not a prime power */
    6061       82726 :     q = upowuu(P[1],E[1]);
    6062       82726 :     S = gmul(gel(v, q + 1), gel(v, n/q + 1));
    6063       82726 :     gel(v, n+1) = fix_pol(S, Pn, &trace);
    6064             :   }
    6065       10465 :   if (trace)
    6066             :   {
    6067        5369 :     if (lg(Tinit) == 4) v = QabV_tracerel(Tinit, jdeg, v);
    6068             :     /* Apply Galois Mod(k0, ordw) */
    6069        5369 :     if (k0 > 1) { GEN Pm = gel(Tinit,1); v = vecGalois(v, k0, Pm); }
    6070             :   }
    6071       10465 :   return v;
    6072             : }
    6073             : 
    6074             : /* as cyc_normalize for t_VECSMALL cyc */
    6075             : static GEN
    6076       13391 : cyc_normalize_zv(GEN cyc)
    6077             : {
    6078       13391 :   long i, o = cyc[1], l = lg(cyc); /* > 1 */
    6079       13391 :   GEN D = cgetg(l, t_VECSMALL);
    6080       13391 :   D[1] = o; for (i = 2; i < l; i++) D[i] = o / cyc[i];
    6081       13391 :   return D;
    6082             : }
    6083             : /* as char_normalize for t_VECSMALLs */
    6084             : static GEN
    6085       58975 : char_normalize_zv(GEN chi, GEN ncyc)
    6086             : {
    6087       58975 :   long i, l = lg(chi);
    6088       58975 :   GEN c = cgetg(l, t_VECSMALL);
    6089       58975 :   if (l > 1) {
    6090       58975 :     c[1] = chi[1];
    6091       58975 :     for (i = 2; i < l; i++) c[i] = chi[i] * ncyc[i];
    6092             :   }
    6093       58975 :   return c;
    6094             : }
    6095             : 
    6096             : static GEN
    6097        6097 : dihan_bnf(long D)
    6098        6097 : { setrand(gen_1); return Buchall(quadpoly(stoi(D)), 0, LOWDEFAULTPREC); }
    6099             : static GEN
    6100       20391 : dihan_bnr(GEN bnf, GEN A)
    6101       20391 : { setrand(gen_1); return bnrinit0(bnf, A, 1); }
    6102             : 
    6103             : /* Hecke xi * (D/.) = Dirichlet chi, return v in Q^r st chi(g_i) = e(v[i]).
    6104             :  * cycn = cyc_normalize_zv(bnr.cyc), chin = char_normalize_zv(chi,cyc) */
    6105             : static GEN
    6106       17206 : bnrchartwist2conrey(GEN chin, GEN cycn, GEN bnrconreyN, GEN kroconreyN)
    6107             : {
    6108       17206 :   long l = lg(bnrconreyN), c1 = cycn[1], i;
    6109       17206 :   GEN v = cgetg(l, t_COL);
    6110       62566 :   for (i = 1; i < l; i++)
    6111             :   {
    6112       45360 :     GEN d = sstoQ(zv_dotproduct(chin, gel(bnrconreyN,i)), c1);
    6113       45360 :     if (kroconreyN[i] < 0) d = gadd(d, ghalf);
    6114       45360 :     gel(v,i) = d;
    6115             :   }
    6116       17206 :   return v;
    6117             : }
    6118             : 
    6119             : /* chi(g_i) = e(v[i]) denormalize wrt Conrey generators orders */
    6120             : static GEN
    6121       17206 : conreydenormalize(GEN znN, GEN v)
    6122             : {
    6123       17206 :   GEN gcyc = znstar_get_conreycyc(znN), w;
    6124       17206 :   long l = lg(v), i;
    6125       17206 :   w = cgetg(l, t_COL);
    6126       62566 :   for (i = 1; i < l; i++)
    6127       45360 :     gel(w,i) = modii(gmul(gel(v,i), gel(gcyc,i)), gel(gcyc,i));
    6128       17206 :   return w;
    6129             : }
    6130             : 
    6131             : static long
    6132       41769 : Miyake(GEN vchi, GEN gb, GEN cycn)
    6133             : {
    6134       41769 :   long i, e = cycn[1], lb = lg(gb);
    6135       41769 :   GEN v = char_normalize_zv(vchi, cycn);
    6136       62132 :   for (i = 1; i < lb; i++)
    6137       49833 :     if ((zv_dotproduct(v, gel(gb,i)) -  v[i]) % e) return 1;
    6138       12299 :   return 0;
    6139             : }
    6140             : 
    6141             : /* list of Hecke characters not induced by a Dirichlet character up to Galois
    6142             :  * conjugation, whose conductor is bnr.cond; cycn = cyc_normalize(bnr.cyc)*/
    6143             : static GEN
    6144       13391 : mklvchi(GEN bnr, GEN con, GEN cycn)
    6145             : {
    6146       13391 :   GEN gb = NULL, cyc = bnr_get_cyc(bnr), cycsmall = ZV_to_zv(cyc);
    6147       13391 :   GEN vchi = cyc2elts(cycsmall);
    6148       13391 :   long ordmax = cycsmall[1], c, i, l;
    6149       13391 :   if (con)
    6150             :   {
    6151        3892 :     GEN g = bnr_get_gen(bnr), nf = bnr_get_nf(bnr);
    6152        3892 :     long lg = lg(g);
    6153        3892 :     gb = cgetg(lg, t_VEC);
    6154        9135 :     for (i = 1; i < lg; i++)
    6155        5243 :       gel(gb,i) = ZV_to_zv(isprincipalray(bnr, galoisapply(nf, con, gel(g,i))));
    6156             :   }
    6157       13391 :   l = lg(vchi);
    6158      151725 :   for (i = c = 1; i < l; i++)
    6159             :   {
    6160      138334 :     GEN chi = gel(vchi,i);
    6161      138334 :     if (!con || Miyake(chi, gb, cycn)) gel(vchi, c++) = Flv_to_ZV(chi);
    6162             :   }
    6163       13391 :   setlg(vchi, c); l = c;
    6164      139426 :   for (i = 1; i < l; i++)
    6165             :   {
    6166      126035 :     GEN chi = gel(vchi,i);
    6167             :     long n;
    6168      126035 :     if (!chi) continue;
    6169      527289 :     for (n = 2; n < ordmax; n++)
    6170      482748 :       if (ugcd(n, ordmax) == 1)
    6171             :       {
    6172      198597 :         GEN tmp = vecmodii(gmulsg(n, chi), cyc);
    6173             :         long j;
    6174     3809050 :         for (j = i+1; j < l; j++)
    6175     3610453 :           if (gel(vchi,j) && gequal(gel(vchi,j), tmp)) gel(vchi,j) = NULL;
    6176             :       }
    6177             :   }
    6178      139426 :   for (i = c = 1; i < l; i++)
    6179             :   {
    6180      126035 :     GEN chi = gel(vchi,i);
    6181      126035 :     if (chi && bnrisconductor(bnr, chi)) gel(vchi, c++) = chi;
    6182             :   }
    6183       13391 :   setlg(vchi, c); return vchi;
    6184             : }
    6185             : 
    6186             : /* con = NULL if D > 0 or if D < 0 and id != idcon. */
    6187             : static GEN
    6188       16835 : mfdihedralcommon(GEN bnf, GEN id, GEN znN, GEN kroconreyN, long N, long D, GEN con)
    6189             : {
    6190             :   GEN bnr, bnrconreyN, cyc, cycn, cycN, Lvchi, res, g, P;
    6191             :   long i, j, ordmax, l, lc, deghecke, degrel;
    6192             : 
    6193       16835 :   bnr = dihan_bnr(bnf, id);
    6194       16835 :   cyc = ZV_to_zv( bnr_get_cyc(bnr) );
    6195       16835 :   lc = lg(cyc); if (lc == 1) return NULL;
    6196             : 
    6197       13391 :   g = znstar_get_conreygen(znN); l = lg(g);
    6198       13391 :   bnrconreyN = cgetg(l, t_VEC);
    6199       50288 :   for (i = 1; i < l; i++)
    6200       36897 :     gel(bnrconreyN,i) = ZV_to_zv(isprincipalray(bnr,gel(g,i)));
    6201             : 
    6202       13391 :   cycn = cyc_normalize_zv(cyc);
    6203       13391 :   cycN = ZV_to_zv(znstar_get_cyc(znN));
    6204       13391 :   ordmax = cyc[1];
    6205       13391 :   P = polcyclo(ord_canon(ordmax), fetch_user_var("t"));
    6206       13391 :   deghecke = myeulerphiu(ordmax);
    6207       13391 :   Lvchi = mklvchi(bnr, con, cycn); l = lg(Lvchi);
    6208       13391 :   if (l == 1) return NULL;
    6209        7917 :   res = cgetg(l, t_VEC);
    6210       25123 :   for (j = 1; j < l; j++)
    6211             :   {
    6212       17206 :     GEN T, Tinit, v, vchi = ZV_to_zv(gel(Lvchi,j));
    6213       17206 :     GEN chi, chin = char_normalize_zv(vchi, cycn);
    6214             :     long ordw, vnum, k0;
    6215       17206 :     v = bnrchartwist2conrey(chin, cycn, bnrconreyN, kroconreyN);
    6216       17206 :     ordw = itou(Q_denom(v));
    6217       17206 :     Tinit = Qab_trace_init(P, ord_canon(ordmax), ord_canon(ordw));
    6218       17206 :     chi = conreydenormalize(znN, v);
    6219       17206 :     vnum = itou(znconreyexp(znN, chi));
    6220       17206 :     chi = ZV_to_zv(znconreychar(znN,chi));
    6221       17206 :     degrel = deghecke / myeulerphiu(ordw);
    6222       17206 :     k0 = zv_cyc_minimize(cycN, chi, coprimes_zv(ordw));
    6223       17206 :     vnum = Fl_powu(vnum, k0, N);
    6224             :     /* encodes degrel forms: jdeg = 0..degrel-1 */
    6225       17206 :     T = mkvecsmalln(6, N, k0, vnum, D, ordmax, degrel);
    6226       17206 :     gel(res,j) = mkvec3(T, id, mkvec3(cycn,chin,Tinit));
    6227             :   }
    6228        7917 :   return res;
    6229             : }
    6230             : 
    6231             : /* Append to v all dihedral weight 1 forms coming from D, if fundamental. */
    6232             : /* B a t_VECSMALL: if #B=1, only that level; if B=[Bmin,Bmax], Bmin <= Bmax:
    6233             :  * between those levels. */
    6234             : static void
    6235        9289 : append_dihedral(GEN v, long D, GEN B)
    6236             : {
    6237        9289 :   long Da = labs(D), no, N, i, numi, ct, min, max;
    6238             :   GEN bnf, con, LI, resall, varch;
    6239             :   pari_sp av;
    6240             : 
    6241        9289 :   if (lg(B) == 2)
    6242             :   {
    6243           0 :     long b = B[1], m = D > 0? 3: 1;
    6244           0 :     min = b / Da;
    6245           0 :     if (b % Da || min < m) return;
    6246           0 :     max = min;
    6247             :   }
    6248             :   else
    6249             :   { /* assume B[1] < B[2] */
    6250        9289 :     min = (B[1] + Da-1)/Da;
    6251        9289 :     max = B[2]/Da;
    6252             :   }
    6253        9289 :   if (!sisfundamental(D)) return;
    6254             : 
    6255        2842 :   av = avma;
    6256        2842 :   bnf = dihan_bnf(D);
    6257        2842 :   con = gel(galoisconj(bnf,gen_1), 2);
    6258        2842 :   LI = ideallist(bnf, max);
    6259        2842 :   numi = 0; for (i = min; i <= max; i++) numi += lg(gel(LI, i)) - 1;
    6260        2842 :   if (D > 0)
    6261             :   {
    6262         707 :     numi <<= 1;
    6263         707 :     varch = mkvec2(mkvec2(gen_1,gen_0), mkvec2(gen_0,gen_1));
    6264             :   }
    6265             :   else
    6266        2135 :     varch = NULL;
    6267        2842 :   resall = cgetg(numi+1, t_VEC); ct = 1;
    6268       27503 :   for (no = min; no <= max; no++)
    6269             :   {
    6270             :     GEN LIs, znN, conreyN, kroconreyN;
    6271             :     long flcond, lgc, lglis;
    6272       24661 :     if (D < 0)
    6273       15043 :       flcond = (no == 2 || no == 3 || (no == 4 && (D&7L)==1));
    6274             :     else
    6275        9618 :       flcond = (no == 4 && (D&7L) != 1);
    6276       24661 :     if (flcond) continue;
    6277       22302 :     LIs = gel(LI, no);
    6278       22302 :     N = Da*no;
    6279       22302 :     znN = znstar0(utoi(N), 1);
    6280       22302 :     conreyN = znstar_get_conreygen(znN); lgc = lg(conreyN);
    6281       22302 :     kroconreyN = cgetg(lgc, t_VECSMALL);
    6282       22302 :     for (i = 1; i < lgc; i++) kroconreyN[i] = krosi(D, gel(conreyN, i));
    6283       22302 :     lglis = lg(LIs);
    6284       43876 :     for (i = 1; i < lglis; i++)
    6285             :     {
    6286       21574 :       GEN id = gel(LIs, i), idcon, conk;
    6287             :       long j, inf, maxinf;
    6288       21574 :       if (typ(id) == t_INT) continue;
    6289       14077 :       idcon = galoisapply(bnf, con, id);
    6290       14077 :       conk = (D < 0 && gequal(idcon, id)) ? con : NULL;
    6291       42294 :       for (j = i; j < lglis; j++)
    6292       28217 :         if (gequal(idcon, gel(LIs, j))) gel(LIs, j) = gen_0;
    6293       14077 :       maxinf = (D < 0 || gequal(idcon,id))? 1: 2;
    6294       30912 :       for (inf = 1; inf <= maxinf; inf++)
    6295             :       {
    6296       16835 :         GEN ide = (D > 0)? mkvec2(id, gel(varch,inf)): id;
    6297       16835 :         GEN res = mfdihedralcommon(bnf, ide, znN, kroconreyN, N, D, conk);
    6298       16835 :         if (res) gel(resall, ct++) = res;
    6299             :       }
    6300             :     }
    6301             :   }
    6302        2842 :   if (ct == 1) avma = av;
    6303             :   else
    6304             :   {
    6305        2394 :     setlg(resall, ct);
    6306        2394 :     vectrunc_append(v, gerepilecopy(av, shallowconcat1(resall)));
    6307             :   }
    6308             : }
    6309             : 
    6310             : static long
    6311       21021 : di_N(GEN a) { return gel(a,1)[1]; }
    6312             : /* All primitive dihedral wt1 forms: LIM a t_VECSMALL with a single component
    6313             :  * (only level LIM) or 2 components [m,M], m < M (between m and M) */
    6314             : static GEN
    6315           7 : mfdihedralall(GEN LIM)
    6316             : {
    6317             :   GEN res, z;
    6318             :   long limD, ct, i, l1, l2;
    6319             : 
    6320           7 :   if (lg(LIM) == 2) l1 = l2 = LIM[1]; else { l1 = LIM[1]; l2 = LIM[2]; }
    6321           7 :   limD = l2;
    6322           7 :   res = vectrunc_init(2*limD);
    6323           7 :   if (l1 == l2)
    6324             :   {
    6325           0 :     GEN D = mydivisorsu(l1);
    6326           0 :     long l = lg(D), j;
    6327           0 :     for (j = 2; j < l; j++)
    6328             :     {
    6329           0 :       long d = D[j];
    6330           0 :       append_dihedral(res, -d, LIM);
    6331           0 :       if (d >= 5 && D[l-j] >= 3) append_dihedral(res, d, LIM);
    6332             :     }
    6333             :   }
    6334             :   else
    6335             :   {
    6336             :     long D;
    6337           7 :     for (D = -3; D >= -limD; D--) append_dihedral(res, D, LIM);
    6338           7 :     limD /= 3;
    6339           7 :     for (D = 5; D <= limD;   D++) append_dihedral(res, D, LIM);
    6340             :   }
    6341           7 :   if (l1 == l2) return gel(res,1); /* single level */
    6342           7 :   ct = lg(res);
    6343           7 :   if (ct > 1)
    6344             :   { /* concat and sort wrt N */
    6345           7 :     res = shallowconcat1(res);
    6346           7 :     res = vecpermute(res, indexvecsort(res, mkvecsmall(1)));
    6347           7 :     ct = lg(res);
    6348             :   }
    6349           7 :   z = const_vec(l2-l1+1, cgetg(1,t_VEC));
    6350        3836 :   for (i = 1; i < ct;)
    6351             :   { /* regroup result sharing the same N */
    6352        3822 :     long n = di_N(gel(res,i)), j = i+1, k;
    6353             :     GEN v;
    6354        3822 :     while (j < ct && di_N(gel(res,j)) == n) j++;
    6355        3822 :     n -= l1-1;
    6356        3822 :     gel(z, n) = v = cgetg(j-i+1, t_VEC);
    6357        3822 :     for (k = 1; i < j; k++,i++) gel(v,k) = gel(res,i);
    6358             :   }
    6359           7 :   return z;
    6360             : }
    6361             : 
    6362             : /* return [vF, index], where vecpermute(vF,index) generates dihedral forms
    6363             :  * for character CHI */
    6364             : static GEN
    6365       23429 : mfdihedralnew_i(long N, GEN CHI)
    6366             : {
    6367             :   GEN bnf, Tinit, Pm, vf, M, V, NK, SP;
    6368             :   long Dold, d, ordw, i, SB, c, l, k0, k1, chino, chinoorig, lv;
    6369             : 
    6370       23429 :   SP = cache_get(cache_DIH, N);
    6371       23429 :   if (!SP) SP = mfdihedralall(mkvecsmall(N));
    6372       23429 :   lv = lg(SP); if (lv == 1) return NULL;
    6373       11151 :   CHI = mfcharinduce(CHI,N);
    6374       11151 :   ordw = mfcharorder(CHI);
    6375       11151 :   chinoorig = mfcharno(CHI);
    6376       11151 :   k0 = mfconreyminimize(CHI);
    6377       11151 :   chino = Fl_powu(chinoorig, k0, N);
    6378       11151 :   k1 = Fl_inv(k0 % ordw, ordw);
    6379       11151 :   V = cgetg(lv, t_VEC);
    6380       11151 :   d = 0;
    6381       34615 :   for (i = l = 1; i < lv; i++)
    6382             :   {
    6383       23464 :     GEN sp = gel(SP,i), T = gel(sp,1);
    6384       23464 :     if (T[3] != chino) continue;
    6385        3556 :     d += T[6];
    6386        3556 :     if (k1 != 1)
    6387             :     {
    6388          77 :       GEN t = leafcopy(T);
    6389          77 :       t[3] = chinoorig;
    6390          77 :       t[2] = (t[2]*k1)%ordw;
    6391          77 :       sp = mkvec4(t, gel(sp,2), gel(sp,3), gel(sp,4));
    6392             :     }
    6393        3556 :     gel(V, l++) = sp;
    6394             :   }
    6395       11151 :   setlg(V, l); /* dihedral forms of level N and character CHI */
    6396       11151 :   if (l == 1) return NULL;
    6397             : 
    6398        2331 :   SB = myeulerphiu(ordw) * mfsturmNk(N,1) + 1;
    6399        2331 :   M = cgetg(d+1, t_MAT);
    6400        2331 :   vf = cgetg(d+1, t_VEC);
    6401        2331 :   NK = mkNK(N, 1, CHI);
    6402        2331 :   bnf = NULL; Dold = 0;
    6403        5887 :   for (i = c = 1; i < l; i++)
    6404             :   { /* T = [N, k0, conreyno, D, ordmax, degrel] */
    6405        3556 :     GEN bnr, Vi = gel(V,i), T = gel(Vi,1), id = gel(Vi,2), w = gel(Vi,3);
    6406        3556 :     long jdeg, k0i = T[2], D = T[4], degrel = T[6];
    6407             : 
    6408        3556 :     if (D != Dold) { Dold = D; bnf = dihan_bnf(D); }
    6409        3556 :     bnr = dihan_bnr(bnf, id);
    6410       10430 :     for (jdeg = 0; jdeg < degrel; jdeg++,c++)
    6411             :     {
    6412        6874 :       GEN k0j = mkvecsmall2(k0i, jdeg), an = dihan(bnr, w, k0j, SB);
    6413        6874 :       settyp(an, t_COL); gel(M,c) = Q_primpart(an);
    6414        6874 :       gel(vf,c) = tag3(t_MF_DIHEDRAL, NK, bnr, w, k0j);
    6415             :     }
    6416             :   }
    6417        2331 :   Tinit = gmael3(V,1,3,3); Pm = gel(Tinit,1);
    6418        2331 :   V = QabM_indexrank(M, degpol(Pm)==1? NULL: Pm, ord_canon(ordw));
    6419        2331 :   return mkvec2(vf,gel(V,2));
    6420             : }
    6421             : static long
    6422       15764 : mfdihedralnewdim(long N, GEN CHI)
    6423             : {
    6424       15764 :   pari_sp av = avma;
    6425       15764 :   GEN S = mfdihedralnew_i(N, CHI);
    6426       15764 :   long d = S ? lg(gel(S,2))-1: 0;
    6427       15764 :   avma = av; return d;
    6428             : }
    6429             : static GEN
    6430        7665 : mfdihedralnew(long N, GEN CHI)
    6431             : {
    6432        7665 :   pari_sp av = avma;
    6433        7665 :   GEN S = mfdihedralnew_i(N, CHI);
    6434        7665 :   if (!S) { avma = av; return cgetg(1, t_VEC); }
    6435         777 :   return vecpermute(gel(S,1), gel(S,2));
    6436             : }
    6437             : 
    6438             : static long
    6439        7014 : mfdihedralcuspdim(long N, GEN CHI)
    6440             : {
    6441        7014 :   pari_sp av = avma;
    6442             :   GEN D, CHIP;
    6443             :   long F, i, lD, dim;
    6444             : 
    6445        7014 :   CHIP = mfchartoprimitive(CHI, &F);
    6446        7014 :   D = mydivisorsu(N/F); lD = lg(D);
    6447        7014 :   dim = mfdihedralnewdim(N, CHI); /* d = 1 */
    6448       15764 :   for (i = 2; i < lD; i++)
    6449             :   {
    6450        8750 :     long d = D[i], M = N/d, a = mfdihedralnewdim(M, CHIP);
    6451        8750 :     if (a) dim += a * mynumdivu(d);
    6452             :   }
    6453        7014 :   avma = av; return dim;
    6454             : }
    6455             : 
    6456             : static GEN
    6457        5446 : mfbdall(GEN E, long N)
    6458             : {
    6459        5446 :   GEN v, D = mydivisorsu(N);
    6460        5446 :   long i, j, nD = lg(D) - 1, nE = lg(E) - 1;
    6461        5446 :   v = cgetg(nD*nE + 1, t_VEC);
    6462        6965 :   for (j = 1; j <= nE; j++)
    6463             :   {
    6464        1519 :     GEN Ej = gel(E, j);
    6465        1519 :     for (i = 0; i < nD; i++) gel(v, i*nE + j) = mfbd_i(Ej, D[i+1]);
    6466             :   }
    6467        5446 :   return v;
    6468             : }
    6469             : static GEN
    6470        3381 : mfdihedralcusp(long N, GEN CHI)
    6471             : {
    6472        3381 :   pari_sp av = avma;
    6473             :   GEN D, CHIP, z;
    6474             :   long F, i, lD;
    6475             : 
    6476        3381 :   CHIP = mfchartoprimitive(CHI, &F);
    6477        3381 :   D = mydivisorsu(N/F); lD = lg(D);
    6478        3381 :   z = cgetg(lD, t_VEC);
    6479        3381 :   gel(z,1) = mfdihedralnew(N, CHI);
    6480        7609 :   for (i = 2; i < lD; i++) /* skip 1 */
    6481             :   {
    6482        4228 :     long d = D[i], M = N / d;
    6483        4228 :     GEN LF = mfdihedralnew(M, mfcharinduce(CHIP, M));
    6484        4228 :     gel(z,i) = mfbdall(LF, d);
    6485             :   }
    6486        3381 :   return gerepilecopy(av, shallowconcat1(z));
    6487             : }
    6488             : 
    6489             : /* used to decide between ratlift and comatrix for ZM_inv; ratlift is better
    6490             :  * when N has many divisors */
    6491             : static int
    6492        2317 : abundant(ulong N) { return mynumdivu(N) >= 8; }
    6493             : 
    6494             : /* CHI an mfchar */
    6495             : static int
    6496         294 : cmp_ord(void *E, GEN a, GEN b)
    6497             : {
    6498         294 :   GEN chia = MF_get_CHI(a), chib = MF_get_CHI(b);
    6499         294 :   (void)E; return cmpii(gmfcharorder(chia), gmfcharorder(chib));
    6500             : }
    6501             : /* mfinit structure.
    6502             : -- mf[1] contains [N,k,CHI,space],
    6503             : -- mf[2] contains vector of closures of Eisenstein series, empty if not
    6504             :    full space.
    6505             : -- mf[3] contains vector of closures, so #mf[3] = dimension of cusp/new space.
    6506             : -- mf[4] contains the corresponding indices: either j for T(j)tf if newspace,
    6507             :    or [M,j,d] for B(d)T(j)tf_M if cuspspace or oldspace.
    6508             : -- mf[5] contains the matrix M of first coefficients of basis, never cleaned.
    6509             :  * NK is either [N,k] or [N,k,CHI].
    6510             :  * mfinit does not do the splitting, only the basis generation. */
    6511             : 
    6512             : /* Set flraw to 1 if do not need mf[5]: no mftobasis etc..., only the
    6513             :    expansions of the basis elements are needed. */
    6514             : 
    6515             : static GEN
    6516        4501 : mfinit_Nkchi(long N, long k, GEN CHI, long space, long flraw)
    6517             : {
    6518        4501 :   GEN M = NULL, mf = NULL, mf1 = mkvec4(utoi(N), stoi(k), CHI, utoi(space));
    6519        4501 :   long sb = mfsturmNk(N, k);
    6520             :   cachenew_t cache;
    6521        4501 :   if (k < 0 || badchar(N, k, CHI)) return mfEMPTY(mf1);
    6522        4466 :   if (k == 0) /*nothing*/;
    6523        4424 :   else if (k == 1)
    6524             :   {
    6525         259 :     switch (space)
    6526             :     {
    6527             :       case mf_NEW:
    6528             :       case mf_FULL:
    6529         231 :       case mf_CUSP: mf = mfwt1init(N, CHI, NULL, space, flraw); break;
    6530          14 :       case mf_EISEN:break;
    6531           7 :       case mf_OLD: pari_err_IMPL("mfinit in weight 1 for old space");
    6532           7 :       default: pari_err_FLAG("mfinit");
    6533             :     }
    6534             :   }
    6535             :   else /* k >= 2 */
    6536             :   {
    6537        4165 :     long ord = mfcharorder_canon(CHI);
    6538        4165 :     GEN z = NULL, P = (ord == 1)? NULL: mfcharpol(CHI);
    6539        4165 :     switch(space)
    6540             :     {
    6541             :       case mf_EISEN:
    6542         105 :         break;
    6543             :       case mf_NEW:
    6544        1169 :         mf = mfnewinit(N, k, CHI, &cache, 1);
    6545        1169 :         if (mf && !flraw) { M = MF_get_M(mf); z = MF_get_Mindex(mf); }
    6546        1169 :         break;
    6547             :       case mf_OLD:
    6548             :       case mf_CUSP:
    6549             :       case mf_FULL:
    6550        2884 :         mf = mfinitcusp(N, k, CHI, &cache, space);
    6551        2884 :         if (mf && !flraw)
    6552             :         {
    6553        2058 :           GEN S = MF_get_S(mf);
    6554        2058 :           M = bhnmat_extend(M, sb+1, 1, S, &cache);
    6555        2058 :           if (space != mf_FULL) gel(mf,5) = mfcleanCHI(M, CHI, abundant(N));
    6556             :         }
    6557        2884 :         dbg_cachenew(&cache);
    6558        2884 :         break;
    6559           7 :       default: pari_err_FLAG("mfinit");
    6560             :     }
    6561        4158 :     if (z) gel(mf,5) = mfclean2(M, z, P, ord);
    6562             :   }
    6563        4445 :   if (!mf) mf = mfEMPTY(mf1);
    6564             :   else
    6565             :   {
    6566        3577 :     gel(mf,1) = mf1;
    6567        3577 :     if (flraw) gel(mf,5) = zerovec(3);
    6568             :   }
    6569        4445 :   if (!space_is_cusp(space))
    6570             :   {
    6571         637 :     GEN E = mfeisensteinbasis(N, k, CHI);
    6572         637 :     gel(mf,2) = E;
    6573         637 :     if (!flraw)
    6574             :     {
    6575         427 :       if (M)
    6576         168 :         M = shallowconcat(mfvectomat(E, sb+1, 1), M);
    6577             :       else
    6578         259 :         M = mfcoefs_mf(mf, sb+1, 1);
    6579         427 :       gel(mf,5) = mfcleanCHI(M, CHI, abundant(N));
    6580             :     }
    6581             :   }
    6582        4445 :   return mf;
    6583             : }
    6584             : 
    6585             : /* mfinit for k = nk/dk */
    6586             : static GEN
    6587        2422 : mfinit_Nndkchi(long N, long nk, long dk, GEN CHI, long space, long flraw)
    6588         210 : { return (dk == 2)? mf2init_Nkchi(N, nk >> 1, CHI, space, flraw)
    6589        2632 :                   : mfinit_Nkchi(N, nk, CHI, space, flraw); }
    6590             : static GEN
    6591        3087 : mfinit_i(GEN NK, long space)
    6592             : {
    6593             :   GEN CHI, mf;
    6594             :   long N, k, dk, joker;
    6595        3087 :   if (checkmf_i(NK))
    6596             :   {
    6597         126 :     N = mf_get_N(NK);
    6598         126 :     Qtoss(mf_get_gk(NK), &k, &dk);
    6599         126 :     CHI = mf_get_CHI(NK);
    6600             :   }
    6601        2961 :   else if ((mf = checkMF_i(NK)))
    6602             :   {
    6603          21 :     long s = MF_get_space(mf);
    6604          21 :     if (s == space) return mf;
    6605          21 :     Qtoss(MF_get_gk(mf), &k, &dk);
    6606          21 :     if (dk == 1 && k > 1 && space == mf_NEW && (s == mf_CUSP || s == mf_FULL))
    6607          21 :       return mfinittonew(mf);
    6608           0 :     N = MF_get_N(mf);
    6609           0 :     CHI = MF_get_CHI(mf);
    6610             :   }
    6611             :   else
    6612        2940 :     checkNK2(NK, &N, &k, &dk, &CHI, 1);
    6613        3045 :   joker = !CHI || typ(CHI) == t_COL;
    6614        3045 :   if (joker)
    6615             :   {
    6616        1141 :     GEN mf, vCHI = CHI;
    6617             :     long i, j, l;
    6618        1141 :     if (CHI && lg(CHI) == 1) return cgetg(1,t_VEC);
    6619        1134 :     if (k < 0) return mfEMPTYall(N, sstoQ(k,dk), CHI, space);
    6620        1120 :     if (k == 1 && dk == 1 && space != mf_EISEN)
    6621         483 :     {
    6622             :       GEN TMP, gN, gs;
    6623        1085 :       if (space != mf_CUSP && space != mf_NEW)
    6624           0 :         pari_err_IMPL("mfinit([N,1,wildcard], space != cusp or new space)");
    6625        1085 :       if (wt1empty(N)) return mfEMPTYall(N, gen_1, CHI, space);
    6626         483 :       vCHI = mfwt1chars(N,vCHI);
    6627         483 :       l = lg(vCHI); mf = cgetg(l, t_VEC); if (l == 1) return mf;
    6628         483 :       TMP = mfwt1_pre(N); gN = utoipos(N); gs = utoi(space);
    6629        3717 :       for (i = j = 1; i < l; i++)
    6630             :       {
    6631        3234 :         GEN c = gel(vCHI,i), z = mfwt1init(N, c, TMP, space, 0);
    6632        3234 :         if (CHI && !z) z = mfEMPTY(mkvec4(gN,gen_1,c,gs));
    6633        3234 :         if (z) gel(mf, j++) = z;
    6634             :       }
    6635             :     }
    6636             :     else
    6637             :     {
    6638          35 :       vCHI = mfchars(N,k,dk,vCHI);
    6639          35 :       l = lg(vCHI); mf = cgetg(l, t_VEC);
    6640         119 :       for (i = j = 1; i < l; i++)
    6641             :       {
    6642          84 :         GEN v = mfinit_Nndkchi(N, k, dk, gel(vCHI,i), space, 0);
    6643          84 :         if (MF_get_dim(v) || CHI) gel(mf, j++) = v;
    6644             :       }
    6645             :     }
    6646         518 :     setlg(mf,j);
    6647         518 :     if (!CHI) gen_sort_inplace(mf, NULL, &cmp_ord, NULL);
    6648         518 :     return mf;
    6649             :   }
    6650        1904 :   return mfinit_Nndkchi(N, k, dk, CHI, space, 0);
    6651             : }
    6652             : GEN
    6653        2135 : mfinit(GEN NK, long space)
    6654             : {
    6655        2135 :   pari_sp av = avma;
    6656        2135 :   return gerepilecopy(av, mfinit_i(NK, space));
    6657             : }
    6658             : 
    6659             : /* UTILITY FUNCTIONS */
    6660             : static void
    6661         357 : cusp_canon(GEN cusp, long N, long *pA, long *pC)
    6662             : {
    6663         357 :   pari_sp av = avma;
    6664             :   long A, C, tc, cg;
    6665         357 :   if (N <= 0) pari_err_DOMAIN("mfcuspwidth","N","<=",gen_0,stoi(N));
    6666         350 :   if (!cusp || (tc = typ(cusp)) == t_INFINITY) { *pA = 1; *pC = N; return; }
    6667         343 :   if (tc != t_INT && tc != t_FRAC) pari_err_TYPE("checkcusp", cusp);
    6668         343 :   Qtoss(cusp, &A,&C);
    6669         343 :   if (N % C)
    6670             :   {
    6671             :     ulong uC;
    6672          14 :     long u = Fl_invgen((C-1)%N + 1, N, &uC);
    6673          14 :     A = Fl_mul(A, u, N);
    6674          14 :     C = (long)uC;
    6675             :   }
    6676         343 :   cg = ugcd(C, N/C);
    6677         343 :   while (ugcd(A, N) > 1) A += cg;
    6678         343 :   *pA = A % N; *pC = C; avma = av;
    6679             : }
    6680             : static long
    6681         805 : mfcuspcanon_width(long N, long C)
    6682         805 : { return (!C || C == N)? 1 : N / ugcd(N, Fl_sqr(umodsu(C,N),N)); }
    6683             : /* v = [a,c] a ZC, width of cusp (a:c) */
    6684             : static long
    6685        7378 : mfZC_width(long N, GEN v)
    6686             : {
    6687        7378 :   ulong C = umodiu(gel(v,2), N);
    6688        7378 :   return (C == 0)? 1: N / ugcd(N, Fl_sqr(C,N));
    6689             : }
    6690             : long
    6691         161 : mfcuspwidth(GEN gN, GEN cusp)
    6692             : {
    6693         161 :   long N = 0, A, C;
    6694             :   GEN mf;
    6695         161 :   if (typ(gN) == t_INT) N = itos(gN);
    6696          42 :   else if ((mf = checkMF_i(gN))) N = MF_get_N(mf);
    6697           0 :   else pari_err_TYPE("mfcuspwidth", gN);
    6698         161 :   cusp_canon(cusp, N, &A, &C);
    6699         154 :   return mfcuspcanon_width(N, C);
    6700             : }
    6701             : 
    6702             : /* Q a t_INT */
    6703             : static GEN
    6704          14 : findq(GEN al, GEN Q)
    6705             : {
    6706             :   long n;
    6707          14 :   if (typ(al) == t_FRAC && cmpii(gel(al,2), Q) <= 0)
    6708           0 :     return mkvec(mkvec2(gel(al,1), gel(al,2)));
    6709          14 :   n = 1 + (long)ceil(2.0781*gtodouble(glog(Q, LOWDEFAULTPREC)));
    6710          14 :   return contfracpnqn(gboundcf(al,n), n);
    6711             : }
    6712             : static GEN
    6713          91 : findqga(long N, GEN z)
    6714             : {
    6715          91 :   GEN Q, LDC, CK = NULL, DK = NULL, ma, x, y = imag_i(z);
    6716             :   long j, l;
    6717          91 :   if (gcmpgs(gmulsg(2*N, y), 1) >= 0) return NULL;
    6718          14 :   x = real_i(z);
    6719          14 :   Q = ground(ginv(gsqrt(gmulsg(N, y), LOWDEFAULTPREC)));
    6720          14 :   LDC = findq(gmulsg(-N,x), Q);
    6721          14 :   ma = gen_1; l = lg(LDC);
    6722          35 :   for (j = 1; j < l; j++)
    6723             :   {
    6724          21 :     GEN D, DC = gel(LDC,j), C1 = gel(DC,2);
    6725          21 :     if (cmpii(C1,Q) > 0) break;
    6726          21 :     D = gel(DC,1);
    6727          21 :     if (ugcdiu(D,N) == 1)
    6728             :     {
    6729           7 :       GEN C = mului(N, C1), den;
    6730           7 :       den = gadd(gsqr(gmul(C,y)), gsqr(gadd(D, gmul(C,x))));
    6731           7 :       if (gcmp(den, ma) < 0) { ma = den; CK = C; DK = D; }
    6732             :     }
    6733             :   }
    6734          14 :   return DK? mkvec2(CK, DK): NULL;
    6735             : }
    6736             : 
    6737             : static long
    6738          70 : valNC2(GEN P, GEN E, long e)
    6739             : {
    6740          70 :   long i, d = 1, l = lg(P);
    6741         168 :   for (i = 1; i < l; i++)
    6742             :   {
    6743          98 :     long v = u_lval(e, P[i]) << 1;
    6744          98 :     if (v == E[i] + 1) v--;
    6745          98 :     d *= upowuu(P[i], v);
    6746             :   }
    6747          70 :   return d;
    6748             : }
    6749             : 
    6750             : static GEN
    6751          28 : findqganew(long N, GEN z)
    6752             : {
    6753          28 :   GEN MI, DI, x = real_i(z), y = imag_i(z), Ck = gen_0, Dk = gen_1, fa, P, E;
    6754             :   long i;
    6755          28 :   MI = ginv(utoi(N));
    6756          28 :   DI = mydivisorsu(mysqrtu(N));
    6757          28 :   fa = myfactoru(N); P = gel(fa,1); E = gel(fa,2);
    6758          98 :   for (i = 1; i < lg(DI); i++)
    6759             :   {
    6760          70 :     long e = DI[i], g;
    6761             :     GEN U, C, D, m;
    6762          70 :     (void)cxredsl2(gmulsg(e, z), &U);
    6763          70 :     C = gcoeff(U,2,1); if (!signe(C)) continue;
    6764          70 :     D = gcoeff(U,2,2);
    6765          70 :     g = ugcdiu(D,e);
    6766          70 :     if (g > 1) { C = muliu(C,e/g); D = diviuexact(D,g); } else C = muliu(C,e);
    6767          70 :     m = gadd(gsqr(gadd(gmul(C, x), D)), gsqr(gmul(C, y)));
    6768          70 :     m = gdivgs(m, valNC2(P, E, e));
    6769          70 :     if (gcmp(m, MI) < 0) { MI = m; Ck = C; Dk = D; }
    6770             :   }
    6771          28 :   return signe(Ck)? mkvec2(Ck, Dk): NULL;
    6772             : }
    6773             : 
    6774             : /* Return z' and U = [a,b;c,d] \in SL_2(Z), z' = U*z,
    6775             :  * Im(z')/width(U.oo) > sqrt(3)/(2N). Set *pczd = c*z+d */
    6776             : static GEN
    6777         154 : cxredga0N(long N, GEN z, GEN *pU, GEN *pczd, long flag)
    6778             : {
    6779         154 :   GEN v = NULL, A, B, C, D;
    6780             :   long e;
    6781         154 :   if (N == 1) return cxredsl2_i(z, pU, pczd);
    6782         119 :   e = gexpo(gel(z,2));
    6783         119 :   if (e < 0) z = gprec_wensure(z, precision(z) + nbits2extraprec(-e));
    6784         119 :   v = flag? findqganew(N,z): findqga(N,z);
    6785         119 :   if (!v) { *pU = matid(2); *pczd = gen_1; return z; }
    6786          35 :   C = gel(v,1);
    6787          35 :   D = gel(v,2);
    6788          35 :   if (!is_pm1(bezout(C,D, &B,&A))) pari_err_BUG("cxredga0N [gcd > 1]");
    6789          35 :   B = negi(B);
    6790          35 :   *pU = mkmat2(mkcol2(A,C), mkcol2(B,D));
    6791          35 :   *pczd = gadd(gmul(C,z), D);
    6792          35 :   return gdiv(gadd(gmul(A,z), B), *pczd);
    6793             : }
    6794             : 
    6795             : static GEN
    6796         147 : lfunthetaall(GEN b, GEN vL, GEN t, long bitprec)
    6797             : {
    6798         147 :   long i, l = lg(vL);
    6799         147 :   GEN v = cgetg(l, t_VEC);
    6800         322 :   for (i = 1; i < l; i++)
    6801             :   {
    6802         175 :     GEN T, L = gel(vL,i), a0 = gel(L,1), ldata = gel(L,2);
    6803         175 :     GEN van = gel(ldata_get_an(ldata),2);
    6804         175 :     if (lg(van) == 1)
    6805             :     {
    6806           0 :       T = gmul(b, a0);
    6807           0 :       if (isexactzero(T)) { GEN z = real_0_bit(-bitprec); T = mkcomplex(z,z); }
    6808             :     }
    6809             :     else
    6810             :     {
    6811         175 :       T = gmul2n(lfuntheta(ldata, t, 0, bitprec), -1);
    6812         175 :       T = gmul(b, gadd(a0, T));
    6813             :     }
    6814         175 :     gel(v,i) = T;
    6815             :   }
    6816         147 :   return l == 2? gel(v,1): v;
    6817             : }
    6818             : 
    6819             : /* P in ZX */
    6820             : static GEN
    6821         154 : ZX_roots(GEN P, long prec)
    6822             : {
    6823         154 :   long d = degpol(P);
    6824         154 :   if (d == 1) return mkvec(gen_0);
    6825         154 :   if (d == 2 && isint1(gel(P,2)) && isintzero(gel(P,3)) && isint1(gel(P,4)))
    6826           7 :     return mkvec2(powIs(3), gen_I()); /* order as polroots */
    6827         147 :   return (ZX_sturm(P) == d)? realroots(P,NULL,prec): QX_complex_roots(P,prec);
    6828             : }
    6829             : /* initializations for RgX_RgV_eval / RgC_embed */
    6830             : static GEN
    6831         189 : rootspowers(GEN v)
    6832             : {
    6833         189 :   long i, l = lg(v);
    6834         189 :   GEN w = cgetg(l, t_VEC);
    6835         189 :   for (i = 1; i < l; i++) gel(w,i) = gpowers(gel(v,i), l-2);
    6836         189 :   return w;
    6837             : }
    6838             : /* mf embeddings attached to Q(chi)/(T), chi attached to cyclotomic P */
    6839             : static GEN
    6840         805 : getembed(GEN P, GEN T, GEN zcyclo, long prec)
    6841             : {
    6842             :   long i, l;
    6843             :   GEN v;
    6844         805 :   if (degpol(P) == 1) P = NULL; /* mfcharpol for quadratic char */
    6845         805 :   if (degpol(T) == 1) T = NULL; /* dim 1 orbit */
    6846         805 :   if (T && P)
    6847          35 :   { /* K(y) / (T(y)), K = Q(t)/(P) cyclotomic */
    6848          35 :     GEN vr = RgX_is_ZX(T)? ZX_roots(T,prec): roots(RgX_embed1(T,zcyclo), prec);
    6849          35 :     v = rootspowers(vr); l = lg(v);
    6850          35 :     for (i = 1; i < l; i++) gel(v,i) = mkcol3(P,zcyclo,gel(v,i));
    6851             :   }
    6852         770 :   else if (T)
    6853             :   { /* Q(y) / (T(y)), T non-cyclotomic */
    6854         154 :     GEN vr = ZX_roots(T, prec);
    6855         154 :     v = rootspowers(vr); l = lg(v);
    6856         154 :     for (i = 1; i < l; i++) gel(v,i) = mkcol2(T, gel(v,i));
    6857             :   }
    6858             :   else /* cyclotomic or rational */
    6859         616 :     v = mkvec(P? mkvec2(P, zcyclo): cgetg(1,t_VEC));
    6860         805 :   return v;
    6861             : }
    6862             : static GEN
    6863         665 : grootsof1_CHI(GEN CHI, long prec)
    6864         665 : { return grootsof1(mfcharorder_canon(CHI), prec); }
    6865             : /* return the [Q(F):Q(chi)] embeddings of F */
    6866             : static GEN
    6867         518 : mfgetembed(GEN F, long prec)
    6868             : {
    6869         518 :   GEN T = mf_get_field(F), CHI = mf_get_CHI(F), P = mfcharpol(CHI);
    6870         518 :   return getembed(P, T, grootsof1_CHI(CHI, prec), prec);
    6871             : }
    6872             : static GEN
    6873           7 : mfchiembed(GEN mf, long prec)
    6874             : {
    6875           7 :   GEN CHI = MF_get_CHI(mf), P = mfcharpol(CHI);
    6876           7 :   return getembed(P, pol_x(0), grootsof1_CHI(CHI, prec), prec);
    6877             : }
    6878             : /* mfgetembed for the successive eigenforms in MF_get_newforms */
    6879             : static GEN
    6880         140 : mfeigenembed(GEN mf, long prec)
    6881             : {
    6882         140 :   GEN vP = MF_get_fields(mf), CHI = MF_get_CHI(mf), P = mfcharpol(CHI);
    6883         140 :   long i, l = lg(vP);
    6884         140 :   GEN zcyclo = grootsof1_CHI(CHI, prec), vE = cgetg(l, t_VEC);
    6885         140 :   for (i = 1; i < l; i++) gel(vE,i) = getembed(P, gel(vP,i), zcyclo, prec);
    6886         140 :   return vE;
    6887             : }
    6888             : 
    6889             : static int
    6890          28 : checkPv(GEN P, GEN v)
    6891          28 : { return typ(P) == t_POL && typ(v) == t_VEC && lg(v)-1 >= degpol(P); }
    6892             : static int
    6893          28 : checkemb_i(GEN E)
    6894             : {
    6895          28 :   long t = typ(E), l = lg(E);
    6896          28 :   if (t == t_VEC) return l == 1 || (l == 3 && checkPv(gel(E,1), gel(E,2)));
    6897          21 :   if (t != t_COL) return 0;
    6898          21 :   if (l == 3) return checkPv(gel(E,1), gel(E,2));
    6899          21 :   return l == 4 && typ(gel(E,2)) == t_VEC && checkPv(gel(E,1), gel(E,3));
    6900             : }
    6901             : static GEN
    6902          28 : anyembed(GEN v, GEN E)
    6903             : {
    6904          28 :   switch(typ(v))
    6905             :   {
    6906          21 :     case t_VEC: case t_COL: return mfvecembed(E, v);
    6907           7 :     case t_MAT: return mfmatembed(E, v);
    6908             :   }
    6909           0 :   return mfembed(E, v);
    6910             : }
    6911             : GEN
    6912          49 : mfembed0(GEN E, GEN v, long prec)
    6913             : {
    6914          49 :   pari_sp av = avma;
    6915          49 :   GEN mf, vE = NULL;
    6916          49 :   if (checkmf_i(E)) vE = mfgetembed(E, prec);
    6917          35 :   else if ((mf = checkMF_i(E))) vE = mfchiembed(mf, prec);
    6918          49 :   if (vE)
    6919             :   {
    6920          21 :     long i, l = lg(vE);
    6921             :     GEN w;
    6922          21 :     if (!v) return gerepilecopy(av, l == 2? gel(vE,1): vE);
    6923           0 :     w = cgetg(l, t_VEC);
    6924           0 :     for (i = 1; i < l; i++) gel(w,i) = anyembed(v, gel(vE,i));
    6925           0 :     return gerepilecopy(av, l == 2? gel(w,1): w);
    6926             :   }
    6927          28 :   if (!checkemb_i(E) || !v) pari_err_TYPE("mfembed", E);
    6928          28 :   return gerepilecopy(av, anyembed(v,E));
    6929             : }
    6930             : 
    6931             : /* dummy lfun create for theta evaluation */
    6932             : static GEN
    6933         700 : mfthetaancreate(GEN van, GEN N, GEN k)
    6934             : {
    6935         700 :   GEN L = zerovec(6);
    6936         700 :   gel(L,1) = lfuntag(t_LFUN_GENERIC, van);
    6937         700 :   gel(L,3) = mkvec2(gen_0, gen_1);
    6938         700 :   gel(L,4) = k;
    6939         700 :   gel(L,5) = N; return L;
    6940             : }
    6941             : /* destroy van and prepare to evaluate theta(sigma(van)), for all sigma in
    6942             :  * embeddings vector vE */
    6943             : static GEN
    6944         287 : van_embedall(GEN van, GEN vE, GEN gN, GEN gk)
    6945             : {
    6946         287 :   GEN a0 = gel(van,1), vL;
    6947         287 :   long i, lE = lg(vE), l = lg(van);
    6948         287 :   van++; van[0] = evaltyp(t_VEC) | evallg(l-1); /* remove a0 */
    6949         287 :   vL = cgetg(lE, t_VEC);
    6950         658 :   for (i = 1; i < lE; i++)
    6951             :   {
    6952         371 :     GEN E = gel(vE,i), v = mfvecembed(E, van);
    6953         371 :     gel(vL,i) = mkvec2(mfembed(E,a0), mfthetaancreate(v, gN, gk));
    6954             :   }
    6955         287 :   return vL;
    6956             : }
    6957             : 
    6958             : static int
    6959         973 : cusp_AC(GEN cusp, long *A, long *C)
    6960             : {
    6961         973 :   switch(typ(cusp))
    6962             :   {
    6963          84 :     case t_INFINITY: *A = 1; *C = 0; break;
    6964         273 :     case t_INT:  *A = itos(cusp); *C = 1; break;
    6965         420 :     case t_FRAC: *A = itos(gel(cusp, 1)); *C = itos(gel(cusp, 2)); break;
    6966             :     case t_REAL: case t_COMPLEX:
    6967         196 :       *A = 0; *C = 0;
    6968         196 :       if (gsigne(imag_i(cusp)) <= 0)
    6969           7 :         pari_err_DOMAIN("mfeval","imag(tau)","<=",gen_0,cusp);
    6970         189 :       return 0;
    6971           0 :     default: pari_err_TYPE("cusp_AC", cusp);
    6972             :   }
    6973         777 :   return 1;
    6974             : }
    6975             : static GEN
    6976         511 : cusp2mat(long A, long C)
    6977             : { long B, D;
    6978         511 :   cbezout(A, C, &D, &B);
    6979         511 :   return mkmat22s(A, -B, C, D);
    6980             : }
    6981             : static GEN
    6982           7 : mkS(void) { return mkmat22s(0,-1,1,0); }
    6983             : 
    6984             : /* if t is a cusp, return F(t), else NULL */
    6985             : static GEN
    6986         343 : evalcusp(GEN mf, GEN F, GEN t, long prec)
    6987             : {
    6988             :   long A, C;
    6989             :   GEN R;
    6990         343 :   if (!cusp_AC(t, &A,&C)) return NULL;
    6991         189 :   if (C % mf_get_N(F) == 0) return gel(mfcoefs_i(F, 0, 1), 1);
    6992         175 :   R = mfgaexpansion(mf, F, cusp2mat(A,C), 0, prec);
    6993         175 :   return gequal0(gel(R,1))? gmael(R,3,1): gen_0;
    6994             : }
    6995             : /* Evaluate an mf closure numerically, i.e., in the usual sense, either for a
    6996             :  * single tau or a vector of tau; for each, return a vector of results
    6997             :  * corresponding to all complex embeddings of F. If flag is non-zero, allow
    6998             :  * replacing F by F | gamma to increase imag(gamma^(-1).tau) [ expensive if
    6999             :  * MF_EISENSPACE not present ] */
    7000             : static GEN
    7001         154 : mfeval_i(GEN mf, GEN F, GEN vtau, long flag, long bitprec)
    7002             : {
    7003             :   GEN L0, vL, vb, sqN, vczd, vTAU, vs, van, vE;
    7004         154 :   long N = mf_get_N(F), N0, ta, lv, i, prec = nbits2prec(bitprec);
    7005         154 :   GEN gN = utoipos(N), gk = mf_get_gk(F), gk1 = gsubgs(gk,1), vgk;
    7006         154 :   long flscal = 0;
    7007             : 
    7008             :   /* gen_0 is ignored, second component assumes Ramanujan-Petersson in
    7009             :    * 1/2-integer weight */
    7010         154 :   vgk = mkvec2(gen_0, mfiscuspidal(mf,F)? gmul2n(gk1,-1): gk1);
    7011         154 :   ta = typ(vtau);
    7012         154 :   if (!is_vec_t(ta)) { flscal = 1; vtau = mkvec(vtau); ta = t_VEC; }
    7013         154 :   lv = lg(vtau);
    7014         154 :   sqN = sqrtr_abs(utor(N, prec));
    7015         154 :   vs = const_vec(lv-1, NULL);
    7016         154 :   vb = const_vec(lv-1, NULL);
    7017         154 :   vL = cgetg(lv, t_VEC);
    7018         154 :   vTAU = cgetg(lv, t_VEC);
    7019         154 :   vczd = cgetg(lv, t_VEC);
    7020         154 :   L0 = mfthetaancreate(NULL, gN, vgk); /* only for thetacost */
    7021         154 :   vE = mfgetembed(F, prec);
    7022         154 :   N0 = 0;
    7023         329 :   for (i = 1; i < lv; i++)
    7024             :   {
    7025         182 :     GEN z = gel(vtau,i), tau, U;
    7026             :     long w, n;
    7027             : 
    7028         182 :     gel(vs,i) = evalcusp(mf, F, z, prec);
    7029         175 :     if (gel(vs,i)) continue;
    7030         147 :     tau = cxredga0N(N, z, &U, &gel(vczd,i), flag);
    7031         147 :     if (!flag) w = 0; else { w = mfZC_width(N, gel(U,1)); tau = gdivgs(tau,w); }
    7032         147 :     gel(vTAU,i) = mulcxmI(gmul(tau, sqN));
    7033         147 :     n = lfunthetacost(L0, real_i(gel(vTAU,i)), 0, bitprec);
    7034         147 :     if (N0 < n) N0 = n;
    7035         147 :     if (flag)
    7036             :     {
    7037          35 :       GEN A, al, v = mfslashexpansion(mf, F, ZM_inv(U,NULL), N0, 0, &A, prec);
    7038          35 :       gel(vL,i) = van_embedall(v, vE, gN, vgk);
    7039          35 :       al = gel(A,1);
    7040          35 :       if (!gequal0(al))
    7041           7 :         gel(vb,i) = gexp(gmul(gmul(gmulsg(w,al),PiI2(prec)), tau), prec);
    7042             :     }
    7043             :   }
    7044         147 :   if (!flag)
    7045             :   {
    7046         112 :     van = mfcoefs_i(F, N0, 1);
    7047         112 :     vL = const_vec(lv-1, van_embedall(van, vE, gN, vgk));
    7048             :   }
    7049         322 :   for (i = 1; i < lv; i++)
    7050             :   {
    7051             :     GEN T;
    7052         175 :     if (gel(vs,i)) continue;
    7053         147 :     T = gpow(gel(vczd,i), gneg(gk), prec);
    7054         147 :     if (flag && gel(vb,i)) T = gmul(T, gel(vb,i));
    7055         147 :     gel(vs,i) = lfunthetaall(T, gel(vL,i), gel(vTAU,i), bitprec);
    7056             :   }
    7057         147 :   return flscal? gel(vs,1): vs;
    7058             : }
    7059             : 
    7060             : static long
    7061        1078 : mfistrivial(GEN F)
    7062             : {
    7063        1078 :   switch(mf_get_type(F))
    7064             :   {
    7065           7 :     case t_MF_CONST: return lg(gel(F,2)) == 1;
    7066         224 :     case t_MF_LINEAR: case t_MF_LINEAR_BHN: return gequal0(gel(F,3));
    7067         847 :     default: return 0;
    7068             :   }
    7069             : }
    7070             : 
    7071             : static long
    7072         896 : mf_same_k(GEN mf, GEN f) { return gequal(MF_get_gk(mf), mf_get_gk(f)); }
    7073             : static long
    7074         854 : mf_same_CHI(GEN mf, GEN f)
    7075             : {
    7076         854 :   GEN F1, F2, chi1, chi2, CHI1 = MF_get_CHI(mf), CHI2 = mf_get_CHI(f);
    7077             :   /* are the primitive chars attached to CHI1 and CHI2 equal ? */
    7078         854 :   F1 = znconreyconductor(gel(CHI1,1), gel(CHI1,2), &chi1);
    7079         854 :   if (typ(F1) == t_VEC) F1 = gel(F1,1);
    7080         854 :   F2 = znconreyconductor(gel(CHI2,1), gel(CHI2,2), &chi2);
    7081         854 :   if (typ(F2) == t_VEC) F2 = gel(F2,1);
    7082         854 :   return equalii(F1,F2) && ZV_equal(chi1,chi2);
    7083             : }
    7084             : /* check k and CHI rigorously, but not coefficients nor N */
    7085             : static long
    7086         189 : mfisinspace_i(GEN mf, GEN F)
    7087             : {
    7088         189 :   return mfistrivial(F) || (mf_same_k(mf,F) && mf_same_CHI(mf,F));
    7089             : }
    7090             : static void
    7091           7 : err_space(GEN F)
    7092           7 : { pari_err_DOMAIN("mftobasis", "form", "does not belong to",
    7093           0 :                   strtoGENstr("space"), F); }
    7094             : 
    7095             : static long
    7096         140 : mfcheapeisen(GEN mf)
    7097             : {
    7098         140 :   long k, L, N = MF_get_N(mf);
    7099             :   GEN P;
    7100         140 :   if (N <= 70) return 1;
    7101          84 :   k = itos(gceil(MF_get_gk(mf)));
    7102          84 :   if (odd(k)) k--;
    7103          84 :   switch (k)
    7104             :   {
    7105           0 :     case 2:  L = 190; break;
    7106          14 :     case 4:  L = 162; break;
    7107             :     case 6:
    7108          70 :     case 8:  L = 88; break;
    7109           0 :     case 10: L = 78; break;
    7110           0 :     default: L = 66; break;
    7111             :   }
    7112          84 :   P = gel(myfactoru(N), 1);
    7113          84 :   return P[lg(P)-1] <= L;
    7114             : }
    7115             : 
    7116             : static GEN
    7117         175 : myimag_i(GEN tau)
    7118             : {
    7119         175 :   long tc = typ(tau);
    7120         175 :   if (tc == t_INFINITY || tc == t_INT || tc == t_FRAC)
    7121          28 :     return gen_1;
    7122         147 :   if (tc == t_VEC)
    7123             :   {
    7124             :     long ltau, i;
    7125           7 :     GEN z = cgetg_copy(tau, &ltau);
    7126           7 :     for (i=1; i<ltau; i++) gel(z,i) = myimag_i(gel(tau,i));
    7127           7 :     return z;
    7128             :   }
    7129         140 :   return imag_i(tau);
    7130             : }
    7131             : 
    7132             : static GEN
    7133         140 : mintau(GEN vtau)
    7134             : {
    7135         140 :   if (!is_vec_t(typ(vtau))) return myimag_i(vtau);
    7136           7 :   return (lg(vtau) == 1)? gen_1: vecmin(myimag_i(vtau));
    7137             : }
    7138             : 
    7139             : /* initialization for mfgaexpansion: what does not depend on cusp */
    7140             : static GEN
    7141         826 : mf_eisendec(GEN mf, GEN F, long prec)
    7142             : {
    7143         826 :   GEN B = liftpol_shallow(mfeisensteindec(mf, F)), v = variables_vecsmall(B);
    7144         826 :   GEN Mvecj = obj_check(mf, MF_EISENSPACE);
    7145         826 :   long l = lg(v), i, ord;
    7146         826 :   if (lg(Mvecj) < 5) Mvecj = gel(Mvecj,1);
    7147         826 :   ord = itou(gel(Mvecj,4));
    7148         882 :   for (i = 1; i < l; i++)
    7149         637 :     if (v[i] != 1) { B = gsubst(B, v[i], rootsof1u_cx(ord, prec)); break; }
    7150         826 :   return B;
    7151             : }
    7152             : 
    7153             : GEN
    7154         154 : mfeval(GEN mf0, GEN F, GEN vtau, long bitprec)
    7155             : {
    7156         154 :   pari_sp av = avma;
    7157         154 :   long flnew = 1;
    7158         154 :   GEN mf = checkMF_i(mf0);
    7159         154 :   if (!mf) pari_err_TYPE("mfeval", mf0);
    7160         154 :   if (!checkmf_i(F)) pari_err_TYPE("mfeval", F);
    7161         154 :   if (!mfisinspace_i(mf, F)) err_space(F);
    7162         154 :   if (!obj_check(mf, MF_EISENSPACE)) flnew = mfcheapeisen(mf);
    7163         154 :   if (flnew && gcmpgs(gmulsg(2*MF_get_N(mf), mintau(vtau)), 1) >= 0) flnew = 0;
    7164         154 :   return gerepilecopy(av, mfeval_i(mf, F, vtau, flnew, bitprec));
    7165             : }
    7166             : 
    7167             : static long
    7168         182 : val(GEN v, long bit)
    7169             : {
    7170         182 :   long c, l = lg(v);
    7171         399 :   for (c = 1; c < l; c++)
    7172         385 :     if (gexpo(gel(v,c)) > -bit) return c-1;
    7173          14 :   return -1;
    7174             : }
    7175             : GEN
    7176         196 : mfcuspval(GEN mf, GEN F, GEN cusp, long bitprec)
    7177             : {
    7178         196 :   pari_sp av = avma;
    7179         196 :   long lvE, w, N, sb, n, A, C, prec = nbits2prec(bitprec);
    7180             :   GEN ga, gk, vE;
    7181         196 :   mf = checkMF(mf);
    7182         196 :   if (!checkmf_i(F)) pari_err_TYPE("mfcuspval",F);
    7183         196 :   N = MF_get_N(mf);
    7184         196 :   cusp_canon(cusp, N, &A, &C);
    7185         196 :   gk = mf_get_gk(F);
    7186         196 :   if (typ(gk) != t_INT)
    7187             :   {
    7188          42 :     GEN FT = mfmultheta(F), mf2 = obj_checkbuild(mf, MF_MF2INIT, &mf2init);
    7189          42 :     GEN r = mfcuspval(mf2, FT, cusp, bitprec);
    7190          42 :     if ((C & 3L) == 2)
    7191             :     {
    7192          14 :       GEN z = sstoQ(1,4);
    7193          14 :       r = gsub(r, typ(r) == t_VEC? const_vec(lg(r)-1, z): z);
    7194             :     }
    7195          42 :     return gerepileupto(av, r);
    7196             :   }
    7197         154 :   vE = mfgetembed(F, prec);
    7198         154 :   lvE = lg(vE);
    7199         154 :   w = mfcuspcanon_width(N, C);
    7200         154 :   sb = w * mfsturmNk(N, itos(gk));
    7201         154 :   ga = cusp2mat(A,C);
    7202         161 :   for (n = 8;; n = minss(sb, n << 1))
    7203           7 :   {
    7204         161 :     GEN R = mfgaexpansion(mf, F, ga, n, prec), res = liftpol_shallow(gel(R,3));
    7205         161 :     GEN v = cgetg(lvE-1, t_VECSMALL);
    7206         161 :     long j, ok = 1;
    7207         161 :     res = RgC_embedall(res, vE);
    7208         343 :     for (j = 1; j < lvE; j++)
    7209             :     {
    7210         182 :       v[j] = val(gel(res,j), bitprec/2);
    7211         182 :       if (v[j] < 0) ok = 0;
    7212             :     }
    7213         161 :     if (ok)
    7214             :     {
    7215         147 :       res = cgetg(lvE, t_VEC);
    7216         147 :       for (j = 1; j < lvE; j++) gel(res,j) = gadd(gel(R,1), sstoQ(v[j], w));
    7217         147 :       return gerepilecopy(av, lvE==2? gel(res,1): res);
    7218             :     }
    7219          14 :     if (n == sb) return lvE==2? mkoo(): const_vec(lvE-1, mkoo()); /* 0 */
    7220             :   }
    7221             : }
    7222             : 
    7223             : long
    7224         196 : mfiscuspidal(GEN mf, GEN F)
    7225             : {
    7226         196 :   pari_sp av = avma;
    7227             :   GEN mf2;
    7228         196 :   if (space_is_cusp(MF_get_space(mf))) return 1;
    7229          77 :   if (typ(mf_get_gk(F)) == t_INT)
    7230             :   {
    7231          49 :     GEN v = mftobasis(mf, F, 0);
    7232          49 :     long s = gequal0(vecslice(v, 1, lg(MF_get_E(mf)) - 1));
    7233          49 :     avma = av; return s;
    7234             :   }
    7235          28 :   if (!gequal0(mfak_i(F, 0))) return 0;
    7236          14 :   mf2 = obj_checkbuild(mf, MF_MF2INIT, &mf2init);
    7237          14 :   return mfiscuspidal(mf2, mfmultheta(F));
    7238             : }
    7239             : 
    7240             : /* F = vector of newforms in mftobasis format */
    7241             : static GEN
    7242          70 : mffrickeeigen_i(GEN mf, GEN F, GEN vE, long prec)
    7243             : {
    7244          70 :   GEN M, Z, L0, gN = MF_get_gN(mf), gk = MF_get_gk(mf);
    7245          70 :   long N0, i, lM, bit = prec2nbits(prec), k = itou(gk);
    7246          70 :   long LIM = 5; /* Sturm bound is enough */
    7247             : 
    7248          70 :   L0 = mfthetaancreate(NULL, gN, gk); /* only for thetacost */
    7249             : START:
    7250          70 :   N0 = lfunthetacost(L0, gen_1, LIM, bit);
    7251          70 :   M = mfcoefs_mf(mf, N0, 1);
    7252          70 :   lM = lg(F);
    7253          70 :   Z = cgetg(lM, t_VEC);
    7254         210 :   for (i = 1; i < lM; i++)
    7255             :   { /* expansion of D * F[i] */
    7256         140 :     GEN D, z, van = RgM_RgC_mul(M, Q_remove_denom(gel(F,i), &D));
    7257         140 :     GEN L = van_embedall(van, gel(vE,i), gN, gk);
    7258         140 :     long l = lg(L), j, bit_add = D? expi(D): 0;
    7259         140 :     gel(Z,i) = z = cgetg(l, t_VEC);
    7260         336 :     for (j = 1; j < l; j++)
    7261             :     {
    7262             :       GEN v, C, C0;
    7263             :       long m, e;
    7264         266 :       for (m = 0; m <= LIM; m++)
    7265             :       {
    7266         266 :         v = lfuntheta(gmael(L,j,2), gen_1, m, bit);
    7267         266 :         if (gexpo(v) > bit_add - bit/2) break;
    7268             :       }
    7269         196 :       if (m > LIM) { LIM <<= 1; goto START; }
    7270         196 :       C = mulcxpowIs(gdiv(v,conj_i(v)), 2*m - k);
    7271         196 :       C0 = grndtoi(C, &e); if (e < 5-bit) C = C0;
    7272         196 :       gel(z,j) = C;
    7273             :     }
    7274             :   }
    7275          70 :   return Z;
    7276             : }
    7277             : static GEN
    7278          63 : mffrickeeigen(GEN mf, GEN vE, long prec)
    7279             : {
    7280          63 :   GEN D = obj_check(mf, MF_FRICKE);
    7281          63 :   if (D) { long p = gprecision(D); if (!p || p >= prec) return D; }
    7282          56 :   D = mffrickeeigen_i(mf, MF_get_newforms(mf), vE, prec);
    7283          56 :   return obj_insert(mf, MF_FRICKE, D);
    7284             : }
    7285             : 
    7286             : /* integral weight, new space for primitive quadratic character CHIP;
    7287             :  * MF = vector of embedded eigenforms coefs on mfbasis, by orbit.
    7288             :  * Assume N > Q > 1 and (Q,f(CHIP)) = 1 */
    7289             : static GEN
    7290          56 : mfatkineigenquad(GEN mf, GEN CHIP, long Q, GEN MF, long bitprec)
    7291             : {
    7292             :   GEN L0, la2, S, F, vP, tau, wtau, Z, va, vb, den, coe, sqrtQ, sqrtN;
    7293          56 :   GEN M, gN, gk = MF_get_gk(mf);
    7294          56 :   long N0, x, yq, i, j, lF, dim, muQ, prec = nbits2prec(bitprec);
    7295          56 :   long N = MF_get_N(mf), k = itos(gk), NQ = N / Q;
    7296             : 
    7297             :   /* Q coprime to FC */
    7298          56 :   F = MF_get_newforms(mf);
    7299          56 :   vP = MF_get_fields(mf);
    7300          56 :   lF = lg(F);
    7301          56 :   Z = cgetg(lF, t_VEC);
    7302          56 :   S = MF_get_S(mf); dim = lg(S) - 1;
    7303          56 :   muQ = mymoebiusu(Q);
    7304          56 :   if (muQ)
    7305             :   {
    7306          42 :     GEN SQ = cgetg(dim+1,t_VEC), Qk = gpow(stoi(Q), sstoQ(k-2, 2), prec);
    7307          42 :     long i, bit2 = bitprec >> 1;
    7308          42 :     for (j = 1; j <= dim; j++) gel(SQ,j) = mfak_i(gel(S,j), Q);
    7309          84 :     for (i = 1; i < lF; i++)
    7310             :     {
    7311          42 :       GEN S = RgV_dotproduct(gel(F,i), SQ), T = gel(vP,i);
    7312             :       long e;
    7313          42 :       if (degpol(T) > 1 && typ(S) != t_POLMOD) S = gmodulo(S, T);
    7314          42 :       S = grndtoi(gdiv(conjvec(S, prec), Qk), &e);
    7315          42 :       if (e > -bit2) pari_err_PREC("mfatkineigenquad");
    7316          42 :       if (muQ == -1) S = gneg(S);
    7317          42 :       gel(Z,i) = S;
    7318             :     }
    7319          42 :     return Z;
    7320             :   }
    7321          14 :   la2 = mfchareval_i(CHIP, Q); /* 1 or -1 */
    7322          14 :   (void)cbezout(Q, NQ, &x, &yq);
    7323          14 :   sqrtQ = sqrtr_abs(utor(Q,prec));
    7324          14 :   tau = mkcomplex(gadd(sstoQ(-1, NQ), ginv(utoi(1000))),
    7325             :                   divru(sqrtQ, N));
    7326          14 :   den = gaddgs(gmulsg(NQ, tau), 1);
    7327          14 :   wtau = gdiv(gsub(gmulsg(x, tau), sstoQ(yq, Q)), den);
    7328          14 :   coe = gpowgs(gmul(sqrtQ, den), k);
    7329             : 
    7330          14 :   sqrtN = sqrtr_abs(utor(N,prec));
    7331          14 :   tau  = mulcxmI(gmul(tau,  sqrtN));
    7332          14 :   wtau = mulcxmI(gmul(wtau, sqrtN));
    7333          14 :   gN = utoipos(N);
    7334          14 :   L0 = mfthetaancreate(NULL, gN, gk); /* only for thetacost */
    7335          14 :   N0 = maxss(lfunthetacost(L0,real_i(tau), 0,bitprec),
    7336             :              lfunthetacost(L0,real_i(wtau),0,bitprec));
    7337          14 :   M = mfcoefs_mf(mf, N0, 1);
    7338          14 :   va = cgetg(dim+1, t_VEC);
    7339          14 :   vb = cgetg(dim+1, t_VEC);
    7340         105 :   for (j = 1; j <= dim; j++)
    7341             :   {
    7342          91 :     GEN L, v = vecslice(gel(M,j), 2, N0+1); /* remove a0 */
    7343          91 :     settyp(v, t_VEC); L = mfthetaancreate(v, gN, gk);
    7344          91 :     gel(va,j) = lfuntheta(L, tau,0,bitprec);
    7345          91 :     gel(vb,j) = lfuntheta(L,wtau,0,bitprec);
    7346             :   }
    7347          84 :   for (i = 1; i < lF; i++)
    7348             :   {
    7349          70 :     GEN z, FE = gel(MF,i);
    7350          70 :     long l = lg(FE);
    7351          70 :     z = cgetg(l, t_VEC);
    7352          70 :     for (j = 1; j < l; j++)
    7353             :     {
    7354          70 :       GEN f = gel(FE,j), a = RgV_dotproduct(va,f), b = RgV_dotproduct(vb,f);
    7355          70 :       GEN la = ground( gdiv(b, gmul(a,coe)) );
    7356          70 :       if (!gequal(gsqr(la), la2)) pari_err_PREC("mfatkineigenquad");
    7357          70 :       if (typ(la) == t_INT)
    7358             :       {
    7359          70 :         if (j != 1) pari_err_BUG("mfatkineigenquad");
    7360          70 :         z = const_vec(l-1, la); break;
    7361             :       }
    7362           0 :       gel(z,j) = la;
    7363             :     }
    7364          70 :     gel(Z,i) = z;
    7365             :   }
    7366          14 :   return Z;
    7367             : }
    7368             : 
    7369             : static GEN
    7370          70 : myusqrt(ulong a, long prec)
    7371             : {
    7372          70 :   if (a == 1UL) return gen_1;
    7373          56 :   if (uissquareall(a, &a)) return utoipos(a);
    7374          42 :   return sqrtr_abs(utor(a, prec));
    7375             : }
    7376             : /* Assume mf is a non-trivial new space, rational primitive character CHIP
    7377             :  * and (Q,FC) = 1 */
    7378             : static GEN
    7379          98 : mfatkinmatnewquad(GEN mf, GEN CHIP, long Q, long flag, long PREC)
    7380             : {
    7381          98 :   GEN cM, M, D, MF, den, vE, F = MF_get_newforms(mf);
    7382          98 :   long i, c, e, prec, bitprec, lF = lg(F), N = MF_get_N(mf), k = MF_get_k(mf);
    7383             : 
    7384          98 :   if (Q == 1) return mkvec4(gen_0, matid(MF_get_dim(mf)), gen_1, mf);
    7385          98 :   den = gel(MF_get_Minv(mf), 2);
    7386          98 :   bitprec = expi(den) + 64;
    7387          98 :   if (!flag) bitprec = maxss(bitprec, prec2nbits(PREC));
    7388             : 
    7389             : START:
    7390          98 :   prec = nbits2prec(bitprec);
    7391          98 :   vE = mfeigenembed(mf, prec);
    7392          98 :   M = cgetg(lF, t_VEC);
    7393          98 :   for (i = 1; i < lF; i++) gel(M,i) = RgC_embedall(gel(F,i), gel(vE,i));
    7394          98 :   if (Q != N)
    7395             :   {
    7396          56 :     D = mfatkineigenquad(mf, CHIP, Q, M, bitprec);
    7397          56 :     c = odd(k)? Q: 1;
    7398             :   }
    7399             :   else
    7400             :   {
    7401          42 :     D = mffrickeeigen(mf, vE, DEFAULTPREC);
    7402          42 :     c = mfcharmodulus(CHIP); if (odd(k)) c = -Q/c;
    7403             :   }
    7404          98 :   D = shallowconcat1(D);
    7405          98 :   if (vec_isconst(D)) { MF = diagonal_shallow(D); flag = 0; }
    7406             :   else
    7407             :   {
    7408          63 :     M = shallowconcat1(M);
    7409          63 :     MF = RgM_mul(matmuldiagonal(M,D), ginv(M));
    7410             :   }
    7411          98 :   if (!flag) return mkvec4(gen_0, MF, gen_1, mf);
    7412             : 
    7413          21 :   if (c > 0)
    7414          21 :     cM = myusqrt(c, PREC);
    7415             :   else
    7416             :   {
    7417           0 :     MF = imag_i(MF); c = -c;
    7418           0 :     cM = mkcomplex(gen_0, myusqrt(c,PREC));
    7419             :   }
    7420          21 :   if (c != 1) MF = RgM_Rg_mul(MF, myusqrt(c,prec));
    7421          21 :   MF = grndtoi(RgM_Rg_mul(MF,den), &e);
    7422          21 :   if (e > -32) { bitprec <<= 1; goto START; }
    7423          21 :   MF = RgM_Rg_div(MF, den);
    7424          21 :   if (is_rational_t(typ(cM)) && !isint1(cM))
    7425           0 :   { MF = RgM_Rg_div(MF, cM); cM = gen_1; }
    7426          21 :   return mkvec4(gen_0, MF, cM, mf);
    7427             : }
    7428             : 
    7429             : /* let CHI mod N, Q || N, return \bar{CHI_Q} * CHI_{N/Q} */
    7430             : static GEN
    7431          70 : mfcharAL(GEN CHI, long Q)
    7432             : {
    7433          70 :   GEN G = gel(CHI,1), c = gel(CHI,2), cycc, d, P, E, F;
    7434          70 :   long l = lg(c), N = mfcharmodulus(CHI), i;
    7435          70 :   if (N == Q) return mfcharconj(CHI);
    7436          42 :   if (N == 1) return CHI;
    7437          42 :   CHI = leafcopy(CHI);
    7438          42 :   gel(CHI,2) = d = leafcopy(c);
    7439          42 :   F = znstar_get_faN(G);
    7440          42 :   P = gel(F,1);
    7441          42 :   E = gel(F,2);
    7442          42 :   cycc = znstar_get_conreycyc(G);
    7443          42 :   if (!odd(Q) && equaliu(gel(P,1), 2) && E[1] >= 3)
    7444          14 :     gel(d,2) = Fp_neg(gel(d,2), gel(cycc,2));
    7445          56 :   else for (i = 1; i < l; i++)
    7446          28 :     if (!umodui(Q, gel(P,i))) gel(d,i) = Fp_neg(gel(d,i), gel(cycc,i));
    7447          42 :   return CHI;
    7448             : }
    7449             : static long
    7450         189 : atkin_get_NQ(long N, long Q, const char *f)
    7451             : {
    7452         189 :   long NQ = N / Q;
    7453         189 :   if (N % Q) pari_err_DOMAIN(f,"N % Q","!=",gen_0,utoi(Q));
    7454         189 :   if (ugcd(NQ, Q) > 1) pari_err_DOMAIN(f,"gcd(Q,N/Q)","!=",gen_1,utoi(Q));
    7455         189 :   return NQ;
    7456             : }
    7457             : 
    7458             : /* transform mf to new_NEW if possible */
    7459             : static GEN
    7460        1148 : MF_set_new(GEN mf)
    7461             : {
    7462        1148 :   GEN vMjd, vj, gk = MF_get_gk(mf);
    7463             :   long l, j;
    7464        1148 :   if (MF_get_space(mf) != mf_CUSP
    7465         182 :       || typ(gk) != t_INT || itou(gk) == 1) return mf;
    7466         168 :   vMjd = MFcusp_get_vMjd(mf); l = lg(vMjd);
    7467         168 :   if (l > 1 && gel(vMjd,1)[1] != MF_get_N(mf)) return mf; /* oldspace != 0 */
    7468         168 :   mf = shallowcopy(mf);
    7469         168 :   gel(mf,1) = shallowcopy(gel(mf,1));
    7470         168 :   MF_set_space(mf, mf_NEW);
    7471         168 :   vj = cgetg(l, t_VECSMALL);
    7472         168 :   for (j = 1; j < l; j++) vj[j] = gel(vMjd, j)[2];
    7473         168 :   gel(mf,4) = vj; return mf;
    7474             : }
    7475             : 
    7476             : /* if flag = 1, rationalize, else don't */
    7477             : static GEN
    7478         168 : mfatkininit_i(GEN mf, long Q, long flag, long prec)
    7479             : {
    7480             :   GEN M, B, C, CHI, CHIAL, G, chi, P, z, g, mfB, s, Mindex, Minv;
    7481         168 :   long j, l, lim, ord, FC, NQ, cQ, nk, dk, N = MF_get_N(mf);
    7482             : 
    7483         168 :   B = MF_get_basis(mf); l = lg(B);
    7484         168 :   M = cgetg(l, t_MAT); if (l == 1) return mkvec4(gen_0,M,gen_1,mf);
    7485         168 :   Qtoss(MF_get_gk(mf), &nk,&dk);
    7486         168 :   Q = labs(Q);
    7487         168 :   NQ = atkin_get_NQ(N, Q, "mfatkininit");
    7488         168 :   CHI = MF_get_CHI(mf);
    7489         168 :   CHI = mfchartoprimitive(CHI, &FC);
    7490         168 :   ord = mfcharorder_canon(CHI);
    7491         168 :   mf = MF_set_new(mf);
    7492         168 :   if (MF_get_space(mf) == mf_NEW && ord == 1 && NQ % FC == 0 && dk == 1)
    7493          98 :     return mfatkinmatnewquad(mf, CHI, Q, flag, prec);
    7494             :   /* now flag != 0 */
    7495          70 :   G   = gel(CHI,1);
    7496          70 :   chi = gel(CHI,2);
    7497          70 :   if (Q == N) { g = mkmat22s(0, -1, N, 0); cQ = NQ; } /* Fricke */
    7498             :   else
    7499             :   {
    7500          28 :     GEN F, gQP = utoi(ugcd(Q, FC));
    7501             :     long t, v;
    7502          28 :     chi = znchardecompose(G, chi, gQP);
    7503          28 :     F = znconreyconductor(G, chi, &chi);
    7504          28 :     G = znstar0(F,1);
    7505          28 :     (void)cbezout(Q, NQ, &t, &v);
    7506          28 :     g = mkmat22s(Q*t, 1, -N*v, Q);
    7507          28 :     cQ = -NQ*v;
    7508             :   }
    7509          70 :   C = s = gen_1;
    7510             :   /* N.B. G,chi are G_Q,chi_Q [primitive] at this point */
    7511          70 :   if (lg(chi) != 1) C = ginv( znchargauss(G, chi, gen_1, prec2nbits(prec)) );
    7512          70 :   if (dk == 1)
    7513          63 :   { if (odd(nk)) s = myusqrt(Q,prec); }
    7514             :   else
    7515             :   {
    7516           7 :     long r = nk >> 1; /* k-1/2 */
    7517           7 :     s = gpow(utoipos(Q), mkfracss(odd(r)? 1: 3, 4), prec);
    7518           7 :     if (odd(cQ))
    7519             :     {
    7520           7 :       long t = r + ((cQ-1) >> 1);
    7521           7 :       s = mkcomplex(s, odd(t)? gneg(s): s);
    7522             :     }
    7523             :   }
    7524          70 :   if (!isint1(s)) C = gmul(C, s);
    7525          70 :   CHIAL = mfcharAL(CHI, Q);
    7526          70 :   if (dk == 2)
    7527           7 :     CHIAL = mfcharmul(CHIAL, induce(gel(CHIAL,1), utoipos(odd(Q) ? Q<<2 : Q)));
    7528          70 :   CHIAL = mfchartoprimitive(CHIAL,NULL);
    7529          70 :   mfB = gequal(CHIAL,CHI)? mf: mfinit_Nndkchi(N,nk,dk,CHIAL,MF_get_space(mf),0);
    7530          70 :   Mindex = MF_get_Mindex(mfB);
    7531          70 :   Minv = MF_get_Minv(mfB);
    7532          70 :   P = z = NULL;
    7533          70 :   if (ord != 1) { P = mfcharpol(CHI); z = rootsof1u_cx(ord, prec); }
    7534          70 :   lim = maxss(mfsturm(mfB), mfsturm(mf)) + 1;
    7535         217 :   for (j = 1; j < l; j++)
    7536             :   {
    7537         147 :     GEN v = mfslashexpansion(mf, gel(B,j), g, lim, 0, NULL, prec+1);
    7538             :     long junk;
    7539         147 :     if (!isint1(C)) v = RgV_Rg_mul(v, C);
    7540         147 :     v = bestapprnf(v, P, z, prec);
    7541         147 :     v = vecpermute_partial(v, Mindex, &junk);
    7542         147 :     v = Minv_RgC_mul(Minv, v); /* cf mftobasis_i */
    7543         147 :     gel(M, j) = v;
    7544             :   }
    7545          70 :   if (is_rational_t(typ(C)) && !gequal1(C)) { M = gdiv(M, C); C = gen_1; }
    7546          70 :   if (mfB == mf) mfB = gen_0;
    7547          70 :   return mkvec4(mfB, M, C, mf);
    7548             : }
    7549             : GEN
    7550          77 : mfatkininit(GEN mf, long Q, long prec)
    7551             : {
    7552          77 :   pari_sp av = avma;
    7553          77 :   mf = checkMF(mf); return gerepilecopy(av, mfatkininit_i(mf, Q, 1, prec));
    7554             : }
    7555             : static void
    7556          21 : checkmfa(GEN z)
    7557             : {
    7558          21 :   if (typ(z) != t_VEC || lg(z) != 5 || typ(gel(z,2)) != t_MAT
    7559          21 :       || !checkMF_i(gel(z,4))
    7560          21 :       || (!isintzero(gel(z,1)) && !checkMF_i(gel(z,1))))
    7561           0 :     pari_err_TYPE("mfatkin [please apply mfatkininit()]",z);
    7562          21 : }
    7563             : 
    7564             : /* Apply atkin Q to closure F */
    7565             : GEN
    7566          21 : mfatkin(GEN mfa, GEN F)
    7567             : {
    7568          21 :   pari_sp av = avma;
    7569             :   GEN z, mfB, MQ, mf;
    7570          21 :   checkmfa(mfa);
    7571          21 :   mfB= gel(mfa,1);
    7572          21 :   MQ = gel(mfa,2);
    7573          21 :   mf = gel(mfa,4);
    7574          21 :   if (typ(mfB) == t_INT) mfB = mf;
    7575          21 :   z = RgM_RgC_mul(MQ, mftobasis_i(mf,F));
    7576          21 :   return gerepileupto(av, mflinear(mfB, z));
    7577             : }
    7578             : 
    7579             : GEN
    7580          42 : mfatkineigenvalues(GEN mf, long Q, long prec)
    7581             : {
    7582          42 :   pari_sp av = avma;
    7583             :   GEN vF, L, CHI, M, mfatk, C, MQ, vE, mfB;
    7584             :   long N, NQ, l, i;
    7585             : 
    7586          42 :   mf = checkMF(mf); N = MF_get_N(mf); CHI = MF_get_CHI(mf);
    7587          42 :   vF = MF_get_newforms(mf); l = lg(vF);
    7588          42 :   if (l == 1) { avma = av; return cgetg(1, t_VEC); }
    7589          42 :   L = cgetg(l, t_VEC);
    7590          42 :   if (Q == 1)
    7591             :   {
    7592           7 :     GEN vP = MF_get_fields(mf);
    7593           7 :     for (i = 1; i < l; i++) gel(L,i) = const_vec(degpol(gel(vP,i)), gen_1);
    7594           7 :     return L;
    7595             :   }
    7596          35 :   vE = mfeigenembed(mf,prec);
    7597          35 :   if (Q == N) return gerepileupto(av, mffrickeeigen(mf, vE, prec));
    7598          21 :   Q = labs(Q);
    7599          21 :   NQ = atkin_get_NQ(N, Q, "mfatkineigenvalues");
    7600          21 :   mfatk = mfatkininit(mf, Q, prec);
    7601          21 :   mfB= gel(mfatk,1); if (typ(mfB) != t_VEC) mfB = mf;
    7602          21 :   MQ = gel(mfatk,2);
    7603          21 :   C  = gel(mfatk,3);
    7604          21 :   M = row(mfcoefs_mf(mfB,1,1), 2); /* vec of a_1(b_i) for mfbasis functions */
    7605          56 :   for (i = 1; i < l; i++)
    7606             :   {
    7607          35 :     GEN c = RgV_dotproduct(RgM_RgC_mul(MQ,gel(vF,i)), M); /* C * eigen_i */
    7608          35 :     gel(L,i) = Rg_embedall_i(c, gel(vE,i));
    7609             :   }
    7610          21 :   if (!gequal1(C)) L = gdiv(L, C);
    7611          21 :   mf = MF_set_new(mf);
    7612          21 :   if (MF_get_space(mf) == mf_NEW && mfcharorder(CHI) <= 2
    7613           7 :       && (NQ==1 || NQ % mfcharconductor(CHI) == 0)
    7614           7 :       && typ(MF_get_gk(mf)) == t_INT) L = ground(L);
    7615          21 :   return gerepilecopy(av, L);
    7616             : }
    7617             : 
    7618             : /* expand B_d V, keeping same length */
    7619             : static GEN
    7620        4767 : bdexpand(GEN V, long d)
    7621             : {
    7622             :   GEN W;
    7623             :   long N, n;
    7624        4767 :   if (d == 1) return V;
    7625        1624 :   N = lg(V)-1; W = zerovec(N);
    7626        1624 :   for (n = 0; n <= (N-1)/d; n++) gel(W, n*d+1) = gel(V, n+1);
    7627        1624 :   return W;
    7628             : }
    7629             : /* expand B_d V, increasing length up to lim */
    7630             : static GEN
    7631         259 : bdexpandall(GEN V, long d, long lim)
    7632             : {
    7633             :   GEN W;
    7634             :   long N, n;
    7635         259 :   if (d == 1) return V;
    7636          35 :   N = lg(V)-1; W = zerovec(lim);
    7637          35 :   for (n = 0; n <= N-1 && n*d <= lim; n++) gel(W, n*d+1) = gel(V, n+1);
    7638          35 :   return W;
    7639             : }
    7640             : 
    7641             : static void
    7642        7798 : parse_vecj(GEN T, GEN *E1, GEN *E2)
    7643             : {
    7644        7798 :   if (lg(T)==3) { *E1 = gel(T,1); *E2 = gel(T,2); }
    7645        4165 :   else { *E1 = T; *E2 = NULL; }
    7646        7798 : }
    7647             : 
    7648             : /* g in M_2(Z) ? */
    7649             : static int
    7650        2380 : check_M2Z(GEN g)
    7651        2380 : {  return typ(g) == t_MAT && lg(g) == 3 && lgcols(g) == 3 && RgM_is_ZM(g); }
    7652             : /* g in SL_2(Z) ? */
    7653             : static int
    7654        1463 : check_SL2Z(GEN g) { return check_M2Z(g) && equali1(ZM_det(g)); }
    7655             : 
    7656             : static GEN
    7657        7518 : mfcharcxeval(GEN CHI, long n, long prec)
    7658             : {
    7659             :   GEN ordg;
    7660             :   ulong ord;
    7661        7518 :   if (ugcd(mfcharmodulus(CHI), labs(n)) > 1) return gen_0;
    7662        7518 :   ordg = gmfcharorder(CHI);
    7663        7518 :   ord = itou(ordg);
    7664        7518 :   return rootsof1q_cx(znchareval_i(CHI,n,ordg), ord, prec);
    7665             : }
    7666             : 
    7667             : static GEN
    7668        4403 : RgV_shift(GEN V, GEN gn)
    7669             : {
    7670             :   long i, n, l;
    7671             :   GEN W;
    7672        4403 :   if (typ(gn) != t_INT) pari_err_BUG("RgV_shift [n not integral]");
    7673        4403 :   n = itos(gn);
    7674        4403 :   if (n < 0) pari_err_BUG("RgV_shift [n negative]");
    7675        4403 :   if (!n) return V;
    7676          98 :   W = cgetg_copy(V, &l); if (n > l-1) n = l-1;
    7677          98 :   for (i=1; i <= n; i++) gel(W,i) = gen_0;
    7678          98 :   for (    ; i < l; i++) gel(W,i) = gel(V, i-n);
    7679          98 :   return W;
    7680             : }
    7681             : static GEN
    7682        6776 : hash_eisengacx(hashtable *H, void *E, long w, GEN ga, long n, long prec)
    7683             : {
    7684        6776 :   ulong h = H->hash(E);
    7685        6776 :   hashentry *e = hash_search2(H, E, h);
    7686             :   GEN v;
    7687        6776 :   if (e) v = (GEN)e->val;
    7688             :   else
    7689             :   {
    7690        4410 :     v = mfeisensteingacx((GEN)E, w, ga, n, prec);
    7691        4410 :     hash_insert2(H, E, (void*)v, h);
    7692             :   }
    7693        6776 :   return v;
    7694             : }
    7695             : static GEN
    7696        4403 : vecj_expand(GEN B, hashtable *H, long w, GEN ga, long n, long prec)
    7697             : {
    7698             :   GEN E1, E2, v;
    7699        4403 :   parse_vecj(B, &E1, &E2);
    7700        4403 :   v = hash_eisengacx(H, (void*)E1, w, ga, n, prec);
    7701        4403 :   if (E2)
    7702             :   {
    7703        2352 :     GEN u = hash_eisengacx(H, (void*)E2, w, ga, n, prec);
    7704        2352 :     GEN a = gadd(gel(v,1), gel(u,1));
    7705        2352 :     GEN b = RgV_mul_RgXn(gel(v,2), gel(u,2));
    7706        2352 :     v = mkvec2(a,b);
    7707             :   }
    7708        4403 :   return v;
    7709             : }
    7710             : static GEN
    7711         889 : shift_M(GEN M, GEN Valpha, long w)
    7712             : {
    7713         889 :   long i, l = lg(Valpha);
    7714         889 :   GEN almin = vecmin(Valpha);
    7715        5292 :   for (i = 1; i < l; i++)
    7716             :   {
    7717        4403 :     GEN alpha = gel(Valpha, i), gsh = gmulsg(w, gsub(alpha,almin));
    7718        4403 :     gel(M,i) = RgV_shift(gel(M,i), gsh);
    7719             :   }
    7720         889 :   return almin;
    7721             : }
    7722             : static GEN mfeisensteinspaceinit(GEN NK);
    7723             : #if 0
    7724             : /* ga in M_2^+(Z)), n >= 0 */
    7725             : static GEN
    7726             : mfgaexpansion_init(GEN mf, GEN ga, long n, long prec)
    7727             : {
    7728             :   GEN M, Mvecj, vecj, almin, Valpha;
    7729             :   long i, w, l, N = MF_get_N(mf), c = itos(gcoeff(ga,2,1));
    7730             :   hashtable *H;
    7731             : 
    7732             :   if (c % N == 0)
    7733             :   { /* ga in G_0(N), trivial case; w = 1 */
    7734             :     GEN chid = mfcharcxeval(MF_get_CHI(mf), itos(gcoeff(ga,2,2)), prec);
    7735             :     return mkvec2(chid, utoi(n));
    7736             :   }
    7737             : 
    7738             :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    7739             :   if (lg(Mvecj) < 5) pari_err_IMPL("mfgaexpansion_init in this case");
    7740             :   w = mfcuspcanon_width(N, c);
    7741             :   vecj = gel(Mvecj, 3);
    7742             :   l = lg(vecj);
    7743             :   M = cgetg(l, t_VEC);
    7744             :   Valpha = cgetg(l, t_VEC);
    7745             :   H = hash_create(l, (ulong(*)(void*))&hash_GEN,
    7746             :                      (int(*)(void*,void*))&gidentical, 1);
    7747             :   for (i = 1; i < l; i++)
    7748             :   {
    7749             :     GEN v = vecj_expand(gel(vecj,i), H, w, ga, n, prec);
    7750             :     gel(Valpha,i) = gel(v,1);
    7751             :     gel(M,i) = gel(v,2);
    7752             :   }
    7753             :   almin = shift_M(M, Valpha, w);
    7754             :   return mkvec3(almin, utoi(w), M);
    7755             : }
    7756             : /* half-integer weight not supported; vF = [F,eisendec(F)].
    7757             :  * Minit = mfgaexpansion_init(mf, ga, n, prec) */
    7758             : static GEN
    7759             : mfgaexpansion_with_init(GEN Minit, GEN vF)
    7760             : {
    7761             :   GEN v;
    7762             :   if (lg(Minit) == 3)
    7763             :   { /* ga in G_0(N) */
    7764             :     GEN chid = gel(Minit,1), gn = gel(Minit,2);
    7765             :     v = mfcoefs_i(gel(vF,1), itou(gn), 1);
    7766             :     v = mkvec3(gen_0, gen_1, RgV_Rg_mul(v,chid));
    7767             :   }
    7768             :   else
    7769             :   {
    7770             :     GEN V = RgM_RgC_mul(gel(Minit,3), gel(vF,2));
    7771             :     v = mkvec3(gel(Minit,1), gel(Minit,2), V);
    7772             :   }
    7773             :   return v;
    7774             : }
    7775             : #endif
    7776             : 
    7777             : /* B = mfeisensteindec(F) already embedded, ga in M_2^+(Z)), n >= 0 */
    7778             : static GEN
    7779         889 : mfgaexpansion_i(GEN mf, GEN B0, GEN ga, long n, long prec)
    7780             : {
    7781         889 :   GEN M, Mvecj, vecj, almin, Valpha, B, E = NULL;
    7782         889 :   long i, j, w, nw, l, N = MF_get_N(mf), bit = prec2nbits(prec) / 2;
    7783             :   hashtable *H;
    7784             : 
    7785         889 :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    7786         889 :   if (lg(Mvecj) < 5) { E = gel(Mvecj, 2); Mvecj = gel(Mvecj, 1); }
    7787         889 :   vecj = gel(Mvecj, 3);
    7788         889 :   l = lg(vecj);
    7789         889 :   B = cgetg(l, t_COL);
    7790         889 :   M = cgetg(l, t_VEC);
    7791         889 :   Valpha = cgetg(l, t_VEC);
    7792         889 :   w = mfZC_width(N, gel(ga,1));
    7793         889 :   nw = E ? n + w : n;
    7794         889 :   H = hash_create(l, (ulong(*)(void*))&hash_GEN,
    7795             :                      (int(*)(void*,void*))&gidentical, 1);
    7796        7882 :   for (i = j = 1; i < l; i++)
    7797             :   {
    7798             :     GEN v;
    7799        6993 :     if (gequal0(gel(B0,i))) continue;
    7800        4403 :     v = vecj_expand(gel(vecj,i), H, w, ga, nw, prec);
    7801        4403 :     gel(B,j) = gel(B0,i);
    7802        4403 :     gel(Valpha,j) = gel(v,1);
    7803        4403 :     gel(M,j) = gel(v,2); j++;
    7804             :   }
    7805         889 :   setlg(Valpha, j);
    7806         889 :   setlg(B, j);
    7807         889 :   setlg(M, j); l = j;
    7808         889 :   if (l == 1) return mkvec3(gen_0, utoi(w), zerovec(n+1));
    7809         889 :   almin = shift_M(M, Valpha, w);
    7810         889 :   B = RgM_RgC_mul(M, B); l = lg(B);
    7811      138110 :   for (i = 1; i < l; i++)
    7812      137221 :     if (gexpo(gel(B,i)) < -bit) gel(B,i) = gen_0;
    7813         889 :   settyp(B, t_VEC);
    7814         889 :   if (E)
    7815             :   {
    7816          21 :     GEN v = hash_eisengacx(H, (void*)E, w, ga, n, prec);
    7817          21 :     long ell = 0;
    7818          21 :     almin = gsub(almin, gel(v,1));
    7819          21 :     if (gsigne(almin) < 0)
    7820             :     {
    7821           0 :       GEN gell = gceil(gmulsg(-w, almin));
    7822           0 :       ell = itos(gell);
    7823           0 :       almin = gadd(almin, gdivgs(gell, w));
    7824           0 :       if (nw < ell) pari_err_IMPL("alpha < 0 in mfgaexpansion");
    7825             :     }
    7826          21 :     B = vecslice(B, ell + 1, n + ell + 1);
    7827          21 :     B = RgV_div_RgXn(B, gel(v,2));
    7828             :   }
    7829         889 :   return mkvec3(almin, utoi(w), B);
    7830             : }
    7831             : 
    7832             : /* Theta multiplier: assume 4 | C, (C,D)=1 */
    7833             : static GEN
    7834         238 : mfthetamultiplier(long C, long D)
    7835             : {
    7836         238 :   long s = kross(C, D);
    7837         238 :   if ((D&3L) == 1) return stoi(s);
    7838          49 :   return s > 0 ? powIs(3) : gen_I();
    7839             : }
    7840             : static GEN
    7841         238 : mfthetaexpansion(GEN M, long n)
    7842             : {
    7843         238 :   GEN s, al, sla, V = zerovec(n + 1);
    7844         238 :   long w, lim, la, f, C = itos(gcoeff(M, 2, 1)), D = itos(gcoeff(M, 2, 2));
    7845         238 :   switch (C & 3L)
    7846             :   {
    7847          56 :     case 0: al = gen_0; w = 1;
    7848          56 :       s = mfthetamultiplier(C,D);
    7849          56 :       lim = usqrt(n); gel(V, 1) = s;
    7850          56 :       s = gmul2n(s, 1);
    7851          56 :       for (f = 1; f <= lim; f++) gel(V, f*f + 1) = s;
    7852          56 :       break;
    7853          84 :     case 2: al = sstoQ(1,4); w = 1;
    7854          84 :       s = gmul2n(mfthetamultiplier(C - 2*D, D), 1);
    7855          84 :       lim = (usqrt(n << 2) - 1) >> 1;
    7856          84 :       for (f = 0; f <= lim; f++) gel(V, f*(f+1) + 1) = s;
    7857          84 :       break;
    7858          98 :     default: al = gen_0; w = 4; la = (-D*C) & 3L;
    7859          98 :       s = mfthetamultiplier(-(D + la*C), C);
    7860          98 :       s = gsub(s, mulcxI(s));
    7861          98 :       sla = gmul(s, powIs(-la));
    7862          98 :       lim = usqrt(n); gel(V, 1) = gmul2n(s, -1);
    7863          98 :       for (f = 1; f <= lim; f++) gel(V, f*f + 1) = odd(f) ? sla : s;
    7864          98 :       break;
    7865             :   }
    7866         238 :   return mkvec3(al, stoi(w), V);
    7867             : }
    7868             : 
    7869             : /* F 1/2 integral weight */
    7870             : static GEN
    7871         238 : mf2gaexpansion(GEN mf2, GEN F, GEN ga, long n, long prec)
    7872             : {
    7873         238 :   GEN FT = mfmultheta(F), mf = obj_checkbuild(mf2, MF_MF2INIT, &mf2init);
    7874             :   GEN res, V1, Tres, V2, al, V, gsh;
    7875         238 :   long w2, C = itos(gcoeff(ga,2,1)), w = mfcuspcanon_width(MF_get_N(mf), C);
    7876         238 :   long ext = ((C & 3L) != 2)? 0: (w+3) >> 2;
    7877         238 :   long prec2 = prec + nbits2extraprec((long)M_PI/(2*M_LN2)*sqrt(n + ext));
    7878         238 :   res = mfgaexpansion(mf, FT, ga, n + ext, prec2);
    7879         238 :   Tres = mfthetaexpansion(ga, n + ext);
    7880         238 :   V1 = gel(res,3);
    7881         238 :   V2 = gel(Tres,3);
    7882         238 :   al = gsub(gel(res,1), gel(Tres,1));
    7883         238 :   w2 = itos(gel(Tres,2));
    7884         238 :   if (w != itos(gel(res,2)) || w % w2)
    7885           0 :     pari_err_BUG("mf2gaexpansion [incorrect w2 or w]");
    7886         238 :   if (w2 != w) V2 = bdexpand(V2, w/w2);
    7887         238 :   V = RgV_div_RgXn(V1, V2);
    7888         238 :   gsh = gfloor(gmulsg(w, al));
    7889         238 :   if (!gequal0(gsh))
    7890             :   {
    7891          28 :     al = gsub(al, gdivgs(gsh, w));
    7892          28 :     if (gsigne(gsh) > 0)
    7893             :     {
    7894           0 :       V = RgV_shift(V, gsh);
    7895           0 :       V = vecslice(V, 1, n + 1);
    7896             :     }
    7897             :     else
    7898             :     {
    7899          28 :       long sh = -itos(gsh), i;
    7900          28 :       if (sh > ext) pari_err_BUG("mf2gaexpansion [incorrect sh]");
    7901         119 :       for (i = 1; i <= sh; i++)
    7902          91 :         if (!gequal0(gel(V,i))) pari_err_BUG("mf2gaexpansion [sh too large]");
    7903          28 :       V = vecslice(V, sh+1, n + sh+1);
    7904             :     }
    7905             :   }
    7906         238 :   obj_free(mf); return mkvec3(al, stoi(w), gprec_wtrunc(V, prec));
    7907             : }
    7908             : 
    7909             : static GEN
    7910          70 : mfgaexpansionatkin(GEN mf, GEN F, GEN C, GEN D, long Q, long n, long prec)
    7911             : {
    7912          70 :   GEN mfa = mfatkininit_i(mf, Q, 0, prec), MQ = gel(mfa,2);
    7913          70 :   long i, FC, k = MF_get_k(mf);
    7914          70 :   GEN x, v, V, z, s, CHI = mfchartoprimitive(MF_get_CHI(mf), &FC);
    7915             : 
    7916             :   /* V = mfcoefs(F | w_Q, n), can't use mfatkin because MQ non-rational */
    7917          70 :   V = RgM_RgC_mul(mfcoefs_mf(mf,n,1), RgM_RgC_mul(MQ, mftobasis_i(mf,F)));
    7918          70 :   (void)bezout(utoipos(Q), C, &x, &v);
    7919          70 :   s = mfchareval_i(CHI, (umodiu(x, FC) * umodiu(D, FC)) % FC);
    7920          70 :   s = gdiv(s, gpow(utoipos(Q), sstoQ(k,2), prec));
    7921          70 :   V = RgV_Rg_mul(V, s);
    7922          70 :   z = rootsof1powinit(umodiu(D,Q)*umodiu(v,Q) % Q, Q, prec);
    7923          70 :   for (i = 1; i <= n+1; i++) gel(V,i) = gmul(gel(V,i), rootsof1pow(z, i-1));
    7924          70 :   return mkvec3(gen_0, utoipos(Q), V);
    7925             : }
    7926             : 
    7927             : /* allow F of the form [F, mf_eisendec(F)]~ */
    7928             : static GEN
    7929        1456 : mfgaexpansion(GEN mf, GEN F, GEN ga, long n, long prec)
    7930             : {
    7931        1456 :   GEN v, EF = NULL, res, Mvecj, c, d;
    7932             :   long precnew;
    7933             : 
    7934        1456 :   if (n < 0) pari_err_DOMAIN("mfgaexpansion", "n", "<", gen_0, stoi(n));
    7935        1456 :   if (typ(F) == t_COL && lg(F) == 3) { EF = gel(F,2); F = gel(F,1); }
    7936        1456 :   if (!checkmf_i(F)) pari_err_TYPE("mfgaexpansion", F);
    7937        1456 :   if (!check_SL2Z(ga)) pari_err_TYPE("mfgaexpansion",ga);
    7938        1456 :   if (typ(mf_get_gk(F)) != t_INT) return mf2gaexpansion(mf, F, ga, n, prec);
    7939        1218 :   c = gcoeff(ga,2,1);
    7940        1218 :   d = gcoeff(ga,2,2);
    7941        1218 :   if (!umodiu(c, mf_get_N(F)))
    7942             :   { /* trivial case: ga in Gamma_0(N) */
    7943         259 :     long N = MF_get_N(mf), w = mfcuspcanon_width(N, umodiu(c,N));
    7944         259 :     GEN CHI = mf_get_CHI(F);
    7945         259 :     GEN chid = mfcharcxeval(CHI, umodiu(d,mfcharmodulus(CHI)), prec);
    7946         259 :     v = mfcoefs_i(F, n/w, 1); if (!isint1(chid)) v = RgV_Rg_mul(v,chid);
    7947         259 :     return mkvec3(gen_0, stoi(w), bdexpandall(v,w,n+1));
    7948             :   }
    7949         959 :   mf = MF_set_new(mf);
    7950         959 :   if (MF_get_space(mf) == mf_NEW)
    7951             :   {
    7952         441 :     long N = MF_get_N(mf), cN = umodiu(c,N), g = ugcd(cN,N), Q = N/g;
    7953         441 :     GEN CHI = MF_get_CHI(mf);
    7954         441 :     if (ugcd(cN, Q)==1 && mfcharorder(CHI) <= 2
    7955         217 :                        && g % mfcharconductor(CHI) == 0
    7956         112 :                        && degpol(mf_get_field(F)) == 1)
    7957          70 :       return mfgaexpansionatkin(mf, F, c, d, Q, n, prec);
    7958             :   }
    7959         889 :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    7960         889 :   precnew = (lg(Mvecj) < 5)? prec + nbits2extraprec(n >> 1): prec;
    7961         889 :   if (!EF) EF = mf_eisendec(mf, F, precnew);
    7962         889 :   res = mfgaexpansion_i(mf, EF, ga, n, precnew);
    7963         889 :   return precnew == prec ? res : gprec_wtrunc(res, prec);
    7964             : }
    7965             : 
    7966             : /* parity = -1 or +1 */
    7967             : static GEN
    7968         217 : findd(long N, long parity)
    7969             : {
    7970         217 :   GEN L, D = mydivisorsu(N);
    7971         217 :   long i, j, l = lg(D);
    7972         217 :   L = cgetg(l, t_VEC);
    7973        1218 :   for (i = j = 1; i < l; i++)
    7974             :   {
    7975        1001 :     long d = D[i];
    7976        1001 :     if (parity == -1) d = -d;
    7977        1001 :     if (sisfundamental(d)) gel(L,j++) = stoi(d);
    7978             :   }
    7979         217 :   setlg(L,j); return L;
    7980             : }
    7981             : /* does ND contain a divisor of N ? */
    7982             : static int
    7983         413 : seenD(long N, GEN ND)
    7984             : {
    7985         413 :   long j, l = lg(ND);
    7986         427 :   for (j = 1; j < l; j++)
    7987          14 :     if (N % ND[j] == 0) return 1;
    7988         413 :   return 0;
    7989             : }
    7990             : static GEN
    7991          42 : search_levels(GEN vN, const char *f)
    7992             : {
    7993          42 :   switch(typ(vN))
    7994             :   {
    7995           7 :     case t_INT: vN = mkvecsmall(itos(vN)); break;
    7996          35 :     case t_VEC: case t_COL: vN = ZV_to_zv(vN); break;
    7997           0 :     case t_VECSMALL: vN = leafcopy(vN); break;
    7998           0 :     default: pari_err_TYPE(f, vN);
    7999             :   }
    8000          42 :   vecsmall_sort(vN); return vN;
    8001             : }
    8002             : GEN
    8003          14 : mfsearch(GEN NK, GEN V, long space)
    8004             : {
    8005          14 :   pari_sp av = avma;
    8006             :   GEN F, gk, NbyD, vN;
    8007             :   long n, nk, dk, parity, nV, i, lvN;
    8008             : 
    8009          14 :   if (typ(NK) != t_VEC || lg(NK) != 3) pari_err_TYPE("mfsearch", NK);
    8010          14 :   gk = gel(NK,2);
    8011          14 :   if (typ(gmul2n(gk, 1)) != t_INT) pari_err_TYPE("mfsearch [k]", gk);
    8012          14 :   switch(typ(V))
    8013             :   {
    8014          14 :     case t_VEC: V = shallowtrans(V);
    8015          14 :     case t_COL: break;
    8016           0 :     default: pari_err_TYPE("mfsearch [V]", V);
    8017             :   }
    8018          14 :   vN = search_levels(gel(NK,1), "mfsearch [N]");
    8019          14 :   lvN = lg(vN);
    8020             : 
    8021          14 :   Qtoss(gk, &nk,&dk);
    8022          14 :   parity = (dk == 1 && odd(nk)) ? -1 : 1;
    8023          14 :   nV = lg(V)-2;
    8024          14 :   F = cgetg(1, t_VEC);
    8025          14 :   NbyD = const_vec(vN[lvN-1], cgetg(1,t_VECSMALL));
    8026         231 :   for (n = 1; n < lvN; n++)
    8027             :   {
    8028         217 :     long N = vN[n];
    8029             :     GEN L;
    8030         217 :     if (N <= 0 || (dk == 2 && (N & 3))) continue;
    8031         217 :     L = findd(N, parity);
    8032         630 :     for (i = 1; i < lg(L); i++)
    8033             :     {
    8034         413 :       GEN mf, M, CO, gD = gel(L,i);
    8035         413 :       GEN *ND = (GEN*)NbyD + itou(gD); /* points to NbyD[|D|] */
    8036             : 
    8037         413 :       if (seenD(N, *ND)) continue;
    8038         413 :       mf = mfinit_Nndkchi(N, nk, dk, get_mfchar(gD), space, 1);
    8039         413 :       M = mfcoefs_mf(mf, nV, 1);
    8040         413 :       CO = inverseimage(M, V); if (lg(CO) == 1) continue;
    8041             : 
    8042          42 :       F = vec_append(F, mflinear(mf,CO));
    8043          42 :       *ND = vecsmall_append(*ND, N); /* add to NbyD[|D|] */
    8044             :     }
    8045             :   }
    8046          14 :   return gerepilecopy(av, F);
    8047             : }
    8048             : 
    8049             : static GEN
    8050         882 : search_from_split(GEN mf, GEN vap, GEN vlp)
    8051             : {
    8052         882 :   pari_sp av = avma;
    8053         882 :   long lvlp = lg(vlp), j, jv, l1;
    8054         882 :   GEN v, NK, S1, S, M = NULL;
    8055             : 
    8056         882 :   S1 = gel(split_i(mf, 1, 0), 1); /* rational newforms */
    8057         882 :   l1 = lg(S1);
    8058         882 :   if (l1 == 1) { avma = av; return NULL; }
    8059         448 :   v = cgetg(l1, t_VEC);
    8060         448 :   S = MF_get_S(mf);
    8061         448 :   NK = mf_get_NK(gel(S,1));
    8062         448 :   if (lvlp > 1) M = rowpermute(mfcoefs_mf(mf, vlp[lvlp-1], 1), vlp);
    8063         966 :   for (j = jv = 1; j < l1; j++)
    8064             :   {
    8065         518 :     GEN vF = gel(S1,j);
    8066             :     long t;
    8067         651 :     for (t = lvlp-1; t > 0; t--)
    8068             :     { /* lhs = vlp[j]-th coefficient of eigenform */
    8069         595 :       GEN rhs = gel(vap,t), lhs = RgMrow_RgC_mul(M, vF, t);
    8070         595 :       if (!gequal(lhs, rhs)) break;
    8071             :     }
    8072         518 :     if (!t) gel(v,jv++) = mflinear_i(NK,S,vF);
    8073             :   }
    8074         448 :   if (jv == 1) { avma = av; return NULL; }
    8075          56 :   setlg(v,jv); return v;
    8076             : }
    8077             : GEN
    8078          28 : mfeigensearch(GEN NK, GEN AP)
    8079             : {
    8080          28 :   pari_sp av = avma;
    8081          28 :   GEN k, vN, vap, vlp, vres = cgetg(1, t_VEC), D;
    8082             :   long n, lvN, i, l, even;
    8083             : 
    8084          28 :   if (!AP) l = 1;
    8085             :   else
    8086             :   {
    8087          28 :     l = lg(AP);
    8088          28 :     if (typ(AP) != t_VEC) pari_err_TYPE("mfeigensearch",AP);
    8089             :   }
    8090          28 :   vap = cgetg(l, t_VEC);
    8091          28 :   vlp = cgetg(l, t_VECSMALL);
    8092          28 :   if (l > 1)
    8093             :   {
    8094          28 :     GEN perm = indexvecsort(AP, mkvecsmall(1));
    8095          77 :     for (i = 1; i < l; i++)
    8096             :     {
    8097          49 :       GEN v = gel(AP,perm[i]), gp, ap;
    8098          49 :       if (typ(v) != t_VEC || lg(v) != 3) pari_err_TYPE("mfeigensearch", AP);
    8099          49 :       gp = gel(v,1);
    8100          49 :       ap = gel(v,2);
    8101          49 :       if (typ(gp) != t_INT || (typ(ap) != t_INT && typ(ap) != t_INTMOD))
    8102           0 :         pari_err_TYPE("mfeigensearch", AP);
    8103          49 :       gel(vap,i) = ap;
    8104          49 :       vlp[i] = itos(gp)+1; if (vlp[i] < 0) pari_err_TYPE("mfeigensearch", AP);
    8105             :     }
    8106             :   }
    8107          28 :   l = lg(NK);
    8108          28 :   if (typ(NK) != t_VEC || l != 3) pari_err_TYPE("mfeigensearch",NK);
    8109          28 :   k = gel(NK,2);
    8110          28 :   vN = search_levels(gel(NK,1), "mfeigensearch [N]");
    8111          28 :   lvN = lg(vN);
    8112          28 :   vecsmall_sort(vlp);
    8113          28 :   even = !mpodd(k);
    8114         966 :   for (n = 1; n < lvN; n++)
    8115             :   {
    8116         938 :     pari_sp av2 = avma;
    8117             :     GEN mf, L;
    8118         938 :     long N = vN[n];
    8119         938 :     if (even) D = gen_1;
    8120             :     else
    8121             :     {
    8122         112 :       long r = (N&3L);
    8123         112 :       if (r == 1 || r == 2) continue;
    8124          56 :       D = stoi( corediscs(-N, NULL) ); /* < 0 */
    8125             :     }
    8126         882 :     mf = mfinit_i(mkvec3(utoipos(N), k, D), mf_NEW);
    8127         882 :     L = search_from_split(mf, vap, vlp);
    8128         882 :     if (L) vres = shallowconcat(vres, L); else avma = av2;
    8129             :   }
    8130          28 :   return gerepilecopy(av, vres);
    8131             : }
    8132             : 
    8133             : /* tf_{N,k}(n) */
    8134             : static GEN
    8135     2201416 : mfnewtracecache(long N, long k, long n, cachenew_t *cache)
    8136             : {
    8137     2201416 :   GEN C = NULL, S;
    8138             :   long lcache;
    8139     2201416 :   if (!n) return gen_0;
    8140     2123688 :   S = gel(cache->vnew,N);
    8141     2123688 :   lcache = lg(S);
    8142     2123688 :   if (n < lcache) C = gel(S, n);
    8143     2123688 :   if (C) cache->newHIT++;
    8144     1277668 :   else C = mfnewtrace_i(N,k,n,cache);
    8145     2123688 :   cache->newTOTAL++;
    8146     2123688 :   if (n < lcache) gel(S,n) = C;
    8147     2123688 :   return C;
    8148             : }
    8149             : 
    8150             : static long
    8151        1386 : mfdim_Nkchi(long N, long k, GEN CHI, long space)
    8152             : {
    8153        1386 :   if (k < 0 || badchar(N,k,CHI)) return 0;
    8154        1085 :   if (k == 0)
    8155          35 :     return mfcharistrivial(CHI) && !space_is_cusp(space)? 1: 0;
    8156        1050 :   switch(space)
    8157             :   {
    8158         238 :     case mf_NEW: return mfnewdim(N,k,CHI);
    8159         196 :     case mf_CUSP:return mfcuspdim(N,k,CHI);
    8160         168 :     case mf_OLD: return mfolddim(N,k,CHI);
    8161         217 :     case mf_FULL:return mffulldim(N,k,CHI);
    8162         231 :     case mf_EISEN: return mfeisensteindim(N,k,CHI);
    8163           0 :     default: pari_err_FLAG("mfdim");
    8164             :   }
    8165             :   return 0;/*LCOV_EXCL_LINE*/
    8166             : }
    8167             : static long
    8168        2114 : mfwt1dimsum(long N, long space)
    8169             : {
    8170        2114 :   switch(space)
    8171             :   {
    8172        1050 :     case mf_NEW:  return mfwt1newdimsum(N);
    8173        1057 :     case mf_CUSP: return mfwt1cuspdimsum(N);
    8174           7 :     case mf_OLD:  return mfwt1olddimsum(N);
    8175             :   }
    8176           0 :   pari_err_FLAG("mfdim");
    8177             :   return 0; /*LCOV_EXCL_LINE*/
    8178             : }
    8179             : /* mfdim for k = nk/dk */
    8180             : static long
    8181       44744 : mfdim_Nndkchi(long N, long nk, long dk, GEN CHI, long space)
    8182       43463 : { return (dk == 2)? mf2dim_Nkchi(N, nk >> 1, CHI, space)
    8183       88207 :                   : mfdim_Nkchi(N, nk, CHI, space); }
    8184             : /* FIXME: use direct dim Gamma1(N) formula, don't compute individual spaces */
    8185             : static long
    8186         252 : mfwtkdimsum(long N, long k, long dk, long space)
    8187             : {
    8188         252 :   GEN w = mfchars(N, k, dk, NULL);
    8189         252 :   long i, j, D = 0, l = lg(w);
    8190        1239 :   for (i = j = 1; i < l; i++)
    8191             :   {
    8192         987 :     GEN CHI = gel(w,i);
    8193         987 :     long d = mfdim_Nndkchi(N,k,dk,CHI,space);
    8194         987 :     if (d) D += d * myeulerphiu(mfcharorder(CHI));
    8195             :   }
    8196         252 :   return D;
    8197             : }
    8198             : static GEN
    8199         105 : mfwt1dims(long N, GEN vCHI, long space)
    8200             : {
    8201         105 :   GEN D = NULL;
    8202         105 :   switch(space)
    8203             :   {
    8204          56 :     case mf_NEW: D = mfwt1newdimall(N, vCHI); break;
    8205          21 :     case mf_CUSP:D = mfwt1cuspdimall(N, vCHI); break;
    8206          28 :     case mf_OLD: D = mfwt1olddimall(N, vCHI); break;
    8207           0 :     default: pari_err_FLAG("mfdim");
    8208             :   }
    8209         105 :   return D;
    8210             : }
    8211             : static GEN
    8212        2961 : mfwtkdims(long N, long k, long dk, GEN vCHI, long space)
    8213             : {
    8214        2961 :   GEN D, w = mfchars(N, k, dk, vCHI);
    8215        2961 :   long i, j, l = lg(w);
    8216        2961 :   D = cgetg(l, t_VEC);
    8217       46592 :   for (i = j = 1; i < l; i++)
    8218             :   {
    8219       43631 :     GEN CHI = gel(w,i);
    8220       43631 :     long d = mfdim_Nndkchi(N,k,dk,CHI,space);
    8221       43631 :     if (vCHI)
    8222         574 :       gel(D, j++) = mkvec2s(d, 0);
    8223       43057 :     else if (d)
    8224        2520 :       gel(D, j++) = fmt_dim(CHI, d, 0);
    8225             :   }
    8226        2961 :   setlg(D,j); return D;
    8227             : }
    8228             : GEN
    8229        5719 : mfdim(GEN NK, long space)
    8230             : {
    8231        5719 :   pari_sp av = avma;
    8232             :   long N, k, dk, joker;
    8233             :   GEN CHI, mf;
    8234        5719 :   if ((mf = checkMF_i(NK))) return utoi(MF_get_dim(mf));
    8235        5586 :   checkNK2(NK, &N, &k, &dk, &CHI, 2);
    8236        5586 :   if (!CHI) joker = 1;
    8237             :   else
    8238        2611 :     switch(typ(CHI))
    8239             :     {
    8240        2373 :       case t_INT: joker = 2; break;
    8241         112 :       case t_COL: joker = 3; break;
    8242         126 :       default: joker = 0; break;
    8243             :     }
    8244        5586 :   if (joker)
    8245             :   {
    8246             :     long d;
    8247             :     GEN D;
    8248        5460 :     if (k < 0) switch(joker)
    8249             :     {
    8250           0 :       case 1: return cgetg(1,t_VEC);
    8251           7 :       case 2: return gen_0;
    8252           0 :       case 3: return mfdim0all(CHI);
    8253             :     }
    8254        5453 :     if (k == 0)
    8255             :     {
    8256          28 :       if (space_is_cusp(space)) switch(joker)
    8257             :       {
    8258           7 :         case 1: return cgetg(1,t_VEC);
    8259           0 :         case 2: return gen_0;
    8260           7 :         case 3: return mfdim0all(CHI);
    8261             :       }
    8262          14 :       switch(joker)
    8263             :       {
    8264             :         long i, l;
    8265           7 :         case 1: retmkvec(fmt_dim(mfchartrivial(),0,0));
    8266           0 :         case 2: return gen_1;
    8267           7 :         case 3: l = lg(CHI); D = cgetg(l,t_VEC);
    8268          35 :                 for (i = 1; i < l; i++)
    8269             :                 {
    8270          28 :                   long t = mfcharistrivial(gel(CHI,i));
    8271          28 :                   gel(D,i) = mkvec2(t? gen_1: gen_0, gen_0);
    8272             :                 }
    8273           7 :                 return D;
    8274             :       }
    8275             :     }
    8276        5425 :     if (dk == 1 && k == 1 && space != mf_EISEN)
    8277         105 :     {
    8278        2219 :       long fix = 0, space0 = space;
    8279        2219 :       if (space == mf_FULL) space = mf_CUSP; /* remove Eisenstein part */
    8280        2219 :       if (joker == 2)
    8281             :       {
    8282        2114 :         d = mfwt1dimsum(N, space);
    8283        2114 :         if (space0 == mf_FULL) d += mfwtkdimsum(N,k,dk,mf_EISEN);/*add it back*/
    8284        2114 :         avma = av; return utoi(d);
    8285             :       }
    8286             :       /* must initialize explicitly: trivial spaces for E_k/S_k differ */
    8287         105 :       if (space0 == mf_FULL)
    8288             :       {
    8289           7 :         if (!CHI) fix = 1; /* must remove 0 spaces */
    8290           7 :         CHI = mfchars(N, k, dk, CHI);
    8291             :       }
    8292         105 :       D = mfwt1dims(N, CHI, space);
    8293         105 :       if (space0 == mf_FULL)
    8294             :       {
    8295           7 :         GEN D2 = mfwtkdims(N, k, dk, CHI, mf_EISEN);
    8296           7 :         D = merge_dims(D, D2, fix? CHI: NULL);
    8297             :       }
    8298             :     }
    8299             :     else
    8300             :     {
    8301        3206 :       if (joker==2) { d = mfwtkdimsum(N,k,dk,space); avma=av; return utoi(d); }
    8302        2954 :       D = mfwtkdims(N, k, dk, CHI, space);
    8303             :     }
    8304        3059 :     if (!CHI) return gerepileupto(av, vecsort(D, mkvecsmall(1)));
    8305         105 :     return gerepilecopy(av, D);
    8306             :   }
    8307         126 :   return utoi( mfdim_Nndkchi(N, k, dk, CHI, space) );
    8308             : }
    8309             : 
    8310             : GEN
    8311         308 : mfbasis(GEN NK, long space)
    8312             : {
    8313         308 :   pari_sp av = avma;
    8314             :   long N, k, dk;
    8315             :   GEN mf, CHI;
    8316         308 :   if ((mf = checkMF_i(NK))) return concat(gel(mf,2), gel(mf,3));
    8317           7 :   checkNK2(NK, &N, &k, &dk, &CHI, 0);
    8318           7 :   if (dk == 2) return gerepilecopy(av, mf2basis(N, k>>1, CHI, space));
    8319           7 :   mf = mfinit_Nkchi(N, k, CHI, space, 1);
    8320           7 :   return gerepilecopy(av, MF_get_basis(mf));
    8321             : }
    8322             : 
    8323             : static GEN
    8324          28 : deg1ser_shallow(GEN a1, GEN a0, long v, long e)
    8325          28 : { return RgX_to_ser(deg1pol_shallow(a1, a0, v), e+2); }
    8326             : /* r / x + O(1) */
    8327             : static GEN
    8328          28 : simple_pole(GEN r)
    8329             : {
    8330          28 :   GEN S = deg1ser_shallow(gen_0, r, 0, 1);
    8331          28 :   setvalp(S, -1); return S;
    8332             : }
    8333             : 
    8334             : /* F form, E embedding; mfa = mfatkininit or root number (eigenform case) */
    8335             : static GEN
    8336         105 : mflfuncreate(GEN mfa, GEN F, GEN E, GEN N, GEN gk)
    8337             : {
    8338         105 :   GEN LF = cgetg(8,t_VEC), polar = cgetg(1,t_COL), eps;
    8339         105 :   long k = itou(gk);
    8340         105 :   gel(LF,1) = lfuntag(t_LFUN_MFCLOS, mkvec3(F,E,gen_1));
    8341         105 :   if (typ(mfa) != t_VEC)
    8342          84 :     eps = mfa; /* cuspidal eigenform: root number; no poles */
    8343             :   else
    8344             :   { /* mfatkininit */
    8345          21 :     GEN a0, b0, vF, vG, G = NULL, M = gdiv(gel(mfa,2), gel(mfa,3)), mf = gel(mfa,4);
    8346          21 :     vF = mftobasis_i(mf, F);
    8347          21 :     vG = RgM_RgC_mul(M, vF);
    8348          21 :     if (gequal(vF,vG)) eps = gen_1;
    8349           7 :     else if (gequal(vF,gneg(vG))) eps = gen_m1;
    8350             :     else
    8351             :     { /* not self-dual */
    8352           7 :       eps = NULL;
    8353           7 :       G = mfatkin(mfa, F);
    8354           7 :       gel(LF,2) = lfuntag(t_LFUN_MFCLOS, mkvec3(G,E,ginv(gel(mfa,3))));
    8355           7 :       gel(LF,6) = powIs(k);
    8356             :     }
    8357             :     /* polar part */
    8358          21 :     a0 = mfcoef(F,0);
    8359          21 :     b0 = eps? gmul(eps,a0): mfcoef(G,0);
    8360          21 :     if (!gequal0(b0))
    8361             :     {
    8362          14 :       b0 = mulcxpowIs(gmul2n(b0,1), k);
    8363          14 :       polar = vec_append(polar, mkvec2(gk, simple_pole(b0)));
    8364             :     }
    8365          21 :     if (!gequal0(a0))
    8366             :     {
    8367          14 :       a0 = gneg(gmul2n(a0,1));
    8368          14 :       polar = vec_append(polar, mkvec2(gen_0, simple_pole(a0)));
    8369             :     }
    8370             :   }
    8371         105 :   if (eps) /* self-dual */
    8372             :   {
    8373          98 :     gel(LF,2) = mfcharorder(mf_get_CHI(F)) <= 2? gen_0: gen_1;
    8374          98 :     gel(LF,6) = mulcxpowIs(eps,k);
    8375             :   }
    8376         105 :   gel(LF,3) = mkvec2(gen_0, gen_1);
    8377         105 :   gel(LF,4) = gk;
    8378         105 :   gel(LF,5) = N;
    8379         105 :   if (lg(polar) == 1) setlg(LF,7); else gel(LF,7) = polar;
    8380         105 :   return LF;
    8381             : }
    8382             : static GEN
    8383          91 : mflfuncreateall(long sd, GEN mfa, GEN F, GEN vE, GEN gN, GEN gk)
    8384             : {
    8385          91 :   long i, l = lg(vE);
    8386          91 :   GEN L = cgetg(l, t_VEC);
    8387         196 :   for (i = 1; i < l; i++)
    8388         105 :     gel(L,i) = mflfuncreate(sd? gel(mfa,i): mfa, F, gel(vE,i), gN, gk);
    8389          91 :   return L;
    8390             : }
    8391             : GEN
    8392          42 : lfunmf(GEN mf, GEN F, long bitprec)
    8393             : {
    8394          42 :   pari_sp av = avma;
    8395          42 :   long i, l, prec = nbits2prec(bitprec);
    8396             :   GEN L, gk, gN;
    8397          42 :   mf = checkMF(mf);
    8398          42 :   gk = MF_get_gk(mf);
    8399          42 :   gN = MF_get_gN(mf);
    8400          42 :   if (typ(gk)!=t_INT) pari_err_IMPL("half-integral weight");
    8401          42 :   if (F)
    8402             :   {
    8403             :     GEN v;
    8404          35 :     long s = MF_get_space(mf);
    8405          35 :     if (!checkmf_i(F)) pari_err_TYPE("lfunmf", F);
    8406          35 :     if (!mfisinspace_i(mf, F)) err_space(F);
    8407          35 :     L = NULL;
    8408          35 :     if ((s == mf_NEW || s == mf_CUSP || s == mf_FULL)
    8409          21 :         && gequal(mfcoefs_i(F,1,1), mkvec2(gen_0,gen_1)))
    8410             :     { /* check if eigenform */
    8411          14 :       GEN vP, vF, b = mftobasis_i(mf, F);
    8412          14 :       long lF, d = degpol(mf_get_field(F));
    8413          14 :       v = mfsplit(mf, d, 0);
    8414          14 :       vF = gel(v,1);
    8415          14 :       vP = gel(v,2); lF = lg(vF);
    8416          14 :       for (i = 1; i < lF; i++)
    8417          14 :         if (degpol(gel(vP,i)) == d && gequal(gel(vF,i), b))
    8418             :         {
    8419          14 :           GEN vE = mfgetembed(F, prec);
    8420          14 :           GEN Z = mffrickeeigen_i(mf, mkvec(b), mkvec(vE), prec);
    8421          14 :           L = mflfuncreateall(1, gel(Z,1), F, vE, gN, gk);
    8422          14 :           break;
    8423             :         }
    8424             :     }
    8425          35 :     if (!L)
    8426             :     { /* not an eigenform: costly general case */
    8427          21 :       GEN mfa = mfatkininit_i(mf, itou(gN), 1, prec);
    8428          21 :       L = mflfuncreateall(0,mfa, F, mfgetembed(F,prec), gN, gk);
    8429             :     }
    8430          35 :     if (lg(L) == 2) L = gel(L,1);
    8431             :   }
    8432             :   else
    8433             :   {
    8434           7 :     GEN M = mfeigenbasis(mf), vE = mfeigenembed(mf, prec);
    8435           7 :     GEN v = mffrickeeigen(mf, vE, prec);
    8436           7 :     l = lg(vE); L = cgetg(l, t_VEC);
    8437          63 :     for (i = 1; i < l; i++)
    8438          56 :       gel(L,i) = mflfuncreateall(1,gel(v,i), gel(M,i), gel(vE,i), gN, gk);
    8439             :   }
    8440          42 :   return gerepilecopy(av, L);
    8441             : }
    8442             : 
    8443             : GEN
    8444          21 : mffromell(GEN E)
    8445             : {
    8446          21 :   pari_sp av = avma;
    8447             :   GEN mf, F, z, v, S;
    8448             :   long N, i, l;
    8449             : 
    8450          21 :   checkell(E);
    8451          21 :   if (ell_get_type(E) != t_ELL_Q) pari_err_TYPE("mfffromell [E not over Q]", E);
    8452          21 :   N = itos(ellQ_get_N(E));
    8453          21 :   mf = mfinit_i(mkvec2(utoi(N), gen_2), mf_NEW);
    8454          21 :   v = split_i(mf, 1, 0);
    8455          21 :   S = gel(v,1); l = lg(S); /* rational newforms */
    8456          21 :   F = tag(t_MF_ELL, mkNK(N,2,mfchartrivial()), E);
    8457          21 :   z = mftobasis_i(mf, F);
    8458          21 :   for(i = 1; i < l; i++)
    8459          21 :     if (gequal(z, gel(S,i))) break;
    8460          21 :   if (i == l) pari_err_BUG("mffromell [E is not modular]");
    8461          21 :   return gerepilecopy(av, mkvec3(mf, F, z));
    8462             : }
    8463             : 
    8464             : /* returns -1 if not, degree otherwise */
    8465             : long
    8466          98 : polishomogeneous(GEN P)
    8467             : {
    8468             :   long i, D, l;
    8469          98 :   if (typ(P) != t_POL) return 0;
    8470          49 :   D = -1; l = lg(P);
    8471         231 :   for (i = 2; i < l; i++)
    8472             :   {
    8473         182 :     GEN c = gel(P,i);
    8474             :     long d;
    8475         182 :     if (gequal0(c)) continue;
    8476          84 :     d = polishomogeneous(c);
    8477          84 :     if (d < 0) return -1;
    8478          84 :     if (D < 0) D = d + i-2; else if (D != d + i-2) return -1;
    8479             :   }
    8480          49 :   return D;
    8481             : }
    8482             : 
    8483             : /* P a t_POL, 1 if spherical, 0 otherwise */
    8484             : static long
    8485          14 : RgX_isspherical(GEN Qi, GEN P)
    8486             : {
    8487          14 :   pari_sp av = avma;
    8488             :   GEN va, S;
    8489             :   long lva, i, j, r;
    8490          14 :   if (degpol(P) <= 1) return 1;
    8491          14 :   va = variables_vecsmall(P); lva = lg(va);
    8492          14 :   if (lva > lg(Qi)) pari_err(e_MISC, "too many variables in mffromqf");
    8493          14 :   S = gen_0;
    8494          42 :   for (j = 1; j < lva; j++)
    8495             :   {
    8496          28 :     GEN col = gel(Qi, j), Pj = deriv(P, va[j]);
    8497          70 :     for (i = 1; i <= j; i++)
    8498             :     {
    8499          42 :       GEN coe = gel(col, i);
    8500          42 :       if (i != j) coe = gmul2n(coe, 1);
    8501          42 :       if (!gequal0(coe)) S = gadd(S, gmul(coe, deriv(Pj, va[i])));
    8502             :     }
    8503             :   }
    8504          14 :   r = gequal0(S); avma = av; return r;
    8505             : }
    8506             : 
    8507             : static GEN
    8508          28 : c_QFsimple_i(long n, GEN Q, GEN P)
    8509             : {
    8510          28 :   pari_sp av = avma;
    8511          28 :   GEN V, v = qfrep0(Q, utoi(n), 1);
    8512          28 :   long i, l = lg(v);
    8513          28 :   V = cgetg(l+1, t_VEC);
    8514          49 :   if (!P || equali1(P))
    8515             :   {
    8516          21 :     gel(V,1) = gen_1;
    8517          21 :     for (i = 2; i <= l; i++) gel(V,i) = utoi(v[i-1] << 1);
    8518             :   }
    8519             :   else
    8520             :   {
    8521           7 :     gel(V,1) = gcopy(P);
    8522           7 :     for (i = 2; i <= l; i++) gel(V,i) = gmulgs(P, v[i-1] << 1);
    8523             :   }
    8524          28 :   return gerepileupto(av, V);
    8525             : }
    8526             : static GEN
    8527          35 : c_QF_i(long n, GEN Q, GEN P)
    8528             : {
    8529          35 :   pari_sp av = avma;
    8530             :   GEN V, v, va;
    8531             :   long i, lva, lq, l;
    8532          35 :   if (!P || typ(P) != t_POL) return c_QFsimple_i(n, Q, P);
    8533           7 :   v = gel(minim(Q, utoi(2*n), NULL), 3);
    8534           7 :   va = variables_vec(P); lq = lg(Q) - 1; lva = lg(va) - 1;
    8535           7 :   V = zerovec(n + 1); l = lg(v);
    8536          35 :   for (i = 1; i < l; i++)
    8537             :   {
    8538          28 :     GEN X = gel(v,i);
    8539          28 :     long ind = (itos(qfeval0(Q, X, NULL)) >> 1) + 1;
    8540          28 :     if (lq > lva) X = vecslice(X, 1, lva);
    8541          28 :     gel(V, ind) = gadd(gel(V, ind), gsubstvec(P, va, X));
    8542             :   }
    8543           7 :   return gerepilecopy(av, gmul2n(V, 1));
    8544             : }
    8545             : 
    8546             : GEN
    8547          42 : mffromqf(GEN Q, GEN P)
    8548             : {
    8549          42 :   pari_sp av = avma;
    8550             :   GEN G, Qi, F, D, N, mf, v, gk, gwt, chi;
    8551             :   long m, d, space;
    8552          42 :   if (typ(Q) != t_MAT) pari_err_TYPE("mffromqf", Q);
    8553          42 :   if (!RgM_is_ZM(Q) || !qf_iseven(Q))
    8554           0 :     pari_err_TYPE("mffromqf [not integral or even]", Q);
    8555          42 :   m = lg(Q)-1;
    8556          42 :   gk = sstoQ(m, 2);
    8557          42 :   Qi = ZM_inv(Q, &N);
    8558          42 :   if (!qf_iseven(Qi)) N = shifti(N, 1);
    8559          42 :   d = 0;
    8560          42 :   if (!P || gequal1(P)) P = NULL;
    8561             :   else
    8562             :   {
    8563          21 :     P = simplify_shallow(P);
    8564          21 :     if (typ(P) == t_POL)
    8565             :     {
    8566          14 :       d = polishomogeneous(P);
    8567          14 :       if (d < 0) pari_err_TYPE("mffromqf [not homogeneous t_POL]", P);
    8568          14 :       if (!RgX_isspherical(Qi, P))
    8569           0 :         pari_err_TYPE("mffromqf [not a spherical t_POL]", P);
    8570             :     }
    8571             :   }
    8572          42 :   D = ZM_det(Q);
    8573          42 :   if (typ(gk) == t_INT) { if (mpodd(gk)) D = negi(D); } else D = shifti(D, 1);
    8574          42 :   space = d > 0 ? mf_CUSP : mf_FULL;
    8575          42 :   G = znstar0(N,1);
    8576          42 :   chi = mkvec2(G, znchar_quad(G,D));
    8577          42 :   gwt = gaddgs(gk, d);
    8578          42 :   mf = mfinit(mkvec3(N, gwt, chi), space);
    8579          42 :   if (odd(d))
    8580             :   {
    8581           7 :     F = mftrivial();
    8582           7 :     v = zerocol(MF_get_dim(mf));
    8583             :   }
    8584             :   else
    8585             :   {
    8586          35 :     F = c_QF_i(mfsturm(mf), Q, P);
    8587          35 :     v = mftobasis_i(mf, F);
    8588          35 :     F = mflinear(mf, v);
    8589             :   }
    8590          42 :   return gerepilecopy(av, mkvec3(mf, F, v));
    8591             : }
    8592             : 
    8593             : /***********************************************************************/
    8594             : /*                          Eisenstein Series                          */
    8595             : /***********************************************************************/
    8596             : /* \sigma_{k-1}(\chi,n) */
    8597             : static GEN
    8598       22162 : sigchi(long k, GEN CHI, long n)
    8599             : {
    8600       22162 :   pari_sp av = avma;
    8601       22162 :   GEN S = gen_1, D = mydivisorsu(u_ppo(n,mfcharmodulus(CHI)));
    8602       22162 :   long i, l = lg(D), ord = mfcharorder(CHI), vt = varn(mfcharpol(CHI));
    8603       77994 :   for (i = 2; i < l; i++) /* skip D[1] = 1 */
    8604             :   {
    8605       55832 :     long d = D[i], a = mfcharevalord(CHI, d, ord);
    8606       55832 :     S = gadd(S, mygmodulo_lift(a, ord, powuu(d, k-1), vt));
    8607             :   }
    8608       22162 :   return gerepileupto(av,S);
    8609             : }
    8610             : 
    8611             : /* write n = n0*n1*n2, (n0,N1*N2) = 1, n1 | N1^oo, n2 | N2^oo;
    8612             :  * return NULL if (n,N1,N2) > 1, else return factoru(n0) */
    8613             : static GEN
    8614      198828 : sigchi2_dec(long n, long N1, long N2, long *pn1, long *pn2)
    8615             : {
    8616      198828 :   GEN P0, E0, P, E, fa = myfactoru(n);
    8617             :   long i, j, l;
    8618      198828 :   *pn1 = 1;
    8619      198828 :   *pn2 = 1;
    8620      198828 :   if (N1 == 1 && N2 == 1) return fa;
    8621      185430 :   P = gel(fa,1); l = lg(P);
    8622      185430 :   E = gel(fa,2);
    8623      185430 :   P0 = cgetg(l, t_VECSMALL);
    8624      185430 :   E0 = cgetg(l, t_VECSMALL);
    8625      402507 :   for (i = j = 1; i < l; i++)
    8626             :   {
    8627      239022 :     long p = P[i], e = E[i];
    8628      239022 :     if (N1 % p == 0)
    8629             :     {
    8630       37205 :       if (N2 % p == 0) return NULL;
    8631       15260 :       *pn1 *= upowuu(p,e);
    8632             :     }
    8633      201817 :     else if (N2 % p == 0)
    8634       39872 :       *pn2 *= upowuu(p,e);
    8635      161945 :     else { P0[j] = p; E0[j] = e; j++; }
    8636             :   }
    8637      163485 :   setlg(P0, j);
    8638      163485 :   setlg(E0, j); return mkvec2(P0,E0);
    8639             : }
    8640             : 
    8641             : /* sigma_{k-1}(\chi_1,\chi_2,n), ord multiple of lcm(ord(CHI1),ord(CHI2)) */
    8642             : static GEN
    8643      150871 : sigchi2(long k, GEN CHI1, GEN CHI2, long n, long ord)
    8644             : {
    8645      150871 :   pari_sp av = avma;
    8646      150871 :   GEN S = gen_0, D;
    8647      150871 :   long i, l, n1, n2, vt, N1 = mfcharmodulus(CHI1), N2 = mfcharmodulus(CHI2);
    8648      150871 :   D = sigchi2_dec(n, N1, N2, &n1, &n2); if (!D) { avma = av; return S; }
    8649      133294 :   D = divisorsu_fact(D); l = lg(D);
    8650      133294 :   vt = varn(mfcharpol(CHI1));
    8651      476434 :   for (i = 1; i < l; i++)
    8652             :   { /* S += d^(k-1)*chi1(d)*chi2(n/d) */
    8653      343140 :     long a, d = n2*D[i], nd = n1*D[l-i]; /* (d,N1)=1; (n/d,N2) = 1 */
    8654      343140 :     a = mfcharevalord(CHI1, d, ord) + mfcharevalord(CHI2, nd, ord);
    8655      343140 :     if (a >= ord) a -= ord;
    8656      343140 :     S = gadd(S, mygmodulo_lift(a, ord, powuu(d, k-1), vt));
    8657             :   }
    8658      133294 :   return gerepileupto(av, S);
    8659             : }
    8660             : 
    8661             : /**************************************************************************/
    8662             : /**           Dirichlet characters with precomputed values               **/
    8663             : /**************************************************************************/
    8664             : /* CHI mfchar */
    8665             : static GEN
    8666       11312 : mfcharcxinit(GEN CHI, long prec)
    8667             : {
    8668       11312 :   GEN G = gel(CHI,1), chi = gel(CHI,2), z, V;
    8669       11312 :   GEN v = ncharvecexpo(G, znconrey_normalized(G,chi));
    8670       11312 :   long n, l = lg(v), o = mfcharorder(CHI);
    8671       11312 :   V = cgetg(l, t_VEC);
    8672       11312 :   z = grootsof1(o, prec); /* Mod(t, Phi_o(t)) -> e(1/o) */
    8673       11312 :   for (n = 1; n < l; n++) gel(V,n) = v[n] < 0? gen_0: gel(z, v[n]+1);
    8674       11312 :   return mkvecn(6, G, chi, gmfcharorder(CHI), v, V, mfcharpol(CHI));
    8675             : }
    8676             : /* v a "CHIvec" */
    8677             : static long
    8678    19943840 : CHIvec_N(GEN v) { return itou(znstar_get_N(gel(v,1))); }
    8679             : static GEN
    8680       11564 : CHIvec_CHI(GEN v)
    8681       11564 : { return mkvec4(gel(v,1), gel(v,2), gel(v,3), gel(v,6)); }
    8682             : /* character order */
    8683             : static long
    8684       32403 : CHIvec_ord(GEN v) { return itou(gel(v,3)); }
    8685             : /* character exponents, i.e. t such that chi(n) = e(t) */
    8686             : static GEN
    8687      333410 : CHIvec_expo(GEN v) { return gel(v,4); }
    8688             : /* character values chi(n) */
    8689             : static GEN
    8690    19441555 : CHIvec_val(GEN v) { return gel(v,5); }
    8691             : /* CHI(n) */
    8692             : static GEN
    8693    19428528 : mychareval(GEN v, long n)
    8694             : {
    8695    19428528 :   long N = CHIvec_N(v), ind = n%N;
    8696    19428528 :   if (ind <= 0) ind += N;
    8697    19428528 :   return gel(CHIvec_val(v), ind);
    8698             : }
    8699             : /* return c such that CHI(n) = e(c / ordz) or -1 if (n,N) > 1 */
    8700             : static long
    8701      333410 : mycharexpo(GEN v, long n)
    8702             : {
    8703      333410 :   long N = CHIvec_N(v), ind = n%N;
    8704      333410 :   if (ind <= 0) ind += N;
    8705      333410 :   return CHIvec_expo(v)[ind];
    8706             : }
    8707             : /* faster than mfcharparity */
    8708             : static long
    8709       37338 : CHIvec_parity(GEN v) { return mycharexpo(v,-1) ? -1: 1; }
    8710             : /**************************************************************************/
    8711             : 
    8712             : static ulong
    8713       47957 : sigchi2_Fl(long k, GEN CHI1vec, GEN CHI2vec, long n, GEN vz, ulong p)
    8714             : {
    8715       47957 :   pari_sp av = avma;
    8716       47957 :   long ordz = lg(vz)-2, i, l, n1, n2;
    8717       47957 :   ulong S = 0;
    8718       47957 :   GEN D = sigchi2_dec(n, CHIvec_N(CHI1vec), CHIvec_N(CHI2vec), &n1, &n2);
    8719       47957 :   if (!D) { avma = av; return S; }
    8720       43589 :   D = divisorsu_fact(D);
    8721       43589 :   l = lg(D);
    8722      147294 :   for (i = 1; i < l; i++)
    8723             :   { /* S += d^(k-1)*chi1(d)*chi2(n/d) */
    8724      103705 :     long a, d = n2*D[i], nd = n1*D[l-i]; /* (d,N1)=1, (n/d,N2)=1 */
    8725      103705 :     a = mycharexpo(CHI2vec, nd) + mycharexpo(CHI1vec, d);
    8726      103705 :     if (a >= ordz) a -= ordz;
    8727      103705 :     S = Fl_add(S, mygmodulo_Fl(a, vz, Fl_powu(d,k-1,p), p), p);
    8728             :   }
    8729       43589 :   avma = av; return S;
    8730             : }
    8731             : 
    8732             : /**********************************************************************/
    8733             : /* Fourier expansions of Eisenstein series                            */
    8734             : /**********************************************************************/
    8735             : /* L(CHI,0) / 2, order(CHI) | ord != 0 */
    8736             : static GEN
    8737        1624 : charLFwt1(GEN CHI, long ord)
    8738             : {
    8739             :   GEN S;
    8740        1624 :   long r, vt, m = mfcharmodulus(CHI);
    8741             : 
    8742        1624 :   if (m == 1) return mkfrac(gen_m1,stoi(4));
    8743        1624 :   S = gen_0; vt = varn(mfcharpol(CHI));
    8744       42469 :   for (r = 1; r < m; r++)
    8745             :   { /* S += r*chi(r) */
    8746             :     long a;
    8747       40845 :     if (ugcd(m,r) != 1) continue;
    8748       31402 :     a = mfcharevalord(CHI,r,ord);
    8749       31402 :     S = gadd(S, mygmodulo_lift(a, ord, utoi(r), vt));
    8750             :   }
    8751        1624 :   return gdivgs(S, -2*m);
    8752             : }
    8753             : /* L(CHI,0) / 2, mod p */
    8754             : static ulong
    8755        1323 : charLFwt1_Fl(GEN CHIvec, GEN vz, ulong p)
    8756             : {
    8757        1323 :   long r, m = CHIvec_N(CHIvec);
    8758             :   ulong S;
    8759        1323 :   if (m == 1) return Rg_to_Fl(mkfrac(gen_m1,stoi(4)), p);
    8760        1323 :   S = 0;
    8761       64659 :   for (r = 1; r < m; r++)
    8762             :   { /* S += r*chi(r) */
    8763       63336 :     long a = mycharexpo(CHIvec,r);
    8764       63336 :     if (a < 0) continue;
    8765       61166 :     S = Fl_add(S, mygmodulo_Fl(a, vz, r, p), p);
    8766             :   }
    8767        1323 :   return Fl_div(Fl_neg(S,p), 2*m, p);
    8768             : }
    8769             : /* L(CHI,1-k) / 2, order(CHI) | ord != 0 */
    8770             : static GEN
    8771        1400 : charLFwtk(long k, GEN CHI, long ord)
    8772             : {
    8773             :   GEN S, P, dS;
    8774             :   long r, m, vt;
    8775             : 
    8776        1400 :   if (k == 1) return charLFwt1(CHI, ord);
    8777        1386 :   m = mfcharmodulus(CHI);
    8778        1386 :   if (m == 1) return gdivgs(bernfrac(k),-2*k);
    8779         791 :   S = gen_0; vt = varn(mfcharpol(CHI));
    8780         791 :   P = ZX_rescale(Q_remove_denom(bernpol(k,0), &dS), utoi(m));
    8781         791 :   dS = mul_denom(dS, stoi(-2*m*k));
    8782       10171 :   for (r = 1; r < m; r++)
    8783             :   { /* S += P(r)*chi(r) */
    8784             :     long a;
    8785        9380 :     if (ugcd(r,m) != 1) continue;
    8786        7602 :     a = mfcharevalord(CHI,r,ord);
    8787        7602 :     S = gadd(S, mygmodulo_lift(a, ord, poleval(P, utoi(r)), vt));
    8788             :   }
    8789         791 :   return gdiv(S, dS);
    8790             : }
    8791             : /* L(CHI,1-k) / 2, mod p */
    8792             : static ulong
    8793        1988 : charLFwtk_Fl(long k, GEN CHIvec, GEN vz, ulong p)
    8794             : {
    8795             :   GEN P;
    8796             :   long r, m;
    8797             :   ulong S;
    8798        1988 :   if (k == 1) return charLFwt1_Fl(CHIvec, vz, p);
    8799         665 :   m = CHIvec_N(CHIvec);
    8800         665 :   if (m == 1) return Rg_to_Fl(gdivgs(bernfrac(k),-2*k), p);
    8801         399 :   S = 0;
    8802         399 :   P = RgX_to_Flx(RgX_rescale(bernpol(k,0), utoi(m)), p);
    8803        8085 :   for (r = 1; r < m; r++)
    8804             :   { /* S += P(r)*chi(r) */
    8805        7686 :     long a = mycharexpo(CHIvec,r);
    8806        7686 :     if (a < 0) continue;
    8807        6566 :     S = Fl_add(S, mygmodulo_Fl(a, vz, Flx_eval(P,r,p), p), p);
    8808             :   }
    8809         399 :   return Fl_div(Fl_neg(S,p), 2*k*m, p);
    8810             : }
    8811             : 
    8812             : static GEN
    8813        5600 : mfeisenstein2_0(long k, GEN CHI1, GEN CHI2, long ord)
    8814             : {
    8815        5600 :   if (k == 1 && mfcharistrivial(CHI1))
    8816        1610 :     return charLFwt1(CHI2, ord);
    8817        3990 :   else if (mfcharistrivial(CHI2))
    8818        1232 :     return charLFwtk(k, CHI1, ord);
    8819        2758 :   else return gen_0;
    8820             : }
    8821             : static ulong
    8822        3290 : mfeisenstein2_0_Fl(long k, GEN CHI1vec, GEN CHI2vec, GEN vz, ulong p)
    8823             : {
    8824        3290 :   if (k == 1 && CHIvec_ord(CHI1vec) == 1)
    8825        1323 :     return charLFwtk_Fl(k, CHI2vec, vz, p);
    8826        1967 :   else if (CHIvec_ord(CHI2vec) == 1)
    8827         665 :     return charLFwtk_Fl(k, CHI1vec, vz, p);
    8828        1302 :   else return 0;
    8829             : }
    8830             : static GEN
    8831          63 : NK_eisen2(long k, GEN CHI1, GEN CHI2)
    8832             : {
    8833          63 :   long N = mfcharmodulus(CHI1)*mfcharmodulus(CHI2);
    8834          63 :   return mkNK(N, k, mfcharmul(CHI1,CHI2));
    8835             : }
    8836             : static GEN
    8837         259 : mfeisenstein_i(long k, GEN CHI1, GEN CHI2)
    8838             : {
    8839         259 :   long s = 1, ord, vt;
    8840             :   GEN E0, NK, vchi, CHI, T;
    8841         259 :   if (CHI2) { CHI2 = get_mfchar(CHI2); if (mfcharparity(CHI2) < 0) s = -s; }
    8842         259 :   if (CHI1) { CHI1 = get_mfchar(CHI1); if (mfcharparity(CHI1) < 0) s = -s; }
    8843         245 :   if (s != m1pk(k)) return mftrivial();
    8844         224 :   if (!CHI1)
    8845         147 :     CHI = CHI2? CHI2: mfchartrivial();
    8846          77 :   else if (!CHI2)
    8847          14 :     CHI = CHI1;
    8848             :   else
    8849          63 :     CHI = NULL;
    8850         224 :   if (CHI)
    8851             :   { /* E_k(chi) */
    8852         161 :     vt = varn(mfcharpol(CHI));
    8853         161 :     ord = mfcharorder(CHI);
    8854         161 :     NK = mkNK(mfcharmodulus(CHI), k, CHI);
    8855         161 :     E0 = charLFwtk(k, CHI, ord);
    8856         161 :     vchi = mkvec3(E0, mkvec(mfcharpol(CHI)), CHI);
    8857         161 :     return tag(t_MF_EISEN, NK, vchi);
    8858             :   }
    8859             :   /* E_k(chi1,chi2) */
    8860          63 :   vt = varn(mfcharpol(CHI1));
    8861          63 :   NK = NK_eisen2(k, CHI1, CHI2);
    8862          63 :   ord = ulcm(mfcharorder(CHI1), mfcharorder(CHI2));
    8863          63 :   E0 = mfeisenstein2_0(k, CHI1, CHI2, ord);
    8864          63 :   T = mkvec(polcyclo(ord_canon(ord), vt));
    8865          63 :   vchi = mkvec4(E0, T, CHI1, CHI2);
    8866          63 :   return tag2(t_MF_EISEN, NK, vchi, mkvecsmall2(ord,0));
    8867             : }
    8868             : GEN
    8869         259 : mfeisenstein(long k, GEN CHI1, GEN CHI2)
    8870             : {
    8871         259 :   pari_sp av = avma;
    8872         259 :   if (k < 1) pari_err_DOMAIN("mfeisenstein", "k", "<", gen_1, stoi(k));
    8873         259 :   return gerepilecopy(av, mfeisenstein_i(k, CHI1, CHI2));
    8874             : }
    8875             : 
    8876             : static GEN
    8877        1218 : mfeisenstein2all(long N0, GEN NK, long k, GEN CHI1, GEN CHI2, GEN T, long o)
    8878             : {
    8879        1218 :   GEN E, E0 = mfeisenstein2_0(k, CHI1,CHI2, o), vchi = mkvec4(E0, T, CHI1,CHI2);
    8880        1218 :   long j, d = (lg(T)==4)? itou(gmael(T,3,1)): 1;
    8881        1218 :   E = cgetg(d+1, t_VEC);
    8882        1218 :   for (j=1; j<=d; j++) gel(E,j) = tag2(t_MF_EISEN, NK,vchi,mkvecsmall2(o,j-1));
    8883        1218 :   return mfbdall(E, N0 / mf_get_N(gel(E,1)));
    8884             : }
    8885             : 
    8886             : static GEN
    8887         532 : zncharsG(GEN G)
    8888             : {
    8889         532 :   long i, l, N = itou(znstar_get_N(G));
    8890             :   GEN vCHI, V;
    8891         532 :   if (N == 1) return mkvec2(gen_1,cgetg(1,t_COL));
    8892         532 :   vCHI = const_vec(N,NULL);
    8893         532 :   V = cyc2elts(znstar_get_conreycyc(G));
    8894         532 :   l = lg(V);
    8895       15848 :   for (i = 1; i < l; i++)
    8896             :   {
    8897       15316 :     GEN chi0, chi = zc_to_ZC(gel(V,i)), n, F;
    8898       15316 :     F = znconreyconductor(G, chi, &chi0);
    8899       15316 :     if (typ(F) != t_INT) F = gel(F,1);
    8900       15316 :     n = znconreyexp(G, chi);
    8901       15316 :     gel(vCHI, itos(n)) = mkvec2(F, chi0);
    8902             :   }
    8903         532 :   return vCHI;
    8904             : }
    8905             : 
    8906             : /* CHI primitive, f(CHI) | N. Return pairs (CHI1,CHI2) both primitive
    8907             :  * such that f(CHI1)*f(CHI2) | N and CHI1 * CHI2 = CHI;
    8908             :  * if k = 1, CHI1 is even; if k = 2, omit (1,1) if CHI = 1 */
    8909             : static GEN
    8910         728 : mfeisensteinbasis_i(long N0, long k, GEN CHI)
    8911             : {
    8912         728 :   GEN G = gel(CHI,1), chi = gel(CHI,2), vT = const_vec(myeulerphiu(N0), NULL);
    8913             :   GEN CHI0, GN, chiN, Lchi, LG, V, RES, NK, T;
    8914         728 :   long i, j, l, n, n1, N, ord = mfcharorder(CHI), OC = ord_canon(ord);
    8915         728 :   long F = mfcharmodulus(CHI), vt = varn(mfcharpol(CHI));
    8916             : 
    8917         728 :   CHI0 = (F == 1)? CHI: mfchartrivial();
    8918         728 :   j = 1; RES = cgetg(N0+1, t_VEC);
    8919         728 :   T = gel(vT,OC) = Qab_trace_init(polcyclo(OC,vt), OC, OC);
    8920         728 :   if (F != 1 || k != 2)
    8921             :   { /* N1 = 1 */
    8922         602 :     NK = mkNK(F, k, CHI);
    8923         602 :     gel(RES, j++) = mfeisenstein2all(N0, NK, k, CHI0, CHI, T, ord);
    8924         602 :     if (F != 1 && k != 1)
    8925         203 :       gel(RES, j++) = mfeisenstein2all(N0, NK, k, CHI, CHI0, T, ord);
    8926             :   }
    8927         728 :   if (N0 == 1) { setlg(RES,j); return RES; }
    8928         658 :   GN = G; chiN = chi;
    8929         658 :   if (F == N0) N = N0;
    8930             :   else
    8931             :   {
    8932         413 :     GEN faN = myfactoru(N0), P = gel(faN,1), E = gel(faN,2);
    8933         413 :     long lP = lg(P);
    8934        1050 :     for (i = N = 1; i < lP; i++)
    8935             :     {
    8936         637 :       long p = P[i];
    8937         637 :       N *= upowuu(p, maxuu(E[i]/2, z_lval(F,p)));
    8938             :     }
    8939         413 :     if ((N & 3) == 2) N >>= 1;
    8940         413 :     if (N == 1) { setlg(RES,j); return RES; }
    8941         287 :     if (F != N)
    8942             :     {
    8943          91 :       GN = znstar0(utoipos(N),1);
    8944          91 :       chiN = zncharinduce(G, chi, GN);
    8945             :     }
    8946             :   }
    8947         532 :   LG = const_vec(N, NULL); /* LG[d] = znstar(d,1) or NULL */
    8948         532 :   gel(LG,1) = gel(CHI0,1);
    8949         532 :   gel(LG,F) = G;
    8950         532 :   gel(LG,N) = GN;
    8951         532 :   Lchi = coprimes_zv(N);
    8952         532 :   n = itou(znconreyexp(GN,chiN));
    8953         532 :   V = zncharsG(GN); l = lg(V);
    8954       20342 :   for (n1 = 2; n1 < l; n1++) /* skip 1 (trivial char) */
    8955             :   {
    8956       19810 :     GEN v = gel(V,n1), w, chi1, chi2, G1, G2, CHI1, CHI2;
    8957             :     long N12, N1, N2, no, oc, o12, t, m;
    8958       19810 :     if (!Lchi[n1]) continue;
    8959       14735 :     chi1 = gel(v,2); N1 = itou(gel(v,1)); /* conductor of chi1 */
    8960       14735 :     w = gel(V, Fl_div(n,n1,N));
    8961       14735 :     chi2 = gel(w,2); N2 = itou(gel(w,1)); /* conductor of chi2 */
    8962       14735 :     N12 = N1 * N2;
    8963       14735 :     if (N2 == 1 || N0 % N12) continue;
    8964             : 
    8965         546 :     G1 = gel(LG,N1); if (!G1) gel(LG,N1) = G1 = znstar0(utoipos(N1), 1);
    8966         546 :     if (k == 1 && zncharisodd(G1,chi1)) continue;
    8967         413 :     G2 = gel(LG,N2); if (!G2) gel(LG,N2) = G2 = znstar0(utoipos(N2), 1);
    8968         413 :     CHI1 = mfcharGL(G1, chi1);
    8969         413 :     CHI2 = mfcharGL(G2, chi2);
    8970         413 :     o12 = ulcm(mfcharorder(CHI1), mfcharorder(CHI2));
    8971             :     /* remove Galois orbit: same trace */
    8972         413 :     no = Fl_powu(n1, ord, N);
    8973         707 :     for (t = 1+ord, m = n1; t <= o12; t += ord)
    8974             :     { /* m <-> CHI1^t, if t in Gal(Q(chi1,chi2)/Q), omit (CHI1^t,CHI2^t) */
    8975         294 :       m = Fl_mul(m, no, N); if (!m) break;
    8976         294 :       if (ugcd(t, o12) == 1) Lchi[m] = 0;
    8977             :     }
    8978         413 :     oc = ord_canon(o12); T = gel(vT,oc);
    8979         413 :     if (!T) T = gel(vT,oc) = Qab_trace_init(polcyclo(oc,vt), oc, OC);
    8980         413 :     NK = mkNK(N12, k, CHI);
    8981         413 :     gel(RES, j++) = mfeisenstein2all(N0, NK, k, CHI1, CHI2, T, o12);
    8982             :   }
    8983         532 :   setlg(RES,j); return RES;
    8984             : }
    8985             : 
    8986             : static GEN
    8987         616 : mfbd_E2(GEN E2, long d, GEN CHI)
    8988             : {
    8989         616 :   GEN E2d = mfbd_i(E2, d);
    8990         616 :   GEN F = mkvec2(E2, E2d), L = mkvec2(gen_1, utoineg(d));
    8991             :   /* cannot use mflinear_i: E2 and E2d do not have the same level */
    8992         616 :   return tag3(t_MF_LINEAR, mkNK(d,2,CHI), F, L, gen_1);
    8993             : }
    8994             : /* C-basis of E_k(Gamma_0(N),chi). If k = 1, the first basis element must not
    8995             :  * vanish at oo [used in mfwt1basis]. Here E_1(CHI), whose q^0 coefficient
    8996             :  * does not vanish (since L(CHI,0) does not) *if* CHI is not trivial; which
    8997             :  * must be the case in weight 1.
    8998             :  *
    8999             :  * (k>=3): In weight k >= 3, basis is B(d) E(CHI1,(CHI/CHI1)_prim), where
    9000             :  * CHI1 is primitive modulo N1, and if N2 is the conductor of CHI/CHI1
    9001             :  * then d*N1*N2 | N.
    9002             :  * (k=2): In weight k=2, same if CHI is nontrivial. If CHI is trivial, must
    9003             :  * not take CHI1 trivial, and must add E_2(tau)-dE_2(d tau)), where
    9004             :  * d|N, d > 1.
    9005             :  * (k=1): In weight k=1, same as k >= 3 except that we restrict to CHI1 even */
    9006             : static GEN
    9007         756 : mfeisensteinbasis(long N, long k, GEN CHI)
    9008             : {
    9009             :   long i, F;
    9010             :   GEN L;
    9011         756 :   if (badchar(N, k, CHI)) return cgetg(1, t_VEC);
    9012         756 :   if (k == 0) return mfcharistrivial(CHI)? mkvec(mf1()): cgetg(1, t_VEC);
    9013         728 :   CHI = mfchartoprimitive(CHI, &F);
    9014         728 :   L = mfeisensteinbasis_i(N, k, CHI);
    9015         728 :   if (F == 1 && k == 2)
    9016             :   {
    9017         126 :     GEN v, E2 = mfeisenstein(2, NULL, NULL), D = mydivisorsu(N);
    9018         126 :     long nD = lg(D)-1;
    9019         126 :     v = cgetg(nD, t_VEC); L = vec_append(L,v);
    9020         126 :     for (i = 1; i < nD; i++) gel(v,i) = mfbd_E2(E2, D[i+1], CHI);
    9021             :   }
    9022         728 :   return lg(L) == 1? L: shallowconcat1(L);
    9023             : }
    9024             : 
    9025             : static GEN
    9026          70 : not_in_space(GEN F, long flag)
    9027             : {
    9028          70 :   if (!flag) err_space(F);
    9029          63 :   return cgetg(1, t_COL);
    9030             : }
    9031             : /* when flag set, no error */
    9032             : GEN
    9033         805 : mftobasis(GEN mf, GEN F, long flag)
    9034             : {
    9035         805 :   pari_sp av2, av = avma;
    9036             :   GEN G, v, y, gk;
    9037         805 :   long N, B, ismf = checkmf_i(F);
    9038             : 
    9039         805 :   mf = checkMF(mf);
    9040         805 :   if (ismf)
    9041             :   {
    9042         714 :     if (mfistrivial(F)) return zerocol(MF_get_dim(mf));
    9043         707 :     if (!mf_same_k(mf, F) || !mf_same_CHI(mf, F)) return not_in_space(F, flag);
    9044             :   }
    9045         756 :   N = MF_get_N(mf);
    9046         756 :   gk = MF_get_gk(mf);
    9047         756 :   if (ismf)
    9048             :   {
    9049         665 :     long NF = mf_get_N(F);
    9050         665 :     B = maxuu(mfsturmNgk(NF,gk), mfsturmNgk(N,gk)) + 1;
    9051         665 :     v = mfcoefs_i(F,B,1);
    9052             :   }
    9053             :   else
    9054             :   {
    9055          91 :     B = mfsturmNgk(N, gk) + 1;
    9056          91 :     switch(typ(F))
    9057             :     { /* F(0),...,F(lg(v)-2) */
    9058          63 :       case t_SER: v = sertocol(F); settyp(v,t_VEC); break;
    9059          14 :       case t_VEC: v = F; break;
    9060           7 :       case t_COL: v = shallowtrans(F); break;
    9061           7 :       default: pari_err_TYPE("mftobasis",F);
    9062             :                v = NULL;/*LCOV_EXCL_LINE*/
    9063             :     }
    9064          84 :     if (flag) B = minss(B, lg(v)-2);
    9065             :   }
    9066         749 :   y = mftobasis_i(mf, v);
    9067         749 :   if (typ(y) == t_VEC)
    9068             :   {
    9069          21 :     if (flag) return gerepilecopy(av, y);
    9070           0 :     pari_err(e_MISC, "not enough coefficients in mftobasis");
    9071             :   }
    9072         728 :   av2 = avma;
    9073         728 :   if (MF_get_space(mf) == mf_FULL || mfsturm(mf)+1 == B) return y;
    9074         210 :   G = mflinear(mf, y);
    9075         210 :   if (!gequal(v, mfcoefs_i(G, lg(v)-2,1))) y = NULL;
    9076         210 :   if (!y) { avma = av; return not_in_space(F, flag); }
    9077         182 :   avma = av2; return gerepileupto(av, y);
    9078             : }
    9079             : 
    9080             : /* assume N > 0; first cusp is always 0 */
    9081             : static GEN
    9082          49 : mfcusps_i(long N)
    9083             : {
    9084             :   long i, c, l;
    9085             :   GEN D, v;
    9086             : 
    9087          49 :   if (N == 1) return mkvec(gen_0);
    9088          49 :   D = mydivisorsu(N); l = lg(D); /* left on stack */
    9089          49 :   c = mfnumcuspsu_fact(myfactoru(N));
    9090          49 :   v = cgetg(c + 1, t_VEC);
    9091         350 :   for (i = c = 1; i < l; i++)
    9092             :   {
    9093         301 :     long C = D[i], NC = D[l-i], lima = ugcd(C, NC), A0, A;
    9094         889 :     for (A0 = 0; A0 < lima; A0++)
    9095         588 :       if (ugcd(A0, lima) == 1)
    9096             :       {
    9097         392 :         A = A0; while (ugcd(A,C) > 1) A += lima;
    9098         392 :         gel(v, c++) = sstoQ(A, C);
    9099             :       }
    9100             :   }
    9101          49 :   return v;
    9102             : }
    9103             : /* List of cusps of Gamma_0(N) */
    9104             : GEN
    9105          28 : mfcusps(GEN gN)
    9106             : {
    9107             :   long N;
    9108             :   GEN mf;
    9109          28 :   if (typ(gN) == t_INT) N = itos(gN);
    9110          14 :   else if ((mf = checkMF_i(gN))) N = MF_get_N(mf);
    9111           0 :   else { pari_err_TYPE("mfcusps", gN); N = 0; }
    9112          28 :   if (N <= 0) pari_err_DOMAIN("mfcusps", "N", "<=", gen_0, stoi(N));
    9113          28 :   return mfcusps_i(N);
    9114             : }
    9115             : 
    9116             : long
    9117         315 : mfcuspisregular(GEN NK, GEN cusp)
    9118             : {
    9119             :   long v, N, dk, nk, t, o;
    9120             :   GEN mf, CHI, go, A, C, g, c, d;
    9121         315 :   if ((mf = checkMF_i(NK)))
    9122             :   {
    9123          49 :     GEN gk = MF_get_gk(mf);
    9124          49 :     N = MF_get_N(mf);
    9125          49 :     CHI = MF_get_CHI(mf);
    9126          49 :     Qtoss(gk, &nk, &dk);
    9127             :   }
    9128             :   else
    9129         266 :     checkNK2(NK, &N, &nk, &dk, &CHI, 0);
    9130         315 :   if (typ(cusp) == t_INFINITY) return 1;
    9131         315 :   if (typ(cusp) == t_FRAC) { A = gel(cusp,1); C = gel(cusp,2); }
    9132          28 :   else { A = cusp; C = gen_1; }
    9133         315 :   g = diviuexact(mului(N,C), ugcd(N, Fl_sqr(umodiu(C,N), N)));
    9134         315 :   c = mulii(negi(C),g);
    9135         315 :   d = addiu(mulii(A,g), 1);
    9136         315 :   if (!CHI) return 1;
    9137         315 :   go = gmfcharorder(CHI);
    9138         315 :   v = vali(go); if (v < 2) go = shifti(go, 2-v);
    9139         315 :   t = itou( znchareval(gel(CHI,1), gel(CHI,2), d, go) );
    9140         315 :   if (dk == 1) return t == 0;
    9141         154 :   o = itou(go);
    9142         154 :   if (kronecker(c,d) < 0) t = Fl_add(t, o/2, o);
    9143         154 :   if (Mod4(d) == 1) return t == 0;
    9144          14 :   t = Fl_sub(t, Fl_mul(o/4, nk, o), o);
    9145          14 :   return t == 0;
    9146             : }
    9147             : 
    9148             : /* Some useful closures */
    9149             : 
    9150             : /* sum_{d|n} d^k */
    9151             : static GEN
    9152       16464 : mysumdivku(ulong n, ulong k)
    9153             : {
    9154       16464 :   GEN fa = myfactoru(n);
    9155       16464 :   return k == 1? usumdiv_fact(fa): usumdivk_fact(fa,k);
    9156             : }
    9157             : static GEN
    9158         658 : c_Ek(long n, long d, GEN F)
    9159             : {
    9160         658 :   GEN E = cgetg(n + 2, t_VEC), C = gel(F,2);
    9161         658 :   long i, k = mf_get_k(F);
    9162         658 :   gel (E, 1) = gen_1;
    9163        8260 :   for (i = 1; i <= n; i++)
    9164             :   {
    9165        7602 :     pari_sp av = avma;
    9166        7602 :     gel(E, i+1) = gerepileupto(av, gmul(C, mysumdivku(i*d, k-1)));
    9167             :   }
    9168         658 :   return E;
    9169             : }
    9170             : 
    9171             : GEN
    9172         189 : mfEk(long k)
    9173             : {
    9174         189 :   pari_sp av = avma;
    9175             :   GEN E0, NK;
    9176         189 :   if (k < 0 || odd(k)) pari_err_TYPE("mfEk [incorrect k]", stoi(k));
    9177         189 :   if (!k) return mf1();
    9178         182 :   E0 = gdivsg(-2*k, bernfrac(k));
    9179         182 :   NK = mkNK(1,k,mfchartrivial());
    9180         182 :   return gerepilecopy(av, tag(t_MF_Ek, NK, E0));
    9181             : }
    9182             : 
    9183             : GEN
    9184          49 : mfDelta(void)
    9185             : {
    9186          49 :   pari_sp av = avma;
    9187          49 :   return gerepilecopy(av, tag0(t_MF_DELTA, mkNK(1,12,mfchartrivial())));
    9188             : }
    9189             : 
    9190             : GEN
    9191         504 : mfTheta(GEN psi)
    9192             : {
    9193         504 :   pari_sp av = avma;
    9194             :   GEN N, gk, psi2;
    9195             :   long par;
    9196         504 :   if (!psi) { psi = mfchartrivial(); N = utoipos(4); par = 1; }
    9197             :   else
    9198             :   {
    9199             :     long FC;
    9200          21 :     psi = get_mfchar(psi);
    9201          21 :     FC = mfcharconductor(psi);
    9202          21 :     if (mfcharmodulus(psi) != FC)
    9203           0 :       pari_err_TYPE("mfTheta [nonprimitive character]", psi);
    9204          21 :     par = mfcharparity(psi);
    9205          21 :     N = shifti(sqru(FC),2);
    9206             :   }
    9207         504 :   if (par > 0) { gk = ghalf; psi2 = psi; }
    9208           7 :   else { gk = gsubsg(2, ghalf); psi2 = mfcharmul(psi, get_mfchar(stoi(-4))); }
    9209         504 :   return gerepilecopy(av, tag(t_MF_THETA, mkgNK(N, gk, psi2, pol_x(1)), psi));
    9210             : }
    9211             : 
    9212             : /* Output 0 if not desired eta product: if flag=0 (default) require
    9213             :  * holomorphic at cusps. If flag set, accept meromorphic, but sill in some
    9214             :  * modular function space */
    9215             : GEN
    9216         140 : mffrometaquo(GEN eta, long flag)
    9217             : {
    9218         140 :   pari_sp av = avma;
    9219             :   GEN NK, N, k, BR, P;
    9220         140 :   long v, cusp = 0;
    9221         140 :   if (!etaquotype(&eta, &N,&k,&P, &v, NULL, flag? NULL: &cusp) || cusp < 0)
    9222             :   {
    9223          14 :     avma = av; return gen_0;
    9224             :   }
    9225         126 :   if (lg(gel(eta,1)) == 1) { avma = av; return mf1(); }
    9226         119 :   BR = mkvec2(ZV_to_zv(gel(eta,1)), ZV_to_zv(gel(eta,2)));
    9227         119 :   if (v < 0) v = 0;
    9228         119 :   NK = mkgNK(N, k, get_mfchar(P), pol_x(1));
    9229         119 :   return gerepilecopy(av, tag2(t_MF_ETAQUO, NK, BR, utoi(v)));
    9230             : }
    9231             : 
    9232             : #if 0
    9233             : /* number of primitive characters modulo N */
    9234             : static ulong
    9235             : numprimchars(ulong N)
    9236             : {
    9237             :   GEN fa, P, E;
    9238             :   long i, l;
    9239             :   ulong n;
    9240             :   if ((N & 3) == 2) return 0;
    9241             :   fa = myfactoru(N);
    9242             :   P = gel(fa,1); l = lg(P);
    9243             :   E = gel(fa,2);
    9244             :   for (i = n = 1; i < l; i++)
    9245             :   {
    9246             :     ulong p = P[i], e = E[i];
    9247             :     if (e == 2) n *= p-2; else n *= (p-1)*(p-1)*upowuu(p,e-2);
    9248             :   }
    9249             :   return n;
    9250             : }
    9251             : #endif
    9252             : 
    9253             : /* Space generated by products of two Eisenstein series */
    9254             : 
    9255             : INLINE int
    9256      112217 : cmp_small(long a, long b) { return a>b? 1: (a<b? -1: 0); }
    9257             : static int
    9258       62657 : cmp_small_priority(void *E, GEN a, GEN b)
    9259             : {
    9260       62657 :   GEN prio = (GEN)E;
    9261       62657 :   return cmp_small(prio[(long)a], prio[(long)b]);
    9262             : }
    9263             : static long
    9264         938 : znstar_get_expo(GEN G)
    9265             : {
    9266         938 :   GEN cyc = znstar_get_cyc(G);
    9267         938 :   return (lg(cyc) == 1)? 1: itou(gel(cyc,1));
    9268             : }
    9269             : 
    9270             : /* Return [vchi, bymod, vG]:
    9271             :  * vG[f] = znstar(f,1) for f a conductor of (at least) a char mod N; else NULL
    9272             :  * bymod[f] = vecsmall of conrey indexes of chars modulo f | N; else NULL
    9273             :  * vchi[n] = a list of CHIvec [G0,chi0,o,ncharvecexpo(G0,nchi0),...]:
    9274             :  *   chi0 = primitive char attached to Conrey Mod(n,N)
    9275             :  * (resp. NULL if (n,N) > 1) */
    9276             : static GEN
    9277         469 : charsmodN(long N)
    9278             : {
    9279         469 :   GEN D, G, prio, phio, dummy = cgetg(1,t_VEC);
    9280         469 :   GEN vP, vG = const_vec(N,NULL), vCHI  = const_vec(N,NULL);
    9281         469 :   GEN bymod = const_vec(N,NULL);
    9282         469 :   long pn, i, l, vt = fetch_user_var("t");
    9283         469 :   D = mydivisorsu(N); l = lg(D);
    9284        3059 :   for (i = 1; i < l; i++)
    9285        2590 :     gel(bymod, D[i]) = vecsmalltrunc_init(myeulerphiu(D[i])+1);
    9286         469 :   gel(vG,N) = G = znstar0(utoipos(N),1);
    9287         469 :   pn = znstar_get_expo(G);  /* exponent(Z/NZ)^* */
    9288         469 :   vP = const_vec(pn,NULL);
    9289       22456 :   for (i = 1; i <= N; i++)
    9290             :   {
    9291             :     GEN P, gF, G0, chi0, nchi0, chi, v, go;
    9292             :     long j, F, o;
    9293       21987 :     if (ugcd(i,N) != 1) continue;
    9294       11067 :     chi = znconreylog(G, utoipos(i));
    9295       11067 :     gF = znconreyconductor(G, chi, &chi0);
    9296       11067 :     F = (typ(gF) == t_INT)? itou(gF): itou(gel(gF,1));
    9297       11067 :     G0 = gel(vG, F); if (!G0) G0 = gel(vG,F) = znstar0(gF, 1);
    9298       11067 :     nchi0 = znconreylog_normalize(G0,chi0);
    9299       11067 :     go = gel(nchi0,1); o = itou(go); /* order(chi0) */
    9300       11067 :     v = ncharvecexpo(G0, nchi0);
    9301       11067 :     if (!equaliu(go, pn)) v = zv_z_mul(v, pn / o);
    9302       11067 :     P = gel(vP, o); if (!P) P = gel(vP,o) = polcyclo(o,vt);
    9303             :     /* mfcharcxinit with dummy complex powers */
    9304       11067 :     gel(vCHI,i) = mkvecn(6, G0, chi0, go, v, dummy, P);
    9305       11067 :     D = mydivisorsu(N / F); l = lg(D);
    9306       11067 :     for (j = 1; j < l; j++) vecsmalltrunc_append(gel(bymod, F*D[j]), i);
    9307             :   }
    9308         469 :   phio = zero_zv(pn); l = lg(vCHI); prio = cgetg(l, t_VEC);
    9309       22456 :   for (i = 1; i < l; i++)
    9310             :   {
    9311       21987 :     GEN CHI = gel(vCHI,i);
    9312             :     long o;
    9313       21987 :     if (!CHI) continue;
    9314       11067 :     o = CHIvec_ord(CHI);
    9315       11067 :     if (!phio[o]) phio[o] = myeulerphiu(o);
    9316       11067 :     prio[i] = phio[o];
    9317             :   }
    9318         469 :   l = lg(bymod);
    9319             :   /* sort characters by increasing value of phi(order) */
    9320       22456 :   for (i = 1; i < l; i++)
    9321             :   {
    9322       21987 :     GEN z = gel(bymod,i);
    9323       21987 :     if (z) gen_sort_inplace(z, (void*)prio, &cmp_small_priority, NULL);
    9324             :   }
    9325         469 :   return mkvec3(vCHI, bymod, vG);
    9326             : }
    9327             : 
    9328             : static GEN
    9329        4319 : mfeisenstein2pure(long k, GEN CHI1, GEN CHI2, long ord, GEN P, long lim)
    9330             : {
    9331        4319 :   GEN c, V = cgetg(lim+2, t_COL);
    9332             :   long n;
    9333        4319 :   c = mfeisenstein2_0(k, CHI1, CHI2, ord);
    9334        4319 :   if (P) c = grem(c, P);
    9335        4319 :   gel(V,1) = c;
    9336       92512 :   for (n=1; n <= lim; n++)
    9337             :   {
    9338       88193 :     c = sigchi2(k, CHI1, CHI2, n, ord);
    9339       88193 :     if (P) c = grem(c, P);
    9340       88193 :     gel(V,n+1) = c;
    9341             :   }
    9342        4319 :   return V;
    9343             : }
    9344             : static GEN
    9345        3290 : mfeisenstein2pure_Fl(long k, GEN CHI1vec, GEN CHI2vec, GEN vz, ulong p, long lim)
    9346             : {
    9347        3290 :   GEN V = cgetg(lim+2, t_VECSMALL);
    9348             :   long n;
    9349        3290 :   V[1] = mfeisenstein2_0_Fl(k, CHI1vec, CHI2vec, vz, p);
    9350        3290 :   for (n=1; n <= lim; n++) V[n+1] = sigchi2_Fl(k, CHI1vec, CHI2vec, n, vz, p);
    9351        3290 :   return V;
    9352             : }
    9353             : 
    9354             : static GEN
    9355         175 : getcolswt2(GEN M, GEN D, ulong p)
    9356             : {
    9357         175 :   GEN R, v = gel(M,1);
    9358         175 :   long i, l = lg(M) - 1;
    9359         175 :   R = cgetg(l, t_MAT); /* skip D[1] = 1 */
    9360         616 :   for (i = 1; i < l; i++)
    9361             :   {
    9362         441 :     GEN w = Flv_Fl_mul(gel(M,i+1), D[i+1], p);
    9363         441 :     gel(R,i) = Flv_sub(v, w, p);
    9364             :   }
    9365         175 :   return R;
    9366             : }
    9367             : static GEN
    9368        4319 : expandbd(GEN V, long d)
    9369             : {
    9370             :   long L, n, nd;
    9371             :   GEN W;
    9372        4319 :   if (d == 1) return V;
    9373        1575 :   L = lg(V)-1; W = zerocol(L); /* nd = n/d */
    9374        1575 :   for (n = nd = 0; n < L; n += d, nd++) gel(W, n+1) = gel(V, nd+1);
    9375        1575 :   return W;
    9376             : }
    9377             : static GEN
    9378        5222 : expandbd_Fl(GEN V, long d)
    9379             : {
    9380             :   long L, n, nd;
    9381             :   GEN W;
    9382        5222 :   if (d == 1) return V;
    9383        1932 :   L = lg(V)-1; W = zero_Flv(L); /* nd = n/d */
    9384        1932 :   for (n = nd = 0; n < L; n += d, nd++) W[n+1] = V[nd+1];
    9385        1932 :   return W;
    9386             : }
    9387             : static void
    9388        3290 : getcols_i(GEN *pM, GEN *pvj, GEN gk, GEN CHI1vec, GEN CHI2vec, long NN1, GEN vz,
    9389             :           ulong p, long lim)
    9390             : {
    9391        3290 :   GEN CHI1 = CHIvec_CHI(CHI1vec), CHI2 = CHIvec_CHI(CHI2vec);
    9392        3290 :   long N2 = CHIvec_N(CHI2vec);
    9393        3290 :   GEN vj, M, D = mydivisorsu(NN1/N2);
    9394        3290 :   long i, l = lg(D), k = gk[2];
    9395        3290 :   GEN V = mfeisenstein2pure_Fl(k, CHI1vec, CHI2vec, vz, p, lim);
    9396        3290 :   M = cgetg(l, t_MAT);
    9397        3290 :   for (i = 1; i < l; i++) gel(M,i) = expandbd_Fl(V, D[i]);
    9398        3290 :   if (k == 2 && N2 == 1 && CHIvec_N(CHI1vec) == 1)
    9399             :   {
    9400         175 :     M = getcolswt2(M, D, p); l--;
    9401         175 :     D = vecslice(D, 2, l);
    9402             :   }
    9403        3290 :   *pM = M;
    9404        3290 :   *pvj = vj = cgetg(l, t_VEC);
    9405        3290 :   for (i = 1; i < l; i++) gel(vj,i) = mkvec4(gk, CHI1, CHI2, utoipos(D[i]));
    9406        3290 : }
    9407             : 
    9408             : /* find all CHI1, CHI2 mod N such that CHI1*CHI2 = CHI, f(CHI1)*f(CHI2) | N.
    9409             :  * set M = mfcoefs(B_e E(CHI1,CHI2), lim), vj = [e,i1,i2] */
    9410             : static void
    9411        1267 : getcols(GEN *pM, GEN *pv, long k, long nCHI, GEN allN, GEN vz, ulong p,
    9412             :         long lim)
    9413             : {
    9414        1267 :   GEN vCHI = gel(allN,1), gk = utoi(k);
    9415        1267 :   GEN M = cgetg(1,t_MAT), v = cgetg(1,t_VEC);
    9416        1267 :   long i1, N = lg(vCHI)-1;
    9417       63322 :   for (i1 = 1; i1 <= N; i1++)
    9418             :   {
    9419       62055 :     GEN CHI1vec = gel(vCHI, i1), CHI2vec, M1, v1;
    9420             :     long NN1, i2;
    9421      121618 :     if (!CHI1vec) continue;
    9422       46718 :     if (k == 1 && CHIvec_parity(CHI1vec) == -1) continue;
    9423       29582 :     NN1 = N/CHIvec_N(CHI1vec); /* N/f(chi1) */;
    9424       29582 :     i2 = Fl_div(nCHI,i1, N);
    9425       29582 :     if (!i2) i2 = 1;
    9426       29582 :     CHI2vec = gel(vCHI,i2);
    9427       29582 :     if (NN1 % CHIvec_N(CHI2vec)) continue; /* f(chi1)f(chi2) | N ? */
    9428        2492 :     getcols_i(&M1, &v1, gk, CHI1vec, CHI2vec, NN1, vz, p, lim);
    9429        2492 :     M = shallowconcat(M, M1);
    9430        2492 :     v = shallowconcat(v, v1);
    9431             :   }
    9432        1267 :   *pM = M;
    9433        1267 :   *pv = v;
    9434        1267 : }
    9435             : 
    9436             : static void
    9437         833 : update_Mj(GEN *M, GEN *vecj, GEN *pz, ulong p)
    9438             : {
    9439             :   GEN perm;
    9440         833 :   *pz = Flm_indexrank(*M, p); perm = gel(*pz,2);
    9441         833 :   *M = vecpermute(*M, perm);
    9442         833 :   *vecj = vecpermute(*vecj, perm);
    9443         833 : }
    9444             : static int
    9445         301 : getcolsgen(long dim, GEN *pM, GEN *pvj, GEN *pz, long k, long ell, long nCHI,
    9446             :            GEN allN, GEN vz, ulong p, long lim)
    9447             : {
    9448         301 :   GEN vCHI = gel(allN,1), bymod = gel(allN,2), gell = utoi(ell);
    9449         301 :   long i1, N = lg(vCHI)-1;
    9450         301 :   long L = lim+1;
    9451         301 :   if (lg(*pvj)-1 >= dim) update_Mj(pM, pvj, pz, p);
    9452         301 :   if (lg(*pvj)-1 == dim) return 1;
    9453        1099 :   for (i1 = 1; i1 <= N; i1++)
    9454             :   {
    9455        1085 :     GEN CHI1vec = gel(vCHI, i1), T;
    9456             :     long par1, j, l, N1, NN1;
    9457             : 
    9458        1085 :     if (!CHI1vec) continue;
    9459        1071 :     par1 = CHIvec_parity(CHI1vec);
    9460        1071 :     if (ell == 1 && par1 == -1) continue;
    9461         672 :     if (odd(ell)) par1 = -par1;
    9462         672 :     N1 = CHIvec_N(CHI1vec);
    9463         672 :     NN1 = N/N1;
    9464         672 :     T = gel(bymod, NN1); l = lg(T);
    9465        2394 :     for (j = 1; j < l; j++)
    9466             :     {
    9467        1995 :       long i2 = T[j], l1, l2, j1, s, nC;
    9468        1995 :       GEN M, M1, M2, vj, vj1, vj2, CHI2vec = gel(vCHI, i2);
    9469        3192 :       if (CHIvec_parity(CHI2vec) != par1) continue;
    9470         798 :       nC = Fl_div(nCHI, Fl_mul(i1,i2,N), N);
    9471         798 :       getcols(&M2, &vj2, k-ell, nC, allN, vz, p, lim);
    9472         798 :       l2 = lg(M2); if (l2 == 1) continue;
    9473         798 :       getcols_i(&M1, &vj1, gell, CHI1vec, CHI2vec, NN1, vz, p, lim);
    9474         798 :       l1 = lg(M1);
    9475         798 :       M1 = Flm_to_FlxV(M1, 0);
    9476         798 :       M2 = Flm_to_FlxV(M2, 0);
    9477         798 :       M  = cgetg((l1-1)*(l2-1) + 1, t_MAT);
    9478         798 :       vj = cgetg((l1-1)*(l2-1) + 1, t_VEC);
    9479        1995 :       for (j1 = s = 1; j1 < l1; j1++)
    9480             :       {
    9481        1197 :         GEN E = gel(M1,j1), v = gel(vj1,j1);
    9482             :         long j2;
    9483        5166 :         for (j2 = 1; j2 < l2; j2++, s++)
    9484             :         {
    9485        3969 :           GEN c = Flx_to_Flv(Flxn_mul(E, gel(M2,j2), L, p), L);
    9486        3969 :           gel(M,s) = c;
    9487        3969 :           gel(vj,s) = mkvec2(v, gel(vj2,j2));
    9488             :         }
    9489             :       }
    9490         798 :       *pM = shallowconcat(*pM, M);
    9491         798 :       *pvj = shallowconcat(*pvj, vj);
    9492         798 :       if (lg(*pvj)-1 >= dim) update_Mj(pM, pvj, pz, p);
    9493         798 :       if (lg(*pvj)-1 == dim) return 1;
    9494             :     }
    9495             :   }
    9496          14 :   if (ell == 1)
    9497             :   {
    9498          14 :     update_Mj(pM, pvj, pz, p);
    9499          14 :     return (lg(*pvj)-1 == dim);
    9500             :   }
    9501           0 :   return 0;
    9502             : }
    9503             : 
    9504             : static GEN
    9505         924 : mkF2bd(long d, long lim)
    9506             : {
    9507         924 :   GEN V = zerovec(lim + 1);
    9508             :   long n;
    9509         924 :   gel(V, 1) = ginv(stoi(-24));
    9510         924 :   for (n = 1; n <= lim/d; n++) gel(V, n*d + 1) = mysumdivku(n, 1);
    9511         924 :   return V;
    9512             : }
    9513             : 
    9514             : static GEN
    9515        4676 : mkeisen(GEN E, long ord, GEN P, long lim)
    9516             : {
    9517        4676 :   long k = itou(gel(E,1)), e = itou(gel(E,4));
    9518        4676 :   GEN CHI1 = gel(E,2), CHI2 = gel(E,3);
    9519        4676 :   if (k == 2 && mfcharistrivial(CHI1) && mfcharistrivial(CHI2))
    9520         357 :     return gsub(mkF2bd(1,lim), gmulgs(mkF2bd(e,lim), e));
    9521             :   else
    9522             :   {
    9523        4319 :     GEN V = mfeisenstein2pure(k, CHI1, CHI2, ord, P, lim);
    9524        4319 :     return expandbd(V, e);
    9525             :   }
    9526             : }
    9527             : static GEN
    9528         441 : mkM(GEN vj, long pn, GEN P, long lim)
    9529             : {
    9530         441 :   long j, l = lg(vj), L = lim+1;
    9531         441 :   GEN M = cgetg(l, t_MAT);
    9532        3836 :   for (j = 1; j < l; j++)
    9533             :   {
    9534             :     GEN E1, E2;
    9535        3395 :     parse_vecj(gel(vj,j), &E1,&E2);
    9536        3395 :     E1 = RgV_to_RgX(mkeisen(E1, pn, P, lim), 0);
    9537        3395 :     if (E2)
    9538             :     {
    9539        1281 :       E2 = RgV_to_RgX(mkeisen(E2, pn, P, lim), 0);
    9540        1281 :       E1 = RgXn_mul(E1, E2, L);
    9541             :     }
    9542        3395 :     E1 = RgX_to_RgC(E1, L);
    9543        3395 :     if (P && E2) E1 = RgXQV_red(E1, P);
    9544        3395 :     gel(M,j) = E1;
    9545             :   }
    9546         441 :   return M;
    9547             : }
    9548             : 
    9549             : /* assume N > 2 */
    9550             : static GEN
    9551          21 : mffindeisen1(long N)
    9552             : {
    9553          21 :   GEN G = znstar0(utoipos(N), 1), L = chargalois(G, NULL), chi0 = NULL;
    9554          21 :   long j, m = N, l = lg(L);
    9555         154 :   for (j = 1; j < l; j++)
    9556             :   {
    9557         147 :     GEN chi = gel(L,j);
    9558         147 :     long r = myeulerphiu(itou(zncharorder(G,chi)));
    9559         147 :     if (r >= m) continue;
    9560         105 :     chi = znconreyfromchar(G, chi);
    9561         105 :     if (zncharisodd(G,chi)) { m = r; chi0 = chi; if (r == 1) break; }
    9562             :   }
    9563          21 :   if (!chi0) pari_err_BUG("mffindeisen1 [no Eisenstein series found]");
    9564          21 :   chi0 = znchartoprimitive(G,chi0);
    9565          21 :   return mfcharGL(gel(chi0,1), gel(chi0,2));
    9566             : }
    9567             : 
    9568             : static GEN
    9569         469 : mfeisensteinspaceinit_i(long N, long k, GEN CHI)
    9570             : {
    9571         469 :   GEN M, Minv, vj, vG, GN, allN, P, vz, z = NULL;
    9572         469 :   long nCHI, lim, ell, ord, pn, dim = mffulldim(N, k, CHI);
    9573             :   ulong r, p;
    9574             : 
    9575         469 :   if (!dim) retmkvec3(cgetg(1,t_VECSMALL),
    9576             :                       mkvec2(cgetg(1,t_MAT),gen_1),cgetg(1,t_VEC));
    9577         469 :   lim = mfsturmNk(N, k) + 1;
    9578         469 :   allN = charsmodN(N);
    9579         469 :   vG = gel(allN,3);
    9580         469 :   GN = gel(vG,N);
    9581         469 :   pn = znstar_get_expo(GN);
    9582         469 :   ord = ord_canon(pn);
    9583         469 :   P = ord == 1? NULL: polcyclo(ord, varn(mfcharpol(CHI)));
    9584         469 :   CHI = induce(GN, CHI); /* lift CHI mod N before mfcharno*/
    9585         469 :   nCHI = mfcharno(CHI);
    9586         469 :   r = QabM_init(ord, &p);
    9587         469 :   vz = Fl_powers(r, pn, p);
    9588         469 :   getcols(&M, &vj, k, nCHI, allN, vz, p, lim);
    9589         483 :   for (ell = k>>1; ell >= 1; ell--)
    9590         301 :     if (getcolsgen(dim, &M, &vj, &z, k, ell, nCHI, allN, vz, p, lim)) break;
    9591         469 :   if (!z) update_Mj(&M, &vj, &z, p);
    9592         469 :   if (lg(vj) - 1 < dim) return NULL;
    9593         441 :   M = mkM(vj, pn, P, lim);
    9594         441 :   Minv = QabM_Minv(rowpermute(M, gel(z,1)), P, ord);
    9595         441 :   return mkvec4(gel(z,1), Minv, vj, utoi(ord));
    9596             : }
    9597             : /* true mf */
    9598             : static GEN
    9599         441 : mfeisensteinspaceinit(GEN mf)
    9600             : {
    9601         441 :   pari_sp av = avma;
    9602         441 :   GEN z, CHI = MF_get_CHI(mf);
    9603         441 :   long N = MF_get_N(mf), k = MF_get_k(mf);
    9604         441 :   if (!CHI) CHI = mfchartrivial();
    9605         441 :   z = mfeisensteinspaceinit_i(N, k, CHI);
    9606         441 :   if (!z)
    9607             :   {
    9608          21 :     GEN E, CHIN = mffindeisen1(N), CHI0 = mfchartrivial();
    9609          21 :     z = mfeisensteinspaceinit_i(N, k+1, mfcharmul(CHI, CHIN));
    9610          21 :     if (z) E = mkvec4(gen_1, CHI0, CHIN, gen_1);
    9611             :     else
    9612             :     {
    9613           7 :       z = mfeisensteinspaceinit_i(N, k+2, CHI);
    9614           7 :       E = mkvec4(gen_2, CHI0, CHI0, utoipos(N));
    9615             :     }
    9616          21 :     z = mkvec2(z, E);
    9617             :   }
    9618         441 :   return gerepilecopy(av, z);
    9619             : }
    9620             : 
    9621             : /* decomposition of modular form on eisenspace */
    9622             : static GEN
    9623         826 : mfeisensteindec(GEN mf, GEN F)
    9624             : {
    9625         826 :   pari_sp av = avma;
    9626             :   GEN M, Mindex, Mvecj, V, B, CHI;
    9627             :   long o, ord;
    9628             : 
    9629         826 :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    9630         826 :   if (lg(Mvecj) < 5)
    9631             :   {
    9632          21 :     GEN E, e = gel(Mvecj,2), gkE = gel(e,1);
    9633          21 :     long dE = itou(gel(e,4));
    9634          21 :     Mvecj = gel(Mvecj,1);
    9635          21 :     E = mfeisenstein(itou(gkE), NULL, gel(e,3));
    9636          21 :     if (dE != 1) E = mfbd_E2(E, dE, gel(e,2)); /* here k = 2 */
    9637          21 :     F = mfmul(F, E);
    9638             :   }
    9639         826 :   M = gel(Mvecj, 2);
    9640         826 :   if (lg(M) == 1) return cgetg(1, t_VEC);
    9641         826 :   Mindex = gel(Mvecj, 1);
    9642         826 :   ord = itou(gel(Mvecj,4));
    9643         826 :   V = mfcoefs(F, Mindex[lg(Mindex)-1]-1, 1); settyp(V, t_COL);
    9644         826 :   CHI = mf_get_CHI(F);
    9645         826 :   o = mfcharorder_canon(CHI);
    9646         826 :   if (o > 1 && o != ord)
    9647             :   { /* convert Mod(.,polcyclo(o)) to Mod(., polcyclo(N)) for o | N,
    9648             :      * o and N both != 2 (mod 4) */
    9649          49 :     GEN z, P = gel(M,4); /* polcyclo(ord) */
    9650          49 :     long vt = varn(P);
    9651          49 :     z = gmodulo(pol_xn(ord/o, vt), P);
    9652          49 :     if (ord % o) pari_err_TYPE("mfeisensteindec", V);
    9653          49 :     V = gsubst(liftpol_shallow(V), vt, z);
    9654             :   }
    9655         826 :   B = Minv_RgC_mul(M, vecpermute(V, Mindex));
    9656         826 :   return gerepileupto(av, B);
    9657             : }
    9658             : 
    9659             : /*********************************************************************/
    9660             : /*                        END EISENSPACE                             */
    9661             : /*********************************************************************/
    9662             : 
    9663             : static GEN
    9664          70 : sertocol2(GEN S, long l)
    9665             : {
    9666          70 :   GEN C = cgetg(l + 2, t_COL);
    9667             :   long i;
    9668          70 :   for (i = 0; i <= l; i++) gel(C, i+1) = polcoef_i(S, i, -1);
    9669          70 :   return C;
    9670             : }
    9671             : 
    9672             : /* Compute polynomial P0 such that F=E4^(k/4)P0(E6/E4^(3/2)). */
    9673             : static GEN
    9674          14 : mfcanfindp0(GEN F, long k)
    9675             : {
    9676          14 :   pari_sp ltop = avma;
    9677             :   GEN E4, E6, V, V1, Q, W, res, M, B;
    9678             :   long l, j;
    9679          14 :   l = k/6 + 2;
    9680          14 :   V = mfcoefsser(F,l);
    9681          14 :   E4 = mfcoefsser(mfEk(4),l);
    9682          14 :   E6 = mfcoefsser(mfEk(6),l);
    9683          14 :   V1 = gdiv(V, gpow(E4, sstoQ(k,4), 0));
    9684          14 :   Q = gdiv(E6, gpow(E4, sstoQ(3,2), 0));
    9685          14 :   W = gpowers(Q, l - 1);
    9686          14 :   M = cgetg(l + 1, t_MAT);
    9687          14 :   for (j = 1; j <= l; j++) gel(M,j) = sertocol2(gel(W,j), l);
    9688          14 :   B = sertocol2(V1, l);
    9689          14 :   res = inverseimage(M, B);
    9690          14 :   if (lg(res) == 1) err_space(F);
    9691          14 :   return gerepilecopy(ltop, gtopolyrev(res, 0));
    9692             : }
    9693             : 
    9694             : /* Compute the first n+1 Taylor coeffs at tau=I of a modular form
    9695             :  * on SL_2(Z). */
    9696             : GEN
    9697          14 : mftaylor(GEN F, long n, long flreal, long prec)
    9698             : {
    9699          14 :   pari_sp ltop = avma;
    9700          14 :   GEN P0, Pm1 = gen_0, v;
    9701          14 :   GEN X2 = mkpoln(3, ghalf,gen_0,gneg(ghalf)); /* (x^2-1) / 2 */
    9702             :   long k, m;
    9703          14 :   if (!checkmf_i(F)) pari_err_TYPE("mftaylor",F);
    9704          14 :   k = mf_get_k(F);
    9705          14 :   if (mf_get_N(F) != 1 || k < 0) pari_err_IMPL("mftaylor for this form");
    9706          14 :   P0 = mfcanfindp0(F, k);
    9707          14 :   v = cgetg(n+2, t_VEC); gel(v, 1) = RgX_coeff(P0,0);
    9708         154 :   for (m = 0; m < n; m++)
    9709             :   {
    9710         140 :     GEN P1 = gdivgs(gmulsg(-(k + 2*m), RgX_shift(P0,1)), 12);
    9711         140 :     P1 = gadd(P1, gmul(X2, RgX_deriv(P0)));
    9712         140 :     if (m) P1 = gsub(P1, gdivgs(gmulsg(m*(m+k-1), Pm1), 144));
    9713         140 :     Pm1 = P0; P0 = P1;
    9714         140 :     gel(v, m+2) = RgX_coeff(P0, 0);
    9715             :   }
    9716          14 :   if (flreal)
    9717             :   {
    9718           7 :     GEN pi2 = Pi2n(1, prec), pim4 = gmulsg(-2, pi2), VPC;
    9719           7 :     GEN C = gmulsg(3, gdiv(gpowgs(ggamma(ginv(utoi(4)), prec), 8), gpowgs(pi2, 6)));
    9720             :     /* E_4(i): */
    9721           7 :     GEN facn = gen_1;
    9722           7 :     VPC = gpowers(gmul(pim4, gsqrt(C, prec)), n);
    9723           7 :     C = gpow(C, sstoQ(k,4), prec);
    9724          84 :     for (m = 0; m <= n; m++)
    9725             :     {
    9726          77 :       gel(v, m+1) = gdiv(gmul(C, gmul(gel(v, m+1), gel(VPC, m+1))), facn);
    9727          77 :       facn = gmulgs(facn, m+1);
    9728             :     }
    9729             :   }
    9730          14 :   return gerepilecopy(ltop, v);
    9731             : }
    9732             : 
    9733             : #if 0
    9734             : /* To be used in mfeigensearch() */
    9735             : GEN
    9736             : mfreadratfile()
    9737             : {
    9738             :   GEN eqn;
    9739             :   pariFILE *F = pari_fopengz("rateigen300.gp");
    9740             :   eqn = gp_readvec_stream(F->file);
    9741             :   pari_fclose(F);
    9742             :   return eqn;
    9743             : }
    9744             : #endif
    9745             :  /*****************************************************************/
    9746             : /*           EISENSTEIN CUSPS: COMPLEX DIRECTLY: one F_k         */
    9747             : /*****************************************************************/
    9748             : 
    9749             : /* CHIvec = charinit(CHI); data = [N1g/g1,N2g/g2,g1/g,g2/g,C/g1,C/g2,
    9750             :  * (N1g/g1)^{-1},(N2g/g2)^{-1}] */
    9751             : 
    9752             : /* nm = n/m;
    9753             :  * z1 = powers of \z_{C/g}^{(Ae/g)^{-1}},
    9754             :  * z2 = powers of \z_N^{A^{-1}(g1g2/C)}]
    9755             :  * N.B. : we compute value and conjugate at the end, so it is (Ae/g)^{-1}
    9756             :  * and not -(Ae/g)^{-1} */
    9757             : static GEN
    9758     5688536 : eiscnm(long nm, long m, GEN CHI1vec, GEN CHI2vec, GEN data, GEN z1)
    9759             : {
    9760     5688536 :   long Cg1 = data[5], s10 = (nm*data[7]) % Cg1, r10 = (nm - data[1]*s10) / Cg1;
    9761     5688536 :   long Cg2 = data[6], s20 = (m *data[8]) % Cg2, r20 = (m  - data[2]*s20) / Cg2;
    9762             :   long j1, r1, s1;
    9763     5688536 :   GEN T = gen_0;
    9764    14497770 :   for (j1 = 0, r1 = r10, s1 = s10; j1 < data[3]; j1++, r1 -= data[1], s1 += Cg1)
    9765             :   {
    9766     8809234 :     GEN c1 = mychareval(CHI1vec, r1);
    9767     8809234 :     if (!gequal0(c1))
    9768             :     {
    9769             :       long j2, r2, s2;
    9770     5958792 :       GEN S = gen_0;
    9771    16575468 :       for (j2 = 0, r2 = r20, s2 = s20; j2 < data[4]; j2++, r2 -= data[2], s2 += Cg2)
    9772             :       {
    9773    10616676 :         GEN c2 = mychareval(CHI2vec, r2);
    9774    10616676 :         if (!gequal0(c2)) S = gadd(S, gmul(c2, rootsof1pow(z1, s1*s2)));
    9775             :       }
    9776     5958792 :       T = gadd(T, gmul(c1, S));
    9777             :     }
    9778             :   }
    9779     5688536 :   return conj_i(T);
    9780             : }
    9781             : 
    9782             : static GEN
    9783      447062 : fg1g2n(long n, long k, GEN CHI1vec, GEN CHI2vec, GEN data, GEN z1, GEN z2)
    9784             : {
    9785      447062 :   pari_sp av = avma;
    9786      447062 :   GEN S = gen_0, D = mydivisorsu(n);
    9787      447062 :   long i, l = lg(D);
    9788     3291330 :   for (i = 1; i < l; i++)
    9789             :   {
    9790     2844268 :     long m = D[i], nm = D[l-i]; /* n/m */
    9791     2844268 :     GEN u = eiscnm( nm,  m, CHI1vec, CHI2vec, data, z1);
    9792     2844268 :     GEN v = eiscnm(-nm, -m, CHI1vec, CHI2vec, data, z1);
    9793     2844268 :     GEN w = odd(k) ? gsub(u, v) : gadd(u, v);
    9794     2844268 :     S = gadd(S, gmul(powuu(m, k-1), w));
    9795             :   }
    9796      447062 :   return gerepileupto(av, gmul(S, rootsof1pow(z2, n)));
    9797             : }
    9798             : 
    9799             : static GEN
    9800       11312 : gausssumcx(GEN CHIvec, long prec)
    9801             : {
    9802       11312 :   GEN z, S, V = CHIvec_val(CHIvec);
    9803       11312 :   long m, N = CHIvec_N(CHIvec);
    9804       11312 :   z = rootsof1u_cx(N, prec);
    9805       11312 :   S = gmul(z, gel(V, N));
    9806       11312 :   for (m = N-1; m >= 1; m--) S = gmul(z, gadd(gel(V, m), S));
    9807       11312 :   return S;
    9808             : }
    9809             : 
    9810             : /* Computation of Q_k(\z_N^s) as a polynomial in \z_N^s. FIXME: explicit
    9811             :  * formula ? */
    9812             : static GEN
    9813        1715 : mfqk(long k, long N)
    9814             : {
    9815        1715 :   GEN X = pol_x(0), P = gsubgs(gpowgs(X,N), 1), ZI, Q, Xm1, invden;
    9816             :   long i;
    9817        1715 :   ZI = cgetg(N, t_VEC);
    9818        1715 :   for (i = 1; i < N; i++) gel(ZI, i) = utoi(i);
    9819        1715 :   ZI = gdivgs(gmul(X, gtopolyrev(ZI, 0)), N);
    9820        1715 :   if (k == 1) return ZI;
    9821        1071 :   invden = RgXQ_powu(ZI, k, P);
    9822        1071 :   Q = gneg(X); Xm1 = gsubgs(X, 1);
    9823        2716 :   for (i = 2; i < k; i++)
    9824        1645 :     Q = gmul(X, ZX_add(gmul(Xm1, ZX_deriv(Q)), gmulsg(-i, Q)));
    9825        1071 :   return RgXQ_mul(Q, invden, P);
    9826             : }
    9827             : /* CHI mfchar */
    9828             : /* Warning: M is a multiple of the conductor of CHI, but is NOT
    9829             :    necessarily its modulus */
    9830             : 
    9831             : static GEN
    9832        2492 : mfskcx(long k, GEN CHI, long M, long prec)
    9833             : {
    9834             :   GEN S, CHIvec, P;
    9835             :   long F, m, i, l;
    9836        2492 :   CHI = mfchartoprimitive(CHI, &F);
    9837        2492 :   CHIvec = mfcharcxinit(CHI, prec);
    9838        2492 :   if (F == 1) S = gdivgs(bernfrac(k), k);
    9839             :   else
    9840             :   {
    9841        1715 :     GEN Q = mfqk(k, F), V = CHIvec_val(CHIvec);
    9842        1715 :     S = gmul(gel(V, F), RgX_coeff(Q, 0));
    9843        1715 :     for (m = 1; m < F; m++) S = gadd(S, gmul(gel(V, m), RgX_coeff(Q, m)));
    9844        1715 :     S = conj_i(S);
    9845             :   }
    9846             :   /* prime divisors of M not dividing f(chi) */
    9847        2492 :   P = gel(myfactoru(u_ppo(M/F,F)), 1); l = lg(P);
    9848        2618 :   for (i = 1; i < l; i++)
    9849             :   {
    9850         126 :     long p = P[i];
    9851         126 :     S = gmul(S, gsubsg(1, gdiv(mychareval(CHIvec, p), powuu(p, k))));
    9852             :   }
    9853        2492 :   return gmul(gmul(gausssumcx(CHIvec, prec), S), powuu(M/F, k));
    9854             : }
    9855             : 
    9856             : static GEN
    9857        4599 : f00_i(long k, GEN CHI1vec, GEN CHI2vec, GEN G2, GEN S, long prec)
    9858             : {
    9859             :   GEN c, a;
    9860        4599 :   long N1 = CHIvec_N(CHI1vec), N2 = CHIvec_N(CHI2vec);
    9861        4599 :   if (S[2] != N1) return gen_0;
    9862        2492 :   c = mychareval(CHI1vec, S[3]);
    9863        2492 :   if (isintzero(c)) return gen_0;
    9864        2492 :   a = mfskcx(k, mfchardiv(CHIvec_CHI(CHI2vec), CHIvec_CHI(CHI1vec)), N1*N2, prec);
    9865        2492 :   a = gmul(a, conj_i(gmul(c,G2)));
    9866        2492 :   return gdiv(a, mulsi(-N2, powuu(S[1], k-1)));
    9867             : }
    9868             : 
    9869             : static GEN
    9870        3836 : f00(long k, GEN CHI1vec,GEN CHI2vec, GEN G1,GEN G2, GEN data, long prec)
    9871             : {
    9872             :   GEN T1, T2;
    9873        3836 :   T2 = f00_i(k, CHI1vec, CHI2vec, G2, data, prec);
    9874        3836 :   if (k > 1) return T2;
    9875         763 :   T1 = f00_i(k, CHI2vec, CHI1vec, G1, data, prec);
    9876         763 :   return gadd(T1, T2);
    9877             : }
    9878             : 
    9879             : /* ga in SL_2(Z), find beta [a,b;c,d] in Gamma_0(N) and mu in Z such that
    9880             :  * beta * ga * T^u = [A',B';C',D'] with C' | N and N | B', C' > 0 */
    9881             : static void
    9882        4410 : mfgatogap(GEN ga, long N, long *pA, long *pC, long *pD, long *pd, long *pmu)
    9883             : {
    9884        4410 :   GEN A = gcoeff(ga,1,1), B = gcoeff(ga,1,2);
    9885        4410 :   GEN C = gcoeff(ga,2,1), D = gcoeff(ga,2,2), a, b, c, d;
    9886             :   long t, Ap, Cp, B1, D1, mu;
    9887        4410 :   Cp = itou(bezout(muliu(A,N), C, &c, &d)); /* divides N */
    9888        4410 :   t = 0;
    9889        4410 :   if (Cp > 1)
    9890             :   { /* (d, N/Cp) = 1, find t such that (d - t*(A*N/Cp), N) = 1 */
    9891        2345 :     long dN = umodiu(d,Cp), Q = (N/Cp * umodiu(A,Cp)) % Cp;
    9892        2345 :     while (ugcd(dN, Cp) > 1) { t++; dN = Fl_sub(dN, Q, Cp); }
    9893             :   }
    9894        4410 :   if (t)
    9895             :   {
    9896         371 :     c = addii(c, mului(t, diviuexact(C,Cp)));
    9897         371 :     d = subii(d, mului(t, muliu(A, N/Cp))); /* (d,N) = 1 */
    9898             :   }
    9899        4410 :   D1 = umodiu(mulii(d,D), N);
    9900        4410 :   (void)bezout(d, mulis(c,-N), &a, &b); /* = 1 */
    9901        4410 :   t = 0; Ap = umodiu(addii(mulii(a,A), mulii(b,C)), N); /* (Ap,Cp) = 1 */
    9902        4410 :   while (ugcd(Ap, N) > 1) { t++; Ap = Fl_add(Ap, Cp, N); }
    9903        4410 :   B1 = umodiu(a,N)*umodiu(B,N) + umodiu(b,N)*umodiu(D,N) + t*D1;
    9904        4410 :   B1 %= N;
    9905        4410 :   *pmu = mu = Fl_neg(Fl_div(B1, Ap, N), N);
    9906             :   /* A', D' and d only needed modulo N */
    9907        4410 :   *pd = umodiu(d, N);
    9908        4410 :   *pA = Ap;
    9909        4410 :   *pC = Cp; *pD = (D1 + Cp*mu) % N;
    9910        4410 : }
    9911             : 
    9912             : #if 0
    9913             : /* CHI is a mfchar, return alpha(CHI) */
    9914             : static long
    9915             : mfalchi(GEN CHI, long AN, long cg)
    9916             : {
    9917             :   GEN G = gel(CHI,1), chi = gel(CHI,2), go = gmfcharorder(CHI);
    9918             :   long o = itou(go), a = itos( znchareval(G, chi, stoi(1 + AN/cg), go) );
    9919             :   if (a < 0 || (cg * a) % o) pari_err_BUG("mfalchi");
    9920             :   return (cg * a) / o;
    9921             : }
    9922             : #endif
    9923             : /* return A such that CHI1(t) * CHI2(t) = e(A) or NULL if (t,N1*N2) > 1 */
    9924             : static GEN
    9925        8820 : mfcharmuleval(GEN CHI1vec, GEN CHI2vec, long t)
    9926             : {
    9927        8820 :   long a1 = mycharexpo(CHI1vec, t), o1 = CHIvec_ord(CHI1vec);
    9928        8820 :   long a2 = mycharexpo(CHI2vec, t), o2 = CHIvec_ord(CHI2vec);;
    9929        8820 :   if (a1 < 0 || a2 < 0) return NULL;
    9930        8820 :   return sstoQ(a1*o2 + a2*o1, o1*o2);
    9931             : }
    9932             : static GEN
    9933        4410 : mfcharmulcxeval(GEN CHI1vec, GEN CHI2vec, long t, long prec)
    9934             : {
    9935        4410 :   GEN A = mfcharmuleval(CHI1vec, CHI2vec, t);
    9936             :   long n, d;
    9937        4410 :   if (!A) return gen_0;
    9938        4410 :   Qtoss(A, &n,&d); return rootsof1q_cx(n, d, prec);
    9939             : }
    9940             : /* alpha(CHI1 * CHI2) */
    9941             : static long
    9942        4410 : mfalchi2(GEN CHI1vec, GEN CHI2vec, long AN, long cg)
    9943             : {
    9944        4410 :   GEN A = mfcharmuleval(CHI1vec, CHI2vec, 1 + AN/cg);
    9945             :   long a;
    9946        4410 :   if (!A) pari_err_BUG("mfalchi2");
    9947        4410 :   A = gmulsg(cg, A);
    9948        4410 :   if (typ(A) != t_INT) pari_err_BUG("mfalchi2");
    9949        4410 :   a = itos(A) % cg; if (a < 0) a += cg;
    9950        4410 :   return a;
    9951             : }
    9952             : 
    9953             : /* return g = (a,b), set u >= 0 s.t. g = a * u (mod b) */
    9954             : static long
    9955       17640 : mybezout(long a, long b, long *pu)
    9956             : {
    9957       17640 :   long junk, g = cbezout(a, b, pu, &junk);
    9958       17640 :   if (*pu < 0) *pu += b/g;
    9959       17640 :   return g;
    9960             : }
    9961             : 
    9962             : /* E = [k, CHI1,CHI2, e], CHI1 and CHI2 primitive mfchars such that,
    9963             :  * CHI1(-1)*CHI2(-1) = (-1)^k; expansion of (B_e (E_k(CHI1,CHI2))) | ga.
    9964             :  * w is the width for the space of the calling function. */
    9965             : static GEN
    9966        4410 : mfeisensteingacx(GEN E, long w, GEN ga, long lim, long prec)
    9967             : {
    9968        4410 :   GEN CHI1vec, CHI2vec, CHI1 = gel(E,2), CHI2 = gel(E,3), v, S, ALPHA;
    9969             :   GEN G1, G2, z1, z2, data;
    9970        4410 :   long k = itou(gel(E,1)), e = itou(gel(E,4));
    9971        4410 :   long N1 = mfcharmodulus(CHI1);
    9972        4410 :   long N2 = mfcharmodulus(CHI2), N = e * N1 * N2;
    9973             :   long NsurC, cg, wN, A, C, Ai, d, mu, alchi, na, da;
    9974             :   long eg, g, gH, U, u0, u1, u2, Aig, H, m, n, t, Cg, NC1, NC2;
    9975             : 
    9976        4410 :   mfgatogap(ga, N, &A, &C, &Ai, &d, &mu);
    9977        4410 :   CHI1vec = mfcharcxinit(CHI1, prec);
    9978        4410 :   CHI2vec = mfcharcxinit(CHI2, prec);
    9979        4410 :   NsurC = N/C; cg  = ugcd(C, NsurC); wN = NsurC / cg;
    9980        4410 :   if (w%wN) pari_err_BUG("mfeisensteingacx [wN does not divide w]");
    9981        4410 :   alchi = mfalchi2(CHI1vec, CHI2vec, A*N, cg);
    9982        4410 :   ALPHA = sstoQ(alchi, NsurC);
    9983             : 
    9984        4410 :   g = mybezout(A*e, C, &u0); Cg = C/g; eg = e/g;
    9985        4410 :   NC1 = mybezout(N1, Cg, &u1);
    9986        4410 :   NC2 = mybezout(N2, Cg, &u2);
    9987        4410 :   H = (NC1*NC2*g)/Cg;
    9988        4410 :   Aig = (Ai*H)%N; if (Aig < 0) Aig += N;
    9989        4410 :   z1 = rootsof1powinit(u0, Cg, prec);
    9990        4410 :   z2 = rootsof1powinit(Aig, N, prec);
    9991        4410 :   data = mkvecsmalln(8, N1/NC1, N2/NC2, NC1, NC2, Cg/NC1, Cg/NC2, u1, u2);
    9992        4410 :   v = zerovec(lim + 1);
    9993             :   /* need n*H = alchi (mod cg) */
    9994        4410 :   gH = mybezout(H, cg, &U);
    9995        4410 :   if (gH > 1)
    9996             :   {
    9997         357 :     if (alchi % gH) return mkvec2(gen_0, v);
    9998         357 :     alchi /= gH; cg /= gH; H /= gH;
    9999             :   }
   10000        4410 :   G1 = gausssumcx(CHI1vec, prec);
   10001        4410 :   G2 = gausssumcx(CHI2vec, prec);
   10002        4410 :   if (!alchi)
   10003        3836 :     gel(v,1) = f00(k, CHI1vec,CHI2vec,G1,G2, mkvecsmall3(NC2,Cg,A*eg), prec);
   10004        4410 :   n = Fl_mul(alchi,U,cg); if (!n) n = cg;
   10005        4410 :   m = (n*H - alchi) / cg; /* positive, exact division */
   10006      451472 :   for (; m <= lim; n+=cg, m+=H)
   10007      447062 :     gel(v, m+1) = fg1g2n(n, k, CHI1vec, CHI2vec, data, z1,z2);
   10008        4410 :   t = (2*e)/g; if (odd(k)) t = -t;
   10009        4410 :   v = gdiv(v, gmul(conj_i(gmul(G1,G2)), mulsi(t, powuu(eg*N2/NC2, k-1))));
   10010        4410 :   if (k == 2 && N1 == 1 && N2 == 1) v = gsub(mkF2bd(wN,lim), gmulsg(e,v));
   10011             : 
   10012        4410 :   Qtoss(ALPHA, &na,&da);
   10013        4410 :   S = conj_i( mfcharmulcxeval(CHI1vec,CHI2vec,d,prec) ); /* CHI(1/d) */
   10014        4410 :   if (wN > 1)
   10015             :   {
   10016        3269 :     GEN z = rootsof1powinit(-mu, wN, prec);
   10017        3269 :     long i, l = lg(v);
   10018        3269 :     for (i = 1; i < l; i++) gel(v,i) = gmul(gel(v,i), rootsof1pow(z,i-1));
   10019             :   }
   10020        4410 :   v = RgV_Rg_mul(v, gmul(S, rootsof1q_cx(-mu*na, da, prec)));
   10021        4410 :   return mkvec2(ALPHA, bdexpand(v, w/wN));
   10022             : }
   10023             : 
   10024             : /*****************************************************************/
   10025             : /*                       END EISENSTEIN CUSPS                    */
   10026             : /*****************************************************************/
   10027             : 
   10028             : static GEN
   10029        1582 : mfchisimpl(GEN CHI)
   10030             : {
   10031             :   GEN G, chi;
   10032        1582 :   if (typ(CHI) == t_INT) return CHI;
   10033        1582 :   G = gel(CHI, 1); chi = gel(CHI, 2);
   10034        1582 :   switch(mfcharorder(CHI))
   10035             :   {
   10036        1134 :     case 1: chi = gen_1; break;
   10037         427 :     case 2: chi = znchartokronecker(G,chi,1); break;
   10038          21 :     default:chi = mkintmod(znconreyexp(G,chi), znstar_get_N(G)); break;
   10039             :   }
   10040        1582 :   return chi;
   10041             : }
   10042             : 
   10043             : GEN
   10044         700 : mfparams(GEN F)
   10045             : {
   10046         700 :   pari_sp av = avma;
   10047             :   GEN z, mf;
   10048         700 :   if ((mf = checkMF_i(F)))
   10049             :   {
   10050          14 :     long N = MF_get_N(mf);
   10051          14 :     GEN gk = MF_get_gk(mf);
   10052          14 :     z = mkvec4(utoi(N), gk, MF_get_CHI(mf), utoi(MF_get_space(mf)));
   10053             :   }
   10054             :   else
   10055             :   {
   10056         686 :     if (!checkmf_i(F)) pari_err_TYPE("mfparams", F);
   10057         686 :     z = shallowcopy( mf_get_NK(F) );
   10058             :   }
   10059         700 :   gel(z,3) = mfchisimpl(gel(z,3));
   10060         700 :   return gerepilecopy(av, z);
   10061             : }
   10062             : 
   10063             : GEN
   10064          14 : mfisCM(GEN F)
   10065             : {
   10066          14 :   pari_sp av = avma;
   10067             :   forprime_t S;
   10068             :   GEN D, v;
   10069             :   long N, k, lD, sb, p, i;
   10070          14 :   if (!checkmf_i(F)) pari_err_TYPE("mfisCM", F);
   10071          14 :   N = mf_get_N(F);
   10072          14 :   k = mf_get_k(F); if (N < 0 || k < 0) pari_err_IMPL("mfisCM for this F");
   10073          14 :   D = mfunram(N, -1);
   10074          14 :   lD = lg(D);
   10075          14 :   sb = maxss(mfsturmNk(N, k), 4*N);
   10076          14 :   v = mfcoefs_i(F, sb, 1);
   10077          14 :   u_forprime_init(&S, 2, sb);
   10078         518 :   while ((p = u_forprime_next(&S)))
   10079             :   {
   10080         490 :     GEN ap = gel(v, p+1);
   10081         490 :     if (!gequal0(ap))
   10082         406 :       for (i = 1; i < lD; i++)
   10083         245 :         if (kross(D[i], p) == -1) { D = vecsplice(D, i); lD--; }
   10084             :   }
   10085          14 :   if (lD == 1) { avma = av; return gen_0; }
   10086          14 :   if (lD == 2) { avma = av; return stoi(D[1]); }
   10087           7 :   if (k > 1) pari_err_BUG("mfisCM");
   10088           7 :   return gerepileupto(av, zv_to_ZV(D));
   10089             : }
   10090             : 
   10091             : static long
   10092         287 : mfspace_i(GEN mf, GEN F)
   10093             : {
   10094             :   GEN v, vF, gk;
   10095             :   long n, nE, i, l, s, N;
   10096             : 
   10097         287 :   mf = checkMF(mf); s = MF_get_space(mf);
   10098         287 :   if (!F) return s;
   10099         287 :   if (!checkmf_i(F)) pari_err_TYPE("mfspace",F);
   10100         287 :   v = mftobasis(mf, F, 1);
   10101         287 :   n = lg(v)-1; if (!n) return -1;
   10102         231 :   nE = lg(MF_get_E(mf))-1;
   10103         231 :   switch(s)
   10104             :   {
   10105          56 :     case mf_NEW: case mf_OLD: case mf_EISEN: return s;
   10106             :     case mf_FULL:
   10107         140 :       if (mf_get_type(F) == t_MF_THETA) return mf_EISEN;
   10108         133 :       if (!gequal0(vecslice(v,1,nE)))
   10109          63 :         return gequal0(vecslice(v,nE+1,n))? mf_EISEN: mf_FULL;
   10110             :   }
   10111             :   /* mf is mf_CUSP or mf_FULL, F a cusp form */
   10112         105 :   gk = mf_get_gk(F);
   10113         105 :   if (typ(gk) == t_FRAC || equali1(gk)) return mf_CUSP;
   10114          91 :   vF = mftonew_i(mf, vecslice(v, nE+1, n), &N);
   10115          91 :   if (N != MF_get_N(mf)) return mf_OLD;
   10116          63 :   l = lg(vF);
   10117         105 :   for (i = 1; i < l; i++)
   10118          63 :     if (itos(gmael(vF,i,1)) != N) return mf_CUSP;
   10119          42 :   return mf_NEW;
   10120             : }
   10121             : long
   10122         287 : mfspace(GEN mf, GEN F)
   10123             : {
   10124         287 :   pari_sp av = avma;
   10125         287 :   long s = mfspace_i(mf,F);
   10126         287 :   avma = av; return s;
   10127             : }
   10128             : static GEN
   10129           7 : lfunfindchi(GEN ldata, GEN van, long prec)
   10130             : {
   10131           7 :   GEN gN = ldata_get_conductor(ldata), G = znstar0(gN,1), L, go, vz;
   10132           7 :   long k = ldata_get_k(ldata), N = itou(gN), bit = 10 - prec2nbits(prec);
   10133           7 :   long i, j, o, l, odd = k & 1, B0 = 2, B = lg(van)-1;
   10134             : 
   10135           7 :   van = shallowcopy(van);
   10136           7 :   L = cyc2elts(znstar_get_conreycyc(G));
   10137           7 :   l = lg(L);
   10138          21 :   for (i = j = 1; i < l; i++)
   10139             :   {
   10140          14 :     GEN chi = zc_to_ZC(gel(L,i));
   10141          14 :     if (zncharisodd(G,chi) == odd) gel(L,j++) = mfcharGL(G,chi);
   10142             :   }
   10143           7 :   setlg(L,j); l = j;
   10144           7 :   if (l <= 2) return gel(L,1);
   10145           0 :   o = znstar_get_expo(G); go = utoi(o);
   10146           0 :   vz = grootsof1(o, prec);
   10147             :   for (;;)
   10148           0 :   {
   10149             :     long n;
   10150           0 :     for (n = B0; n <= B; n++)
   10151             :     {
   10152           0 :       GEN an = gel(van,n), r;
   10153             :       long j;
   10154           0 :       if (ugcd(n, N) != 1 || gexpo(an) < bit) continue;
   10155           0 :       r = gdiv(an, conj_i(an));
   10156           0 :       for (i = 1; i < l; i++)
   10157             :       {
   10158           0 :         GEN CHI = gel(L,i);
   10159           0 :         if (gexpo(gsub(r, gel(vz, znchareval_i(CHI,n,go)+1))) > bit)
   10160           0 :           gel(L,i) = NULL;
   10161             :       }
   10162           0 :       for (i = j = 1; i < l; i++)
   10163           0 :         if (gel(L,i)) gel(L,j++) = gel(L,i);
   10164           0 :       l = j; setlg(L,l);
   10165           0 :       if (l == 2) return gel(L,1);
   10166             :     }
   10167           0 :     B0 = B+1; B <<= 1;
   10168           0 :     van = ldata_vecan(ldata_get_an(ldata), B, prec);
   10169             :   }
   10170             : }
   10171             : 
   10172             : GEN
   10173           7 : mffromlfun(GEN L, long prec)
   10174             : {
   10175           7 :   pari_sp av = avma;
   10176           7 :   GEN ldata = lfunmisc_to_ldata_shallow(L), Vga = ldata_get_gammavec(ldata);
   10177             :   GEN van, a0, CHI, NK;
   10178             :   long k, N, space;
   10179           7 :   if (!gequal(Vga, mkvec2(gen_0, gen_1))) pari_err_TYPE("mffromlfun", L);
   10180           7 :   k = ldata_get_k(ldata);
   10181           7 :   N = itou(ldata_get_conductor(ldata));
   10182           7 :   van = ldata_vecan(ldata_get_an(ldata), mfsturmNk(N,k) + 2, prec);
   10183           7 :   CHI = lfunfindchi(ldata, van, prec);
   10184           7 :   space = (lg(ldata) == 7)? mf_CUSP: mf_FULL;
   10185           7 :   a0 = (space == mf_CUSP)? gen_0: gneg(lfun(L, gen_0, prec2nbits(prec)));
   10186           7 :   NK = mkvec3(utoi(N), utoi(k), mfchisimpl(CHI));
   10187           7 :   return gerepilecopy(av, mkvec3(NK, utoi(space), shallowconcat(a0, van)));
   10188             : }
   10189             : /*******************************************************************/
   10190             : /*                                                                 */
   10191             : /*                       HALF-INTEGRAL WEIGHT                      */
   10192             : /*                                                                 */
   10193             : /*******************************************************************/
   10194             : /* We use the prefix mf2; k represents the weight -1/2, so e.g.
   10195             :    k = 2 is weight 5/2. N is the level, so 4\mid N, and CHI is the
   10196             :    character, always even. */
   10197             : 
   10198             : static long
   10199        3360 : lamCO(long r, long s, long p)
   10200             : {
   10201        3360 :   if ((s << 1) <= r)
   10202             :   {
   10203        1232 :     long rp = r >> 1;
   10204        1232 :     if (odd(r)) return upowuu(p, rp) << 1;
   10205         336 :     else return (p + 1)*upowuu(p, rp - 1);
   10206             :   }
   10207        2128 :   else return upowuu(p, r - s) << 1;
   10208             : }
   10209             : 
   10210             : static int
   10211        1568 : condC(GEN faN, GEN valF)
   10212             : {
   10213        1568 :   GEN P = gel(faN, 1), E = gel(faN, 2);
   10214        1568 :   long l = lg(P), i;
   10215        3696 :   for (i = 1; i < l; i++)
   10216        3024 :     if ((P[i] & 3L) == 3)
   10217             :     {
   10218        1120 :       long r = E[i];
   10219        1120 :       if (odd(r) || r < (valF[i] << 1)) return 1;
   10220             :     }
   10221         672 :   return 0;
   10222             : }
   10223             : 
   10224             : /* returns 2*zetaCO; weight is k + 1/2 */
   10225             : static long
   10226        3696 : zeta2CO(GEN faN, GEN valF, long r2, long s2, long k)
   10227             : {
   10228        3696 :   if (r2 >= 4) return lamCO(r2, s2, 2) << 1;
   10229        2912 :   if (r2 == 3) return 6;
   10230        1568 :   if (condC(faN, valF)) return 4;
   10231         672 :   if (odd(k)) return s2 ? 3 : 5; else return s2 ? 5: 3;
   10232             : }
   10233             : 
   10234             : /* returns 4 times last term in formula */
   10235             : static long
   10236        3696 : dim22(long N, long F, long k)
   10237             : {
   10238        3696 :   pari_sp av = avma;
   10239        3696 :   GEN vF, faN = myfactoru(N), P = gel(faN, 1), E = gel(faN, 2);
   10240        3696 :   long i, D, l = lg(P);
   10241        3696 :   vF = cgetg(l, t_VECSMALL);
   10242        3696 :   for (i = 1; i < l; i++) vF[i] = u_lval(F, P[i]);
   10243        3696 :   D = zeta2CO(faN, vF, E[1], vF[1], k);
   10244        3696 :   for (i = 2; i < l; i++) D *= lamCO(E[i], vF[i], P[i]);
   10245        3696 :   avma = av; return D;
   10246             : }
   10247             : 
   10248             : /* PSI not necessarily primitive, of conductor F */
   10249             : static int
   10250       13846 : charistotallyeven(GEN PSI, long F)
   10251             : {
   10252       13846 :   pari_sp av = avma;
   10253       13846 :   GEN P = gel(myfactoru(F), 1);
   10254       13846 :   GEN G = gel(PSI,1), psi = gel(PSI,2);
   10255             :   long i;
   10256       14350 :   for (i = 1; i < lg(P); i++)
   10257             :   {
   10258         532 :     GEN psip = znchardecompose(G, psi, utoipos(P[i]));
   10259         532 :     if (zncharisodd(G, psip)) { avma = av; return 0; }
   10260             :   }
   10261       13818 :   avma = av; return 1;
   10262             : }
   10263             : 
   10264             : static GEN
   10265      299775 : get_PSI(GEN CHI, long t)
   10266             : {
   10267      299775 :   long r = t & 3L, t2 = (r == 2 || r == 3) ? t << 2 : t;
   10268      299775 :   return mfcharmul_i(CHI, induce(gel(CHI,1), utoipos(t2)));
   10269             : }
   10270             : /* space = mf_CUSP, mf_EISEN or mf_FULL, weight k + 1/2 */
   10271             : static long
   10272       41363 : mf2dimwt12(long N, GEN CHI, long space)
   10273             : {
   10274       41363 :   pari_sp av = avma;
   10275       41363 :   GEN D = mydivisorsu(N >> 2);
   10276       41363 :   long i, l = lg(D), dim3 = 0, dim4 = 0;
   10277             : 
   10278       41363 :   CHI = induceN(N, CHI);
   10279      341138 :   for (i = 1; i < l; i++)
   10280             :   {
   10281      299775 :     long rp, t = D[i], Mt = D[l-i];
   10282      299775 :     GEN PSI = get_PSI(CHI,t);
   10283      299775 :     rp = mfcharconductor(PSI);
   10284      299775 :     if (Mt % (rp*rp) == 0) { dim4++; if (charistotallyeven(PSI,rp)) dim3++; }
   10285             :   }
   10286       41363 :   avma = av;
   10287       41363 :   switch (space)
   10288             :   {
   10289       40439 :     case mf_CUSP: return dim4 - dim3;
   10290         462 :     case mf_EISEN:return dim3;
   10291         462 :     case mf_FULL: return dim4;
   10292             :   }
   10293             :   return 0; /*LCOV_EXCL_LINE*/
   10294             : }
   10295             : 
   10296             : static long
   10297         693 : mf2dimwt32(long N, GEN CHI, long F, long space)
   10298             : {
   10299             :   long D;
   10300         693 :   switch(space)
   10301             :   {
   10302         231 :     case mf_CUSP: D = mypsiu(N) - 6*dim22(N, F, 1);
   10303         231 :       if (D%24) pari_err_BUG("mfdim");
   10304         231 :       return D/24 + mf2dimwt12(N, CHI, 4);
   10305         231 :     case mf_FULL: D = mypsiu(N) + 6*dim22(N, F, 0);
   10306         231 :       if (D%24) pari_err_BUG("mfdim");
   10307         231 :       return D/24 + mf2dimwt12(N, CHI, 1);
   10308         231 :     case mf_EISEN: D = dim22(N, F, 0) + dim22(N, F, 1);
   10309         231 :       if (D & 3L) pari_err_BUG("mfdim");
   10310         231 :       return (D >> 2) - mf2dimwt12(N, CHI, 3);
   10311             :   }
   10312             :   return 0; /*LCOV_EXCL_LINE*/
   10313             : }
   10314             : 
   10315             : /* F = conductor(CHI), weight k = r+1/2 */
   10316             : static long
   10317       43673 : checkmf2(long N, long r, GEN CHI, long F, long space)
   10318             : {
   10319       43673 :   switch(space)
   10320             :   {
   10321       43652 :     case mf_FULL: case mf_CUSP: case mf_EISEN: break;
   10322             :     case mf_NEW: case mf_OLD:
   10323          14 :       pari_err_TYPE("half-integral weight [new/old spaces]", utoi(space));
   10324             :     default:
   10325           7 :       pari_err_TYPE("half-integral weight [incorrect space]",utoi(space));
   10326             :   }
   10327       43652 :   if (N & 3L)
   10328           0 :     pari_err_DOMAIN("half-integral weight", "N % 4", "!=", gen_0, stoi(N));
   10329       43652 :   return r >= 0 && mfcharparity(CHI) == 1 && N % F == 0;
   10330             : }
   10331             : 
   10332             : /* weight k = r + 1/2 */
   10333             : static long
   10334       43463 : mf2dim_Nkchi(long N, long r, GEN CHI, ulong space)
   10335             : {
   10336       43463 :   long D, D2, F = mfcharconductor(CHI);
   10337       43463 :   if (!checkmf2(N, r, CHI, F, space)) return 0;
   10338       43442 :   if (r == 0) return mf2dimwt12(N, CHI, space);
   10339        2772 :   if (r == 1) return mf2dimwt32(N, CHI, F, space);
   10340        2079 :   if (space == mf_EISEN)
   10341             :   {
   10342         693 :     D = dim22(N, F, r) + dim22(N, F, 1-r);
   10343         693 :     if (D & 3L) pari_err_BUG("mfdim");
   10344         693 :     return D >> 2;
   10345             :   }
   10346        1386 :   D2 = space == mf_FULL? dim22(N, F, 1-r): -dim22(N, F, r);
   10347        1386 :   D = (2*r-1)*mypsiu(N) + 6*D2;
   10348        1386 :   if (D%24) pari_err_BUG("mfdim");
   10349        1386 :   return D/24;
   10350             : }
   10351             : 
   10352             : /* weight k=r+1/2 */
   10353             : static GEN
   10354         210 : mf2init_Nkchi(long N, long r, GEN CHI, long space, long flraw)
   10355             : {
   10356         210 :   GEN Minv, Minvmat, B, M, gk = gaddsg(r,ghalf);
   10357         210 :   GEN mf1 = mkvec4(utoi(N),gk,CHI,utoi(space));
   10358             :   long L;
   10359         210 :   if (!checkmf2(N, r, CHI, mfcharconductor(CHI), space)) return mfEMPTY(mf1);
   10360         210 :   if (space==mf_EISEN) pari_err_IMPL("half-integral weight Eisenstein space");
   10361         210 :   L = mfsturmNgk(N, gk) + 1;
   10362         210 :   B = mf2basis(N, r, CHI, space);
   10363         210 :   M = mflineardivtomat(N,B,L);
   10364         210 :   if (flraw) M = mkvec3(gen_0,gen_0,M);
   10365             :   else
   10366             :   {
   10367         210 :     M = mfcleanCHI(M, CHI, 0);
   10368         210 :     Minv = gel(M,2);
   10369         210 :     Minvmat = RgM_Minv_mul(NULL, Minv);
   10370         210 :     B = vecmflineardiv_linear(B, Minvmat);
   10371         210 :     gel(M,3) = RgM_Minv_mul(gel(M,3), Minv);
   10372         210 :     gel(M,2) = mkMinv(matid(lg(B)-1), NULL,NULL,NULL);
   10373             :   }
   10374         210 :   return mkmf(mf1, cgetg(1,t_VEC), B, gen_0, M);
   10375             : }
   10376             : 
   10377             : /**************************************************************************/
   10378             : /*                          Kohnen + space                                */
   10379             : /**************************************************************************/
   10380             : 
   10381             : static GEN
   10382          21 : mfkohnenbasis_i(GEN mf, GEN CHIP, long eps, long sb)
   10383             : {
   10384          21 :   GEN M = shallowtrans(mfcoefs_mf(mf, sb, 1)), ME;
   10385             :   long c, i, n;
   10386          21 :   ME = cgetg(sb + 2, t_MAT);
   10387         784 :   for (i = 0, c = 1; i <= sb; i++)
   10388             :   {
   10389         763 :     long j = i & 3L;
   10390         763 :     if (j == 2 || j == 2 + eps) gel(ME, c++) = gel(M, i+1);
   10391             :   }
   10392          21 :   setlg(ME, c); ME = shallowtrans(Q_primpart(ME));
   10393          21 :   n = mfcharorder_canon(CHIP);
   10394          21 :   return n == 1? ZM_ker(ME): ZabM_ker(liftpol_shallow(ME), mfcharpol(CHIP), n);
   10395             : }
   10396             : GEN
   10397          21 : mfkohnenbasis(GEN mf)
   10398             : {
   10399          21 :   pari_sp av = avma;
   10400             :   GEN gk, CHI, CHIP, K;
   10401             :   long N4, r, eps, sb;
   10402          21 :   mf = checkMF(mf);
   10403          21 :   if (MF_get_space(mf) != mf_CUSP)
   10404           0 :     pari_err_TYPE("mfkohnenbasis [not a cuspidal space", mf);
   10405          21 :   if (!MF_get_dim(mf)) return cgetg(1, t_MAT);
   10406          21 :   N4 = MF_get_N(mf) >> 2; gk = MF_get_gk(mf); CHI = MF_get_CHI(mf);
   10407          21 :   if (typ(gk) == t_INT) pari_err_TYPE("mfkohnenbasis", gk);
   10408          21 :   r = MF_get_r(mf);
   10409          21 :   CHIP = mfcharchiliftprim(CHI, N4);
   10410          21 :   eps = CHIP==CHI? 1: -1;
   10411          21 :   if (!CHIP) pari_err_TYPE("mfkohnenbasis [incorrect CHI]", CHI);
   10412          21 :   if (odd(r)) eps = -eps;
   10413          21 :   if (uissquarefree(N4))
   10414             :   {
   10415          14 :     long d = mfdim_Nkchi(N4, 2*r, mfcharpow(CHI, gen_2), mf_CUSP);
   10416          14 :     sb = mfsturmNgk(N4 << 2, gk) + 1;
   10417          14 :     K = mfkohnenbasis_i(mf, CHIP, eps, sb);
   10418          14 :     if (lg(K) - 1 == d) return gerepilecopy(av, K);
   10419             :   }
   10420           7 :   sb = mfsturmNgk(N4 << 4, gk) + 1;
   10421           7 :   K = mfkohnenbasis_i(mf, CHIP, eps, sb);
   10422           7 :   return gerepilecopy(av, K);
   10423             : }
   10424             : 
   10425             : /* return [mf3, bijection, mfkohnenbasis, codeshi] */
   10426             : static GEN
   10427          14 : mfkohnenbijection_i(GEN mf)
   10428             : {
   10429          14 :   GEN vB, mf3, K, SHI, P, CHI = MF_get_CHI(mf);
   10430          14 :   long n, lK, i, dim, m, lw, sb3, N4 = MF_get_N(mf)>>2, r = MF_get_r(mf);
   10431          14 :   long Dp[] = {1, 5, 8, 12, 13, 17, 21, 24};
   10432          14 :   long Dm[] = {-3, -4, -7, -8, -11, -15, -19, -20}, *D = odd(r)? Dm: Dp;
   10433          14 :   const long nbD = 8, MAXm = 6560; /* #D, 3^#D - 1 */
   10434             : 
   10435          14 :   K = mfkohnenbasis(mf); lK = lg(K);
   10436          14 :   mf3 = mfinit_Nkchi(N4, r<<1, mfcharpow(CHI,gen_2), mf_CUSP, 0);
   10437          14 :   if (MF_get_dim(mf3) != lK - 1)
   10438           0 :     pari_err_BUG("mfkohnenbijection [different dimensions]");
   10439          14 :   if (lK == 1) return mkvec4(mf3, cgetg(1, t_MAT), K, cgetg(1, t_VEC));
   10440          14 :   CHI = mfcharchiliftprim(CHI, N4);
   10441          14 :   if (!CHI) pari_err_TYPE("mfkohnenbijection [incorrect CHI]", CHI);
   10442          14 :   n = mfcharorder_canon(CHI);
   10443          14 :   P = n==1? NULL: mfcharpol(CHI);
   10444          14 :   SHI = cgetg(nbD+1, t_VEC);
   10445          14 :   sb3 = mfsturm(mf3);
   10446          14 :   vB = RgM_mul(mfcoefs_mf(mf, labs(D[nbD-1])*sb3*sb3, 1), K);
   10447          14 :   dim = MF_get_dim(mf3);
   10448          35 :   for (m = 1, lw = 0; m <= MAXm; m += (m%3)? 2: 1)
   10449             :   {
   10450             :     pari_sp av;
   10451          35 :     ulong m1, y, v = u_lvalrem(m, 3, &y);
   10452             :     GEN z, M;
   10453             :     long j;
   10454          35 :     if (y == 1)
   10455             :     {
   10456          28 :       long d = D[v];
   10457          28 :       GEN a = cgetg(lK, t_MAT);
   10458          98 :       for (i = 1; i < lK; i++)
   10459             :       {
   10460          70 :         pari_sp av2 = avma;
   10461          70 :         GEN f = c_deflate(sb3*sb3, labs(d), gel(vB,i));
   10462          70 :         f = mftobasis_i(mf3, RgV_shimura(f, sb3, d, N4, r, CHI));
   10463          70 :         gel(a,i) = gerepileupto(av2, f);
   10464             :       }
   10465          28 :       lw++; gel(SHI,v+1) = a;
   10466             :     }
   10467          35 :     av = avma; M = NULL;
   10468          91 :     for (j = 1, m1 = m; j <= lw; j++, m1/=3)
   10469             :     {
   10470          56 :       long s = m1%3;
   10471          56 :       if (s)
   10472             :       {
   10473          42 :         GEN t = gel(SHI,j);
   10474          42 :         if (M) M = (s == 2)? RgM_sub(M, t): RgM_add(M, t);
   10475          35 :         else   M = (s == 2)? RgM_neg(t): t;
   10476             :       }
   10477             :     }
   10478          35 :     z = QabM_indexrank(M,P,n);
   10479          35 :     if (lg(gel(z,2)) > dim)
   10480             :     {
   10481          14 :       GEN d = ZV_to_zv( digits(utoipos(m), utoipos(3)) );
   10482          14 :       GEN mres, dMi, Mi = QabM_pseudoinv(M,P,n, NULL,&dMi);
   10483          14 :       long ld = lg(d), c = 1;
   10484          14 :       if (DEBUGLEVEL>1)
   10485           0 :         err_printf("mfkohnenbijection: used %ld discriminants\n",lw);
   10486          14 :       mres = cgetg(ld, t_VEC);
   10487          42 :       for (j = ld-1; j >= 1; j--)
   10488          28 :         if (d[j]) gel(mres,c++) = mkvec2s(D[ld-j-1], d[j]);
   10489          14 :       setlg(mres,c); return mkvec4(mf3, RgM_Rg_div(Mi,dMi), K, mres);
   10490             :     }
   10491          21 :     avma = av;
   10492             :   }
   10493           0 :   pari_err_BUG("mfkohnenbijection");
   10494             :   return NULL; /*LCOV_EXCL_LINE*/
   10495             : }
   10496             : GEN
   10497          14 : mfkohnenbijection(GEN mf)
   10498             : {
   10499          14 :   pari_sp av = avma;
   10500             :   long N;
   10501          14 :   mf = checkMF(mf); N = MF_get_N(mf);
   10502          14 :   if (!uissquarefree(N >> 2))
   10503           0 :     pari_err_TYPE("mfkohnenbijection [N/4 not squarefree]", utoi(N));
   10504          14 :   if (MF_get_space(mf) != mf_CUSP || MF_get_r(mf) == 0 || !mfshimura_space_cusp(mf))
   10505           0 :     pari_err_TYPE("mfkohnenbijection [incorrect mf for Kohnen]", mf);
   10506          14 :   return gerepilecopy(av, mfkohnenbijection_i(mf));
   10507             : }
   10508             : 
   10509             : static int
   10510           7 : checkbij_i(GEN b)
   10511             : {
   10512          14 :   return typ(b) == t_VEC && lg(b) == 5 && checkMF_i(gel(b,1))
   10513           7 :          && typ(gel(b,2)) == t_MAT
   10514           7 :          && typ(gel(b,3)) == t_MAT
   10515          14 :          && typ(gel(b,4)) == t_VEC;
   10516             : }
   10517             : 
   10518             : /* bij is the output of mfkohnenbijection */
   10519             : GEN
   10520           7 : mfkohneneigenbasis(GEN mf, GEN bij)
   10521             : {
   10522           7 :   pari_sp av = avma;
   10523             :   GEN mf3, mf30, B, KM, M, k;
   10524             :   long r, i, l, N4;
   10525           7 :   mf = checkMF(mf);
   10526           7 :   if (!checkbij_i(bij))
   10527           0 :     pari_err_TYPE("mfkohneneigenbasis [bijection]", bij);
   10528           7 :   if (MF_get_space(mf) != mf_CUSP)
   10529           0 :     pari_err_TYPE("mfkohneneigenbasis [not a cuspidal space]", mf);
   10530           7 :   if (!MF_get_dim(mf))
   10531           0 :     retmkvec3(cgetg(1, t_VEC), cgetg(1, t_VEC), cgetg(1, t_VEC));
   10532           7 :   N4 = MF_get_N(mf) >> 2; k = MF_get_gk(mf);
   10533           7 :   if (typ(k) == t_INT) pari_err_TYPE("mfkohneneigenbasis", k);
   10534           7 :   if (!uissquarefree(N4))
   10535           0 :     pari_err_TYPE("mfkohneneigenbasis [N not squarefree]", utoipos(N4));
   10536           7 :   r = MF_get_r(mf);
   10537           7 :   KM = RgM_mul(gel(bij,3), gel(bij,2));
   10538           7 :   mf3 = gel(bij,1);
   10539           7 :   mf30 = mfinit_Nkchi(N4, 2*r, MF_get_CHI(mf3), mf_NEW, 0);
   10540           7 :   B = mfcoefs_mf(mf30, mfsturm_mf(mf3), 1); l = lg(B);
   10541           7 :   M = cgetg(l, t_MAT);
   10542           7 :   for (i=1; i<l; i++) gel(M,i) = RgM_RgC_mul(KM, mftobasis_i(mf3, gel(B,i)));
   10543           7 :   return gerepilecopy(av, mkvec3(mf30, M, RgM_mul(M, MF_get_newforms(mf30))));
   10544             : }
   10545             : /*************************** End Kohnen ************************************/
   10546             : /***************************************************************************/
   10547             : 
   10548             : static GEN desc(GEN F);
   10549             : static GEN
   10550         504 : desc_mfeisen(GEN F)
   10551             : {
   10552         504 :   GEN R, gk = mf_get_gk(F);
   10553         504 :   if (typ(gk) == t_FRAC)
   10554           7 :     R = gsprintf("H_{%Ps}", gk);
   10555             :   else
   10556             :   {
   10557         497 :     GEN vchi = gel(F, 2), CHI = mfchisimpl(gel(vchi, 3));
   10558         497 :     long k = itou(gk);
   10559         497 :     if (lg(vchi) < 5) R = gsprintf("F_%ld(%Ps)", k, CHI);
   10560             :     else
   10561             :     {
   10562         294 :       GEN CHI2 = mfchisimpl(gel(vchi, 4));
   10563         294 :       R = gsprintf("F_%ld(%Ps, %Ps)", k, CHI, CHI2);
   10564             :     }
   10565             :   }
   10566         504 :   return R;
   10567             : }
   10568             : static GEN
   10569          35 : desc_hecke(GEN F)
   10570             : {
   10571             :   long n, N;
   10572          35 :   GEN D = gel(F,2);
   10573          35 :   if (typ(D) == t_VECSMALL) { N = D[3]; n = D[1]; }
   10574          14 :   else { GEN nN = gel(D,2); n = nN[1]; N = nN[2]; } /* half integer */
   10575          35 :   return gsprintf("T_%ld(%ld)(%Ps)", N, n, desc(gel(F,3)));
   10576             : }
   10577             : static GEN
   10578          98 : desc_linear(GEN FLD, GEN dL)
   10579             : {
   10580          98 :   GEN F = gel(FLD,2), L = gel(FLD,3), R = strtoGENstr("LIN([");
   10581          98 :   long n = lg(F) - 1, i;
   10582         168 :   for (i = 1; i <= n; i++)
   10583             :   {
   10584         168 :     R = shallowconcat(R, desc(gel(F,i))); if (i == n) break;
   10585          70 :     R = shallowconcat(R, strtoGENstr(", "));
   10586             :   }
   10587          98 :   return shallowconcat(R, gsprintf("], %Ps)", gdiv(L, dL)));
   10588             : }
   10589             : static GEN
   10590          21 : desc_dihedral(GEN F)
   10591             : {
   10592          21 :   GEN bnr = gel(F,2), D = nf_get_disc(bnr_get_nf(bnr)), f = bnr_get_mod(bnr);
   10593          21 :   GEN cyc = bnr_get_cyc(bnr);
   10594          21 :   GEN w = gel(F,3), chin = zv_to_ZV(gel(w,2)), o = utoi(gel(w,1)[1]);
   10595          21 :   GEN chi = char_denormalize(cyc, o, chin);
   10596          21 :   if (lg(gel(f,2)) == 1) f = gel(f,1);
   10597          21 :   return gsprintf("DIH(%Ps, %Ps, %Ps, %Ps)",D,f,cyc,chi);
   10598             : }
   10599