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.10.1 lcov report (development 22708-0f0e6fe44) Lines: 7093 7264 97.6 %
Date: 2018-06-18 05:36:21 Functions: 731 733 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       27244 : mkgNK(GEN N, GEN k, GEN CHI, GEN P) { return mkvec4(N, k, CHI, P); }
      77             : static GEN
      78       12194 : mkNK(long N, long k, GEN CHI) { return mkgNK(stoi(N), stoi(k), CHI, pol_x(1)); }
      79             : GEN
      80        6510 : MF_get_CHI(GEN mf) { return gmael(mf,1,3); }
      81             : GEN
      82       16702 : MF_get_gN(GEN mf) { return gmael(mf,1,1); }
      83             : long
      84       16051 : MF_get_N(GEN mf) { return itou(MF_get_gN(mf)); }
      85             : GEN
      86       10969 : MF_get_gk(GEN mf) { return gmael(mf,1,2); }
      87             : long
      88        5362 : MF_get_k(GEN mf)
      89             : {
      90        5362 :   GEN gk = MF_get_gk(mf);
      91        5362 :   if (typ(gk)!=t_INT) pari_err_IMPL("half-integral weight");
      92        5362 :   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       11445 : MF_get_space(GEN mf) { return itos(gmael(mf,1,4)); }
     103             : GEN
     104        3437 : MF_get_E(GEN mf) { return gel(mf,2); }
     105             : GEN
     106       16303 : MF_get_S(GEN mf) { return gel(mf,3); }
     107             : GEN
     108        1071 : MF_get_basis(GEN mf) { return shallowconcat(gel(mf,2), gel(mf,3)); }
     109             : long
     110        3374 : MF_get_dim(GEN mf)
     111             : {
     112        3374 :   switch(MF_get_space(mf))
     113             :   {
     114             :     case mf_FULL:
     115         546 :       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        2688 :       return lg(MF_get_S(mf)) - 1;
     120             :   }
     121             : }
     122             : GEN
     123        6405 : 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        2212 : MF_get_Minv(GEN mf) { return gmael(mf,5,2); }
     130             : GEN
     131        7000 : 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         574 : QabM_init(long n, ulong *p)
     142             : {
     143         574 :   ulong pinit = 1000000007;
     144             :   forprime_t T;
     145         574 :   if (n <= 1) { *p = pinit; return 0; }
     146         420 :   u_forprime_arith_init(&T, pinit, ULONG_MAX, 1, n);
     147         420 :   *p = u_forprime_next(&T);
     148         420 :   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        9289 : QabM_indexrank(GEN M, GEN P, long n)
     233             : {
     234             :   GEN z;
     235        9289 :   if (n <= 2)
     236             :   {
     237        8183 :     M = vec_Q_primpart(M);
     238        8183 :     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        9289 :   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      105658 : myeulerphiu(ulong n)
     255             : {
     256             :   pari_sp av;
     257             :   GEN fa;
     258      105658 :   if (n == 1) return 1;
     259       90475 :   av = avma; fa = myfactoru(n);
     260       90475 :   avma = av; return eulerphiu_fact(fa);
     261             : }
     262             : static long
     263       77392 : mymoebiusu(ulong n)
     264             : {
     265             :   pari_sp av;
     266             :   GEN fa;
     267       77392 :   if (n == 1) return 1;
     268       70133 :   av = avma; fa = myfactoru(n);
     269       70133 :   avma = av; return moebiusu_fact(fa);
     270             : }
     271             : 
     272             : static long
     273        2688 : mynumdivu(long N)
     274             : {
     275             :   pari_sp av;
     276             :   GEN fa;
     277        2688 :   if (N == 1) return 1;
     278        2583 :   av = avma; fa = myfactoru(N);
     279        2583 :   avma = av; return numdivu_fact(fa);
     280             : }
     281             : 
     282             : /* N\prod_{p|N} (1+1/p) */
     283             : static long
     284      262262 : mypsiu(ulong N)
     285             : {
     286      262262 :   pari_sp av = avma;
     287      262262 :   GEN P = gel(myfactoru(N), 1);
     288      262262 :   long j, l = lg(P), res = N;
     289      262262 :   for (j = 1; j < l; j++) res += res/P[j];
     290      262262 :   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        6083 : mubeta(long n)
     324             : {
     325        6083 :   pari_sp av = avma;
     326        6083 :   GEN E = gel(myfactoru(n), 2);
     327        6083 :   long i, s = 1, l = lg(E);
     328       12628 :   for (i = 1; i < l; i++)
     329             :   {
     330        6545 :     long e = E[i];
     331        6545 :     if (e >= 3) { avma = av; return 0; }
     332        6545 :     if (e == 1) s *= -2;
     333             :   }
     334        6083 :   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     4078081 : mubeta2(long n, long m)
     341             : {
     342     4078081 :   pari_sp av = avma;
     343     4078081 :   GEN fa = myfactoru(n), P = gel(fa,1), E = gel(fa,2);
     344     4078081 :   long i, s = 1, l = lg(P);
     345     8322363 :   for (i = 1; i < l; i++)
     346             :   {
     347     4244282 :     long p = P[i], e = E[i];
     348     4244282 :     if (m % p)
     349             :     { /* p^e in n1 */
     350     3381000 :       if (e >= 3) { avma = av; return 0; }
     351     3381000 :       if (e == 1) s *= -2;
     352             :     }
     353             :     else
     354             :     { /* in n2 */
     355      863282 :       if (e >= 2) { avma = av; return 0; }
     356      863282 :       s = -s;
     357             :     }
     358             :   }
     359     4078081 :   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      926877 : newt_params(long N, long n, long FC, long *pg, long *pN2)
     367             : {
     368      926877 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
     369      926877 :   long i, g = 1, N2 = 1, l = lg(P);
     370     2479022 :   for (i = 1; i < l; i++)
     371             :   {
     372     1552145 :     long p = P[i], e = E[i];
     373     1552145 :     if (e == 1)
     374     1311471 :     { if (FC % p && n % (p*p) == 0) g *= p; }
     375             :     else
     376      240674 :       N2 *= upowuu(p,(n % p)? e-2: e-1);
     377             :   }
     378      926877 :   *pg = g; *pN2 = N2;
     379      926877 : }
     380             : /* simplified version of newt_params for n = 1 (newdim) */
     381             : static void
     382       32522 : newd_params(long N, long *pN2)
     383             : {
     384       32522 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
     385       32522 :   long i, N2 = 1, l = lg(P);
     386       82628 :   for (i = 1; i < l; i++)
     387             :   {
     388       50106 :     long p = P[i], e = E[i];
     389       50106 :     if (e > 2) N2 *= upowuu(p, e-2);
     390             :   }
     391       32522 :   *pN2 = N2;
     392       32522 : }
     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       42567 : phipart(long g, long q)
     413             : {
     414       42567 :   if (g > 1)
     415             :   {
     416       16590 :     GEN P = gel(myfactoru(g), 1);
     417       16590 :     long i, l = lg(P);
     418       16590 :     for (i = 1; i < l; i++) { long p = P[i]; if (q % p) g -= g / p; }
     419             :   }
     420       42567 :   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       77301 : tracerelz(long d, long m, long k, long vt)
     425             : {
     426       77301 :   long s, v, g = ugcd(k, d), q = d / g, muq = mymoebiusu(q);
     427       77301 :   if (!muq) return gen_0;
     428       50267 :   if (m == 1)
     429             :   {
     430       17843 :     s = phipart(g, q); if (muq < 0) s = -s;
     431       17843 :     return stoi(s);
     432             :   }
     433       32424 :   if (ugcd(q, m) > 1) return gen_0;
     434       24724 :   s = phipart(g, m*q); if (muq < 0) s = -s;
     435       24724 :   v = Fl_inv(q % m, m);
     436       24724 :   v = (v*(k/g)) % m;
     437       24724 :   return mygmodulo_lift(v, m, stoi(s), vt);
     438             : }
     439             : /* m | n, both not 2 mod 4. Pn = polcyclo(n) */
     440             : GEN
     441       17962 : Qab_trace_init(GEN Pn, long n, long m)
     442             : {
     443             :   GEN T, Pm;
     444             :   long a, i, d, vt;
     445       17962 :   if (m == n) return mkvec(Pn);
     446       12579 :   d = degpol(Pn);
     447       12579 :   vt = varn(Pn);
     448       12579 :   Pm = polcyclo(m, vt);
     449       12579 :   T = cgetg(d+1, t_VEC);
     450       12579 :   gel(T,1) = utoipos(d / degpol(Pm)); /* Tr 1 */
     451       12579 :   a = n / m;
     452       12579 :   for (i = 1; i < d; i++) gel(T,i+1) = tracerelz(a, m, i, vt);
     453       12579 :   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       48223 : tracerel_i(GEN T, GEN x)
     458             : {
     459       48223 :   long k, l = lg(x);
     460       48223 :   GEN S = gen_0;
     461       48223 :   for (k = 2; k < l; k++) S = gadd(S, gmul(gel(T,k-1), gel(x,k)));
     462       48223 :   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        4067 : QabV_tracerel(GEN v, long t, GEN x)
     468             : {
     469             :   long l, j, degrel;
     470             :   GEN y, z, Pm, Pn, T;
     471        4067 :   if (lg(v) != 4) return x;
     472        4067 :   y = cgetg_copy(x, &l);
     473        4067 :   Pm = gel(v,1);
     474        4067 :   Pn = gel(v,2);
     475        4067 :   T  = gel(v,3);
     476        4067 :   degrel = degpol(Pn) / degpol(Pm);
     477        4067 :   z = RgX_rem(pol_xn(t, varn(Pn)), Pn);
     478      100373 :   for (j = 1; j < l; j++)
     479             :   {
     480       96306 :     GEN a = liftpol_shallow(gel(x,j));
     481       96306 :     a = simplify_shallow( gmul(a, z) );
     482       96306 :     if (typ(a) == t_POL)
     483             :     {
     484       48223 :       a = gdivgs(tracerel_i(T, RgX_rem(a, Pn)), degrel);
     485       48223 :       if (typ(a) == t_POL) a = RgX_rem(a, Pm);
     486             :     }
     487       96306 :     gel(y,j) = a;
     488             :   }
     489        4067 :   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      902839 : gmfcharorder(GEN CHI) { return gel(CHI, 3); }
     501             : long
     502      873663 : mfcharorder(GEN CHI) { return itou(gmfcharorder(CHI)); }
     503             : static long
     504        8092 : mfcharistrivial(GEN CHI) { return !CHI || mfcharorder(CHI) == 1; }
     505             : static GEN
     506      587195 : gmfcharmodulus(GEN CHI) { return gmael3(CHI, 1, 1, 1); }
     507             : long
     508      587195 : mfcharmodulus(GEN CHI) { return itou(gmfcharmodulus(CHI)); }
     509             : GEN
     510      181958 : mfcharpol(GEN CHI) { return gel(CHI,4); }
     511             : static long
     512      448049 : ord_canon(long ord)
     513             : {
     514      448049 :   if ((ord & 3L) == 2) ord >>= 1;
     515      448049 :   return ord;
     516             : }
     517             : static long
     518       27496 : 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      607677 : mygmodulo_lift(long k, long ord, GEN C, long vt)
     543             : {
     544      607677 :   if (!k) return C;
     545      321356 :   if ((k << 1) == ord) return gneg(C);
     546      217259 :   if ((ord&3L) == 2)
     547             :   {
     548       87185 :     if (odd(k)) { C = gneg(C); k += ord >> 1; }
     549       87185 :     k >>= 1;
     550             :   }
     551      217259 :   return monomial(C, k, vt);
     552             : }
     553             : /* vz[i+1] = image of (zeta_ord)^i in Fp */
     554             : static ulong
     555      152530 : mygmodulo_Fl(long k, GEN vz, ulong C, ulong p)
     556             : {
     557             :   long ord;
     558      152530 :   if (!k) return C;
     559       96852 :   ord = lg(vz)-2;
     560       96852 :   if ((k << 1) == ord) return Fl_neg(C,p);
     561       76664 :   if ((ord&3L) == 2)
     562             :   {
     563       70140 :     if (odd(k)) { C = Fl_neg(C,p); k += ord >> 1; }
     564       70140 :     k >>= 1;
     565             :   }
     566       76664 :   return Fl_mul(C, vz[k+1], p);
     567             : }
     568             : 
     569             : static long
     570      443653 : znchareval_i(GEN CHI, long n, GEN ord)
     571      443653 : { 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      368711 : mfcharGL(GEN G, GEN L)
     576             : {
     577      368711 :   GEN o = zncharorder(G,L);
     578      368711 :   long ord = ord_canon(itou(o)), vt = fetch_user_var("t");
     579      368711 :   return mkvec4(G, L, o, polcyclo(ord,vt));
     580             : }
     581             : static GEN
     582        3864 : mfchartrivial()
     583        3864 : { return mfcharGL(znstar0(gen_1,1), cgetg(1,t_COL)); }
     584             : /* convert a generic character into an 'mfchar' */
     585             : static GEN
     586        3836 : get_mfchar(GEN CHI)
     587             : {
     588             :   GEN G, L;
     589        3836 :   if (typ(CHI) != t_VEC)
     590        3010 :     CHI = znchar(CHI);
     591        3829 :   if (lg(CHI) == 5 && checkznstar_i(gel(CHI, 1))) return CHI;
     592        3815 :   else if (lg(CHI) != 3 || !checkznstar_i(gel(CHI,1)))
     593           7 :     pari_err_TYPE("checkNF [chi]", CHI);
     594        3808 :   G = gel(CHI,1);
     595        3808 :   L = gel(CHI,2); if (typ(L) != t_COL) L = znconreylog(G,L);
     596        3808 :   return mfcharGL(G, L);
     597             : }
     598             : 
     599             : /* parse [N], [N,k], [N,k,CHI]. If 'joker' is set, allow wildcard for CHI */
     600             : static GEN
     601        8904 : checkCHI(GEN NK, long N, int joker)
     602             : {
     603             :   GEN CHI;
     604        8904 :   if (lg(NK) == 3)
     605         574 :     CHI = mfchartrivial();
     606             :   else
     607             :   {
     608             :     long i, l;
     609        8330 :     CHI = gel(NK,3); l = lg(CHI);
     610        8330 :     if (isintzero(CHI) && joker)
     611        4095 :       CHI = NULL; /* all character orbits */
     612        4235 :     else if (isintm1(CHI) && joker > 1)
     613        2373 :       CHI = gen_m1; /* sum over all character orbits */
     614        1995 :     else if ((typ(CHI) == t_VEC &&
     615         189 :              (l == 1 || l != 3 || !checkznstar_i(gel(CHI,1)))) && joker)
     616             :     {
     617         133 :       CHI = shallowtrans(CHI); /* list of characters */
     618         133 :       for (i = 1; i < l; i++) gel(CHI,i) = get_mfchar(gel(CHI,i));
     619             :     }
     620             :     else
     621             :     {
     622        1729 :       CHI = get_mfchar(CHI); /* single char */
     623        1729 :       if (N % mfcharmodulus(CHI)) pari_err_TYPE("checkNF [chi]", NK);
     624             :     }
     625             :   }
     626        8890 :   return CHI;
     627             : }
     628             : /* support half-integral weight */
     629             : static void
     630        8911 : checkNK2(GEN NK, long *N, long *nk, long *dk, GEN *CHI, int joker)
     631             : {
     632        8911 :   long l = lg(NK);
     633             :   GEN T;
     634        8911 :   if (typ(NK) != t_VEC || l < 3 || l > 4) pari_err_TYPE("checkNK", NK);
     635        8911 :   T = gel(NK,1); if (typ(T) != t_INT) pari_err_TYPE("checkNF [N]", NK);
     636        8911 :   *N = itos(T); if (*N <= 0) pari_err_TYPE("checkNF [N <= 0]", NK);
     637        8911 :   T = gel(NK,2);
     638        8911 :   switch(typ(T))
     639             :   {
     640        5551 :     case t_INT:  *nk = itos(T); *dk = 1; break;
     641             :     case t_FRAC:
     642        3353 :       *nk = itos(gel(T,1));
     643        3353 :       *dk = itou(gel(T,2)); if (*dk == 2) break;
     644           7 :     default: pari_err_TYPE("checkNF [k]", NK);
     645             :   }
     646        8904 :   *CHI = checkCHI(NK, *N, joker);
     647        8890 : }
     648             : /* don't support half-integral weight */
     649             : static void
     650         126 : checkNK(GEN NK, long *N, long *k, GEN *CHI, int joker)
     651             : {
     652             :   long d;
     653         126 :   checkNK2(NK, N, k, &d, CHI, joker);
     654         126 :   if (d != 1) pari_err_TYPE("checkNF [k]", NK);
     655         126 : }
     656             : 
     657             : static GEN
     658        4851 : mfchargalois(long N, int odd, GEN flagorder)
     659             : {
     660        4851 :   GEN G = znstar0(utoi(N), 1), L = chargalois(G, flagorder);
     661        4851 :   long l = lg(L), i, j;
     662      112735 :   for (i = j = 1; i < l; i++)
     663             :   {
     664      107884 :     GEN chi = znconreyfromchar(G, gel(L,i));
     665      107884 :     if (zncharisodd(G,chi) == odd) gel(L,j++) = mfcharGL(G,chi);
     666             :   }
     667        4851 :   setlg(L, j); return L;
     668             : }
     669             : /* possible characters for non-trivial S_1(N, chi) */
     670             : static GEN
     671        1708 : mfwt1chars(long N, GEN vCHI)
     672             : {
     673        1708 :   if (vCHI) return vCHI; /*do not filter, user knows best*/
     674             :   /* Tate's theorem */
     675        1638 :   return mfchargalois(N, 1, uisprime(N)? mkvecsmall2(2,4): NULL);
     676             : }
     677             : static GEN
     678        3255 : mfchars(long N, long k, long dk, GEN vCHI)
     679        3255 : { return vCHI? vCHI: mfchargalois(N, (dk == 2)? 0: (k & 1), NULL); }
     680             : 
     681             : /* wrappers from mfchar to znchar */
     682             : static long
     683       65310 : mfcharparity(GEN CHI)
     684             : {
     685       65310 :   if (!CHI) return 1;
     686       65310 :   return zncharisodd(gel(CHI,1), gel(CHI,2)) ? -1 : 1;
     687             : }
     688             : /* if CHI is primitive, return CHI itself, not a copy */
     689             : static GEN
     690       63098 : mfchartoprimitive(GEN CHI, long *pF)
     691             : {
     692             :   pari_sp av;
     693             :   GEN chi, F;
     694       63098 :   if (!CHI) { if (pF) *pF = 1; return mfchartrivial(); }
     695       63098 :   av = avma; F = znconreyconductor(gel(CHI,1), gel(CHI,2), &chi);
     696       63098 :   if (typ(F) == t_INT) avma = av;
     697             :   else
     698             :   {
     699        7273 :     CHI = leafcopy(CHI);
     700        7273 :     gel(CHI,1) = znstar0(F, 1);
     701        7273 :     gel(CHI,2) = chi;
     702             :   }
     703       63098 :   if (pF) *pF = mfcharmodulus(CHI);
     704       63098 :   return CHI;
     705             : }
     706             : static long
     707      391076 : mfcharconductor(GEN CHI)
     708             : {
     709      391076 :   pari_sp ltop = avma;
     710      391076 :   GEN res = znconreyconductor(gel(CHI,1), gel(CHI,2), NULL);
     711             :   long FC;
     712      391076 :   if (typ(res) == t_VEC) res = gel(res, 1);
     713      391076 :   FC = itos(res); avma = ltop; return FC;
     714             : }
     715             : 
     716             : /* n coprime with the modulus of CHI */
     717             : static GEN
     718        5523 : mfchareval_i(GEN CHI, long n)
     719             : {
     720        5523 :   GEN ord = gmfcharorder(CHI);
     721        5523 :   if (equali1(ord)) return gen_1;
     722        1113 :   return mygmodulo(CHI, znchareval_i(CHI, n, ord));
     723             : }
     724             : /* d a multiple of ord(CHI); n coprime with char modulus;
     725             :  * return x s.t. CHI(n) = \zeta_d^x] */
     726             : static long
     727      772870 : mfcharevalord(GEN CHI, long n, long d)
     728             : {
     729      772870 :   if (mfcharorder(CHI) == 1) return 0;
     730      435953 :   return znchareval_i(CHI, n, utoi(d));
     731             : }
     732             : 
     733             : /*                      Operations on mf closures                    */
     734             : static GEN
     735       47369 : tagparams(long t, GEN NK) { return mkvec2(mkvecsmall(t), NK); }
     736             : static GEN
     737         812 : lfuntag(long t, GEN x) { return mkvec2(mkvecsmall(t), x); }
     738             : static GEN
     739          49 : tag0(long t, GEN NK) { retmkvec(tagparams(t,NK)); }
     740             : static GEN
     741        8582 : tag(long t, GEN NK, GEN x) { retmkvec2(tagparams(t,NK), x); }
     742             : static GEN
     743       27048 : tag2(long t, GEN NK, GEN x, GEN y) { retmkvec3(tagparams(t,NK), x,y); }
     744             : static GEN
     745       11578 : tag3(long t, GEN NK, GEN x,GEN y,GEN z) { retmkvec4(tagparams(t,NK), x,y,z); }
     746             : /* is F a "modular form" ? */
     747             : int
     748       14350 : checkmf_i(GEN F)
     749       14350 : { return typ(F) == t_VEC
     750       13888 :     && lg(F) > 1 && typ(gel(F,1)) == t_VEC
     751        9919 :     && lg(gel(F,1)) == 3
     752        9758 :     && typ(gmael(F,1,1)) == t_VECSMALL
     753       24108 :     && typ(gmael(F,1,2)) == t_VEC; }
     754      127988 : long mf_get_type(GEN F) { return gmael(F,1,1)[1]; }
     755       94094 : GEN mf_get_gN(GEN F) { return gmael3(F,1,2,1); }
     756       81214 : GEN mf_get_gk(GEN F) { return gmael3(F,1,2,2); }
     757             : /* k - 1/2, assume k in 1/2 + Z */
     758         266 : long mf_get_r(GEN F) { return itou(gel(mf_get_gk(F),1)) >> 1; }
     759       66500 : long mf_get_N(GEN F) { return itou(mf_get_gN(F)); }
     760       45521 : long mf_get_k(GEN F)
     761             : {
     762       45521 :   GEN gk = mf_get_gk(F);
     763       45521 :   if (typ(gk)!=t_INT) pari_err_IMPL("half-integral weight");
     764       45521 :   return itou(gk);
     765             : }
     766       29134 : GEN mf_get_CHI(GEN F) { return gmael3(F,1,2,3); }
     767       17255 : GEN mf_get_field(GEN F) { return gmael3(F,1,2,4); }
     768       15197 : GEN mf_get_NK(GEN F) { return gmael(F,1,2); }
     769             : static void
     770         308 : mf_setfield(GEN f, GEN P)
     771             : {
     772         308 :   gel(f,1) = leafcopy(gel(f,1));
     773         308 :   gmael(f,1,2) = leafcopy(gmael(f,1,2));
     774         308 :   gmael3(f,1,2,4) = P;
     775         308 : }
     776             : 
     777             : /* UTILITY FUNCTIONS */
     778             : GEN
     779        3962 : mftocol(GEN F, long lim, long d)
     780        3962 : { GEN c = mfcoefs_i(F, lim, d); settyp(c,t_COL); return c; }
     781             : GEN
     782        1057 : mfvectomat(GEN vF, long lim, long d)
     783             : {
     784        1057 :   long j, l = lg(vF);
     785        1057 :   GEN M = cgetg(l, t_MAT);
     786        1057 :   for (j = 1; j < l; j++) gel(M,j) = mftocol(gel(vF,j), lim, d);
     787        1057 :   return M;
     788             : }
     789             : 
     790             : static GEN
     791        3892 : RgV_to_ser_full(GEN x) { return RgV_to_ser(x, 0, lg(x)+1); }
     792             : /* TODO: delete */
     793             : static GEN
     794         819 : mfcoefsser(GEN F, long n) { return RgV_to_ser_full(mfcoefs_i(F,n,1)); }
     795             : static GEN
     796         224 : sertovecslice(GEN S, long n)
     797             : {
     798         224 :   GEN v = gtovec0(S, -(lg(S) - 2 + valp(S)));
     799         224 :   long l = lg(v), n2 = n + 2;
     800         224 :   if (l < n2) pari_err_BUG("sertovecslice [n too large]");
     801         224 :   return (l == n2)? v: vecslice(v, 1, n2-1);
     802             : }
     803             : 
     804             : /* a, b two RgV of the same length, multiply as truncated power series */
     805             : static GEN
     806        2905 : RgV_mul_RgXn(GEN a, GEN b)
     807             : {
     808        2905 :   long n = lg(a)-1;
     809             :   GEN c;
     810        2905 :   a = RgV_to_RgX(a,0);
     811        2905 :   b = RgV_to_RgX(b,0); c = RgXn_mul(a, b, n);
     812        2905 :   c = RgX_to_RgC(c,n); settyp(c,t_VEC); return c;
     813             : }
     814             : /* divide as truncated power series */
     815             : static GEN
     816         252 : RgV_div_RgXn(GEN a, GEN b)
     817             : {
     818         252 :   long n = lg(a)-1;
     819             :   GEN c;
     820         252 :   a = RgV_to_RgX(a,0);
     821         252 :   b = RgV_to_RgX(b,0); c = RgXn_mul(a, RgXn_inv(b,n), n);
     822         252 :   c = RgX_to_RgC(c,n); settyp(c,t_VEC); return c;
     823             : }
     824             : /* a^b */
     825             : static GEN
     826          77 : RgV_pows_RgXn(GEN a, long b)
     827             : {
     828          77 :   long n = lg(a)-1;
     829             :   GEN c;
     830          77 :   a = RgV_to_RgX(a,0);
     831          77 :   if (b < 0) { a = RgXn_inv(a, n); b = -b; }
     832          77 :   c = RgXn_powu_i(a,b,n);
     833          77 :   c = RgX_to_RgC(c,n); settyp(c,t_VEC); return c;
     834             : }
     835             : 
     836             : /* assume lg(V) >= n*d + 2 */
     837             : static GEN
     838        5747 : c_deflate(long n, long d, GEN v)
     839             : {
     840        5747 :   long i, id, l = n+2;
     841             :   GEN w;
     842        5747 :   if (d == 1) return lg(v) == l ? v: vecslice(v, 1, l-1);
     843         287 :   w = cgetg(l, t_VEC);
     844         287 :   for (i = id = 1; i < l; i++, id += d) gel(w, i) = gel(v, id);
     845         287 :   return w;
     846             : }
     847             : static GEN
     848         511 : c_mul(long n, long d, GEN F, GEN G)
     849             : {
     850         511 :   pari_sp av = avma;
     851         511 :   long nd = n*d;
     852         511 :   GEN VF = mfcoefs_i(F, nd, 1);
     853         511 :   GEN VG = mfcoefs_i(G, nd, 1);
     854         511 :   return gerepilecopy(av, c_deflate(n, d, RgV_mul_RgXn(VF,VG)));
     855             : }
     856             : static GEN
     857          77 : c_pow(long n, long d, GEN F, GEN a)
     858             : {
     859          77 :   pari_sp av = avma;
     860          77 :   long nd = n*d;
     861          77 :   GEN f = RgV_pows_RgXn(mfcoefs_i(F,nd,1), itos(a));
     862          77 :   return gerepilecopy(av, c_deflate(n, d, f));
     863             : }
     864             : 
     865             : /* F * Theta */
     866             : static GEN
     867         336 : mfmultheta(GEN F)
     868             : {
     869         336 :   if (typ(mf_get_gk(F)) == t_FRAC && mf_get_type(F) == t_MF_DIV)
     870             :   {
     871         112 :     GEN T = gel(F,3); /* hopefully mfTheta() */
     872         112 :     if (mf_get_type(T) == t_MF_THETA && mf_get_N(T) == 4) return gel(F,2);
     873             :   }
     874         224 :   return mfmul(F, mfTheta(NULL));
     875             : }
     876             : 
     877             : static GEN
     878          21 : c_bracket(long n, long d, GEN F, GEN G, GEN gm)
     879             : {
     880          21 :   pari_sp av = avma;
     881          21 :   long i, nd = n*d;
     882          21 :   GEN VF = mfcoefs_i(F, nd, 1), tF = cgetg(nd+2, t_VEC);
     883          21 :   GEN VG = mfcoefs_i(G, nd, 1), tG = cgetg(nd+2, t_VEC);
     884          21 :   GEN C, mpow, res = NULL, gk = mf_get_gk(F), gl = mf_get_gk(G);
     885          21 :   ulong j, m = itou(gm);
     886             :   /* pow[i,j+1] = i^j */
     887          21 :   mpow = cgetg(m+2, t_MAT);
     888          21 :   gel(mpow,1) = const_col(nd, gen_1);
     889          49 :   for (j = 1; j <= m; j++)
     890             :   {
     891          28 :     GEN c = cgetg(nd+1, t_COL);
     892          28 :     gel(mpow,j+1) = c;
     893          28 :     for (i = 1; i <= nd; i++) gel(c,i) = muliu(gcoeff(mpow,i,j), i);
     894             :   }
     895          21 :   C = binomial(gaddgs(gk, m-1), m);
     896          70 :   for (j = 0; j <= m; j++)
     897             :   { /* C = (-1)^j binom(m+l-1, j) binom(m+k-1,m-j) */
     898             :     GEN c;
     899          49 :     gel(tF,1) = j == 0? gel(VF,1): gen_0;
     900          49 :     gel(tG,1) = j == m? gel(VG,1): gen_0;
     901         462 :     for (i = 1; i <= nd; i++)
     902             :     {
     903         413 :       gel(tF, i+1) = gmul(gcoeff(mpow,i,j+1),   gel(VF, i+1));
     904         413 :       gel(tG, i+1) = gmul(gcoeff(mpow,i,m-j+1), gel(VG, i+1));
     905             :     }
     906          49 :     c = gmul(C, c_deflate(n, d, RgV_mul_RgXn(tF, tG)));
     907          49 :     res = res? gadd(res, c): c;
     908          49 :     if (j < m)
     909          56 :       C = gdiv(gmul(C, gmulsg(m-j, gaddgs(gl,m-j-1))),
     910          28 :                gmulsg(-(j+1), gaddgs(gk,j)));
     911             :   }
     912          21 :   return gerepileupto(av, res);
     913             : }
     914             : /* linear combination \sum L[j] vecF[j] */
     915             : static GEN
     916        2366 : c_linear(long n, long d, GEN F, GEN L, GEN dL)
     917             : {
     918        2366 :   pari_sp av = avma;
     919        2366 :   long j, l = lg(L);
     920        2366 :   GEN S = NULL;
     921        7483 :   for (j = 1; j < l; j++)
     922             :   {
     923        5117 :     GEN c = gel(L,j);
     924        5117 :     if (gequal0(c)) continue;
     925        4536 :     c = gmul(c, mfcoefs_i(gel(F,j), n, d));
     926        4536 :     S = S? gadd(S,c): c;
     927             :   }
     928        2366 :   if (!S) return zerovec(n+1);
     929        2366 :   if (!is_pm1(dL)) S = gdiv(S, dL);
     930        2366 :   return gerepileupto(av, S);
     931             : }
     932             : 
     933             : /* B_d(T_j Trace^new) as t_MF_BD(t_MF_HECKE(t_MF_NEWTRACE)) or
     934             :  * t_MF_HECKE(t_MF_NEWTRACE)
     935             :  * or t_MF_NEWTRACE in level N. Set d and j, return t_MF_NEWTRACE component*/
     936             : static GEN
     937       51814 : bhn_parse(GEN f, long *d, long *j)
     938             : {
     939       51814 :   long t = mf_get_type(f);
     940       51814 :   *d = *j = 1;
     941       51814 :   if (t == t_MF_BD) { *d = itos(gel(f,3)); f = gel(f,2); t = mf_get_type(f); }
     942       51814 :   if (t == t_MF_HECKE) { *j = gel(f,2)[1]; f = gel(f,3); }
     943       51814 :   return f;
     944             : }
     945             : /* f as above, return the t_MF_NEWTRACE component */
     946             : static GEN
     947       13580 : bhn_newtrace(GEN f)
     948             : {
     949       13580 :   long t = mf_get_type(f);
     950       13580 :   if (t == t_MF_BD) { f = gel(f,2); t = mf_get_type(f); }
     951       13580 :   if (t == t_MF_HECKE) f = gel(f,3);
     952       13580 :   return f;
     953             : }
     954             : static int
     955        2436 : ok_bhn_linear(GEN vf)
     956             : {
     957        2436 :   long i, N0 = 0, l = lg(vf);
     958             :   GEN CHI, gk;
     959        2436 :   if (l == 1) return 1;
     960        2436 :   gk = mf_get_gk(gel(vf,1));
     961        2436 :   CHI = mf_get_CHI(gel(vf,1));
     962        9534 :   for (i = 1; i < l; i++)
     963             :   {
     964        8568 :     GEN f = bhn_newtrace(gel(vf,i));
     965        8568 :     long N = mf_get_N(f);
     966        8568 :     if (mf_get_type(f) != t_MF_NEWTRACE) return 0;
     967        7098 :     if (N < N0) return 0; /* largest level must come last */
     968        7098 :     N0 = N;
     969        7098 :     if (!gequal(gk,mf_get_gk(f))) return 0; /* same k */
     970        7098 :     if (!gequal(gel(mf_get_CHI(f),2), gel(CHI,2))) return 0; /* same CHI */
     971             :   }
     972         966 :   return 1;
     973             : }
     974             : 
     975             : /* vF not empty, same hypotheses as bhnmat_extend */
     976             : static GEN
     977        5089 : bhnmat_extend_nocache(GEN M, long N, long n, long d, GEN vF)
     978             : {
     979             :   cachenew_t cache;
     980        5089 :   long l = lg(vF);
     981             :   GEN f;
     982        5089 :   if (l == 1) return M? M: cgetg(1, t_MAT);
     983        5012 :   f = bhn_newtrace(gel(vF,1)); /* N.B. mf_get_N(f) divides N */
     984        5012 :   init_cachenew(&cache, n*d, N, f);
     985        5012 :   M = bhnmat_extend(M, n, d, vF, &cache);
     986        5012 :   dbg_cachenew(&cache); return M;
     987             : }
     988             : /* c_linear of "bhn" mf closures, same hypotheses as bhnmat_extend */
     989             : static GEN
     990         973 : c_linear_bhn(long n, long d, GEN F)
     991             : {
     992             :   pari_sp av;
     993         973 :   GEN M, v, vF = gel(F,2), L = gel(F,3), dL = gel(F,4);
     994         973 :   if (lg(L) == 1) return zerovec(n+1);
     995         973 :   av = avma;
     996         973 :   M = bhnmat_extend_nocache(NULL, mf_get_N(F), n, d, vF);
     997         973 :   v = RgM_RgC_mul(M,L); settyp(v, t_VEC);
     998         973 :   if (!is_pm1(dL)) v = gdiv(v, dL);
     999         973 :   return gerepileupto(av, v);
    1000             : }
    1001             : 
    1002             : /* c in K, K := Q[X]/(T) vz = vector of consecutive powers of root z of T
    1003             :  * attached to an embedding s: K -> C. Return s(c) in C */
    1004             : static GEN
    1005       62006 : Rg_embed1(GEN c, GEN vz)
    1006             : {
    1007       62006 :   long t = typ(c);
    1008       62006 :   if (t == t_POLMOD) { c = gel(c,2); t = typ(c); }
    1009       62006 :   if (t == t_POL) c = RgX_RgV_eval(c, vz);
    1010       62006 :   return c;
    1011             : }
    1012             : /* return s(P) in C[X] */
    1013             : static GEN
    1014         861 : RgX_embed1(GEN P, GEN vz)
    1015             : {
    1016             :   long i, l;
    1017         861 :   GEN Q = cgetg_copy(P, &l);
    1018         861 :   Q[1] = P[1];
    1019         861 :   for (i = 2; i < l; i++) gel(Q,i) = Rg_embed1(gel(P,i), vz);
    1020         861 :   return normalizepol_lg(Q,l); /* normally a no-op */
    1021             : }
    1022             : /* return s(P) in C^n */
    1023             : static GEN
    1024         434 : vecembed1(GEN P, GEN vz)
    1025             : {
    1026             :   long i, l;
    1027         434 :   GEN Q = cgetg_copy(P, &l);
    1028         434 :   for (i = 1; i < l; i++) gel(Q,i) = Rg_embed1(gel(P,i), vz);
    1029         434 :   return Q;
    1030             : }
    1031             : /* P in L = K[X]/(U), K = Q[t]/T; s an embedding of K -> C attached
    1032             :  * to a root of T, extended to an embedding of L -> C attached to a root
    1033             :  * of s(U); vT powers of the root of T, vU powers of the root of s(U).
    1034             :  * Return s(P) in C^n */
    1035             : static GEN
    1036       13314 : Rg_embed2(GEN P, long vt, GEN vT, GEN vU)
    1037             : {
    1038             :   long i, l;
    1039             :   GEN Q;
    1040       13314 :   P = liftpol_shallow(P);
    1041       13314 :   if (typ(P) != t_POL) return P;
    1042       13300 :   if (varn(P) == vt) return Rg_embed1(P, vT);
    1043             :   /* varn(P) == vx */
    1044       13293 :   Q = cgetg_copy(P, &l); Q[1] = P[1];
    1045       13293 :   for (i = 2; i < l; i++) gel(Q,i) = Rg_embed1(gel(P,i), vT);
    1046       13293 :   return Rg_embed1(Q, vU);
    1047             : }
    1048             : static GEN
    1049          42 : vecembed2(GEN P, long vt, GEN vT, GEN vU)
    1050             : {
    1051             :   long i, l;
    1052          42 :   GEN Q = cgetg_copy(P, &l);
    1053          42 :   for (i = 1; i < l; i++) gel(Q,i) = Rg_embed2(gel(P,i), vt, vT, vU);
    1054          42 :   return Q;
    1055             : }
    1056             : static GEN
    1057         532 : RgX_embed2(GEN P, long vt, GEN vT, GEN vU)
    1058             : {
    1059             :   long i, l;
    1060         532 :   GEN Q = cgetg_copy(P, &l);
    1061         532 :   for (i = 2; i < l; i++) gel(Q,i) = Rg_embed2(gel(P,i), vt, vT, vU);
    1062         532 :   Q[1] = P[1]; return normalizepol_lg(Q,l);
    1063             : }
    1064             : /* embed polynomial f in variable vx [ may be a scalar ], E from getembed */
    1065             : static GEN
    1066        1575 : RgX_embed(GEN f, long vx, GEN E)
    1067             : {
    1068             :   GEN vT;
    1069        1575 :   if (typ(f) != t_POL || varn(f) != vx) return mfembed(E, f);
    1070        1554 :   if (lg(E) == 1) return f;
    1071        1358 :   vT = gel(E,2);
    1072        1358 :   if (lg(E) == 3)
    1073         826 :     f = RgX_embed1(f, vT);
    1074             :   else
    1075         532 :     f = RgX_embed2(f, varn(gel(E,1)), vT, gel(E,3));
    1076        1358 :   return f;
    1077             : }
    1078             : /* embed vector, E from getembed */
    1079             : GEN
    1080        1274 : mfvecembed(GEN E, GEN v)
    1081             : {
    1082             :   GEN vT;
    1083        1274 :   if (lg(E) == 1) return v;
    1084         476 :   vT = gel(E,2);
    1085         476 :   if (lg(E) == 3)
    1086         434 :     v = vecembed1(v, vT);
    1087             :   else
    1088          42 :     v = vecembed2(v, varn(gel(E,1)), vT, gel(E,3));
    1089         476 :   return v;
    1090             : }
    1091             : GEN
    1092           7 : mfmatembed(GEN E, GEN f)
    1093             : {
    1094             :   long i, l;
    1095             :   GEN g;
    1096           7 :   if (lg(E) == 1) return f;
    1097           7 :   g = cgetg_copy(f, &l);
    1098           7 :   for (i = 1; i < l; i++) gel(g,i) = mfvecembed(E, gel(f,i));
    1099           7 :   return g;
    1100             : }
    1101             : /* embed vector of polynomials in var vx */
    1102             : static GEN
    1103          98 : RgXV_embed(GEN f, long vx, GEN E)
    1104             : {
    1105             :   long i, l;
    1106             :   GEN v;
    1107          98 :   if (lg(E) == 1) return f;
    1108          70 :   v = cgetg_copy(f, &l);
    1109          70 :   for (i = 1; i < l; i++) gel(v,i) = RgX_embed(gel(f,i), vx, E);
    1110          70 :   return v;
    1111             : }
    1112             : 
    1113             : /* embed scalar */
    1114             : GEN
    1115       95262 : mfembed(GEN E, GEN f)
    1116             : {
    1117             :   GEN vT;
    1118       95262 :   if (lg(E) == 1) return f;
    1119       13335 :   vT = gel(E,2);
    1120       13335 :   if (lg(E) == 3)
    1121        4221 :     f = Rg_embed1(f, vT);
    1122             :   else
    1123        9114 :     f = Rg_embed2(f, varn(gel(E,1)), vT, gel(E,3));
    1124       13335 :   return f;
    1125             : }
    1126             : /* vector of the sigma(f), sigma in vE */
    1127             : static GEN
    1128         273 : RgX_embedall(GEN f, long vx, GEN vE)
    1129             : {
    1130         273 :   long i, l = lg(vE);
    1131         273 :   GEN v = cgetg(l, t_VEC);
    1132         273 :   for (i = 1; i < l; i++) gel(v,i) = RgX_embed(f, vx, gel(vE,i));
    1133         273 :   return l == 2? gel(v,1): v;
    1134             : }
    1135             : /* matrix whose colums are the sigma(v), sigma in vE */
    1136             : static GEN
    1137         329 : RgC_embedall(GEN v, GEN vE)
    1138             : {
    1139         329 :   long j, l = lg(vE);
    1140         329 :   GEN M = cgetg(l, t_MAT);
    1141         329 :   for (j = 1; j < l; j++) gel(M,j) = mfvecembed(gel(vE,j), v);
    1142         329 :   return M;
    1143             : }
    1144             : /* vector of the sigma(v), sigma in vE */
    1145             : static GEN
    1146        4907 : Rg_embedall_i(GEN v, GEN vE)
    1147             : {
    1148        4907 :   long j, l = lg(vE);
    1149        4907 :   GEN M = cgetg(l, t_VEC);
    1150        4907 :   for (j = 1; j < l; j++) gel(M,j) = mfembed(gel(vE,j), v);
    1151        4907 :   return M;
    1152             : }
    1153             : /* vector of the sigma(v), sigma in vE; if #vE == 1, return v */
    1154             : static GEN
    1155       89914 : Rg_embedall(GEN v, GEN vE)
    1156       89914 : { return (lg(vE) == 2)? mfembed(gel(vE,1), v): Rg_embedall_i(v, vE); }
    1157             : 
    1158             : static GEN
    1159         224 : c_div_i(long n, GEN F, GEN G)
    1160             : {
    1161             :   GEN VF, VG, a0, a0i, H;
    1162         224 :   VF = mfcoefsser(F, n); VG = mfcoefsser(G, n);
    1163         224 :   a0 = polcoef_i(VG, 0, -1);
    1164         224 :   if (gequal0(a0) || gequal1(a0)) a0 = a0i = NULL;
    1165             :   else
    1166             :   {
    1167          49 :     a0i = ginv(a0);
    1168          49 :     VG = gmul(ser_unscale(VG,a0), a0i);
    1169          49 :     VF = gmul(ser_unscale(VF,a0), a0i);
    1170             :   }
    1171         224 :   H = gdiv(VF, VG);
    1172         224 :   if (a0) H = ser_unscale(H,a0i);
    1173         224 :   return sertovecslice(H, n);
    1174             : }
    1175             : static GEN
    1176         224 : c_div(long n, long d, GEN F, GEN G)
    1177             : {
    1178         224 :   pari_sp av = avma;
    1179         224 :   GEN D = (d==1)? c_div_i(n, F,G): c_deflate(n, d, c_div_i(n*d, F,G));
    1180         224 :   return gerepilecopy(av, D);
    1181             : }
    1182             : 
    1183             : static GEN
    1184          35 : c_shift(long n, long d, GEN F, GEN gsh)
    1185             : {
    1186          35 :   pari_sp av = avma;
    1187             :   GEN vF;
    1188          35 :   long sh = itos(gsh), n1 = n*d + sh;
    1189          35 :   if (n1 < 0) return zerovec(n+1);
    1190          35 :   vF = mfcoefs_i(F, n1, 1);
    1191          35 :   if (sh < 0) vF = shallowconcat(zerovec(-sh), vF);
    1192          35 :   else vF = vecslice(vF, sh+1, n1+1);
    1193          35 :   return gerepilecopy(av, c_deflate(n, d, vF));
    1194             : }
    1195             : 
    1196             : static GEN
    1197          21 : c_deriv(long n, long d, GEN F, GEN gm)
    1198             : {
    1199          21 :   pari_sp av = avma;
    1200          21 :   GEN V = mfcoefs_i(F, n, d), res;
    1201          21 :   long i, m = itos(gm);
    1202          21 :   if (!m) return V;
    1203          21 :   res = cgetg(n+2, t_VEC); gel(res,1) = gen_0;
    1204          21 :   if (m < 0)
    1205           7 :   { for (i=1; i <= n; i++) gel(res, i+1) = gdiv(gel(V, i+1), powuu(i,-m)); }
    1206             :   else
    1207          14 :   { for (i=1; i <= n; i++) gel(res, i+1) = gmul(gel(V,i+1), powuu(i,m)); }
    1208          21 :   return gerepileupto(av, res);
    1209             : }
    1210             : 
    1211             : static GEN
    1212          14 : c_derivE2(long n, long d, GEN F, GEN gm)
    1213             : {
    1214          14 :   pari_sp av = avma;
    1215             :   GEN VF, VE, res, tmp, gk;
    1216          14 :   long i, m = itos(gm), nd;
    1217          14 :   if (m == 0) return mfcoefs_i(F, n, d);
    1218          14 :   nd = n*d;
    1219          14 :   VF = mfcoefs_i(F, nd, 1); VE = mfcoefs_i(mfEk(2), nd, 1);
    1220          14 :   gk = mf_get_gk(F);
    1221          14 :   if (m == 1)
    1222             :   {
    1223           7 :     res = cgetg(n+2, t_VEC);
    1224           7 :     for (i = 0; i <= n; i++) gel(res, i+1) = gmulsg(i, gel(VF, i*d+1));
    1225           7 :     tmp = c_deflate(n, d, RgV_mul_RgXn(VF, VE));
    1226           7 :     return gerepileupto(av, gsub(res, gmul(gdivgs(gk, 12), tmp)));
    1227             :   }
    1228             :   else
    1229             :   {
    1230             :     long j;
    1231          35 :     for (j = 1; j <= m; j++)
    1232             :     {
    1233          28 :       tmp = RgV_mul_RgXn(VF, VE);
    1234          28 :       for (i = 0; i <= nd; i++) gel(VF, i+1) = gmulsg(i, gel(VF, i+1));
    1235          28 :       VF = gsub(VF, gmul(gdivgs(gaddgs(gk, 2*(j-1)), 12), tmp));
    1236             :     }
    1237           7 :     return gerepilecopy(av, c_deflate(n, d, VF));
    1238             :   }
    1239             : }
    1240             : 
    1241             : /* Twist by the character (D/.) */
    1242             : static GEN
    1243           7 : c_twist(long n, long d, GEN F, GEN D)
    1244             : {
    1245           7 :   pari_sp av = avma;
    1246           7 :   GEN V = mfcoefs_i(F, n, d), res = cgetg(n+2, t_VEC);
    1247             :   long i;
    1248         119 :   for (i = 0; i <= n; i++)
    1249         112 :     gel(res, i + 1) = gmulsg(krois(D, i), gel(V, i+1));
    1250           7 :   return gerepileupto(av, res);
    1251             : }
    1252             : 
    1253             : /* form F given by closure, compute T(n)(F) as closure */
    1254             : static GEN
    1255         427 : c_hecke(long m, long l, GEN DATA, GEN F)
    1256             : {
    1257         427 :   pari_sp av = avma;
    1258         427 :   return gerepilecopy(av, hecke_i(m, l, NULL, F, DATA));
    1259             : }
    1260             : static GEN
    1261         147 : c_const(long n, long d, GEN C)
    1262             : {
    1263         147 :   GEN V = zerovec(n+1);
    1264         147 :   long i, j, l = lg(C);
    1265         147 :   if (l > d*n+2) l = d*n+2;
    1266         147 :   for (i = j = 1; i < l; i+=d, j++) gel(V, j) = gcopy(gel(C,i));
    1267         147 :   return V;
    1268             : }
    1269             : 
    1270             : static GEN
    1271         455 : eta3_ZXn(long m)
    1272             : {
    1273         455 :   long l = m+2, n, k;
    1274         455 :   GEN P = cgetg(l,t_POL);
    1275         455 :   P[1] = evalsigne(1)|evalvarn(0);
    1276         455 :   for (n = 2; n < l; n++) gel(P,n) = gen_0;
    1277        2471 :   for (n = k = 0;; n++)
    1278             :   {
    1279        4487 :     k += n; if (k >= m) break;
    1280             :     /* now k = n(n+1) / 2 */
    1281        2016 :     gel(P, k+2) = odd(n)? utoineg(2*n+1): utoipos(2*n+1);
    1282             :   }
    1283         455 :   return P;
    1284             : }
    1285             : 
    1286             : static GEN
    1287         455 : c_delta(long n, long d)
    1288             : {
    1289         455 :   pari_sp ltop = avma;
    1290         455 :   long N = n*d;
    1291         455 :   GEN e = eta3_ZXn(N);
    1292         455 :   e = ZXn_sqr(e,N);
    1293         455 :   e = ZXn_sqr(e,N);
    1294         455 :   e = ZXn_sqr(e,N); /* eta(x)^24 */
    1295         455 :   settyp(e, t_VEC);
    1296         455 :   gel(e,1) = gen_0; /* Delta(x) = x*eta(x)^24 as a t_VEC */
    1297         455 :   return gerepilecopy(ltop, c_deflate(n, d, e));
    1298             : }
    1299             : 
    1300             : /* return s(d) such that s|f <=> d | f^2 */
    1301             : static long
    1302          35 : mysqrtu(ulong d)
    1303             : {
    1304          35 :   GEN fa = myfactoru(d), P = gel(fa,1), E = gel(fa,2);
    1305          35 :   long l = lg(P), i, s = 1;
    1306          35 :   for (i = 1; i < l; i++) s *= upowuu(P[i], (E[i]+1)>>1);
    1307          35 :   return s;
    1308             : }
    1309             : static GEN
    1310        1428 : c_theta(long n, long d, GEN psi)
    1311             : {
    1312        1428 :   long lim = usqrt(n*d), F = mfcharmodulus(psi), par = mfcharparity(psi);
    1313        1428 :   long f, d2 = d == 1? 1: mysqrtu(d);
    1314        1428 :   GEN V = zerovec(n + 1);
    1315        5558 :   for (f = d2; f <= lim; f += d2)
    1316        4130 :     if (ugcd(F, f) == 1)
    1317             :     {
    1318        4123 :       pari_sp av = avma;
    1319        4123 :       GEN c = mfchareval_i(psi, f);
    1320        4123 :       gel(V, f*f/d + 1) = gerepileupto(av, par < 0 ? gmulgs(c,2*f) : gmul2n(c,1));
    1321             :     }
    1322        1428 :   if (F == 1) gel(V, 1) = gen_1;
    1323        1428 :   return V; /* no gerepile needed */
    1324             : }
    1325             : 
    1326             : static GEN
    1327         112 : c_etaquo(long n, long d, GEN eta, GEN gs)
    1328             : {
    1329         112 :   pari_sp av = avma;
    1330         112 :   long s = itos(gs), nd = n*d, nds = nd - s + 1;
    1331             :   GEN c;
    1332         112 :   if (nds <= 0) return zerovec(n+1);
    1333          91 :   c = RgX_to_RgC(eta_product_ZXn(eta, nds), nds); settyp(c, t_VEC);
    1334          91 :   if (s > 0) c = shallowconcat(zerovec(s), c);
    1335          91 :   return gerepilecopy(av, c_deflate(n, d, c));
    1336             : }
    1337             : 
    1338             : static GEN
    1339          49 : c_ell(long n, long d, GEN E)
    1340             : {
    1341          49 :   pari_sp av = avma;
    1342             :   GEN v;
    1343          49 :   if (d == 1) return concat(gen_0, anell(E, n));
    1344           7 :   v = shallowconcat(gen_0, anell(E, n*d));
    1345           7 :   return gerepilecopy(av, c_deflate(n, d, v));
    1346             : }
    1347             : 
    1348             : static GEN
    1349          21 : c_cusptrace(long n, long d, GEN F)
    1350             : {
    1351          21 :   pari_sp av = avma;
    1352          21 :   GEN D = gel(F,2), res = cgetg(n+2, t_VEC);
    1353          21 :   long i, N = mf_get_N(F), k = mf_get_k(F);
    1354          21 :   gel(res, 1) = gen_0;
    1355         140 :   for (i = 1; i <= n; i++)
    1356         119 :     gel(res, i+1) = mfcusptrace_i(N, k, i*d, mydivisorsu(i*d), D);
    1357          21 :   return gerepilecopy(av, res);
    1358             : }
    1359             : 
    1360             : static GEN
    1361         728 : c_newtrace(long n, long d, GEN F)
    1362             : {
    1363         728 :   pari_sp av = avma;
    1364             :   cachenew_t cache;
    1365         728 :   long N = mf_get_N(F);
    1366             :   GEN v;
    1367         728 :   init_cachenew(&cache, n*d, N, F);
    1368         728 :   v = colnewtrace(0, n, d, N, mf_get_k(F), &cache);
    1369         728 :   settyp(v, t_VEC); return gerepilecopy(av, v);
    1370             : }
    1371             : 
    1372             : static GEN
    1373        3773 : c_Bd(long n, long d, GEN F, GEN A)
    1374             : {
    1375        3773 :   pari_sp av = avma;
    1376        3773 :   long a = itou(A), ad = ugcd(a,d), aad = a/ad, i, j;
    1377        3773 :   GEN w, v = mfcoefs_i(F, n/aad, d/ad);
    1378        3773 :   if (a == 1) return v;
    1379        3773 :   n++; w = zerovec(n);
    1380        3773 :   for (i = j = 1; j <= n; i++, j += aad) gel(w,j) = gcopy(gel(v,i));
    1381        3773 :   return gerepileupto(av, w);
    1382             : }
    1383             : 
    1384             : static GEN
    1385        3591 : c_dihedral(long n, long d, GEN bnr, GEN w, GEN k0j)
    1386             : {
    1387        3591 :   pari_sp av = avma;
    1388        3591 :   GEN V = dihan(bnr, w, k0j, n*d);
    1389        3591 :   GEN Tinit = gel(w,3), Pm = gel(Tinit,1);
    1390        3591 :   GEN A = c_deflate(n, d, V);
    1391        3591 :   if (degpol(Pm) == 1 || RgX_is_QX(A)) return gerepilecopy(av, A);
    1392         763 :   return gerepileupto(av, gmodulo(A, Pm));
    1393             : }
    1394             : 
    1395             : static GEN
    1396         140 : c_mfEH(long n, long d, GEN F)
    1397             : {
    1398         140 :   pari_sp av = avma;
    1399             :   GEN v, M, A;
    1400         140 :   long i, r = mf_get_r(F);
    1401         140 :   if (n == 1)
    1402          14 :     return gerepilecopy(av, mkvec2(mfEHcoef(r,0),mfEHcoef(r,d)));
    1403             :   /* speedup mfcoef */
    1404         126 :   if (r == 1)
    1405             :   {
    1406          70 :     v = cgetg(n+2, t_VEC);
    1407          70 :     gel(v,1) = sstoQ(-1,12);
    1408       83258 :     for (i = 1; i <= n; i++)
    1409             :     {
    1410       83188 :       long id = i*d, a = id & 3;
    1411       83188 :       gel(v,i+1) = (a==1 || a==2)? gen_0: sstoQ(hclassno6u(id), 6);
    1412             :     }
    1413          70 :     return v; /* no gerepile needed */
    1414             :   }
    1415          56 :   M = mfEHmat(n*d+1,r);
    1416          56 :   if (d > 1)
    1417             :   {
    1418           7 :     long l = lg(M);
    1419           7 :     for (i = 1; i < l; i++) gel(M,i) = c_deflate(n, d, gel(M,i));
    1420             :   }
    1421          56 :   A = gel(F,2); /* [num(B), den(B)] */
    1422          56 :   v = RgC_Rg_div(RgM_RgC_mul(M, gel(A,1)), gel(A,2));
    1423          56 :   settyp(v,t_VEC); return gerepileupto(av, v);
    1424             : }
    1425             : 
    1426             : static GEN
    1427        5705 : c_mfeisen(long n, long d, GEN F)
    1428             : {
    1429        5705 :   pari_sp av = avma;
    1430        5705 :   GEN v, vchi, E0, P, T, CHI, gk = mf_get_gk(F);
    1431             :   long i, k;
    1432        5705 :   if (typ(gk) != t_INT) return c_mfEH(n, d, F);
    1433        5565 :   k = itou(gk);
    1434        5565 :   vchi = gel(F,2);
    1435        5565 :   E0 = gel(vchi,1);
    1436        5565 :   T = gel(vchi,2);
    1437        5565 :   P = gel(T,1);
    1438        5565 :   CHI = gel(vchi,3);
    1439        5565 :   v = cgetg(n+2, t_VEC);
    1440        5565 :   gel(v, 1) = gcopy(E0); /* E(0) */
    1441        5565 :   if (lg(vchi) == 5)
    1442             :   { /* E_k(chi1,chi2) */
    1443        3787 :     GEN CHI2 = gel(vchi,4), F3 = gel(F,3);
    1444        3787 :     long ord = F3[1], j = F3[2];
    1445        3787 :     for (i = 1; i <= n; i++) gel(v, i+1) = sigchi2(k, CHI, CHI2, i*d, ord);
    1446        3787 :     if (lg(T) == 4) v = QabV_tracerel(T, j, v);
    1447             :   }
    1448             :   else
    1449             :   { /* E_k(chi) */
    1450        1778 :     for (i = 1; i <= n; i++) gel(v, i+1) = sigchi(k, CHI, i*d);
    1451             :   }
    1452        5565 :   if (degpol(P) != 1 && !RgV_is_QV(v)) return gerepileupto(av, gmodulo(v, P));
    1453        4921 :   return gerepilecopy(av, v);
    1454             : }
    1455             : 
    1456             : /* L(chi_D, 1-k) */
    1457             : static GEN
    1458          28 : lfunquadneg(long D, long k)
    1459             : {
    1460          28 :   GEN B, dS, S = gen_0;
    1461          28 :   long r, N = labs(D);
    1462             :   pari_sp av;
    1463          28 :   if (k == 1 && N == 1) return gneg(ghalf);
    1464             :   /* B = N^k * denom(B) * B(x/N) */
    1465          28 :   B = ZX_rescale(Q_remove_denom(bernpol(k, 0), &dS), utoi(N));
    1466          28 :   dS = mul_denom(dS, stoi(-N*k));
    1467          28 :   av = avma;
    1468        7175 :   for (r = 0; r < N; r++)
    1469             :   {
    1470        7147 :     long c = kross(D, r);
    1471        7147 :     if (c)
    1472             :     {
    1473        5152 :       GEN tmp = poleval(B, utoi(r));
    1474        5152 :       S = c > 0 ? addii(S, tmp) : subii(S, tmp);
    1475        5152 :       S = gerepileuptoint(av, S);
    1476             :     }
    1477             :   }
    1478          28 :   return gdiv(S, dS);
    1479             : }
    1480             : 
    1481             : /* Returns vector of coeffs from F[0], F[d], ..., F[d*n] */
    1482             : static GEN
    1483       21280 : mfcoefs_i(GEN F, long n, long d)
    1484             : {
    1485       21280 :   if (n < 0) return gen_0;
    1486       21280 :   switch(mf_get_type(F))
    1487             :   {
    1488         147 :     case t_MF_CONST: return c_const(n, d, gel(F,2));
    1489        5705 :     case t_MF_EISEN: return c_mfeisen(n, d, F);
    1490         658 :     case t_MF_Ek: return c_Ek(n, d, F);
    1491         455 :     case t_MF_DELTA: return c_delta(n, d);
    1492        1365 :     case t_MF_THETA: return c_theta(n, d, gel(F,2));
    1493         112 :     case t_MF_ETAQUO: return c_etaquo(n, d, gel(F,2), gel(F,3));
    1494          49 :     case t_MF_ELL: return c_ell(n, d, gel(F,2));
    1495         511 :     case t_MF_MUL: return c_mul(n, d, gel(F,2), gel(F,3));
    1496          77 :     case t_MF_POW: return c_pow(n, d, gel(F,2), gel(F,3));
    1497          21 :     case t_MF_BRACKET: return c_bracket(n, d, gel(F,2), gel(F,3), gel(F,4));
    1498        2366 :     case t_MF_LINEAR: return c_linear(n, d, gel(F,2), gel(F,3), gel(F,4));
    1499         973 :     case t_MF_LINEAR_BHN: return c_linear_bhn(n, d, F);
    1500         224 :     case t_MF_DIV: return c_div(n, d, gel(F,2), gel(F,3));
    1501          35 :     case t_MF_SHIFT: return c_shift(n, d, gel(F,2), gel(F,3));
    1502          21 :     case t_MF_DERIV: return c_deriv(n, d, gel(F,2), gel(F,3));
    1503          14 :     case t_MF_DERIVE2: return c_derivE2(n, d, gel(F,2), gel(F,3));
    1504           7 :     case t_MF_TWIST: return c_twist(n, d, gel(F,2), gel(F,3));
    1505         427 :     case t_MF_HECKE: return c_hecke(n, d, gel(F,2), gel(F,3));
    1506        3773 :     case t_MF_BD: return c_Bd(n, d, gel(F,2), gel(F,3));
    1507          21 :     case t_MF_TRACE: return c_cusptrace(n, d, F);
    1508         728 :     case t_MF_NEWTRACE: return c_newtrace(n, d, F);
    1509        3591 :     case t_MF_DIHEDRAL: return c_dihedral(n, d, gel(F,2), gel(F,3), gel(F,4));
    1510             :     default: pari_err_TYPE("mfcoefs",F); return NULL;/*LCOV_EXCL_LINE*/
    1511             :   }
    1512             : }
    1513             : 
    1514             : static GEN
    1515         147 : matdeflate(long n, long d, GEN M)
    1516             : {
    1517             :   long i, l;
    1518             :   GEN A;
    1519             :   /*  if (d == 1) return M; */
    1520         147 :   A = cgetg_copy(M,&l);
    1521         147 :   for (i = 1; i < l; i++) gel(A,i) = c_deflate(n,d,gel(M,i));
    1522         147 :   return A;
    1523             : }
    1524             : static int
    1525        4977 : space_is_cusp(long space) { return space != mf_FULL && space != mf_EISEN; }
    1526             : /* safe with flraw mf */
    1527             : static GEN
    1528        1988 : mfcoefs_mf(GEN mf, long n, long d)
    1529             : {
    1530        1988 :   GEN MS, ME, E = MF_get_E(mf), S = MF_get_S(mf), M = MF_get_M(mf);
    1531        1988 :   long lE = lg(E), lS = lg(S), l = lE+lS-1;
    1532             : 
    1533        1988 :   if (l == 1) return cgetg(1, t_MAT);
    1534        1876 :   if (typ(M) == t_MAT && lg(M) != 1 && (n+1)*d < nbrows(M))
    1535          21 :     return matdeflate(n, d, M); /*cached; lg = 1 is possible from mfinit */
    1536        1855 :   ME = (lE == 1)? cgetg(1, t_MAT): mfvectomat(E, n, d);
    1537        1855 :   if (lS == 1)
    1538         357 :     MS = cgetg(1, t_MAT);
    1539        1498 :   else if (mf_get_type(gel(S,1)) == t_MF_DIV) /*k 1/2-integer or k=1 (exotic)*/
    1540         126 :     MS = matdeflate(n,d, mflineardivtomat(MF_get_N(mf), S, n*d));
    1541        1372 :   else if (MF_get_k(mf) == 1) /* k = 1 (dihedral) */
    1542             :   {
    1543         140 :     GEN M = mfvectomat(gmael(S,1,2), n, d);
    1544             :     long i;
    1545         140 :     MS = cgetg(lS, t_MAT);
    1546         448 :     for (i = 1; i < lS; i++)
    1547             :     {
    1548         308 :       GEN f = gel(S,i), dc = gel(f,4), c = RgM_RgC_mul(M, gel(f,3));
    1549         308 :       if (!equali1(dc)) c = RgC_Rg_div(c,dc);
    1550         308 :       gel(MS,i) = c;
    1551             :     }
    1552             :   }
    1553             :   else /* k >= 2 integer */
    1554        1232 :     MS = bhnmat_extend_nocache(NULL, MF_get_N(mf), n, d, S);
    1555        1855 :   return shallowconcat(ME,MS);
    1556             : }
    1557             : GEN
    1558        3241 : mfcoefs(GEN F, long n, long d)
    1559             : {
    1560        3241 :   if (!checkmf_i(F))
    1561             :   {
    1562          42 :     pari_sp av = avma;
    1563          42 :     GEN mf = checkMF_i(F); if (!mf) pari_err_TYPE("mfcoefs", F);
    1564          42 :     return gerepilecopy(av, mfcoefs_mf(mf,n,d));
    1565             :   }
    1566        3199 :   if (d <= 0) pari_err_DOMAIN("mfcoefs", "d", "<=", gen_0, stoi(d));
    1567        3199 :   if (n < 0) return cgetg(1, t_VEC);
    1568        3199 :   return mfcoefs_i(F, n, d);
    1569             : }
    1570             : 
    1571             : /* assume k >= 0 */
    1572             : static GEN
    1573         210 : mfak_i(GEN F, long k)
    1574             : {
    1575         210 :   if (!k) return gel(mfcoefs_i(F,0,1), 1);
    1576         154 :   return gel(mfcoefs_i(F,1,k), 2);
    1577             : }
    1578             : GEN
    1579          70 : mfcoef(GEN F, long n)
    1580             : {
    1581          70 :   pari_sp av = avma;
    1582          70 :   if (!checkmf_i(F)) pari_err_TYPE("mfcoef",F);
    1583          70 :   return n < 0? gen_0: gerepilecopy(av, mfak_i(F, n));
    1584             : }
    1585             : 
    1586             : static GEN
    1587         112 : paramconst() { return tagparams(t_MF_CONST, mkNK(1,0,mfchartrivial())); }
    1588             : static GEN
    1589          70 : mftrivial(void) { retmkvec2(paramconst(), cgetg(1,t_VEC)); }
    1590             : static GEN
    1591          42 : mf1(void) { retmkvec2(paramconst(), mkvec(gen_1)); }
    1592             : 
    1593             : /* induce mfchar CHI to G */
    1594             : static GEN
    1595      306516 : induce(GEN G, GEN CHI)
    1596             : {
    1597             :   GEN o, chi;
    1598      306516 :   if (typ(CHI) == t_INT) /* Kronecker */
    1599             :   {
    1600      300860 :     chi = znchar_quad(G, CHI);
    1601      300860 :     o = ZV_equal0(chi)? gen_1: gen_2;
    1602      300860 :     CHI = mkvec4(G,chi,o,cgetg(1,t_VEC));
    1603             :   }
    1604             :   else
    1605             :   {
    1606        5656 :     if (mfcharmodulus(CHI) == itos(znstar_get_N(G))) return CHI;
    1607        5264 :     CHI = leafcopy(CHI);
    1608        5264 :     chi = zncharinduce(gel(CHI,1), gel(CHI,2), G);
    1609        5264 :     gel(CHI,1) = G;
    1610        5264 :     gel(CHI,2) = chi;
    1611             :   }
    1612      306124 :   return CHI;
    1613             : }
    1614             : /* induce mfchar CHI to znstar(G) */
    1615             : static GEN
    1616       42441 : induceN(long N, GEN CHI)
    1617             : {
    1618       42441 :   if (mfcharmodulus(CHI) != N) CHI = induce(znstar0(utoipos(N),1), CHI);
    1619       42441 :   return CHI;
    1620             : }
    1621             : /* *pCHI1 and *pCHI2 are mfchar, induce to common modulus */
    1622             : static void
    1623        5145 : char2(GEN *pCHI1, GEN *pCHI2)
    1624             : {
    1625        5145 :   GEN CHI1 = *pCHI1, G1 = gel(CHI1,1), N1 = znstar_get_N(G1);
    1626        5145 :   GEN CHI2 = *pCHI2, G2 = gel(CHI2,1), N2 = znstar_get_N(G2);
    1627        5145 :   if (!equalii(N1,N2))
    1628             :   {
    1629        3759 :     GEN G, d = gcdii(N1,N2);
    1630        3759 :     if      (equalii(N2,d)) *pCHI2 = induce(G1, CHI2);
    1631        1239 :     else if (equalii(N1,d)) *pCHI1 = induce(G2, CHI1);
    1632             :     else
    1633             :     {
    1634         154 :       if (!equali1(d)) N2 = diviiexact(N2,d);
    1635         154 :       G = znstar0(mulii(N1,N2), 1);
    1636         154 :       *pCHI1 = induce(G, CHI1);
    1637         154 :       *pCHI2 = induce(G, CHI2);
    1638             :     }
    1639             :   }
    1640        5145 : }
    1641             : /* mfchar or charinit wrt same modulus; outputs a mfchar */
    1642             : static GEN
    1643      301840 : mfcharmul_i(GEN CHI1, GEN CHI2)
    1644             : {
    1645      301840 :   GEN G = gel(CHI1,1), chi3 = zncharmul(G, gel(CHI1,2), gel(CHI2,2));
    1646      301840 :   return mfcharGL(G, chi3);
    1647             : }
    1648             : /* mfchar or charinit; outputs a mfchar */
    1649             : static GEN
    1650         987 : mfcharmul(GEN CHI1, GEN CHI2)
    1651             : {
    1652         987 :   char2(&CHI1, &CHI2); return mfcharmul_i(CHI1,CHI2);
    1653             : }
    1654             : /* mfchar or charinit; outputs a mfchar */
    1655             : static GEN
    1656         105 : mfcharpow(GEN CHI, GEN n)
    1657             : {
    1658             :   GEN G, chi;
    1659         105 :   G = gel(CHI,1); chi = zncharpow(G, gel(CHI,2), n);
    1660         105 :   return mfcharGL(G, chi);
    1661             : }
    1662             : /* mfchar or charinit wrt same modulus; outputs a mfchar */
    1663             : static GEN
    1664        4158 : mfchardiv_i(GEN CHI1, GEN CHI2)
    1665             : {
    1666        4158 :   GEN G = gel(CHI1,1), chi3 = znchardiv(G, gel(CHI1,2), gel(CHI2,2));
    1667        4158 :   return mfcharGL(G, chi3);
    1668             : }
    1669             : /* mfchar or charinit; outputs a mfchar */
    1670             : static GEN
    1671        4158 : mfchardiv(GEN CHI1, GEN CHI2)
    1672             : {
    1673        4158 :   char2(&CHI1, &CHI2); return mfchardiv_i(CHI1,CHI2);
    1674             : }
    1675             : static GEN
    1676          28 : mfcharconj(GEN CHI)
    1677             : {
    1678          28 :   CHI = leafcopy(CHI);
    1679          28 :   gel(CHI,2) = zncharconj(gel(CHI,1), gel(CHI,2));
    1680          28 :   return CHI;
    1681             : }
    1682             : 
    1683             : /* CHI mfchar, assume 4 | N. Multiply CHI by \chi_{-4} */
    1684             : static GEN
    1685        1071 : mfchilift(GEN CHI, long N)
    1686             : {
    1687        1071 :   CHI = induceN(N, CHI);
    1688        1071 :   return mfcharmul_i(CHI, induce(gel(CHI,1), stoi(-4)));
    1689             : }
    1690             : /* CHI defined mod N, N4 = N/4;
    1691             :  * if CHI is defined mod N4 return CHI;
    1692             :  * else if CHI' = CHI*(-4,.) is defined mod N4, return CHI' (primitive)
    1693             :  * else return NULL */
    1694             : static GEN
    1695          70 : mfcharchiliftprim(GEN CHI, long N4)
    1696             : {
    1697          70 :   long FC = mfcharconductor(CHI);
    1698          70 :   if (N4 % FC == 0) return CHI;
    1699          14 :   CHI = mfchilift(CHI, N4 << 2);
    1700          14 :   CHI = mfchartoprimitive(CHI, &FC);
    1701          14 :   return (N4 % FC == 0)? CHI: NULL;
    1702             : }
    1703             : static GEN
    1704        2191 : mfchiadjust(GEN CHI, GEN gk, long N)
    1705             : {
    1706        2191 :   long par = mfcharparity(CHI);
    1707        2191 :   if (typ(gk) == t_INT &&  mpodd(gk)) par = -par;
    1708        2191 :   return par == 1 ? CHI : mfchilift(CHI, N);
    1709             : }
    1710             : 
    1711             : static GEN
    1712        2996 : mfsamefield(GEN P, GEN Q)
    1713             : {
    1714        2996 :   if (degpol(P) == 1) return Q;
    1715         455 :   if (degpol(Q) == 1) return P;
    1716         427 :   if (!gequal(P,Q)) pari_err_TYPE("mfsamefield [different fields]",mkvec2(P,Q));
    1717         420 :   return P;
    1718             : }
    1719             : 
    1720             : GEN
    1721         329 : mfmul(GEN f, GEN g)
    1722             : {
    1723         329 :   pari_sp av = avma;
    1724             :   GEN N, K, NK, CHI;
    1725         329 :   if (!checkmf_i(f)) pari_err_TYPE("mfmul",f);
    1726         329 :   if (!checkmf_i(g)) pari_err_TYPE("mfmul",g);
    1727         329 :   N = lcmii(mf_get_gN(f), mf_get_gN(g));
    1728         329 :   K = gadd(mf_get_gk(f), mf_get_gk(g));
    1729         329 :   CHI = mfcharmul(mf_get_CHI(f), mf_get_CHI(g));
    1730         329 :   CHI = mfchiadjust(CHI, K, itos(N));
    1731         329 :   NK = mkgNK(N, K, CHI, mfsamefield(mf_get_field(f), mf_get_field(g)));
    1732         322 :   return gerepilecopy(av, tag2(t_MF_MUL, NK, f, g));
    1733             : }
    1734             : GEN
    1735          49 : mfpow(GEN f, long n)
    1736             : {
    1737          49 :   pari_sp av = avma;
    1738             :   GEN KK, NK, gn, CHI;
    1739          49 :   if (!checkmf_i(f)) pari_err_TYPE("mfpow",f);
    1740          49 :   if (!n) return mf1();
    1741          49 :   if (n == 1) return gcopy(f);
    1742          49 :   KK = gmulsg(n,mf_get_gk(f));
    1743          49 :   gn = stoi(n);
    1744          49 :   CHI = mfcharpow(mf_get_CHI(f), gn);
    1745          49 :   CHI = mfchiadjust(CHI, KK, mf_get_N(f));
    1746          49 :   NK = mkgNK(mf_get_gN(f), KK, CHI, mf_get_field(f));
    1747          49 :   return gerepilecopy(av, tag2(t_MF_POW, NK, f, gn));
    1748             : }
    1749             : GEN
    1750          21 : mfbracket(GEN f, GEN g, long m)
    1751             : {
    1752          21 :   pari_sp av = avma;
    1753             :   GEN N, K, NK, CHI;
    1754          21 :   if (!checkmf_i(f)) pari_err_TYPE("mfbracket",f);
    1755          21 :   if (!checkmf_i(g)) pari_err_TYPE("mfbracket",g);
    1756          21 :   if (m < 0) pari_err_TYPE("mfbracket [m<0]",stoi(m));
    1757          21 :   K = gaddgs(gadd(mf_get_gk(f), mf_get_gk(g)), 2*m);
    1758          21 :   if (gsigne(K) < 0) pari_err_IMPL("mfbracket for this form");
    1759          21 :   N = lcmii(mf_get_gN(f), mf_get_gN(g));
    1760          21 :   CHI = mfcharmul(mf_get_CHI(f), mf_get_CHI(g));
    1761          21 :   CHI = mfchiadjust(CHI, K, itou(N));
    1762          21 :   NK = mkgNK(N, K, CHI, mfsamefield(mf_get_field(f), mf_get_field(g)));
    1763          21 :   return gerepilecopy(av, tag3(t_MF_BRACKET, NK, f, g, utoi(m)));
    1764             : }
    1765             : 
    1766             : /* remove 0 entries in L */
    1767             : static int
    1768        1050 : mflinear_strip(GEN *pF, GEN *pL)
    1769             : {
    1770        1050 :   pari_sp av = avma;
    1771        1050 :   GEN F = *pF, L = *pL;
    1772        1050 :   long i, j, l = lg(L);
    1773        1050 :   GEN F2 = cgetg(l, t_VEC), L2 = cgetg(l, t_VEC);
    1774        6426 :   for (i = j = 1; i < l; i++)
    1775             :   {
    1776        5376 :     if (gequal0(gel(L,i))) continue;
    1777        2954 :     gel(F2,j) = gel(F,i);
    1778        2954 :     gel(L2,j) = gel(L,i); j++;
    1779             :   }
    1780        1050 :   if (j == l) avma = av;
    1781             :   else
    1782             :   {
    1783         273 :     setlg(F2,j); *pF = F2;
    1784         273 :     setlg(L2,j); *pL = L2;
    1785             :   }
    1786        1050 :   return (j > 1);
    1787             : }
    1788             : static GEN
    1789        4074 : taglinear_i(long t, GEN NK, GEN F, GEN L)
    1790             : {
    1791             :   GEN dL;
    1792        4074 :   L = Q_remove_denom(L, &dL); if (!dL) dL = gen_1;
    1793        4074 :   return tag3(t, NK, F, L, dL);
    1794             : }
    1795             : static GEN
    1796        1442 : taglinear(GEN NK, GEN F, GEN L)
    1797             : {
    1798        1442 :   long t = ok_bhn_linear(F)? t_MF_LINEAR_BHN: t_MF_LINEAR;
    1799        1442 :    return taglinear_i(t, NK, F, L);
    1800             : }
    1801             : /* assume F has parameters NK = [N,K,CHI] */
    1802             : static GEN
    1803         301 : mflinear_i(GEN NK, GEN F, GEN L)
    1804             : {
    1805         301 :   if (!mflinear_strip(&F,&L)) return mftrivial();
    1806         301 :   return taglinear(NK, F,L);
    1807             : }
    1808             : static GEN
    1809         434 : mflinear_bhn(GEN mf, GEN L)
    1810             : {
    1811             :   long i, l;
    1812         434 :   GEN P, NK, F = MF_get_S(mf);
    1813         434 :   if (!mflinear_strip(&F,&L)) return mftrivial();
    1814         427 :   l = lg(L); P = pol_x(1);
    1815        2415 :   for (i = 1; i < l; i++)
    1816             :   {
    1817        1988 :     GEN c = gel(L,i);
    1818        1988 :     if (typ(c) == t_POLMOD && varn(gel(c,1)) == 1) P = mfsamefield(P,gel(c,1));
    1819             :   }
    1820         427 :   NK = mkgNK(MF_get_gN(mf), MF_get_gk(mf), MF_get_CHI(mf), P);
    1821         427 :   return taglinear_i(t_MF_LINEAR_BHN,  NK, F,L);
    1822             : }
    1823             : 
    1824             : /* F vector of forms with same weight and character but varying level, return
    1825             :  * global [N,k,chi,P] */
    1826             : static GEN
    1827        1974 : vecmfNK(GEN F)
    1828             : {
    1829        1974 :   long i, l = lg(F);
    1830             :   GEN N, f;
    1831        1974 :   if (l == 1) return mkNK(1, 0, mfchartrivial());
    1832        1974 :   f = gel(F,1); N = mf_get_gN(f);
    1833        1974 :   for (i = 2; i < l; i++) N = lcmii(N, mf_get_gN(gel(F,i)));
    1834        1974 :   return mkgNK(N, mf_get_gk(f), mf_get_CHI(f), mf_get_field(f));
    1835             : }
    1836             : /* do not use mflinear: mflineardivtomat rely on F being constant across the
    1837             :  * basis where mflinear strips the ones matched by 0 coeffs. Assume k and CHI
    1838             :  * constant, N is allowed to vary. */
    1839             : static GEN
    1840         994 : vecmflinear(GEN F, GEN C)
    1841             : {
    1842         994 :   long i, t, l = lg(C);
    1843         994 :   GEN NK, v = cgetg(l, t_VEC);
    1844         994 :   if (l == 1) return v;
    1845         994 :   t = ok_bhn_linear(F)? t_MF_LINEAR_BHN: t_MF_LINEAR;
    1846         994 :   NK = vecmfNK(F);
    1847         994 :   for (i = 1; i < l; i++) gel(v,i) = taglinear_i(t, NK, F, gel(C,i));
    1848         994 :   return v;
    1849             : }
    1850             : /* vecmflinear(F,C), then divide everything by E, which has valuation 0 */
    1851             : static GEN
    1852         266 : vecmflineardiv0(GEN F, GEN C, GEN E)
    1853             : {
    1854         266 :   GEN v = vecmflinear(F, C);
    1855         266 :   long i, l = lg(v);
    1856         266 :   for (i = 1; i < l; i++) gel(v,i) = mfdiv_val(gel(v,i), E, 0);
    1857         266 :   return v;
    1858             : }
    1859             : 
    1860             : /* Non empty linear combination of linear combinations of same
    1861             :  * F_j=\sum_i \mu_{i,j}G_i so R = \sum_i (\sum_j(\la_j\mu_{i,j})) G_i */
    1862             : static GEN
    1863         980 : mflinear_linear(GEN F, GEN L, int strip)
    1864             : {
    1865         980 :   long l = lg(F), j;
    1866         980 :   GEN vF, M = cgetg(l, t_MAT);
    1867        6839 :   for (j = 1; j < l; j++)
    1868             :   {
    1869        5859 :     GEN f = gel(F,j), c = gel(f,3), d = gel(f,4);
    1870        5859 :     if (typ(c) == t_VEC) c = shallowtrans(c);
    1871        5859 :     if (!isint1(d)) c = RgC_Rg_div(c, d);
    1872        5859 :     gel(M,j) = c;
    1873             :   }
    1874         980 :   vF = gmael(F,1,2);
    1875         980 :   L = RgM_RgC_mul(M,L);
    1876         980 :   if (strip && !mflinear_strip(&vF,&L)) return mftrivial();
    1877         980 :   return taglinear(vecmfNK(vF), vF, L);
    1878             : }
    1879             : /* F non-empty vector of forms of the form mfdiv(mflinear(B,v), E) where E
    1880             :  * does not vanish at oo, or mflinear(B,v). Apply mflinear(F, L) */
    1881             : static GEN
    1882         980 : mflineardiv_linear(GEN F, GEN L, int strip)
    1883             : {
    1884         980 :   long l = lg(F), j;
    1885             :   GEN v, E, f;
    1886         980 :   if (lg(L) != l) pari_err_DIM("mflineardiv_linear");
    1887         980 :   f = gel(F,1); /* l > 1 */
    1888         980 :   if (mf_get_type(f) != t_MF_DIV) return mflinear_linear(F,L,strip);
    1889         812 :   E = gel(f,3);
    1890         812 :   v = cgetg(l, t_VEC);
    1891         812 :   for (j = 1; j < l; j++) { GEN f = gel(F,j); gel(v,j) = gel(f,2); }
    1892         812 :   return mfdiv_val(mflinear_linear(v,L,strip), E, 0);
    1893             : }
    1894             : static GEN
    1895         259 : vecmflineardiv_linear(GEN F, GEN M)
    1896             : {
    1897         259 :   long i, l = lg(M);
    1898         259 :   GEN v = cgetg(l, t_VEC);
    1899         259 :   for (i = 1; i < l; i++) gel(v,i) = mflineardiv_linear(F, gel(M,i), 0);
    1900         259 :   return v;
    1901             : }
    1902             : 
    1903             : static GEN
    1904         448 : tobasis(GEN mf, GEN F, GEN L)
    1905             : {
    1906         448 :   if (checkmf_i(L) && mf) return mftobasis(mf, L, 0);
    1907         441 :   if (typ(F) != t_VEC) pari_err_TYPE("mflinear",F);
    1908         441 :   if (!is_vec_t(typ(L))) pari_err_TYPE("mflinear",L);
    1909         441 :   if (lg(L) != lg(F)) pari_err_DIM("mflinear");
    1910         441 :   return L;
    1911             : }
    1912             : GEN
    1913         476 : mflinear(GEN F, GEN L)
    1914             : {
    1915         476 :   pari_sp av = avma;
    1916         476 :   GEN G, NK, P, mf = checkMF_i(F), N = NULL, K = NULL, CHI = NULL;
    1917             :   long i, l;
    1918         476 :   if (mf)
    1919             :   {
    1920         350 :     GEN gk = MF_get_gk(mf);
    1921         350 :     F = MF_get_basis(F);
    1922         350 :     if (typ(gk) != t_INT)
    1923          28 :       return gerepilecopy(av, mflineardiv_linear(F, L, 1));
    1924         322 :     if (itou(gk) > 1 && space_is_cusp(MF_get_space(mf)))
    1925             :     {
    1926         210 :       L = tobasis(mf, F, L);
    1927         210 :       return gerepilecopy(av, mflinear_bhn(mf, L));
    1928             :     }
    1929             :   }
    1930         238 :   L = tobasis(mf, F, L);
    1931         238 :   if (!mflinear_strip(&F,&L)) return mftrivial();
    1932             : 
    1933         231 :   l = lg(F);
    1934         231 :   if (l == 2 && gequal1(gel(L,1))) return gerepilecopy(av, gel(F,1));
    1935         175 :   P = pol_x(1);
    1936         581 :   for (i = 1; i < l; i++)
    1937             :   {
    1938         413 :     GEN f = gel(F,i), c = gel(L,i), Ni, Ki;
    1939         413 :     if (!checkmf_i(f)) pari_err_TYPE("mflinear", f);
    1940         413 :     Ni = mf_get_gN(f); N = N? lcmii(N, Ni): Ni;
    1941         413 :     Ki = mf_get_gk(f);
    1942         413 :     if (!K) K = Ki;
    1943         238 :     else if (!gequal(K, Ki))
    1944           7 :       pari_err_TYPE("mflinear [different weights]", mkvec2(K,Ki));
    1945         406 :     P = mfsamefield(P, mf_get_field(f));
    1946         406 :     if (typ(c) == t_POLMOD && varn(gel(c,1)) == 1) P = mfsamefield(P, gel(c,1));
    1947             :   }
    1948         168 :   G = znstar0(N,1);
    1949         560 :   for (i = 1; i < l; i++)
    1950             :   {
    1951         399 :     GEN CHI2 = mf_get_CHI(gel(F,i));
    1952         399 :     CHI2 = induce(G, CHI2);
    1953         399 :     if (!CHI) CHI = CHI2;
    1954         231 :     else if (!gequal(CHI, CHI2))
    1955           7 :       pari_err_TYPE("mflinear [different characters]", mkvec2(CHI,CHI2));
    1956             :   }
    1957         161 :   NK = mkgNK(N, K, CHI, P);
    1958         161 :   return gerepilecopy(av, taglinear(NK,F,L));
    1959             : }
    1960             : 
    1961             : GEN
    1962          42 : mfshift(GEN F, long sh)
    1963             : {
    1964          42 :   pari_sp av = avma;
    1965          42 :   if (!checkmf_i(F)) pari_err_TYPE("mfshift",F);
    1966          42 :   return gerepilecopy(av, tag2(t_MF_SHIFT, mf_get_NK(F), F, stoi(sh)));
    1967             : }
    1968             : static long
    1969          42 : mfval(GEN F)
    1970             : {
    1971          42 :   pari_sp av = avma;
    1972          42 :   long i = 0, n, sb;
    1973             :   GEN gk, gN;
    1974          42 :   if (!checkmf_i(F)) pari_err_TYPE("mfval", F);
    1975          42 :   gN = mf_get_gN(F);
    1976          42 :   gk = mf_get_gk(F);
    1977          42 :   sb = mfsturmNgk(itou(gN), gk);
    1978         105 :   for (n = 1; n <= sb;)
    1979             :   {
    1980             :     GEN v;
    1981          56 :     if (n > 0.5*sb) n = sb+1;
    1982          56 :     v = mfcoefs_i(F, n, 1);
    1983         112 :     for (; i <= n; i++)
    1984          91 :       if (!gequal0(gel(v, i+1))) { avma = av; return i; }
    1985          21 :     n <<= 1;
    1986             :   }
    1987           7 :   avma = av; return -1;
    1988             : }
    1989             : 
    1990             : GEN
    1991        1722 : mfdiv_val(GEN f, GEN g, long vg)
    1992             : {
    1993             :   GEN N, K, NK, CHI;
    1994        1722 :   if (vg) { f = mfshift(f,vg); g = mfshift(g,vg); }
    1995        1722 :   N = lcmii(mf_get_gN(f), mf_get_gN(g));
    1996        1722 :   K = gsub(mf_get_gk(f), mf_get_gk(g));
    1997        1722 :   CHI = mfchardiv(mf_get_CHI(f), mf_get_CHI(g));
    1998        1722 :   CHI = mfchiadjust(CHI, K, itos(N));
    1999        1722 :   NK = mkgNK(N, K, CHI, mfsamefield(mf_get_field(f), mf_get_field(g)));
    2000        1722 :   return tag2(t_MF_DIV, NK, f, g);
    2001             : }
    2002             : GEN
    2003          42 : mfdiv(GEN F, GEN G)
    2004             : {
    2005          42 :   pari_sp av = avma;
    2006          42 :   long v = mfval(G);
    2007          42 :   if (!checkmf_i(F)) pari_err_TYPE("mfdiv", F);
    2008          35 :   if (v < 0 || (v && !gequal0(mfcoefs(F, v-1, 1))))
    2009          14 :     pari_err_DOMAIN("mfdiv", "ord(G)", ">", strtoGENstr("ord(F)"),
    2010             :                     mkvec2(F, G));
    2011          21 :   return gerepilecopy(av, mfdiv_val(F, G, v));
    2012             : }
    2013             : GEN
    2014          28 : mfderiv(GEN F, long m)
    2015             : {
    2016          28 :   pari_sp av = avma;
    2017             :   GEN NK, gk;
    2018          28 :   if (!checkmf_i(F)) pari_err_TYPE("mfderiv",F);
    2019          28 :   gk = gaddgs(mf_get_gk(F), 2*m);
    2020          28 :   NK = mkgNK(mf_get_gN(F), gk, mf_get_CHI(F), mf_get_field(F));
    2021          28 :   return gerepilecopy(av, tag2(t_MF_DERIV, NK, F, stoi(m)));
    2022             : }
    2023             : GEN
    2024          21 : mfderivE2(GEN F, long m)
    2025             : {
    2026          21 :   pari_sp av = avma;
    2027             :   GEN NK, gk;
    2028          21 :   if (!checkmf_i(F)) pari_err_TYPE("mfderivE2",F);
    2029          21 :   if (m < 0) pari_err_DOMAIN("mfderivE2","m","<",gen_0,stoi(m));
    2030          21 :   gk = gaddgs(mf_get_gk(F), 2*m);
    2031          21 :   NK = mkgNK(mf_get_gN(F), gk, mf_get_CHI(F), mf_get_field(F));
    2032          21 :   return gerepilecopy(av, tag2(t_MF_DERIVE2, NK, F, stoi(m)));
    2033             : }
    2034             : 
    2035             : GEN
    2036          14 : mftwist(GEN F, GEN D)
    2037             : {
    2038          14 :   pari_sp av = avma;
    2039             :   GEN NK, CHI, NT, Da;
    2040             :   long q;
    2041          14 :   if (!checkmf_i(F)) pari_err_TYPE("mftwist", F);
    2042          14 :   if (typ(D) != t_INT) pari_err_TYPE("mftwist", D);
    2043          14 :   Da = mpabs_shallow(D);
    2044          14 :   CHI = mf_get_CHI(F); q = mfcharconductor(CHI);
    2045          14 :   NT = glcm(glcm(mf_get_gN(F), mulsi(q, Da)), sqri(Da));
    2046          14 :   NK = mkgNK(NT, mf_get_gk(F), CHI, mf_get_field(F));
    2047          14 :   return gerepilecopy(av, tag2(t_MF_TWIST, NK, F, D));
    2048             : }
    2049             : 
    2050             : /***************************************************************/
    2051             : /*                 Generic cache handling                      */
    2052             : /***************************************************************/
    2053             : enum { cache_FACT, cache_DIV, cache_H, cache_D, cache_DIH };
    2054             : typedef struct {
    2055             :   const char *name;
    2056             :   GEN cache;
    2057             :   ulong minself;
    2058             :   ulong maxself;
    2059             :   void (*init)(long);
    2060             :   ulong miss;
    2061             :   ulong maxmiss;
    2062             : } cache;
    2063             : 
    2064             : static void constfact(long lim);
    2065             : static void constdiv(long lim);
    2066             : static void consttabh(long lim);
    2067             : static void consttabdihedral(long lim);
    2068             : static void constcoredisc(long lim);
    2069             : static THREAD cache caches[] = {
    2070             : { "Factors",  NULL,  50000,    50000, &constfact, 0, 0 },
    2071             : { "Divisors", NULL,  50000,    50000, &constdiv, 0, 0 },
    2072             : { "H",        NULL, 100000, 10000000, &consttabh, 0, 0 },
    2073             : { "CorediscF",NULL, 100000, 10000000, &constcoredisc, 0, 0 },
    2074             : { "Dihedral", NULL,   1000,     3000, &consttabdihedral, 0, 0 },
    2075             : };
    2076             : 
    2077             : static void
    2078         315 : cache_reset(long id) { caches[id].miss = caches[id].maxmiss = 0; }
    2079             : static void
    2080        6380 : cache_delete(long id) { if (caches[id].cache) gunclone(caches[id].cache); }
    2081             : static void
    2082         322 : cache_set(long id, GEN S)
    2083             : {
    2084         322 :   GEN old = caches[id].cache;
    2085         322 :   caches[id].cache = gclone(S);
    2086         322 :   if (old) gunclone(old);
    2087         322 : }
    2088             : 
    2089             : /* handle a cache miss: store stats, possibly reset table; return value
    2090             :  * if (now) cached; return NULL on failure. HACK: some caches contain an
    2091             :  * ulong where the 0 value is impossible, and return it (typecase to GEN) */
    2092             : static GEN
    2093   140230876 : cache_get(long id, ulong D)
    2094             : {
    2095   140230876 :   cache *S = &caches[id];
    2096             :   /* cache_H is compressed: D=0,1 mod 4 */
    2097   140230876 :   const ulong d = (id == cache_H)? D>>1: D;
    2098             :   ulong max, l;
    2099             : 
    2100   140230876 :   if (!S->cache)
    2101             :   {
    2102         182 :     max = maxuu(minuu(D, S->maxself), S->minself);
    2103         182 :     S->init(max);
    2104         182 :     l = lg(S->cache);
    2105             :   }
    2106             :   else
    2107             :   {
    2108   140230694 :     l = lg(S->cache);
    2109   140230694 :     if (l <= d)
    2110             :     {
    2111         987 :       if (D > S->maxmiss) S->maxmiss = D;
    2112         987 :       if (DEBUGLEVEL >= 3)
    2113           0 :         err_printf("miss in cache %s: %lu, max = %lu\n",
    2114             :                    S->name, D, S->maxmiss);
    2115         987 :       if (S->miss++ >= 5 && D < S->maxself)
    2116             :       {
    2117          84 :         max = minuu(S->maxself, (long)(S->maxmiss * 1.2));
    2118          84 :         if (max <= S->maxself)
    2119             :         {
    2120          84 :           if (DEBUGLEVEL >= 3)
    2121           0 :             err_printf("resetting cache %s to %lu\n", S->name, max);
    2122          84 :           S->init(max); l = lg(S->cache);
    2123             :         }
    2124             :       }
    2125             :     }
    2126             :   }
    2127   140230876 :   return (l <= d)? NULL: gel(S->cache, d);
    2128             : }
    2129             : static GEN
    2130          70 : cache_report(long id)
    2131             : {
    2132          70 :   cache *S = &caches[id];
    2133          70 :   GEN v = zerocol(5);
    2134          70 :   gel(v,1) = strtoGENstr(S->name);
    2135          70 :   if (S->cache)
    2136             :   {
    2137          35 :     gel(v,2) = utoi(lg(S->cache)-1);
    2138          35 :     gel(v,3) = utoi(S->miss);
    2139          35 :     gel(v,4) = utoi(S->maxmiss);
    2140          35 :     gel(v,5) = utoi(gsizebyte(S->cache));
    2141             :   }
    2142          70 :   return v;
    2143             : }
    2144             : GEN
    2145          14 : getcache(void)
    2146             : {
    2147          14 :   pari_sp av = avma;
    2148          14 :   GEN M = cgetg(6, t_MAT);
    2149          14 :   gel(M,1) = cache_report(cache_FACT);
    2150          14 :   gel(M,2) = cache_report(cache_DIV);
    2151          14 :   gel(M,3) = cache_report(cache_H);
    2152          14 :   gel(M,4) = cache_report(cache_D);
    2153          14 :   gel(M,5) = cache_report(cache_DIH);
    2154          14 :   return gerepilecopy(av, shallowtrans(M));
    2155             : }
    2156             : 
    2157             : void
    2158        1595 : pari_close_mf(void)
    2159             : {
    2160        1595 :   cache_delete(cache_DIH);
    2161        1595 :   cache_delete(cache_DIV);
    2162        1595 :   cache_delete(cache_FACT);
    2163        1595 :   cache_delete(cache_H);
    2164        1595 : }
    2165             : 
    2166             : /*************************************************************************/
    2167             : /* a odd, update local cache (recycle memory) */
    2168             : static GEN
    2169        1885 : update_factor_cache(long a, long lim, long *pb)
    2170             : {
    2171        1885 :   const long step = 16000; /* even; don't increase this: RAM cache thrashing */
    2172        1885 :   if (a + 2*step > lim)
    2173         189 :     *pb = lim; /* fuse last 2 chunks */
    2174             :   else
    2175        1696 :     *pb = a + step;
    2176        1885 :   return vecfactoroddu_i(a, *pb);
    2177             : }
    2178             : /* assume lim < MAX_LONG/8 */
    2179             : static void
    2180          63 : constcoredisc(long lim)
    2181             : {
    2182          63 :   pari_sp av2, av = avma;
    2183          63 :   GEN D = caches[cache_D].cache, CACHE = NULL;
    2184          63 :   long cachea, cacheb, N, LIM = !D ? 4 : lg(D)-1;
    2185          63 :   if (lim <= 0) lim = 5;
    2186          63 :   if (lim <= LIM) return;
    2187          63 :   cache_reset(cache_D);
    2188          63 :   D = zero_zv(lim);
    2189          63 :   av2 = avma;
    2190          63 :   cachea = cacheb = 0;
    2191     7553322 :   for (N = 1; N <= lim; N+=2)
    2192             :   { /* N odd */
    2193             :     long i, d, d2;
    2194             :     GEN F;
    2195     7553259 :     if (N > cacheb)
    2196             :     {
    2197         924 :       avma = av2; cachea = N;
    2198         924 :       CACHE = update_factor_cache(N, lim, &cacheb);
    2199             :     }
    2200     7553259 :     F = gel(CACHE, ((N-cachea)>>1)+1); /* factoru(N) */
    2201     7553259 :     D[N] = d = corediscs_fact(F); /* = 3 mod 4 or 4 mod 16 */
    2202     7553259 :     d2 = odd(d)? d<<3: d<<1;
    2203     7553259 :     for (i = 1;;)
    2204             :     {
    2205    12588751 :       if ((N << i) > lim) break;
    2206     5035484 :       D[N<<i] = d2; i++;
    2207     5035484 :       if ((N << i) > lim) break;
    2208     2517746 :       D[N<<i] = d; i++;
    2209             :     }
    2210             :   }
    2211          63 :   cache_set(cache_D, D);
    2212          63 :   avma = av;
    2213             : }
    2214             : 
    2215             : static void
    2216          70 : constfact(long lim)
    2217             : {
    2218             :   pari_sp av;
    2219          70 :   GEN VFACT = caches[cache_FACT].cache;
    2220          70 :   long LIM = VFACT? lg(VFACT)-1: 4;
    2221          70 :   if (lim <= 0) lim = 5;
    2222          70 :   if (lim <= LIM) return;
    2223          63 :   cache_reset(cache_FACT); av = avma;
    2224          63 :   cache_set(cache_FACT, vecfactoru_i(1,lim)); avma = av;
    2225             : }
    2226             : static void
    2227          63 : constdiv(long lim)
    2228             : {
    2229             :   pari_sp av;
    2230          63 :   GEN VFACT, VDIV = caches[cache_DIV].cache;
    2231          63 :   long N, LIM = VDIV? lg(VDIV)-1: 4;
    2232          63 :   if (lim <= 0) lim = 5;
    2233          63 :   if (lim <= LIM) return;
    2234          63 :   constfact(lim);
    2235          63 :   VFACT = caches[cache_FACT].cache;
    2236          63 :   cache_reset(cache_DIV); av = avma;
    2237          63 :   VDIV  = cgetg(lim+1, t_VEC);
    2238          63 :   for (N = 1; N <= lim; N++) gel(VDIV,N) = divisorsu_fact(gel(VFACT,N));
    2239          63 :   cache_set(cache_DIV, VDIV); avma = av;
    2240             : }
    2241             : 
    2242             : /* n > 1, D = divisors(n); sets L = 2*lambda(n), S = sigma(n) */
    2243             : static void
    2244     9517628 : lamsig(GEN D, long *pL, long *pS)
    2245             : {
    2246     9517628 :   pari_sp av = avma;
    2247     9517628 :   long i, l = lg(D), L = 1, S = D[l-1]+1;
    2248    34382155 :   for (i = 2; i < l; i++) /* skip d = 1 */
    2249             :   {
    2250    34382155 :     long d = D[i], nd = D[l-i]; /* nd = n/d */
    2251    34382155 :     if (d < nd) { L += d; S += d + nd; }
    2252             :     else
    2253             :     {
    2254     9517628 :       L <<= 1; if (d == nd) { L += d; S += d; }
    2255     9517628 :       break;
    2256             :     }
    2257             :   }
    2258     9517628 :   avma = av; *pL = L; *pS = S;
    2259     9517628 : }
    2260             : /* table of 6 * Hurwitz class numbers D <= lim */
    2261             : static void
    2262         126 : consttabh(long lim)
    2263             : {
    2264         126 :   pari_sp av = avma, av2;
    2265         126 :   GEN VHDH0, VDIV, CACHE = NULL;
    2266         126 :   GEN VHDH = caches[cache_H].cache;
    2267         126 :   long r, N, cachea, cacheb, lim0 = VHDH? lg(VHDH)-1: 2, LIM = lim0 << 1;
    2268             : 
    2269         126 :   if (lim <= 0) lim = 5;
    2270         126 :   if (lim <= LIM) return;
    2271         126 :   cache_reset(cache_H);
    2272         126 :   r = lim&3L; if (r) lim += 4-r;
    2273         126 :   cache_get(cache_DIV, lim);
    2274         126 :   VDIV = caches[cache_DIV].cache;
    2275         126 :   VHDH0 = cgetg(lim/2 + 1, t_VECSMALL);
    2276         126 :   VHDH0[1] = 2;
    2277         126 :   VHDH0[2] = 3;
    2278         126 :   for (N = 3; N <= lim0; N++) VHDH0[N] = VHDH[N];
    2279         126 :   av2 = avma;
    2280         126 :   cachea = cacheb = 0;
    2281     4758940 :   for (N = LIM + 3; N <= lim; N += 4)
    2282             :   {
    2283     4758814 :     long s = 0, limt = usqrt(N>>2), flsq = 0, ind, t, L, S;
    2284             :     GEN DN, DN2;
    2285     4758814 :     if (N + 2 >= lg(VDIV))
    2286             :     { /* use local cache */
    2287             :       GEN F;
    2288     3971440 :       if (N + 2 > cacheb)
    2289             :       {
    2290         961 :         avma = av2; cachea = N;
    2291         961 :         CACHE = update_factor_cache(N, lim+2, &cacheb);
    2292             :       }
    2293     3971440 :       F = gel(CACHE, ((N-cachea)>>1)+1); /* factoru(N) */
    2294     3971440 :       DN = divisorsu_fact(F);
    2295     3971440 :       F = gel(CACHE, ((N-cachea)>>1)+2); /* factoru(N+2) */
    2296     3971440 :       DN2 = divisorsu_fact(F);
    2297             :     }
    2298             :     else
    2299             :     { /* use global cache */
    2300      787374 :       DN = gel(VDIV,N);
    2301      787374 :       DN2 = gel(VDIV,N+2);
    2302             :     }
    2303     4758814 :     ind = N >> 1;
    2304  1059809401 :     for (t = 1; t <= limt; t++)
    2305             :     {
    2306  1055050587 :       ind -= (t<<2)-2; /* N/2 - 2t^2 */
    2307  1055050587 :       if (ind) s += VHDH0[ind]; else flsq = 1;
    2308             :     }
    2309     4758814 :     lamsig(DN, &L,&S);
    2310     4758814 :     VHDH0[N >> 1] = 2*S - 3*L - 2*s + flsq;
    2311     4758814 :     s = 0; flsq = 0; limt = (usqrt(N+2) - 1) >> 1;
    2312     4758814 :     ind = (N+1) >> 1;
    2313  1057451335 :     for (t = 1; t <= limt; t++)
    2314             :     {
    2315  1052692521 :       ind -= t<<2; /* (N+1)/2 - 2t(t+1) */
    2316  1052692521 :       if (ind) s += VHDH0[ind]; else flsq = 1;
    2317             :     }
    2318     4758814 :     lamsig(DN2, &L,&S);
    2319     4758814 :     VHDH0[(N+1) >> 1] = S - 3*(L >> 1) - s - flsq;
    2320             :   }
    2321         126 :   cache_set(cache_H, VHDH0); avma = av;
    2322             : }
    2323             : 
    2324             : /*************************************************************************/
    2325             : /* Core functions using factorizations, divisors of class numbers caches */
    2326             : /* TODO: myfactoru and factorization cache should be exported */
    2327             : static GEN
    2328    15293453 : myfactoru(long N)
    2329             : {
    2330    15293453 :   GEN z = cache_get(cache_FACT, N);
    2331    15293453 :   return z? gcopy(z): factoru(N);
    2332             : }
    2333             : static GEN
    2334    37545739 : mydivisorsu(long N)
    2335             : {
    2336    37545739 :   GEN z = cache_get(cache_DIV, N);
    2337    37545739 :   return z? leafcopy(z): divisorsu(N);
    2338             : }
    2339             : /* write -n = Df^2, D < 0 fundamental discriminant. Return D, set f. */
    2340             : static long
    2341    47049072 : mycoredisc2neg(ulong n, long *pf)
    2342             : {
    2343    47049072 :   ulong m, D = (ulong)cache_get(cache_D, n);
    2344    47049072 :   if (D) { *pf = usqrt(n/D); return -(long)D; }
    2345         196 :   m = mycore(n, pf);
    2346         196 :   if ((m&3) != 3) { m <<= 2; *pf >>= 1; }
    2347         196 :   return (long)-m;
    2348             : }
    2349             : /* write n = Df^2, D > 0 fundamental discriminant. Return D, set f. */
    2350             : static long
    2351          14 : mycoredisc2pos(ulong n, long *pf)
    2352             : {
    2353          14 :   ulong m = mycore(n, pf);
    2354          14 :   if ((m&3) != 1) { m <<= 2; *pf >>= 1; }
    2355          14 :   return (long)m;
    2356             : }
    2357             : 
    2358             : /* 1+p+...+p^e, e >= 1 */
    2359             : static ulong
    2360          49 : usumpow(ulong p, long e)
    2361             : {
    2362          49 :   ulong q = 1+p;
    2363             :   long i;
    2364          49 :   for (i = 1; i < e; i++) q = p*q + 1;
    2365          49 :   return q;
    2366             : }
    2367             : /* Hurwitz(D0 F^2)/ Hurwitz(D0)
    2368             :  * = \sum_{f|F}  f \prod_{p|f} (1-kro(D0/p)/p)
    2369             :  * = \prod_{p^e || F} (1 + (p^e-1) / (p-1) * (p-kro(D0/p))) */
    2370             : static long
    2371         294 : get_sh(long F, long D0)
    2372             : {
    2373         294 :   GEN fa = myfactoru(F), P = gel(fa,1), E = gel(fa,2);
    2374         294 :   long i, l = lg(P), t = 1;
    2375         794 :   for (i = 1; i < l; i++)
    2376             :   {
    2377         500 :     long p = P[i], e = E[i], s = kross(D0,p);
    2378         500 :     if (e == 1) { t *= 1 + p - s; continue; }
    2379         153 :     if (s == 1) { t *= upowuu(p,e); continue; }
    2380          49 :     t *= 1 + usumpow(p,e-1)*(p-s);
    2381             :   }
    2382         294 :   return t;
    2383             : }
    2384             : /* d > 0, d = 0,3 (mod 4). Return 6*hclassno(d); -d must be fundamental
    2385             :  * Faster than quadclassunit up to 5*10^5 or so */
    2386             : static ulong
    2387          42 : hclassno6u_count(ulong d)
    2388             : {
    2389          42 :   ulong a, b, b2, h = 0;
    2390          42 :   int f = 0;
    2391             : 
    2392          42 :   if (d > 500000)
    2393           7 :     return 6 * itou(gel(quadclassunit0(utoineg(d), 0, NULL, 0), 1));
    2394             : 
    2395             :   /* this part would work with -d non fundamental */
    2396          35 :   b = d&1; b2 = (1+d)>>2;
    2397          35 :   if (!b)
    2398             :   {
    2399           0 :     for (a=1; a*a<b2; a++)
    2400           0 :       if (b2%a == 0) h++;
    2401           0 :     f = (a*a==b2); b=2; b2=(4+d)>>2;
    2402             :   }
    2403        7168 :   while (b2*3 < d)
    2404             :   {
    2405        7098 :     if (b2%b == 0) h++;
    2406     1188551 :     for (a=b+1; a*a < b2; a++)
    2407     1181453 :       if (b2%a == 0) h += 2;
    2408        7098 :     if (a*a == b2) h++;
    2409        7098 :     b += 2; b2 = (b*b+d)>>2;
    2410             :   }
    2411          35 :   if (b2*3 == d) return 6*h+2;
    2412          35 :   if (f) return 6*h+3;
    2413          35 :   return 6*h;
    2414             : }
    2415             : /* D > 0; 6 * hclassno(D), using D = D0*F^2 */
    2416             : static long
    2417         336 : hclassno6u_2(ulong D, long D0, long F)
    2418             : {
    2419             :   long h;
    2420         336 :   if (F == 1) h = hclassno6u_count(D);
    2421             :   else
    2422             :   { /* second chance */
    2423         294 :     h = (ulong)cache_get(cache_H, -D0);
    2424         294 :     if (!h) h = hclassno6u_count(-D0);
    2425         294 :     h *= get_sh(F,D0);
    2426             :   }
    2427         336 :   return h;
    2428             : }
    2429             : /* D > 0; 6 * hclassno(D) (6*Hurwitz). Beware, cached value for D (=0,3 mod 4)
    2430             :  * is stored at D>>1 */
    2431             : ulong
    2432      155745 : hclassno6u(ulong D)
    2433             : {
    2434      155745 :   ulong z = (ulong)cache_get(cache_H, D);
    2435             :   long D0, F;
    2436      155745 :   if (z) return z;
    2437         336 :   D0 = mycoredisc2neg(D, &F);
    2438         336 :   return hclassno6u_2(D,D0,F);
    2439             : }
    2440             : /* same, where the decomposition D = D0*F^2 is already known */
    2441             : static ulong
    2442    35377818 : hclassno6u_i(ulong D, long D0, long F)
    2443             : {
    2444    35377818 :   ulong z = (ulong)cache_get(cache_H, D);
    2445    35377818 :   if (z) return z;
    2446           0 :   return hclassno6u_2(D,D0,F);
    2447             : }
    2448             : 
    2449             : #if 0
    2450             : /* D > 0, return h(-D) [ordinary class number].
    2451             :  * Assume consttabh(D or more) was previously called */
    2452             : static long
    2453             : hfromH(long D)
    2454             : {
    2455             :   pari_sp ltop = avma;
    2456             :   GEN m, d, fa = myfactoru(D), P = gel(fa,1), E = gel(fa,2);
    2457             :   GEN VH = caches[cache_H].cache;
    2458             :   long i, nd, S, l = lg(P);
    2459             : 
    2460             :   /* n = d[i] loops through squarefree divisors of f, where f^2 = largest square
    2461             :    * divisor of N = |D|; m[i] = moebius(n) */
    2462             :   nd = 1 << (l-1);
    2463             :   d = cgetg(nd+1, t_VECSMALL);
    2464             :   m = cgetg(nd+1, t_VECSMALL);
    2465             :   d[1] = 1; S = VH[D >> 1]; /* 6 hclassno(-D) */
    2466             :   m[1] = 1; nd = 1;
    2467             :   i = 1;
    2468             :   if (P[1] == 2 && E[1] <= 3) /* need D/n^2 to be a discriminant */
    2469             :   { if (odd(E[1]) || (E[1] == 2 && (D & 15) == 4)) i = 2; }
    2470             :   for (; i<l; i++)
    2471             :   {
    2472             :     long j, p = P[i];
    2473             :     if (E[i] == 1) continue;
    2474             :     for (j=1; j<=nd; j++)
    2475             :     {
    2476             :       long n, s, hn;
    2477             :       d[nd+j] = n = d[j] * p;
    2478             :       m[nd+j] = s = - m[j]; /* moebius(n) */
    2479             :       hn = VH[(D/(n*n)) >> 1]; /* 6 hclassno(-D/n^2) */
    2480             :       if (s > 0) S += hn; else S -= hn;
    2481             :     }
    2482             :     nd <<= 1;
    2483             :   }
    2484             :   avma = ltop; return S/6;
    2485             : }
    2486             : #endif
    2487             : /* D < -4 fundamental, h(D), ordinary class number */
    2488             : static long
    2489     4785200 : myh(long D)
    2490             : {
    2491     4785200 :   ulong z = (ulong)cache_get(cache_H, -D);
    2492     4785200 :   if (z) return z/6; /* should be hfromH(-D) if D non-fundamental */
    2493           0 :   return itou(quadclassno(stoi(D)));
    2494             : }
    2495             : 
    2496             : /*************************************************************************/
    2497             : /*                          TRACE FORMULAS                               */
    2498             : /* CHIP primitive, initialize for t_POLMOD output */
    2499             : static GEN
    2500       24731 : mfcharinit(GEN CHIP)
    2501             : {
    2502       24731 :   long n, o, l, vt, N = mfcharmodulus(CHIP);
    2503             :   GEN c, v, V, G, Pn;
    2504       24731 :   if (N == 1) return mkvec2(mkvec(gen_1), pol_x(0));
    2505        3997 :   G = gel(CHIP,1);
    2506        3997 :   v = ncharvecexpo(G, znconrey_normalized(G, gel(CHIP,2)));
    2507        3997 :   l = lg(v); V = cgetg(l, t_VEC);
    2508        3997 :   o = mfcharorder(CHIP);
    2509        3997 :   Pn = mfcharpol(CHIP); vt = varn(Pn);
    2510        3997 :   if (o <= 2)
    2511             :   {
    2512       29176 :     for (n = 1; n < l; n++)
    2513             :     {
    2514       26103 :       if (v[n] < 0) c = gen_0; else c = v[n]? gen_m1: gen_1;
    2515       26103 :       gel(V,n) = c;
    2516             :     }
    2517             :   }
    2518             :   else
    2519             :   {
    2520       16639 :     for (n = 1; n < l; n++)
    2521             :     {
    2522       15715 :       if (v[n] < 0) c = gen_0;
    2523             :       else
    2524             :       {
    2525        8750 :         c = mygmodulo_lift(v[n], o, gen_1, vt);
    2526        8750 :         if (typ(c) == t_POL && lg(c) >= lg(Pn)) c = RgX_rem(c, Pn);
    2527             :       }
    2528       15715 :       gel(V,n) = c;
    2529             :     }
    2530             :   }
    2531        3997 :   return mkvec2(V, Pn);
    2532             : }
    2533             : static GEN
    2534      331744 : vchip_lift(GEN VCHI, long x, GEN C)
    2535             : {
    2536      331744 :   GEN V = gel(VCHI,1);
    2537      331744 :   long F = lg(V)-1;
    2538      331744 :   if (F == 1) return C;
    2539       26068 :   x %= F;
    2540       26068 :   if (!x) return C;
    2541       26068 :   if (x <= 0) x += F;
    2542       26068 :   return gmul(C, gel(V, x));
    2543             : }
    2544             : static long
    2545    78505217 : vchip_FC(GEN VCHI) { return lg(gel(VCHI,1))-1; }
    2546             : static GEN
    2547     3213189 : vchip_mod(GEN VCHI, GEN S)
    2548     3213189 : { return (typ(S) == t_POL)? RgX_rem(S, gel(VCHI,2)): S; }
    2549             : static GEN
    2550      959693 : vchip_polmod(GEN VCHI, GEN S)
    2551      959693 : { return (typ(S) == t_POL)? mkpolmod(S, gel(VCHI,2)): S; }
    2552             : 
    2553             : /* ceil(m/d) */
    2554             : static long
    2555      104181 : ceildiv(long m, long d)
    2556             : {
    2557             :   long q;
    2558      104181 :   if (!m) return 0;
    2559       37303 :   q = m/d; return m%d? q+1: q;
    2560             : }
    2561             : 
    2562             : /* contribution of scalar matrices in dimension formula */
    2563             : static GEN
    2564      224483 : A1(long N, long k)
    2565      224483 : { return sstoQ(mypsiu(N)*(k-1), 12); }
    2566             : static long
    2567        7182 : ceilA1(long N, long k)
    2568        7182 : { return ceildiv(mypsiu(N) * (k-1), 12); }
    2569             : 
    2570             : /* sturm bound, slightly larger than dimension */
    2571             : long
    2572       28140 : mfsturmNk(long N, long k) { return 1 + (mypsiu(N)*k)/12; }
    2573             : long
    2574        1232 : mfsturmNgk(long N, GEN k)
    2575             : {
    2576        1232 :   long n,d; Qtoss(k,&n,&d);
    2577        1232 :   return (d == 1)? mfsturmNk(N,n): 1 + (mypsiu(N)*n)/24;
    2578             : }
    2579             : 
    2580             : /* List of all solutions of x^2 + x + 1 = 0 modulo N, x modulo N */
    2581             : static GEN
    2582         497 : sqrtm3modN(long N)
    2583             : {
    2584             :   pari_sp av;
    2585             :   GEN fa, P, E, B, mB, A, Q, T, R, v, gen_m3;
    2586         497 :   long l, i, n, ct, fl3 = 0, Ninit;
    2587         497 :   if (!odd(N) || (N%9) == 0) return cgetg(1,t_VECSMALL);
    2588         469 :   Ninit = N;
    2589         469 :   if ((N%3) == 0) { N /= 3; fl3 = 1; }
    2590         469 :   fa = myfactoru(N); P = gel(fa, 1); E = gel(fa, 2);
    2591         469 :   l = lg(P);
    2592         651 :   for (i = 1; i < l; i++)
    2593         476 :     if ((P[i]%3) == 2) return cgetg(1,t_VECSMALL);
    2594         175 :   A = cgetg(l, t_VECSMALL);
    2595         175 :   B = cgetg(l, t_VECSMALL);
    2596         175 :   mB= cgetg(l, t_VECSMALL);
    2597         175 :   Q = cgetg(l, t_VECSMALL); gen_m3 = utoineg(3);
    2598         357 :   for (i = 1; i < l; i++)
    2599             :   {
    2600         182 :     long p = P[i], e = E[i];
    2601         182 :     Q[i] = upowuu(p,e);
    2602         182 :     B[i] = itou( Zp_sqrt(gen_m3, utoipos(p), e) );
    2603         182 :     mB[i]= Q[i] - B[i];
    2604             :   }
    2605         175 :   ct = 1 << (l-1);
    2606         175 :   T = ZV_producttree(Q);
    2607         175 :   R = ZV_chinesetree(Q,T);
    2608         175 :   v = cgetg(ct+1, t_VECSMALL);
    2609         175 :   av = avma;
    2610         539 :   for (n = 1; n <= ct; n++)
    2611             :   {
    2612         364 :     long m = n-1, r;
    2613         756 :     for (i = 1; i < l; i++)
    2614             :     {
    2615         392 :       A[i] = (m&1L)? mB[i]: B[i];
    2616         392 :       m >>= 1;
    2617             :     }
    2618         364 :     r = itou( ZV_chinese_tree(A, Q, T, R) );
    2619         364 :     if (fl3) while (r%3) r += N;
    2620         364 :     avma = av; v[n] = odd(r) ? (r-1) >> 1 : (r+Ninit-1) >> 1;
    2621             :   }
    2622         175 :   return v;
    2623             : }
    2624             : 
    2625             : /* number of elliptic points of order 3 in X0(N) */
    2626             : static long
    2627        9212 : nu3(long N)
    2628             : {
    2629             :   long i, l;
    2630             :   GEN P;
    2631        9212 :   if (!odd(N) || (N%9) == 0) return 0;
    2632        8218 :   if ((N%3) == 0) N /= 3;
    2633        8218 :   P = gel(myfactoru(N), 1); l = lg(P);
    2634        8218 :   for (i = 1; i < l; i++) if ((P[i]%3) == 2) return 0;
    2635        3598 :   return 1L<<(l-1);
    2636             : }
    2637             : /* number of elliptic points of order 2 in X0(N) */
    2638             : static long
    2639       16037 : nu2(long N)
    2640             : {
    2641             :   long i, l;
    2642             :   GEN P;
    2643       16037 :   if ((N&3L) == 0) return 0;
    2644       16037 :   if (!odd(N)) N >>= 1;
    2645       16037 :   P = gel(myfactoru(N), 1); l = lg(P);
    2646       16037 :   for (i = 1; i < l; i++) if ((P[i]&3L) == 3) return 0;
    2647        3633 :   return 1L<<(l-1);
    2648             : }
    2649             : 
    2650             : /* contribution of elliptic matrices of order 3 in dimension formula
    2651             :  * Only depends on CHIP the primitive char attached to CHI */
    2652             : static GEN
    2653       39480 : A21(long N, long k, GEN CHI)
    2654             : {
    2655             :   GEN res, G, chi, o;
    2656             :   long a21, i, limx, S;
    2657       39480 :   if ((N&1L) == 0) return gen_0;
    2658       19173 :   a21 = k%3 - 1;
    2659       19173 :   if (!a21) return gen_0;
    2660       18529 :   if (N <= 3) return sstoQ(a21, 3);
    2661        9709 :   if (!CHI) return sstoQ(nu3(N) * a21, 3);
    2662         497 :   res = sqrtm3modN(N); limx = (N - 1) >> 1;
    2663         497 :   G = gel(CHI,1); chi = gel(CHI,2);
    2664         497 :   o = gmfcharorder(CHI);
    2665         861 :   for (S = 0, i = 1; i < lg(res); i++)
    2666             :   { /* (x,N) = 1; S += chi(x) + chi(x^2) */
    2667         364 :     long x = res[i];
    2668         364 :     if (x <= limx)
    2669             :     { /* CHI(x)=e(c/o), 3rd-root of 1 */
    2670         182 :       GEN c = znchareval(G, chi, utoi(x), o);
    2671         182 :       if (!signe(c)) S += 2; else S--;
    2672             :     }
    2673             :   }
    2674         497 :   return sstoQ(a21 * S, 3);
    2675             : }
    2676             : 
    2677             : /* List of all square roots of -1 modulo N */
    2678             : static GEN
    2679         567 : sqrtm1modN(long N)
    2680             : {
    2681             :   pari_sp av;
    2682             :   GEN fa, P, E, B, mB, A, Q, T, R, v;
    2683         567 :   long l, i, n, ct, fleven = 0;
    2684         567 :   if ((N&3L) == 0) return cgetg(1,t_VECSMALL);
    2685         567 :   if ((N&1L) == 0) { N >>= 1; fleven = 1; }
    2686         567 :   fa = myfactoru(N); P = gel(fa,1); E = gel(fa,2);
    2687         567 :   l = lg(P);
    2688         917 :   for (i = 1; i < l; i++)
    2689         637 :     if ((P[i]&3L) == 3) return cgetg(1,t_VECSMALL);
    2690         280 :   A = cgetg(l, t_VECSMALL);
    2691         280 :   B = cgetg(l, t_VECSMALL);
    2692         280 :   mB= cgetg(l, t_VECSMALL);
    2693         280 :   Q = cgetg(l, t_VECSMALL);
    2694         574 :   for (i = 1; i < l; i++)
    2695             :   {
    2696         294 :     long p = P[i], e = E[i];
    2697         294 :     Q[i] = upowuu(p,e);
    2698         294 :     B[i] = itou( Zp_sqrt(gen_m1, utoipos(p), e) );
    2699         294 :     mB[i]= Q[i] - B[i];
    2700             :   }
    2701         280 :   ct = 1 << (l-1);
    2702         280 :   T = ZV_producttree(Q);
    2703         280 :   R = ZV_chinesetree(Q,T);
    2704         280 :   v = cgetg(ct+1, t_VECSMALL);
    2705         280 :   av = avma;
    2706         868 :   for (n = 1; n <= ct; n++)
    2707             :   {
    2708         588 :     long m = n-1, r;
    2709        1232 :     for (i = 1; i < l; i++)
    2710             :     {
    2711         644 :       A[i] = (m&1L)? mB[i]: B[i];
    2712         644 :       m >>= 1;
    2713             :     }
    2714         588 :     r = itou( ZV_chinese_tree(A, Q, T, R) );
    2715         588 :     if (fleven && !odd(r)) r += N;
    2716         588 :     avma = av; v[n] = r;
    2717             :   }
    2718         280 :   return v;
    2719             : }
    2720             : 
    2721             : /* contribution of elliptic matrices of order 4 in dimension formula.
    2722             :  * Only depends on CHIP the primitive char attached to CHI */
    2723             : static GEN
    2724       39480 : A22(long N, long k, GEN CHI)
    2725             : {
    2726             :   GEN G, chi, o, res;
    2727             :   long S, a22, i, limx, o2;
    2728       39480 :   if ((N&3L) == 0) return gen_0;
    2729       27531 :   a22 = (k & 3L) - 1; /* (k % 4) - 1 */
    2730       27531 :   if (!a22) return gen_0;
    2731       27531 :   if (N <= 2) return sstoQ(a22, 4);
    2732       16800 :   if (!CHI) return sstoQ(nu2(N)*a22, 4);
    2733         763 :   if (mfcharparity(CHI) == -1) return gen_0;
    2734         567 :   res = sqrtm1modN(N); limx = (N - 1) >> 1;
    2735         567 :   G = gel(CHI,1); chi = gel(CHI,2);
    2736         567 :   o = gmfcharorder(CHI);
    2737         567 :   o2 = itou(o)>>1;
    2738        1155 :   for (S = 0, i = 1; i < lg(res); i++)
    2739             :   { /* (x,N) = 1, S += real(chi(x)) */
    2740         588 :     long x = res[i];
    2741         588 :     if (x <= limx)
    2742             :     { /* CHI(x)=e(c/o), 4th-root of 1 */
    2743         294 :       long c = itou( znchareval(G, chi, utoi(x), o) );
    2744         294 :       if (!c) S++; else if (c == o2) S--;
    2745             :     }
    2746             :   }
    2747         567 :   return sstoQ(a22 * S, 2);
    2748             : }
    2749             : 
    2750             : /* sumdiv(N,d,eulerphi(gcd(d,N/d))) */
    2751             : static long
    2752       35546 : nuinf(long N)
    2753             : {
    2754       35546 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
    2755       35546 :   long i, t = 1, l = lg(P);
    2756       75677 :   for (i=1; i<l; i++)
    2757             :   {
    2758       40131 :     long p = P[i], e = E[i];
    2759       40131 :     if (odd(e))
    2760       32193 :       t *= upowuu(p,e>>1) << 1;
    2761             :     else
    2762        7938 :       t *= upowuu(p,(e>>1)-1) * (p+1);
    2763             :   }
    2764       35546 :   return t;
    2765             : }
    2766             : 
    2767             : /* contribution of hyperbolic matrices in dimension formula */
    2768             : static GEN
    2769       39879 : A3(long N, long FC)
    2770             : {
    2771             :   long i, S, NF, l;
    2772             :   GEN D;
    2773       39879 :   if (FC == 1) return sstoQ(nuinf(N),2);
    2774        4333 :   D = mydivisorsu(N); l = lg(D);
    2775        4333 :   S = 0; NF = N/FC;
    2776       33152 :   for (i = 1; i < l; i++)
    2777             :   {
    2778       28819 :     long g = ugcd(D[i], D[l-i]);
    2779       28819 :     if (NF%g == 0) S += myeulerphiu(g);
    2780             :   }
    2781        4333 :   return sstoQ(S, 2);
    2782             : }
    2783             : 
    2784             : /* special contribution in weight 2 in dimension formula */
    2785             : static long
    2786       39158 : A4(long k, long FC)
    2787       39158 : { return (k==2 && FC==1)? 1: 0; }
    2788             : /* gcd(x,N) */
    2789             : static long
    2790    92976226 : myugcd(GEN GCD, ulong x)
    2791             : {
    2792    92976226 :   ulong N = lg(GCD)-1;
    2793    92976226 :   if (x >= N) x %= N;
    2794    92976226 :   return GCD[x+1];
    2795             : }
    2796             : /* 1_{gcd(x,N) = 1} * chi(x), return NULL if 0 */
    2797             : static GEN
    2798   125673443 : mychicgcd(GEN GCD, GEN VCHI, long x)
    2799             : {
    2800   125673443 :   long N = lg(GCD)-1;
    2801   125673443 :   if (N == 1) return gen_1;
    2802   108972073 :   x = smodss(x, N);
    2803   108972073 :   if (GCD[x+1] != 1) return NULL;
    2804    75152378 :   x %= vchip_FC(VCHI); if (!x) return gen_1;
    2805     6634362 :   return gel(gel(VCHI,1), x);
    2806             : }
    2807             : 
    2808             : /* contribution of scalar matrices to trace formula */
    2809             : static GEN
    2810     3046470 : TA1(long N, long k, GEN VCHI, GEN GCD, long n)
    2811             : {
    2812             :   GEN S;
    2813             :   ulong m;
    2814     3046470 :   if (!uissquareall(n, &m)) return gen_0;
    2815      240723 :   if (m == 1) return A1(N,k); /* common */
    2816      211064 :   S = mychicgcd(GCD, VCHI, m);
    2817      211064 :   return S? gmul(gmul(powuu(m, k-2), A1(N,k)), S): gen_0;
    2818             : }
    2819             : 
    2820             : /* All square roots modulo 4N, x modulo 2N, precomputed to accelerate TA2 */
    2821             : static GEN
    2822       98343 : mksqr(long N)
    2823             : {
    2824       98343 :   pari_sp av = avma;
    2825       98343 :   long x, N2 = N << 1, N4 = N << 2;
    2826       98343 :   GEN v = const_vec(N2, cgetg(1, t_VECSMALL));
    2827       98343 :   gel(v, N2) = mkvecsmall(0); /* x = 0 */
    2828     2264794 :   for (x = 1; x <= N; x++)
    2829             :   {
    2830     2166451 :     long r = (((x*x - 1)%N4) >> 1) + 1;
    2831     2166451 :     gel(v,r) = vecsmall_append(gel(v,r), x);
    2832             :   }
    2833       98343 :   return gerepilecopy(av, v);
    2834             : }
    2835             : 
    2836             : static GEN
    2837       98343 : mkgcd(long N)
    2838             : {
    2839             :   GEN GCD, d;
    2840             :   long i, N2;
    2841       98343 :   if (N == 1) return mkvecsmall(N);
    2842       81438 :   GCD = cgetg(N + 1, t_VECSMALL);
    2843       81438 :   d = GCD+1; /* GCD[i+1] = d[i] = gcd(i,N) = gcd(N-i,N), i = 0..N-1 */
    2844       81438 :   d[0] = N; d[1] = d[N-1] = 1; N2 = N>>1;
    2845       81438 :   for (i = 2; i <= N2; i++) d[i] = d[N-i] = ugcd(N, i);
    2846       81438 :   return GCD;
    2847             : }
    2848             : 
    2849             : /* Table of \sum_{x^2-tx+n=0 mod Ng}chi(x) for all g dividing gcd(N,F),
    2850             :  * F^2 largest such that (t^2-4n)/F^2=0 or 1 mod 4; t >= 0 */
    2851             : static GEN
    2852     9645874 : mutglistall(long t, long N, long NF, GEN VCHI, long n, GEN MUP, GEN li, GEN GCD)
    2853             : {
    2854     9645874 :   long i, lx = lg(li);
    2855     9645874 :   GEN DNF = mydivisorsu(NF), v = zerovec(NF);
    2856     9645874 :   long j, g, lDNF = lg(DNF);
    2857    26452447 :   for (i = 1; i < lx; i++)
    2858             :   {
    2859    16806573 :     long x = (li[i] + t) >> 1, y, lD;
    2860    16806573 :     GEN D, c = mychicgcd(GCD, VCHI, x);
    2861    16806573 :     if (li[i] && li[i] != N)
    2862             :     {
    2863    11103582 :       GEN c2 = mychicgcd(GCD, VCHI, t - x);
    2864    11103582 :       if (c2) c = c? gadd(c, c2): c2;
    2865             :     }
    2866    16806573 :     if (!c) continue;
    2867     9812922 :     y = (x*(x - t) + n) / N; /* exact division */
    2868     9812922 :     D = mydivisorsu(ugcd(labs(y), NF)); lD = lg(D);
    2869     9812922 :     for (j=1; j < lD; j++) { g = D[j]; gel(v,g) = gadd(gel(v,g), c); }
    2870             :   }
    2871             :   /* j = 1 corresponds to g = 1, and MUP[1] = 1 */
    2872     9645874 :   for (j=2; j < lDNF; j++) { g = DNF[j]; gel(v,g) = gmulsg(MUP[g], gel(v,g)); }
    2873     9645874 :   return v;
    2874             : }
    2875             : 
    2876             : /* special case (N,F) = 1: easier */
    2877             : static GEN
    2878    37402848 : mutg1(long t, long N, GEN VCHI, GEN li, GEN GCD)
    2879             : { /* (N,F) = 1 */
    2880    37402848 :   GEN S = NULL;
    2881    37402848 :   long i, lx = lg(li);
    2882    79467563 :   for (i = 1; i < lx; i++)
    2883             :   {
    2884    42064715 :     long x = (li[i] + t) >> 1;
    2885    42064715 :     GEN c = mychicgcd(GCD, VCHI, x);
    2886    42064715 :     if (c) S = S? gadd(S, c): c;
    2887    42064715 :     if (li[i] && li[i] != N)
    2888             :     {
    2889    24669421 :       c = mychicgcd(GCD, VCHI, t - x);
    2890    24669421 :       if (c) S = S? gadd(S, c): c;
    2891             :     }
    2892    42064715 :     if (S && !signe(S)) S = NULL; /* strive hard to add gen_0 */
    2893             :   }
    2894    37402848 :   return S; /* single value */
    2895             : }
    2896             : 
    2897             : /* Gegenbauer pol; n > 2, P = \sum_{0<=j<=n/2} (-1)^j (n-j)!/j!(n-2*j)! X^j */
    2898             : static GEN
    2899      304234 : mfrhopol(long n)
    2900             : {
    2901             : #ifdef LONG_IS_64BIT
    2902      260772 :   const long M = 2642249;
    2903             : #else
    2904       43462 :   const long M = 1629;
    2905             : #endif
    2906      304234 :   long j, d = n >> 1; /* >= 1 */
    2907      304234 :   GEN P = cgetg(d + 3, t_POL);
    2908             : 
    2909      304234 :   if (n > M) pari_err_IMPL("mfrhopol for large weight"); /* avoid overflow */
    2910      304234 :   P[1] = evalvarn(0)|evalsigne(1);
    2911      304234 :   gel(P,2) = gen_1;
    2912      304234 :   gel(P,3) = utoineg(n-1); /* j = 1 */
    2913      304234 :   if (d > 1) gel(P,4) = utoipos(((n-3)*(n-2)) >> 1); /* j = 2 */
    2914      304234 :   if (d > 2) gel(P,5) = utoineg(((n-5)*(n-4)*(n-3)) / 6); /* j = 3 */
    2915      309792 :   for (j = 4; j <= d; j++)
    2916        5558 :     gel(P,j+2) = divis(mulis(gel(P,j+1), (n-2*j+1)*(n-2*j+2)), (n-j+1)*(-j));
    2917      304234 :   return P;
    2918             : }
    2919             : 
    2920             : /* polrecip(Q)(t2), assume Q(0) = 1 */
    2921             : static GEN
    2922     1911567 : ZXrecip_u_eval(GEN Q, ulong t2)
    2923             : {
    2924     1911567 :   GEN T = addiu(gel(Q,3), t2);
    2925     1911567 :   long l = lg(Q), j;
    2926     1911567 :   for (j = 4; j < l; j++) T = addii(gel(Q,j), mului(t2, T));
    2927     1911567 :   return T;
    2928             : }
    2929             : /* return sh * sqrt(n)^nu * G_nu(t/(2*sqrt(n))) for t != 0
    2930             :  * else (sh/2) * sqrt(n)^nu * G_nu(0) [ implies nu is even ]
    2931             :  * G_nu(z) = \sum_{0<=j<=nu/2} (-1)^j (nu-j)!/j!(nu-2*j)! * (2z)^(nu-2*j)) */
    2932             : static GEN
    2933    40677175 : mfrhopowsimp(GEN Q, GEN sh, long nu, long t, long t2, long n)
    2934             : {
    2935             :   GEN T;
    2936    40677175 :   switch (nu)
    2937             :   {
    2938    33895526 :     case 0: return t? sh: gmul2n(sh,-1);
    2939     3462326 :     case 1: return gmulsg(t, sh);
    2940     1394197 :     case 2: return t? gmulsg(t2 - n, sh): gmul(gmul2n(stoi(-n), -1), sh);
    2941         469 :     case 3: return gmul(mulss(t, t2 - 2*n), sh);
    2942             :     default:
    2943     1924657 :       if (!t) return gmul(gmul2n(gel(Q, lg(Q) - 1), -1), sh);
    2944     1911567 :       T = ZXrecip_u_eval(Q, t2); if (odd(nu)) T = mulsi(t, T);
    2945     1911567 :       return gmul(T, sh);
    2946             :   }
    2947             : }
    2948             : 
    2949             : /* contribution of elliptic matrices to trace formula */
    2950             : static GEN
    2951     3046470 : TA2(long N, long k, GEN VCHI, long n, GEN SQRTS, GEN MUP, GEN GCD)
    2952             : {
    2953     3046470 :   const long n4 = n << 2, N4 = N << 2, nu = k - 2;
    2954     3046470 :   const long st = (!odd(N) && odd(n)) ? 2 : 1;
    2955             :   long limt, t;
    2956             :   GEN S, Q;
    2957             : 
    2958     3046470 :   limt = usqrt(n4);
    2959     3046470 :   if (limt*limt == n4) limt--;
    2960     3046470 :   Q = nu > 3 ? ZX_z_unscale(mfrhopol(nu), n) : NULL;
    2961     3046470 :   S = gen_0;
    2962    84376838 :   for (t = odd(k)? st: 0; t <= limt; t += st) /* t^2 < 4n */
    2963             :   {
    2964    81330368 :     pari_sp av = avma;
    2965    81330368 :     long t2 = t*t, D = n4 - t2, F, D0, NF;
    2966             :     GEN sh, li;
    2967             : 
    2968    81330368 :     li = gel(SQRTS, (smodss(-D - 1, N4) >> 1) + 1);
    2969   121983561 :     if (lg(li) == 1) continue;
    2970    47048722 :     D0 = mycoredisc2neg(D, &F);
    2971    47048722 :     NF = myugcd(GCD, F);
    2972    47048722 :     if (NF == 1)
    2973             :     { /* (N,F) = 1 => single value in mutglistall */
    2974    37402848 :       GEN mut = mutg1(t, N, VCHI, li, GCD);
    2975    37402848 :       if (!mut) { avma = av; continue; }
    2976    35377818 :       sh = gmul(sstoQ(hclassno6u_i(D,D0,F),6), mut);
    2977             :     }
    2978             :     else
    2979             :     {
    2980     9645874 :       GEN v = mutglistall(t, N, NF, VCHI, n, MUP, li, GCD);
    2981     9645874 :       GEN DF = mydivisorsu(F);
    2982     9645874 :       long i, lDF = lg(DF);
    2983     9645874 :       sh = gen_0;
    2984    37298198 :       for (i = 1; i < lDF; i++)
    2985             :       {
    2986    27652324 :         long Ff, f = DF[i], g = myugcd(GCD, f);
    2987    27652324 :         GEN mut = gel(v, g);
    2988    27652324 :         if (gequal0(mut)) continue;
    2989    12791016 :         Ff = DF[lDF-i]; /* F/f */
    2990    12791016 :         if (Ff == 1) sh = gadd(sh, mut);
    2991             :         else
    2992             :         {
    2993     9174599 :           GEN P = gel(myfactoru(Ff), 1);
    2994     9174599 :           long j, lP = lg(P);
    2995     9174599 :           for (j = 1; j < lP; j++) { long p = P[j]; Ff -= kross(D0, p)*Ff/p; }
    2996     9174599 :           sh = gadd(sh, gmulsg(Ff, mut));
    2997             :         }
    2998             :       }
    2999     9645874 :       if (gequal0(sh)) { avma = av; continue; }
    3000     5299357 :       if (D0 == -3) sh = gdivgs(sh, 3);
    3001     5042954 :       else if (D0 == -4) sh = gdivgs(sh, 2);
    3002     4785200 :       else sh = gmulgs(sh, myh(D0));
    3003             :     }
    3004    40677175 :     S = gerepileupto(av, gadd(S, mfrhopowsimp(Q,sh,nu,t,t2,n)));
    3005             :   }
    3006     3046470 :   return S;
    3007             : }
    3008             : 
    3009             : /* compute global auxiliary data for TA3 */
    3010             : static GEN
    3011       98343 : mkbez(long N, long FC)
    3012             : {
    3013       98343 :   long ct, i, NF = N/FC;
    3014       98343 :   GEN w, D = mydivisorsu(N);
    3015       98343 :   long l = lg(D);
    3016             : 
    3017       98343 :   w = cgetg(l, t_VEC);
    3018      283962 :   for (i = ct = 1; i < l; i++)
    3019             :   {
    3020      267057 :     long u, v, h, c = D[i], Nc = D[l-i];
    3021      267057 :     if (c > Nc) break;
    3022      185619 :     h = cbezout(c, Nc, &u, &v);
    3023      185619 :     if (h == 1) /* shortcut */
    3024      135758 :       gel(w, ct++) = mkvecsmall4(1,u*c,1,i);
    3025       49861 :     else if (!(NF%h))
    3026       44205 :       gel(w, ct++) = mkvecsmall4(h,u*(c/h),myeulerphiu(h),i);
    3027             :   }
    3028       98343 :   setlg(w,ct); stackdummy((pari_sp)(w+ct),(pari_sp)(w+l));
    3029       98343 :   return w;
    3030             : }
    3031             : 
    3032             : /* contribution of hyperbolic matrices to trace formula, d * nd = n,
    3033             :  * DN = divisorsu(N) */
    3034             : static GEN
    3035    11983671 : auxsum(GEN VCHI, GEN GCD, long d, long nd, GEN DN, GEN BEZ)
    3036             : {
    3037    11983671 :   GEN S = gen_0;
    3038    11983671 :   long ct, g = nd - d, lDN = lg(DN), lBEZ = lg(BEZ);
    3039    33783813 :   for (ct = 1; ct < lBEZ; ct++)
    3040             :   {
    3041    21800142 :     GEN y, B = gel(BEZ, ct);
    3042    21800142 :     long ic, c, Nc, uch, h = B[1];
    3043    21800142 :     if (g%h) continue;
    3044    21331856 :     uch = B[2];
    3045    21331856 :     ic  = B[4];
    3046    21331856 :     c = DN[ic];
    3047    21331856 :     Nc= DN[lDN - ic]; /* Nc = N/c */
    3048    21331856 :     if (ugcd(Nc, nd) == 1)
    3049    15801149 :       y = mychicgcd(GCD, VCHI, d + uch*g); /* 0 if (c,d) > 1 */
    3050             :     else
    3051     5530707 :       y = NULL;
    3052    21331856 :     if (c != Nc && ugcd(Nc, d) == 1)
    3053             :     {
    3054    15016939 :       GEN y2 = mychicgcd(GCD, VCHI, nd - uch*g); /* 0 if (c,nd) > 1 */
    3055    15016939 :       if (y2) y = y? gadd(y, y2): y2;
    3056             :     }
    3057    21331856 :     if (y) S = gadd(S, gmulsg(B[3], y));
    3058             :   }
    3059    11983671 :   return S;
    3060             : }
    3061             : 
    3062             : static GEN
    3063     3046470 : TA3(long N, long k, GEN VCHI, GEN GCD, GEN Dn, GEN BEZ)
    3064             : {
    3065     3046470 :   GEN S = gen_0, DN = mydivisorsu(N);
    3066     3046470 :   long i, l = lg(Dn);
    3067    15030141 :   for (i = 1; i < l; i++)
    3068             :   {
    3069    15000482 :     long d = Dn[i], nd = Dn[l-i]; /* = n/d */
    3070             :     GEN t, u;
    3071    15000482 :     if (d > nd) break;
    3072    11983671 :     t = auxsum(VCHI, GCD, d, nd, DN, BEZ);
    3073    11983671 :     if (isintzero(t)) continue;
    3074    10893967 :     u = powuu(d,k-1); if (d == nd) u = gmul2n(u,-1);
    3075    10893967 :     S = gadd(S, gmul(u,t));
    3076             :   }
    3077     3046470 :   return S;
    3078             : }
    3079             : 
    3080             : /* special contribution in weight 2 in trace formula */
    3081             : static long
    3082     3046470 : TA4(long k, GEN VCHIP, GEN Dn, GEN GCD)
    3083             : {
    3084             :   long i, l, S;
    3085     3046470 :   if (k != 2 || vchip_FC(VCHIP) != 1) return 0;
    3086     2407020 :   l = lg(Dn); S = 0;
    3087    20682200 :   for (i = 1; i < l; i++)
    3088             :   {
    3089    18275180 :     long d = Dn[i]; /* gcd(N,n/d) == 1? */
    3090    18275180 :     if (myugcd(GCD, Dn[l-i]) == 1) S += d;
    3091             :   }
    3092     2407020 :   return S;
    3093             : }
    3094             : 
    3095             : /* precomputation of products occurring im mutg, again to accelerate TA2 */
    3096             : static GEN
    3097       98343 : mkmup(long N)
    3098             : {
    3099       98343 :   GEN fa = myfactoru(N), P = gel(fa,1), D = divisorsu_fact(fa);
    3100       98343 :   long i, lP = lg(P), lD = lg(D);
    3101       98343 :   GEN MUP = zero_zv(N);
    3102       98343 :   MUP[1] = 1;
    3103      342650 :   for (i = 2; i < lD; i++)
    3104             :   {
    3105      244307 :     long j, g = D[i], Ng = D[lD-i]; /*  N/g */
    3106      244307 :     for (j = 1; j < lP; j++) { long p = P[j]; if (Ng%p) g += g/p; }
    3107      244307 :     MUP[D[i]] = g;
    3108             :   }
    3109       98343 :   return MUP;
    3110             : }
    3111             : 
    3112             : /* quadratic non-residues mod p; p odd prime, p^2 fits in a long */
    3113             : static GEN
    3114        1400 : non_residues(long p)
    3115             : {
    3116        1400 :   long i, j, p2 = p >> 1;
    3117        1400 :   GEN v = cgetg(p2+1, t_VECSMALL), w = const_vecsmall(p-1, 1);
    3118        1400 :   for (i = 2; i <= p2; i++) w[(i*i) % p] = 0; /* no need to check 1 */
    3119        1400 :   for (i = 2, j = 1; i < p; i++) if (w[i]) v[j++] = i;
    3120        1400 :   return v;
    3121             : }
    3122             : 
    3123             : /* CHIP primitive. Return t_VECSMALL v of length q such that
    3124             :  * Tr^new_{N,CHIP}(n) = 0 whenever v[(n%q) + 1] is non-zero */
    3125             : static GEN
    3126       24759 : mfnewzerodata(long N, GEN CHIP)
    3127             : {
    3128       24759 :   GEN V, M, L, faN = myfactoru(N), PN = gel(faN,1), EN = gel(faN,2);
    3129       24759 :   GEN G = gel(CHIP,1), chi = gel(CHIP,2);
    3130       24759 :   GEN fa = znstar_get_faN(G), P = ZV_to_zv(gel(fa,1)), E = gel(fa,2);
    3131       24759 :   long i, mod, j = 1, l = lg(PN);
    3132             : 
    3133       24759 :   M = cgetg(l, t_VECSMALL); M[1] = 0;
    3134       24759 :   V = cgetg(l, t_VEC);
    3135             :   /* Tr^new(n) = 0 if (n mod M[i]) in V[i]  */
    3136       24759 :   if ((N & 3) == 0)
    3137             :   {
    3138        9513 :     long e = EN[1];
    3139        9513 :     long c = (lg(P) > 1 && P[1] == 2)? E[1]: 0; /* c = v_2(FC) */
    3140             :     /* e >= 2 */
    3141        9513 :     if (c == e-1) return NULL; /* Tr^new = 0 */
    3142        9478 :     if (c == e)
    3143             :     {
    3144        2422 :       if (e == 2)
    3145             :       { /* sc: -4 */
    3146        1652 :         gel(V,1) = mkvecsmall(3);
    3147        1652 :         M[1] = 4;
    3148             :       }
    3149         770 :       else if (e == 3)
    3150             :       { /* sc: -8 (CHI_2(-1)=-1<=>chi[1]=1) and 8 (CHI_2(-1)=1 <=> chi[1]=0) */
    3151         770 :         long t = signe(gel(chi,1))? 7: 3;
    3152         770 :         gel(V,1) = mkvecsmall2(5, t);
    3153         770 :         M[1] = 8;
    3154             :       }
    3155             :     }
    3156        7056 :     else if (e == 5 && c == 3)
    3157         154 :     { /* sc: -8 (CHI_2(-1)=-1<=>chi[1]=1) and 8 (CHI_2(-1)=1 <=> chi[1]=0) */
    3158         154 :       long t = signe(gel(chi,1))? 7: 3;
    3159         154 :       gel(V,1) = mkvecsmalln(6, 2L,4L,5L,6L,8L,t);
    3160         154 :       M[1] = 8;
    3161             :     }
    3162        6902 :     else if ((e == 4 && c == 2) || (e == 5 && c <= 2) || (e == 6 && c <= 2)
    3163        5705 :          || (e >= 7 && c == e - 3))
    3164             :     { /* sc: 4 */
    3165        1197 :       gel(V,1) = mkvecsmall3(0,2,3);
    3166        1197 :       M[1] = 4;
    3167             :     }
    3168        5705 :     else if ((e <= 4 && c == 0) || (e >= 5 && c == e - 2))
    3169             :     { /* sc: 2 */
    3170        5439 :       gel(V,1) = mkvecsmall(0);
    3171        5439 :       M[1] = 2;
    3172             :     }
    3173         266 :     else if ((e == 6 && c == 3) || (e >= 7 && c <= e - 4))
    3174             :     { /* sc: -2 */
    3175         266 :       gel(V,1) = mkvecsmalln(7, 0L,2L,3L,4L,5L,6L,7L);
    3176         266 :       M[1] = 8;
    3177             :     }
    3178             :   }
    3179       24724 :   j = M[1]? 2: 1;
    3180       53165 :   for (i = odd(N)? 1: 2; i < l; i++) /* skip p=2, done above */
    3181             :   {
    3182       28441 :     long p = PN[i], e = EN[i];
    3183       28441 :     long z = zv_search(P, p), c = z? E[z]: 0; /* c = v_p(FC) */
    3184       28441 :     if ((e <= 2 && c == 1 && itos(gel(chi,z)) == (p>>1)) /* ord(CHI_p)=2 */
    3185       27503 :         || (e >= 3 && c <= e - 2))
    3186        1400 :     { /* sc: -p */
    3187        1400 :       GEN v = non_residues(p);
    3188        1400 :       if (e != 1) v = vecsmall_prepend(v, 0);
    3189        1400 :       gel(V,j) = v;
    3190        1400 :       M[j] = p; j++;
    3191             :     }
    3192       27041 :     else if (e >= 2 && c < e)
    3193             :     { /* sc: p */
    3194        1827 :       gel(V,j) = mkvecsmall(0);
    3195        1827 :       M[j] = p; j++;
    3196             :     }
    3197             :   }
    3198       24724 :   if (j == 1) return cgetg(1, t_VECSMALL);
    3199       11179 :   setlg(V,j); setlg(M,j); mod = zv_prod(M);
    3200       11179 :   L = zero_zv(mod);
    3201       23884 :   for (i = 1; i < j; i++)
    3202             :   {
    3203       12705 :     GEN v = gel(V,i);
    3204       12705 :     long s, m = M[i], lv = lg(v);
    3205       33068 :     for (s = 1; s < lv; s++)
    3206             :     {
    3207       20363 :       long a = v[s] + 1;
    3208       30149 :       do { L[a] = 1; a += m; } while (a <= mod);
    3209             :     }
    3210             :   }
    3211       11179 :   return L;
    3212             : }
    3213             : /* v=mfnewzerodata(N,CHI); returns TRUE if newtrace(n) must be zero,
    3214             :  * (but newtrace(n) may still be zero if we return FALSE) */
    3215             : static long
    3216     1272439 : mfnewchkzero(GEN v, long n) { long q = lg(v)-1; return q && v[(n%q) + 1]; }
    3217             : 
    3218             : /* if (!VCHIP): from mftraceform_cusp;
    3219             :  * else from initnewtrace and CHI is known to be primitive */
    3220             : static GEN
    3221       98343 : inittrace(long N, GEN CHI, GEN VCHIP)
    3222             : {
    3223             :   long FC;
    3224       98343 :   if (VCHIP)
    3225       98336 :     FC = mfcharmodulus(CHI);
    3226             :   else
    3227           7 :     VCHIP = mfcharinit(mfchartoprimitive(CHI, &FC));
    3228       98343 :   return mkvecn(5, mksqr(N), mkmup(N), mkgcd(N), VCHIP, mkbez(N, FC));
    3229             : }
    3230             : 
    3231             : /* p > 2 prime; return a sorted t_VECSMALL of primes s.t Tr^new(p) = 0 for all
    3232             :  * weights > 2 */
    3233             : static GEN
    3234       24724 : inittrconj(long N, long FC)
    3235             : {
    3236             :   GEN fa, P, E, v;
    3237             :   long i, k, l;
    3238             : 
    3239       24724 :   if (FC != 1) return cgetg(1,t_VECSMALL);
    3240             : 
    3241       20727 :   fa = myfactoru(N >> vals(N));
    3242       20727 :   P = gel(fa,1); l = lg(P);
    3243       20727 :   E = gel(fa,2);
    3244       20727 :   v = cgetg(l, t_VECSMALL);
    3245       45507 :   for (i = k = 1; i < l; i++)
    3246             :   {
    3247       24780 :     long j, p = P[i]; /* > 2 */
    3248       60242 :     for (j = 1; j < l; j++)
    3249       35462 :       if (j != i && E[j] == 1 && kross(-p, P[j]) == 1) v[k++] = p;
    3250             :   }
    3251       20727 :   setlg(v,k); return v;
    3252             : }
    3253             : 
    3254             : /* assume CHIP primitive, f(CHIP) | N; NZ = mfnewzerodata(N,CHIP) */
    3255             : static GEN
    3256       24724 : initnewtrace_i(long N, GEN CHIP, GEN NZ)
    3257             : {
    3258       24724 :   GEN T = const_vec(N, cgetg(1,t_VEC)), D, VCHIP;
    3259       24724 :   long FC = mfcharmodulus(CHIP), N1, N2, i, l;
    3260             : 
    3261       24724 :   if (!NZ) NZ = mkvecsmall(1); /*Tr^new = 0; initialize data nevertheless*/
    3262       24724 :   VCHIP = mfcharinit(CHIP);
    3263       24724 :   N1 = N/FC; newd_params(N1, &N2);
    3264       24724 :   D = mydivisorsu(N1/N2); l = lg(D);
    3265       24724 :   N2 *= FC;
    3266      123060 :   for (i = 1; i < l; i++)
    3267             :   {
    3268       98336 :     long M = D[i]*N2;
    3269       98336 :     gel(T,M) = inittrace(M, CHIP, VCHIP);
    3270             :   }
    3271       24724 :   gel(T,N) = shallowconcat(gel(T,N), mkvec2(NZ, inittrconj(N,FC)));
    3272       24724 :   return T;
    3273             : }
    3274             : /* don't initialize if Tr^new = 0, return NULL */
    3275             : static GEN
    3276       24759 : initnewtrace(long N, GEN CHI)
    3277             : {
    3278       24759 :   GEN CHIP = mfchartoprimitive(CHI, NULL), NZ = mfnewzerodata(N,CHIP);
    3279       24759 :   return NZ? initnewtrace_i(N, CHIP, NZ): NULL;
    3280             : }
    3281             : 
    3282             : /* (-1)^k */
    3283             : static long
    3284        6846 : m1pk(long k) { return odd(k)? -1 : 1; }
    3285             : static long
    3286        6615 : badchar(long N, long k, GEN CHI)
    3287        6615 : { return mfcharparity(CHI) != m1pk(k) || (CHI && N % mfcharconductor(CHI)); }
    3288             : 
    3289             : /* dimension of space of cusp forms S_k(\G_0(N),CHI)
    3290             :  * Only depends on CHIP the primitive char attached to CHI */
    3291             : long
    3292       39109 : mfcuspdim(long N, long k, GEN CHI)
    3293             : {
    3294       39109 :   pari_sp av = avma;
    3295             :   long FC;
    3296             :   GEN s;
    3297       39109 :   if (k <= 0) return 0;
    3298       39109 :   if (k == 1) return mfwt1cuspdim(N, CHI);
    3299       38976 :   FC = CHI? mfcharconductor(CHI): 1;
    3300       38976 :   if (FC == 1) CHI = NULL;
    3301       38976 :   s = gsub(A1(N, k), gadd(A21(N, k, CHI), A22(N, k, CHI)));
    3302       38976 :   s = gadd(s, gsubsg(A4(k, FC), A3(N, FC)));
    3303       38976 :   avma = av; return itos(s);
    3304             : }
    3305             : 
    3306             : /* dimension of whole space M_k(\G_0(N),CHI)
    3307             :  * Only depends on CHIP the primitive char attached to CHI; assumes !badchar */
    3308             : long
    3309         672 : mffulldim(long N, long k, GEN CHI)
    3310             : {
    3311         672 :   pari_sp av = avma;
    3312         672 :   long FC = CHI? mfcharconductor(CHI): 1;
    3313             :   GEN s;
    3314         672 :   if (k <= 0) return (k == 0 && FC == 1)? 1: 0;
    3315         672 :   if (k == 1)
    3316             :   {
    3317         168 :     long dim = itos(A3(N, FC));
    3318         168 :     avma = av; return dim + mfwt1cuspdim(N, CHI);
    3319             :   }
    3320         504 :   if (FC == 1) CHI = NULL;
    3321         504 :   s = gsub(A1(N, k), gadd(A21(N, k, CHI), A22(N, k, CHI)));
    3322         504 :   s = gadd(s, A3(N, FC));
    3323         504 :   avma = av; return itos(s);
    3324             : }
    3325             : 
    3326             : /* Dimension of the space of Eisenstein series */
    3327             : long
    3328         231 : mfeisensteindim(long N, long k, GEN CHI)
    3329             : {
    3330         231 :   pari_sp av = avma;
    3331         231 :   long s, FC = CHI? mfcharconductor(CHI): 1;
    3332         231 :   if (k <= 0) return (k == 0 && FC == 1)? 1: 0;
    3333         231 :   s = itos(gmul2n(A3(N, FC), 1));
    3334         231 :   if (k > 1) s -= A4(k, FC);
    3335          49 :   else s >>= 1;
    3336         231 :   avma = av; return s;
    3337             : }
    3338             : 
    3339             : enum { _SQRTS = 1, _MUP, _GCD, _VCHIP, _BEZ, _NEWLZ, _TRCONJ };
    3340             : /* Trace of T(n) on space of cuspforms; only depends on CHIP the primitive char
    3341             :  * attached to CHI */
    3342             : static GEN
    3343     3046470 : mfcusptrace_i(long N, long k, long n, GEN Dn, GEN S)
    3344             : {
    3345     3046470 :   pari_sp av = avma;
    3346             :   GEN a, b, VCHIP, GCD;
    3347             :   long t;
    3348     3046470 :   if (!n) return gen_0;
    3349     3046470 :   VCHIP = gel(S,_VCHIP);
    3350     3046470 :   GCD = gel(S,_GCD);
    3351     3046470 :   t = TA4(k, VCHIP, Dn, GCD);
    3352     3046470 :   a = TA1(N, k, VCHIP, GCD, n); if (t) a = gaddgs(a,t);
    3353     3046470 :   b = TA2(N, k, VCHIP, n, gel(S,_SQRTS), gel(S,_MUP), GCD);
    3354     3046470 :   b = gadd(b, TA3(N, k, VCHIP, GCD, Dn, gel(S,_BEZ)));
    3355     3046470 :   b = gsub(a,b);
    3356     3046470 :   if (typ(b) != t_POL) return gerepileupto(av, b);
    3357       32816 :   return gerepilecopy(av, vchip_polmod(VCHIP, b));
    3358             : }
    3359             : 
    3360             : static GEN
    3361     4078081 : mfcusptracecache(long N, long k, long n, GEN Dn, GEN S, cachenew_t *cache)
    3362             : {
    3363     4078081 :   GEN C = NULL, T = gel(cache->vfull,N);
    3364     4078081 :   long lcache = lg(T);
    3365     4078081 :   if (n < lcache) C = gel(T, n);
    3366     4078081 :   if (C) cache->cuspHIT++; else C = mfcusptrace_i(N, k, n, Dn, S);
    3367     4078081 :   cache->cuspTOTAL++;
    3368     4078081 :   if (n < lcache) gel(T,n) = C;
    3369     4078081 :   return C;
    3370             : }
    3371             : 
    3372             : /* return the divisors of n, known to be among the elements of D */
    3373             : static GEN
    3374      286559 : div_restrict(GEN D, ulong n)
    3375             : {
    3376             :   long i, j, l;
    3377      286559 :   GEN v, VDIV = caches[cache_DIV].cache;
    3378      286559 :   if (lg(VDIV) > n) return gel(VDIV,n);
    3379           0 :   l = lg(D);
    3380           0 :   v = cgetg(l, t_VECSMALL);
    3381           0 :   for (i = j = 1; i < l; i++)
    3382             :   {
    3383           0 :     ulong d = D[i];
    3384           0 :     if (n % d == 0) v[j++] = d;
    3385             :   }
    3386           0 :   setlg(v,j); return v;
    3387             : }
    3388             : 
    3389             : /* for some prime divisors of N, Tr^new(p) = 0 */
    3390             : static int
    3391      200767 : trconj(GEN T, long N, long n)
    3392      200767 : { return (lg(T) > 1 && N % n == 0 && zv_search(T, n)); }
    3393             : 
    3394             : /* n > 0; trace formula on new space */
    3395             : static GEN
    3396     1272439 : mfnewtrace_i(long N, long k, long n, cachenew_t *cache)
    3397             : {
    3398     1272439 :   GEN VCHIP, s, Dn, DN1, SN, S = cache->DATA;
    3399             :   long FC, N1, N2, N1N2, g, i, j, lDN1;
    3400             : 
    3401     1272439 :   if (!S) return gen_0;
    3402     1272439 :   SN = gel(S,N);
    3403     1272439 :   if (mfnewchkzero(gel(SN,_NEWLZ), n)) return gen_0;
    3404      926912 :   if (k > 2 && trconj(gel(SN,_TRCONJ), N, n)) return gen_0;
    3405      926877 :   VCHIP = gel(SN, _VCHIP); FC = vchip_FC(VCHIP);
    3406      926877 :   N1 = N/FC; newt_params(N1, n, FC, &g, &N2);
    3407      926877 :   N1N2 = N1/N2;
    3408      926877 :   DN1 = mydivisorsu(N1N2); lDN1 = lg(DN1);
    3409      926877 :   N2 *= FC;
    3410      926877 :   Dn = mydivisorsu(n); /* this one is probably out of cache */
    3411      926877 :   s = gmulsg(mubeta2(N1N2,n), mfcusptracecache(N2, k, n, Dn, gel(S,N2), cache));
    3412     3791522 :   for (i = 2; i < lDN1; i++)
    3413             :   { /* skip M1 = 1, done above */
    3414     2864645 :     long M1 = DN1[i], N1M1 = DN1[lDN1-i];
    3415     2864645 :     GEN Dg = mydivisorsu(ugcd(M1, g));
    3416     2864645 :     M1 *= N2;
    3417     2864645 :     s = gadd(s, gmulsg(mubeta2(N1M1,n),
    3418     2864645 :                        mfcusptracecache(M1, k, n, Dn, gel(S,M1), cache)));
    3419     3151204 :     for (j = 2; j < lg(Dg); j++) /* skip d = 1, done above */
    3420             :     {
    3421      286559 :       long d = Dg[j], ndd = n/(d*d), M = M1/d;
    3422      286559 :       GEN z = mulsi(mubeta2(N1M1,ndd), powuu(d,k-1)), C = vchip_lift(VCHIP,d,z);
    3423      286559 :       GEN Dndd = div_restrict(Dn, ndd);
    3424      286559 :       s = gadd(s, gmul(C, mfcusptracecache(M, k, ndd, Dndd, gel(S,M), cache)));
    3425             :     }
    3426     2864645 :     s = vchip_mod(VCHIP, s);
    3427             :   }
    3428      926877 :   return vchip_polmod(VCHIP, s);
    3429             : }
    3430             : 
    3431             : /* mfcuspdim(N,k,CHI) - mfnewdim(N,k,CHI); CHIP primitive (for efficiency) */
    3432             : static long
    3433        7329 : mfolddim_i(long N, long k, GEN CHIP)
    3434             : {
    3435        7329 :   long S, i, l, FC = mfcharmodulus(CHIP), N1 = N/FC, N2;
    3436             :   GEN D;
    3437        7329 :   newd_params(N1, &N2); /* will ensure mubeta != 0 */
    3438        7329 :   D = mydivisorsu(N1/N2); l = lg(D);
    3439        7329 :   N2 *= FC; S = 0;
    3440       29064 :   for (i = 2; i < l; i++)
    3441             :   {
    3442       21735 :     long M = D[l-i]*N2, d = mfcuspdim(M, k, CHIP);
    3443       21735 :     if (d) S -= mubeta(D[i]) * d;
    3444             :   }
    3445        7329 :   return S;
    3446             : }
    3447             : long
    3448         322 : mfolddim(long N, long k, GEN CHI)
    3449             : {
    3450         322 :   pari_sp av = avma;
    3451         322 :   GEN CHIP = mfchartoprimitive(CHI, NULL);
    3452         322 :   long S = mfolddim_i(N, k, CHIP);
    3453         322 :   avma = av; return S;
    3454             : }
    3455             : /* Only depends on CHIP the primitive char attached to CHI; assumes !badchar */
    3456             : long
    3457       14329 : mfnewdim(long N, long k, GEN CHI)
    3458             : {
    3459       14329 :   pari_sp av = avma;
    3460             :   long S;
    3461       14329 :   GEN CHIP = mfchartoprimitive(CHI, NULL);
    3462       14329 :   S = mfcuspdim(N, k, CHIP); if (!S) return 0;
    3463        6993 :   S -= mfolddim_i(N, k, CHIP);
    3464        6993 :   avma = av; return S;
    3465             : }
    3466             : 
    3467             : /* trace form, given as closure */
    3468             : static GEN
    3469         882 : mftraceform_new(long N, long k, GEN CHI)
    3470             : {
    3471             :   GEN T;
    3472         882 :   if (k == 1) return initwt1newtrace(mfinit_Nkchi(N, 1, CHI, mf_CUSP, 0));
    3473         861 :   T = initnewtrace(N,CHI); if (!T) return mftrivial();
    3474         861 :   return tag(t_MF_NEWTRACE, mkNK(N,k,CHI), T);
    3475             : }
    3476             : static GEN
    3477          14 : mftraceform_cusp(long N, long k, GEN CHI)
    3478             : {
    3479          14 :   if (k == 1) return initwt1trace(mfinit_Nkchi(N, 1, CHI, mf_CUSP, 0));
    3480           7 :   return tag(t_MF_TRACE, mkNK(N,k,CHI), inittrace(N,CHI,NULL));
    3481             : }
    3482             : static GEN
    3483          91 : mftraceform_i(GEN NK, long space)
    3484             : {
    3485             :   GEN CHI;
    3486             :   long N, k;
    3487          91 :   checkNK(NK, &N, &k, &CHI, 0);
    3488          91 :   if (!mfdim_Nkchi(N, k, CHI, space)) return mftrivial();
    3489          70 :   switch(space)
    3490             :   {
    3491          49 :     case mf_NEW: return mftraceform_new(N, k, CHI);
    3492          14 :     case mf_CUSP:return mftraceform_cusp(N, k, CHI);
    3493             :   }
    3494           7 :   pari_err_DOMAIN("mftraceform", "space", "=", utoi(space), NK);
    3495             :   return NULL;/*LCOV_EXCL_LINE*/
    3496             : }
    3497             : GEN
    3498          91 : mftraceform(GEN NK, long space)
    3499          91 : { pari_sp av = avma; return gerepilecopy(av, mftraceform_i(NK,space)); }
    3500             : 
    3501             : static GEN
    3502       13895 : hecke_data(long N, long n)
    3503       13895 : { return mkvecsmall3(n, u_ppo(n, N), N); }
    3504             : /* 1/2-integral weight */
    3505             : static GEN
    3506          84 : heckef2_data(long N, long n)
    3507             : {
    3508             :   ulong f, fN, fN2;
    3509          84 :   if (!uissquareall(n, &f)) return NULL;
    3510          77 :   fN = u_ppo(f, N); fN2 = fN*fN;
    3511          77 :   return mkvec2(myfactoru(fN), mkvecsmall4(n, N, fN2, n/fN2));
    3512             : }
    3513             : /* N = mf_get_N(F) or a multiple */
    3514             : static GEN
    3515       20419 : mfhecke_i(long n, long N, GEN F)
    3516             : {
    3517       20419 :   if (n == 1) return F;
    3518       13776 :   return tag2(t_MF_HECKE, mf_get_NK(F), hecke_data(N,n), F);
    3519             : }
    3520             : 
    3521             : GEN
    3522          98 : mfhecke(GEN mf, GEN F, long n)
    3523             : {
    3524          98 :   pari_sp av = avma;
    3525             :   GEN NK, CHI, gk, DATA;
    3526             :   long N, nk, dk;
    3527          98 :   mf = checkMF(mf);
    3528          98 :   if (!checkmf_i(F)) pari_err_TYPE("mfhecke",F);
    3529          98 :   if (n <= 0) pari_err_TYPE("mfhecke [n <= 0]", stoi(n));
    3530          98 :   if (n == 1) return gcopy(F);
    3531          98 :   gk = mf_get_gk(F);
    3532          98 :   Qtoss(gk,&nk,&dk);
    3533          98 :   CHI = mf_get_CHI(F);
    3534          98 :   N = MF_get_N(mf);
    3535          98 :   if (dk == 2)
    3536             :   {
    3537          77 :     DATA = heckef2_data(N,n);
    3538          77 :     if (!DATA) return mftrivial();
    3539             :   }
    3540             :   else
    3541          21 :     DATA = hecke_data(N,n);
    3542          91 :   NK = mkgNK(lcmii(stoi(N), mf_get_gN(F)), gk, CHI, mf_get_field(F));
    3543          91 :   return gerepilecopy(av, tag2(t_MF_HECKE, NK, DATA, F));
    3544             : }
    3545             : 
    3546             : /* form F given by closure, compute B(d)(F) as closure (q -> q^d) */
    3547             : static GEN
    3548       26677 : mfbd_i(GEN F, long d)
    3549             : {
    3550             :   GEN D, NK, gk, CHI;
    3551       26677 :   if (d == 1) return F;
    3552        9541 :   if (d <= 0) pari_err_TYPE("mfbd [d <= 0]", stoi(d));
    3553        9541 :   if (mf_get_type(F) != t_MF_BD) D = utoi(d);
    3554           7 :   else { D = mului(d, gel(F,3)); F = gel(F,2); }
    3555        9541 :   gk = mf_get_gk(F); CHI = mf_get_CHI(F);
    3556        9541 :   if (typ(gk) != t_INT) CHI = mfcharmul(CHI, get_mfchar(utoi(d << 2)));
    3557        9541 :   NK = mkgNK(muliu(mf_get_gN(F), d), gk, CHI, mf_get_field(F));
    3558        9541 :   return tag2(t_MF_BD, NK, F, D);
    3559             : }
    3560             : GEN
    3561          35 : mfbd(GEN F, long d)
    3562             : {
    3563          35 :   pari_sp av = avma;
    3564          35 :   if (!checkmf_i(F)) pari_err_TYPE("mfbd",F);
    3565          35 :   return gerepilecopy(av, mfbd_i(F, d));
    3566             : }
    3567             : 
    3568             : /* CHI is a character defined modulo N4 */
    3569             : static GEN
    3570          98 : RgV_shimura(GEN V, long n, long D, long N4, long r, GEN CHI)
    3571             : {
    3572          98 :   GEN R, a0, Pn = mfcharpol(CHI);
    3573          98 :   long m, Da, ND, ord = mfcharorder(CHI), vt = varn(Pn), d4 = D & 3L;
    3574             : 
    3575          98 :   if (d4 == 2 || d4 == 3) D <<= 2;
    3576          98 :   Da = labs(D); ND = N4*Da;
    3577          98 :   R = cgetg(n + 2, t_VEC);
    3578          98 :   a0 = gel(V, 1);
    3579          98 :   if (!gequal0(a0))
    3580             :   {
    3581           7 :     long D4 = D << 2;
    3582           7 :     GEN CHID = induceN(ulcm(mfcharmodulus(CHI), labs(D4)), CHI);
    3583           7 :     CHID = mfcharmul_i(CHID, induce(gel(CHID,1), stoi(D4)));
    3584           7 :     a0 = gmul(a0, charLFwtk(r, CHID, mfcharorder(CHID)));
    3585             :   }
    3586          98 :   if (odd(ND) && !odd(mfcharmodulus(CHI))) ND <<= 1;
    3587          98 :   gel(R, 1) = a0;
    3588         567 :   for (m = 1; m <= n; m++)
    3589             :   {
    3590         469 :     GEN Dm = mydivisorsu(u_ppo(m, ND)), S = gel(V, m*m + 1);
    3591         469 :     long i, l = lg(Dm);
    3592         770 :     for (i = 2; i < l; i++)
    3593             :     { /* (e,ND) = 1; skip i = 1: e = 1, done above */
    3594         301 :       long e = Dm[i], me = m / e;
    3595         301 :       long a = mfcharevalord(CHI, e, ord);
    3596         301 :       GEN c, C = powuu(e, r - 1);
    3597         301 :       if (kross(D, e) == -1) C = negi(C);
    3598         301 :       c = mygmodulo_lift(a, ord, C, vt);
    3599         301 :       S = gadd(S, gmul(c, gel(V, me*me + 1)));
    3600             :     }
    3601         469 :     gel(R, m+1) = S;
    3602             :   }
    3603          98 :   return degpol(Pn) > 1? gmodulo(R, Pn): R;
    3604             : }
    3605             : static GEN
    3606          28 : c_shimura(long n, GEN F, long D, GEN CHI)
    3607             : {
    3608          28 :   GEN v = mfcoefs_i(F, n*n, labs(D));
    3609          28 :   return RgV_shimura(v, n, D, mf_get_N(F)>>2, mf_get_r(F), CHI);
    3610             : }
    3611             : 
    3612             : static long
    3613          14 : mfisinkohnen(GEN mf, GEN F)
    3614             : {
    3615          14 :   GEN v, gk = MF_get_gk(mf), CHI = MF_get_CHI(mf);
    3616          14 :   long i, sb, eps, N4 = MF_get_N(mf) >> 2, r = MF_get_r(mf);
    3617          14 :   sb = mfsturmNgk(N4 << 4, gk) + 1;
    3618          14 :   eps = N4 % mfcharconductor(CHI)? -1 : 1;
    3619          14 :   if (odd(r)) eps = -eps;
    3620          14 :   v = mfcoefs(F, sb, 1);
    3621         896 :   for (i = 0; i <= sb; i++)
    3622             :   {
    3623         882 :     long j = i & 3L;
    3624         882 :     if ((j == 2 || j == 2 + eps) && !gequal0(gel(v,i+1))) return 0;
    3625             :   }
    3626          14 :   return 1;
    3627             : }
    3628             : 
    3629             : static long
    3630          35 : mfshimura_space_cusp(GEN mf)
    3631             : {
    3632          35 :   long fl = 1, r = MF_get_r(mf), M = MF_get_N(mf) >> 2;
    3633          35 :   if (r == 1 && M >= 4)
    3634             :   {
    3635          14 :     GEN E = gel(myfactoru(M), 2);
    3636          14 :     long ma = vecsmall_max(E);
    3637          14 :     if (ma > 2 || (ma == 2 && !mfcharistrivial(MF_get_CHI(mf)))) fl = 0;
    3638             :   }
    3639          35 :   return fl;
    3640             : }
    3641             : 
    3642             : /* D is either a discriminant (not necessarily fundamental) with
    3643             :    sign(D)=(-1)^{k-1/2}*eps, or a positive squarefree integer t, which is then
    3644             :    transformed into a fundamental discriminant of the correct sign. */
    3645             : GEN
    3646          35 : mfshimura(GEN mf, GEN F, long D)
    3647             : {
    3648          35 :   pari_sp av = avma;
    3649             :   GEN gk, G, res, mf2, CHI, CHIP;
    3650          35 :   long M, r, space, cusp, N4, flagdisc = 0;
    3651          35 :   if (!checkmf_i(F)) pari_err_TYPE("mfshimura",F);
    3652          35 :   gk = mf_get_gk(F);
    3653          35 :   if (typ(gk) != t_FRAC) pari_err_TYPE("mfshimura [integral weight]", F);
    3654          35 :   r = MF_get_r(mf);
    3655          35 :   if (r <= 0) pari_err_DOMAIN("mfshimura", "weight", "<=", ghalf, gk);
    3656          35 :   N4 = MF_get_N(mf) >> 2; CHI = MF_get_CHI(mf);
    3657          35 :   CHIP = mfcharchiliftprim(CHI, N4);
    3658          35 :   if (!CHIP) CHIP = CHI;
    3659             :   else
    3660             :   {
    3661          35 :     long epsD = CHI == CHIP? D: -D, rd = D & 3L;
    3662          35 :     if (odd(r)) epsD = -epsD;
    3663          35 :     if (epsD > 0 && (rd == 0 || rd == 1)) flagdisc = 1;
    3664             :     else
    3665             :     {
    3666          14 :       if (D < 0 || !uissquarefree(D))
    3667           7 :         pari_err_TYPE("shimura [incorrect D]", stoi(D));
    3668           7 :       D = epsD;
    3669             :     }
    3670             :   }
    3671          28 :   M = N4;
    3672          28 :   cusp = mfiscuspidal(mf,F);
    3673          28 :   space = cusp && mfshimura_space_cusp(mf)? mf_CUSP : mf_FULL;
    3674          28 :   if (!cusp || !flagdisc || !mfisinkohnen(mf,F)) M <<= 1;
    3675          28 :   mf2 = mfinit_Nkchi(M, r << 1, mfcharpow(CHI, gen_2), space, 0);
    3676          28 :   G = c_shimura(mfsturm(mf2), F, D, CHIP);
    3677          28 :   res = mftobasis_i(mf2, G);
    3678             :   /* not mflinear(mf2,): we want lowest possible level */
    3679          28 :   G = mflinear(MF_get_basis(mf2), res);
    3680          28 :   return gerepilecopy(av, mkvec3(mf2, G, res));
    3681             : }
    3682             : 
    3683             : /* W ZabM (ZM if n = 1), a t_INT or NULL, b t_INT, ZXQ mod P or NULL.
    3684             :  * Write a/b = A/d with d t_INT and A Zab return [W,d,A,P] */
    3685             : static GEN
    3686        6552 : mkMinv(GEN W, GEN a, GEN b, GEN P)
    3687             : {
    3688        6552 :   GEN A = (b && typ(b) == t_POL)? Q_remove_denom(QXQ_inv(b,P), &b): NULL;
    3689        6552 :   if (a && b)
    3690             :   {
    3691         924 :     a = Qdivii(a,b);
    3692         924 :     if (typ(a) == t_INT) b = gen_1; else { b = gel(a,2); a = gel(a,1); }
    3693         924 :     if (is_pm1(a)) a = NULL;
    3694             :   }
    3695        6552 :   if (a) A = A? ZX_Z_mul(A,a): a; else if (!A) A = gen_1;
    3696        6552 :   if (!b) b = gen_1;
    3697        6552 :   if (!P) P = gen_0;
    3698        6552 :   return mkvec4(W,b,A,P);
    3699             : }
    3700             : /* M square invertible QabM, return [M',d], M*M' = d*Id */
    3701             : static GEN
    3702         434 : QabM_Minv(GEN M, GEN P, long n)
    3703             : {
    3704             :   GEN dW, W, dM;
    3705         434 :   M = Q_remove_denom(M, &dM);
    3706         434 :   W = P? ZabM_inv(liftpol_shallow(M), P, n, &dW): ZM_inv(M, &dW);
    3707         434 :   return mkMinv(W, dM, dW, P);
    3708             : }
    3709             : /* Simplified form of mfclean, after a QabM_indexrank: M a ZabM with full
    3710             :  * column rank and z = indexrank(M) is known */
    3711             : static GEN
    3712         798 : mfclean2(GEN M, GEN z, GEN P, long n)
    3713             : {
    3714         798 :   GEN d, Minv, y = gel(z,1), W = rowpermute(M, y);
    3715         798 :   W = P? ZabM_inv(liftpol_shallow(W), P, n, &d): ZM_inv(W, &d);
    3716         798 :   M = rowslice(M, 1, y[lg(y)-1]);
    3717         798 :   Minv = mkMinv(W, NULL, d, P);
    3718         798 :   return mkvec3(y, Minv, M);
    3719             : }
    3720             : /* M QabM, lg(M)>1 and [y,z] its rank profile. Let Minv be the inverse of the
    3721             :  * invertible square matrix in mkMinv format. Return [y,Minv, M[..y[#y],]]
    3722             :  * P cyclotomic polynomial of order n != 2 mod 4 or NULL */
    3723             : static GEN
    3724        4179 : mfclean(GEN M, GEN P, long n, int ratlift)
    3725             : {
    3726        4179 :   GEN W, v, y, z, d, Minv, dM, MdM = Q_remove_denom(M, &dM);
    3727        4179 :   if (n == 1)
    3728        3458 :     W = ZM_pseudoinv(MdM, &v, &d);
    3729             :   else
    3730         721 :     W = ZabM_pseudoinv_i(liftpol_shallow(MdM), P, n, &v, &d, ratlift);
    3731        4179 :   y = gel(v,1);
    3732        4179 :   z = gel(v,2);
    3733        4179 :   if (lg(z) != lg(MdM)) M = vecpermute(M,z);
    3734        4179 :   M = rowslice(M, 1, y[lg(y)-1]);
    3735        4179 :   Minv = mkMinv(W, dM, d, P);
    3736        4179 :   return mkvec3(y, Minv, M);
    3737             : }
    3738             : /* call mfclean using only CHI */
    3739             : static GEN
    3740        3388 : mfcleanCHI(GEN M, GEN CHI, int ratlift)
    3741             : {
    3742        3388 :   long n = mfcharorder_canon(CHI);
    3743        3388 :   GEN P = (n == 1)? NULL: mfcharpol(CHI);
    3744        3388 :   return mfclean(M, P, n, ratlift);
    3745             : }
    3746             : 
    3747             : /* DATA component of a t_MF_NEWTRACE. Was it stripped to save memory ? */
    3748             : static int
    3749       25606 : newtrace_stripped(GEN DATA)
    3750       25606 : { return DATA && (lg(DATA) == 5 && typ(gel(DATA,3)) == t_INT); }
    3751             : /* f a t_MF_NEWTRACE */
    3752             : static GEN
    3753       25606 : newtrace_DATA(long N, GEN f)
    3754             : {
    3755       25606 :   GEN DATA = gel(f,2);
    3756       25606 :   return newtrace_stripped(DATA)? initnewtrace(N, DATA): DATA;
    3757             : }
    3758             : /* reset cachenew for new level incorporating new DATA, tf a t_MF_NEWTRACE
    3759             :  * (+ possibly initialize 'full' for new allowed levels) */
    3760             : static void
    3761       25606 : reset_cachenew(cachenew_t *cache, long N, GEN tf)
    3762             : {
    3763             :   long i, n, l;
    3764       25606 :   GEN v, DATA = newtrace_DATA(N,tf);
    3765       25606 :   cache->DATA = DATA;
    3766       25606 :   if (!DATA) return;
    3767       25571 :   n = cache->n;
    3768       25571 :   v = cache->vfull; l = N+1; /* = lg(DATA) */
    3769     1380953 :   for (i = 1; i < l; i++)
    3770     1355382 :     if (typ(gel(v,i)) == t_INT && lg(gel(DATA,i)) != 1)
    3771       40446 :       gel(v,i) = const_vec(n, NULL);
    3772       25571 :   cache->VCHIP = gel(gel(DATA,N),_VCHIP);
    3773             : }
    3774             : /* initialize a cache of newtrace / cusptrace up to index n and level | N;
    3775             :  * DATA may be NULL (<=> Tr^new = 0). tf a t_MF_NEWTRACE */
    3776             : static void
    3777        9744 : init_cachenew(cachenew_t *cache, long n, long N, GEN tf)
    3778             : {
    3779        9744 :   long i, l = N+1; /* = lg(tf.DATA) when DATA != NULL */
    3780             :   GEN v;
    3781        9744 :   cache->n = n;
    3782        9744 :   cache->vnew = v = cgetg(l, t_VEC);
    3783        9744 :   for (i = 1; i < l; i++) gel(v,i) = (N % i)? gen_0: const_vec(n, NULL);
    3784        9744 :   cache->newHIT = cache->newTOTAL = cache->cuspHIT = cache->cuspTOTAL = 0;
    3785        9744 :   cache->vfull = v = zerovec(N);
    3786        9744 :   reset_cachenew(cache, N, tf);
    3787        9744 : }
    3788             : static void
    3789       14455 : dbg_cachenew(cachenew_t *C)
    3790             : {
    3791       14455 :   if (DEBUGLEVEL >= 2 && C)
    3792           0 :     err_printf("newtrace cache hits: new = %ld/%ld, cusp = %ld/%ld\n",
    3793             :                     C->newHIT, C->newTOTAL, C->cuspHIT, C->cuspTOTAL);
    3794       14455 : }
    3795             : 
    3796             : /* newtrace_{N,k}(d*i), i = n0, ..., n */
    3797             : static GEN
    3798      107555 : colnewtrace(long n0, long n, long d, long N, long k, cachenew_t *cache)
    3799             : {
    3800      107555 :   GEN v = cgetg(n-n0+2, t_COL);
    3801             :   long i;
    3802      107555 :   for (i = n0; i <= n; i++) gel(v, i-n0+1) = mfnewtracecache(N, k, i*d, cache);
    3803      107555 :   return v;
    3804             : }
    3805             : /* T_n(l*m0, l*(m0+1), ..., l*m) F, F = t_MF_NEWTRACE [N,k],DATA, cache
    3806             :  * contains DATA != NULL as well as cached values of F */
    3807             : static GEN
    3808       61642 : heckenewtrace(long m0, long m, long l, long N, long NBIG, long k, long n, cachenew_t *cache)
    3809             : {
    3810       61642 :   long lD, a, k1, nl = n*l;
    3811       61642 :   GEN D, V, v = colnewtrace(m0, m, nl, N, k, cache); /* d=1 */
    3812             :   GEN VCHIP;
    3813       61642 :   if (n == 1) return v;
    3814       39214 :   VCHIP = cache->VCHIP;
    3815       39214 :   D = mydivisorsu(u_ppo(n, NBIG)); lD = lg(D);
    3816       39214 :   k1 = k - 1;
    3817       84399 :   for (a = 2; a < lD; a++)
    3818             :   { /* d > 1, (d,NBIG) = 1 */
    3819       45185 :     long i, j, d = D[a], c = ugcd(l, d), dl = d/c, m0d = ceildiv(m0, dl);
    3820       45185 :     GEN C = vchip_lift(VCHIP, d, powuu(d, k1));
    3821             :     /* m0=0: i = 1 => skip F(0) = 0 */
    3822       45185 :     if (!m0) { i = 1; j = dl; } else { i = 0; j = m0d*dl; }
    3823       45185 :     V = colnewtrace(m0d, m/dl, nl/(d*c), N, k, cache);
    3824             :     /* C = chi(d) d^(k-1) */
    3825      393729 :     for (; j <= m; i++, j += dl)
    3826      348544 :       gel(v,j-m0+1) = gadd(gel(v,j-m0+1), vchip_mod(VCHIP, gmul(C,gel(V,i+1))));
    3827             :   }
    3828       39214 :   return v;
    3829             : }
    3830             : 
    3831             : /* Given v = an[i], return an[d*i] */
    3832             : static GEN
    3833         406 : anextract(GEN v, long n, long d)
    3834             : {
    3835         406 :   GEN w = cgetg(n+2, t_VEC);
    3836             :   long i;
    3837         406 :   for (i = 0; i <= n; i++) gel(w, i+1) = gel(v, i*d+1);
    3838         406 :   return w;
    3839             : }
    3840             : /* T_n(F)(0, l, ..., l*m) */
    3841             : static GEN
    3842         693 : hecke_i(long m, long l, GEN V, GEN F, GEN DATA)
    3843             : {
    3844             :   long k, n, nNBIG, NBIG, lD, M, a, t, nl;
    3845             :   GEN D, v, CHI;
    3846         693 :   if (typ(DATA) == t_VEC)
    3847             :   { /* 1/2-integral k */
    3848          98 :     if (!V) { GEN S = gel(DATA,2); V = mfcoefs_i(F, m*l*S[3], S[4]); }
    3849          98 :     return RgV_heckef2(m, l, V, F, DATA);
    3850             :   }
    3851         595 :   k = mf_get_k(F);
    3852         595 :   n = DATA[1]; nl = n*l;
    3853         595 :   nNBIG = DATA[2];
    3854         595 :   NBIG = DATA[3];
    3855         595 :   if (nNBIG == 1) return V? V: mfcoefs_i(F,m,nl);
    3856         413 :   if (!V && mf_get_type(F) == t_MF_NEWTRACE)
    3857             :   { /* inline F to allow cache, T_n at level NBIG acting on Tr^new(N,k,CHI) */
    3858             :     cachenew_t cache;
    3859         210 :     long N = mf_get_N(F);
    3860         210 :     init_cachenew(&cache, m*nl, N, F);
    3861         210 :     v = heckenewtrace(0, m, l, N, NBIG, k, n, &cache);
    3862         210 :     dbg_cachenew(&cache);
    3863         210 :     settyp(v, t_VEC); return v;
    3864             :   }
    3865         203 :   CHI = mf_get_CHI(F);
    3866         203 :   D = mydivisorsu(nNBIG); lD = lg(D);
    3867         203 :   M = m + 1;
    3868         203 :   t = nNBIG * ugcd(nNBIG, l);
    3869         203 :   if (!V) V = mfcoefs_i(F, m * t, nl / t); /* usually nl = t */
    3870         203 :   v = anextract(V, m, t); /* mfcoefs(F, m, nl); d = 1 */
    3871         406 :   for (a = 2; a < lD; a++)
    3872             :   { /* d > 1, (d, NBIG) = 1 */
    3873         203 :     long d = D[a], c = ugcd(l, d), dl = d/c, i, idl;
    3874         203 :     GEN C = gmul(mfchareval_i(CHI, d), powuu(d, k-1));
    3875         203 :     GEN w = anextract(V, m/dl, t/(d*c)); /* mfcoefs(F, m/dl, nl/(d*c)) */
    3876         644 :     for (i = idl = 1; idl <= M; i++, idl += dl)
    3877         441 :       gel(v,idl) = gadd(gel(v,idl), gmul(C, gel(w,i)));
    3878             :   }
    3879         203 :   return v;
    3880             : }
    3881             : 
    3882             : static GEN
    3883       10955 : mkmf(GEN x1, GEN x2, GEN x3, GEN x4, GEN x5)
    3884             : {
    3885       10955 :   GEN MF = obj_init(5, MF_SPLITN);
    3886       10955 :   gel(MF,1) = x1;
    3887       10955 :   gel(MF,2) = x2;
    3888       10955 :   gel(MF,3) = x3;
    3889       10955 :   gel(MF,4) = x4;
    3890       10955 :   gel(MF,5) = x5; return MF;
    3891             : }
    3892             : 
    3893             : /* return an integer b such that p | b => T_p^k Tr^new = 0, for all k > 0 */
    3894             : static long
    3895        6797 : get_badj(long N, long FC)
    3896             : {
    3897        6797 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
    3898        6797 :   long i, b = 1, l = lg(P);
    3899       18137 :   for (i = 1; i < l; i++)
    3900       11340 :     if (E[i] > 1 && u_lval(FC, P[i]) < E[i]) b *= P[i];
    3901        6797 :   return b;
    3902             : }
    3903             : /* in place, assume perm strictly increasing */
    3904             : static void
    3905        1106 : vecpermute_inplace(GEN v, GEN perm)
    3906             : {
    3907        1106 :   long i, l = lg(perm);
    3908        1106 :   for (i = 1; i < l; i++) gel(v,i) = gel(v,perm[i]);
    3909        1106 : }
    3910             : 
    3911             : /* Find basis of newspace using closures; assume k >= 2 and !badchar.
    3912             :  * Return NULL if space is empty, else
    3913             :  * [mf1, list of closures T(j)traceform, list of corresponding j, matrix] */
    3914             : static GEN
    3915       14091 : mfnewinit(long N, long k, GEN CHI, cachenew_t *cache, long init)
    3916             : {
    3917             :   GEN S, vj, M, CHIP, mf1, listj, P, tf;
    3918             :   long j, ct, ctlj, dim, jin, SB, sb, two, ord, FC, badj;
    3919             : 
    3920       14091 :   dim = mfnewdim(N, k, CHI);
    3921       14091 :   if (!dim && !init) return NULL;
    3922        6797 :   sb = mfsturmNk(N, k);
    3923        6797 :   CHIP = mfchartoprimitive(CHI, &FC);
    3924             :   /* remove newtrace data from S to save space in output: negligible slowdown */
    3925        6797 :   tf = tag(t_MF_NEWTRACE, mkNK(N,k,CHIP), CHIP);
    3926        6797 :   badj = get_badj(N, FC);
    3927             :   /* try sbsmall first: Sturm bound not sharp for new space */
    3928        6797 :   SB = ceilA1(N, k);
    3929        6797 :   listj = cgetg(2*sb + 3, t_VECSMALL);
    3930      313803 :   for (j = ctlj = 1; ctlj < 2*sb + 3; j++)
    3931      307006 :     if (ugcd(j, badj) == 1) listj[ctlj++] = j;
    3932        6797 :   if (init)
    3933             :   {
    3934        3794 :     init_cachenew(cache, (SB+1)*listj[dim+1], N, tf);
    3935        3794 :     if (init == -1 || !dim) return NULL; /* old space or dim = 0 */
    3936             :   }
    3937             :   else
    3938        3003 :     reset_cachenew(cache, N, tf);
    3939             :   /* cache.DATA is not NULL */
    3940        6370 :   ord = mfcharorder_canon(CHIP);
    3941        6370 :   P = ord == 1? NULL: mfcharpol(CHIP);
    3942        6370 :   vj = cgetg(dim+1, t_VECSMALL);
    3943        6370 :   M = cgetg(dim+1, t_MAT);
    3944        6377 :   for (two = 1, ct = 0, jin = 1; two <= 2; two++)
    3945             :   {
    3946        6377 :     long a, jlim = jin + sb;
    3947       18039 :     for (a = jin; a <= jlim; a++)
    3948             :     {
    3949             :       GEN z, vecz;
    3950       18032 :       ct++; vj[ct] = listj[a];
    3951       18032 :       gel(M, ct) = heckenewtrace(0, SB, 1, N, N, k, vj[ct], cache);
    3952       18032 :       if (ct < dim) continue;
    3953             : 
    3954        6923 :       z = QabM_indexrank(M, P, ord);
    3955        6923 :       vecz = gel(z, 2); ct = lg(vecz) - 1;
    3956        6923 :       if (ct == dim) { M = mkvec3(z, gen_0, M); break; } /*maximal rank, done*/
    3957         553 :       vecpermute_inplace(M, vecz);
    3958         553 :       vecpermute_inplace(vj, vecz);
    3959             :     }
    3960        6377 :     if (a <= jlim) break;
    3961             :     /* sbsmall was not sufficient, use Sturm bound: must extend M */
    3962          70 :     for (j = 1; j <= ct; j++)
    3963             :     {
    3964          63 :       GEN t = heckenewtrace(SB + 1, sb, 1, N, N, k, vj[j], cache);
    3965          63 :       gel(M,j) = shallowconcat(gel(M, j), t);
    3966             :     }
    3967           7 :     jin = jlim + 1; SB = sb;
    3968             :   }
    3969        6370 :   S = cgetg(dim + 1, t_VEC);
    3970        6370 :   for (j = 1; j <= dim; j++) gel(S, j) = mfhecke_i(vj[j], N, tf);
    3971        6370 :   dbg_cachenew(cache);
    3972        6370 :   mf1 = mkvec4(utoipos(N), utoipos(k), CHI, utoi(mf_NEW));
    3973        6370 :   return mkmf(mf1, cgetg(1,t_VEC), S, vj, M);
    3974             : }
    3975             : /* k > 1 integral, mf space is mf_CUSP or mf_FULL */
    3976             : static GEN
    3977          28 : mfinittonew(GEN mf)
    3978             : {
    3979          28 :   GEN CHI = MF_get_CHI(mf), S = MF_get_S(mf), vMjd = MFcusp_get_vMjd(mf);
    3980          28 :   GEN M = MF_get_M(mf), vj, mf1;
    3981          28 :   long i, j, l, l0 = lg(S), N0 = MF_get_N(mf);
    3982         161 :   for (i = l0-1; i > 0; i--)
    3983             :   {
    3984         161 :     long N = gel(vMjd,i)[1];
    3985         161 :     if (N != N0) break;
    3986             :   }
    3987          28 :   if (i == l0-1) return NULL;
    3988          28 :   S = vecslice(S, i+1, l0-1); /* forms of conductor N0 */
    3989          28 :   l = lg(S); vj = cgetg(l, t_VECSMALL);
    3990          28 :   for (j = 1; j < l; j++) vj[j] = gel(vMjd,j+i)[2];
    3991          28 :   M = vecslice(M, lg(M)-lg(S)+1, lg(M)-1); /* their coefficients */
    3992          28 :   M = mfcleanCHI(M, CHI, 0);
    3993          28 :   mf1 = mkvec4(utoipos(N0), MF_get_gk(mf), CHI, utoi(mf_NEW));
    3994          28 :   return mkmf(mf1, cgetg(1,t_VEC), S, vj, M);
    3995             : }
    3996             : 
    3997             : /* Bd(f)[m0..m], v = f[ceil(m0/d)..floor(m/d)], m0d = ceil(m0/d) */
    3998             : static GEN
    3999       51814 : RgC_Bd_expand(long m0, long m, GEN v, long d, long m0d)
    4000             : {
    4001             :   long i, j;
    4002             :   GEN w;
    4003       51814 :   if (d == 1) return v;
    4004       14371 :   w = zerocol(m-m0+1);
    4005       14371 :   if (!m0) { i = 1; j = d; } else { i = 0; j = m0d*d; }
    4006       14371 :   for (; j <= m; i++, j += d) gel(w,j-m0+1) = gel(v,i+1);
    4007       14371 :   return w;
    4008             : }
    4009             : /* S a non-empty vector of t_MF_BD(t_MF_HECKE(t_MF_NEWTRACE)); M the matrix
    4010             :  * of their coefficients r*0, r*1, ..., r*m0 (~ mfvectomat) or NULL (empty),
    4011             :  * extend it to coeffs up to m > m0. The forms B_d(T_j(tf_N))in S should be
    4012             :  * sorted by level N, then j, then increasing d. No reordering here. */
    4013             : static GEN
    4014        7049 : bhnmat_extend(GEN M, long m, long r, GEN S, cachenew_t *cache)
    4015             : {
    4016        7049 :   long i, mr, m0, m0r, Nold = 0, jold = 0, l = lg(S);
    4017        7049 :   GEN MAT = cgetg(l, t_MAT), v = NULL;
    4018        7049 :   if (M) { m0 = nbrows(M); m0r = m0 * r; } else m0 = m0r = 0;
    4019        7049 :   mr = m*r;
    4020       58863 :   for (i = 1; i < l; i++)
    4021             :   {
    4022             :     long d, j, md, N;
    4023       51814 :     GEN c, f = bhn_parse(gel(S,i), &d,&j); /* t_MF_NEWTRACE */
    4024       51814 :     N = mf_get_N(f);
    4025       51814 :     md = ceildiv(m0r,d);
    4026       51814 :     if (N != Nold) { reset_cachenew(cache, N, f); Nold = N; jold = 0; }
    4027       51814 :     if (!cache->DATA) { gel(MAT,i) = zerocol(m+1); continue; }
    4028       51814 :     if (j != jold || md)
    4029       43337 :     { v = heckenewtrace(md, mr/d, 1, N, N, mf_get_k(f), j,cache); jold=j; }
    4030       51814 :     c = RgC_Bd_expand(m0r, mr, v, d, md);
    4031       51814 :     if (r > 1) c = c_deflate(m-m0, r, c);
    4032       51814 :     if (M) c = shallowconcat(gel(M,i), c);
    4033       51814 :     gel(MAT,i) = c;
    4034             :   }
    4035        7049 :   return MAT;
    4036             : }
    4037             : 
    4038             : static GEN
    4039        2863 : mfinitcusp(long N, long k, GEN CHI, cachenew_t *cache, long space)
    4040             : {
    4041             :   long L, l, lDN1, FC, N1, d1, i, init;
    4042        2863 :   GEN vS, vMjd, DN1, vmf, CHIP = mfchartoprimitive(CHI, &FC);
    4043             : 
    4044        2863 :   d1 = (space == mf_OLD)? mfolddim_i(N, k, CHIP): mfcuspdim(N, k, CHIP);
    4045        2863 :   if (!d1) return NULL;
    4046        2625 :   N1 = N/FC; DN1 = mydivisorsu(N1); lDN1 = lg(DN1);
    4047        2625 :   init = (space == mf_OLD)? -1: 1;
    4048        2625 :   vmf = cgetg(lDN1, t_VEC);
    4049       15547 :   for (i = lDN1 - 1, l = 1; i; i--)
    4050             :   { /* by decreasing level to allow cache */
    4051       12922 :     GEN mf = mfnewinit(FC*DN1[i], k, CHIP, cache, init);
    4052       12922 :     if (mf) gel(vmf, l++) = mf;
    4053       12922 :     init = 0;
    4054             :   }
    4055        2625 :   setlg(vmf,l); vmf = vecreverse(vmf); /* reorder by increasing level */
    4056             : 
    4057        2625 :   L = mfsturmNk(N, k)+1;
    4058        2625 :   vS = vectrunc_init(L);
    4059        2625 :   vMjd = vectrunc_init(L);
    4060        8197 :   for (i = 1; i < l; i++)
    4061             :   {
    4062        5572 :     GEN DNM, mf = gel(vmf,i), S = MF_get_S(mf), vj = MFnew_get_vj(mf);
    4063        5572 :     long a, lDNM, lS = lg(S), M = MF_get_N(mf);
    4064        5572 :     DNM = mydivisorsu(N / M); lDNM = lg(DNM);
    4065       21189 :     for (a = 1; a < lS; a++)
    4066             :     {
    4067       15617 :       GEN tf = gel(S,a);
    4068       15617 :       long b, j = vj[a];
    4069       38486 :       for (b = 1; b < lDNM; b++)
    4070             :       {
    4071       22869 :         long d = DNM[b];
    4072       22869 :         vectrunc_append(vS, mfbd_i(tf, d));
    4073       22869 :         vectrunc_append(vMjd, mkvecsmall3(M, j, d));
    4074             :       }
    4075             :     }
    4076             :   }
    4077        2625 :   return mkmf(NULL, cgetg(1, t_VEC), vS, vMjd, NULL);
    4078             : }
    4079             : 
    4080             : long
    4081        2933 : mfsturm_mf(GEN mf)
    4082             : {
    4083        2933 :   GEN Mindex = MF_get_Mindex(mf);
    4084        2933 :   long n = lg(Mindex)-1;
    4085        2933 :   return n? Mindex[n]: 0;
    4086             : }
    4087             : 
    4088             : long
    4089         504 : mfsturm(GEN T)
    4090             : {
    4091             :   long N, nk, dk;
    4092         504 :   GEN CHI, mf = checkMF_i(T);
    4093         504 :   if (mf) return mfsturm_mf(mf);
    4094           7 :   checkNK2(T, &N, &nk, &dk, &CHI, 0);
    4095           7 :   return dk == 1 ? mfsturmNk(N, nk) : mfsturmNk(N, (nk + 1) >> 1);
    4096             : }
    4097             : 
    4098             : long
    4099           7 : mfisequal(GEN F, GEN G, long lim)
    4100             : {
    4101           7 :   pari_sp av = avma;
    4102             :   long t, sb;
    4103           7 :   if (!checkmf_i(F)) pari_err_TYPE("mfisequal",F);
    4104           7 :   if (!checkmf_i(G)) pari_err_TYPE("mfisequal",G);
    4105           7 :   if (lim) sb = lim;
    4106             :   else
    4107             :   {
    4108             :     GEN gN, gk;
    4109           7 :     gN = mf_get_gN(F); gk = mf_get_gk(F);
    4110           7 :     sb = mfsturmNgk(itou(gN), gk);
    4111           7 :     gN = mf_get_gN(G); gk = mf_get_gk(G);
    4112           7 :     sb = maxss(sb, mfsturmNgk(itou(gN), gk));
    4113             :   }
    4114           7 :   t = gequal(mfcoefs_i(F, sb+1, 1), mfcoefs_i(G, sb+1, 1));
    4115           7 :   avma = av; return t;
    4116             : }
    4117             : 
    4118             : GEN
    4119          35 : mffields(GEN mf)
    4120             : {
    4121          35 :   if (checkmf_i(mf)) return gcopy(mf_get_field(mf));
    4122          35 :   mf = checkMF(mf); return gcopy(MF_get_fields(mf));
    4123             : }
    4124             : 
    4125             : GEN
    4126         154 : mfeigenbasis(GEN mf)
    4127             : {
    4128         154 :   pari_sp ltop = avma;
    4129             :   GEN F, S, v, vP;
    4130             :   long i, l, k, dS;
    4131             : 
    4132         154 :   mf = checkMF(mf);
    4133         154 :   k = MF_get_k(mf);
    4134         154 :   S = MF_get_S(mf); dS = lg(S)-1;
    4135         154 :   if (!dS) return cgetg(1, t_VEC);
    4136         147 :   F = MF_get_newforms(mf);
    4137         147 :   vP = MF_get_fields(mf);
    4138         147 :   if (k == 1)
    4139             :   {
    4140          49 :     if (MF_get_space(mf) == mf_FULL)
    4141             :     {
    4142           7 :       long dE = lg(MF_get_E(mf)) - 1;
    4143           7 :       if (dE) F = rowslice(F, dE+1, dE+dS);
    4144             :     }
    4145          49 :     v = vecmflineardiv_linear(S, F);
    4146          49 :     l = lg(v);
    4147             :   }
    4148             :   else
    4149             :   {
    4150          98 :     GEN (*L)(GEN, GEN) = (MF_get_space(mf) == mf_FULL)? mflinear: mflinear_bhn;
    4151          98 :     l = lg(F); v = cgetg(l, t_VEC);
    4152          98 :     for (i = 1; i < l; i++) gel(v,i) = L(mf, gel(F,i));
    4153             :   }
    4154         147 :   for (i = 1; i < l; i++) mf_setfield(gel(v,i), gel(vP,i));
    4155         147 :   return gerepilecopy(ltop, v);
    4156             : }
    4157             : 
    4158             : /* Minv = [M, d, A], v a t_COL; A a Zab, d a t_INT; return (A/d) * M*v */
    4159             : static GEN
    4160        5096 : Minv_RgC_mul(GEN Minv, GEN v)
    4161             : {
    4162        5096 :   GEN M = gel(Minv,1), d = gel(Minv,2), A = gel(Minv,3);
    4163        5096 :   v = RgM_RgC_mul(M, v);
    4164        5096 :   if (!equali1(A))
    4165             :   {
    4166        1218 :     if (typ(A) == t_POL && degpol(A) > 0) A = mkpolmod(A, gel(Minv,4));
    4167        1218 :     v = RgC_Rg_mul(v, A);
    4168             :   }
    4169        5096 :   if (!equali1(d)) v = RgC_Rg_div(v, d);
    4170        5096 :   return v;
    4171             : }
    4172             : static GEN
    4173         973 : Minv_RgM_mul(GEN Minv, GEN B)
    4174             : {
    4175         973 :   long j, l = lg(B);
    4176         973 :   GEN M = cgetg(l, t_MAT);
    4177         973 :   for (j = 1; j < l; j++) gel(M,j) = Minv_RgC_mul(Minv, gel(B,j));
    4178         973 :   return M;
    4179             : }
    4180             : /* B * Minv; allow B = NULL for Id */
    4181             : static GEN
    4182        2002 : RgM_Minv_mul(GEN B, GEN Minv)
    4183             : {
    4184        2002 :   GEN M = gel(Minv,1), d = gel(Minv,2), A = gel(Minv,3);
    4185        2002 :   if (B) M = RgM_mul(B, M);
    4186        2002 :   if (!equali1(A))
    4187             :   {
    4188         714 :     if (typ(A) == t_POL) A = mkpolmod(A, gel(Minv,4));
    4189         714 :     M = RgM_Rg_mul(M, A);
    4190             :   }
    4191        2002 :   if (!equali1(d)) M = RgM_Rg_div(M,d);
    4192        2002 :   return M;
    4193             : }
    4194             : 
    4195             : /* perm vector of strictly increasing indices, v a vector or arbitrary length;
    4196             :  * the last r entries of perm fall beyond v.
    4197             :  * Return v o perm[1..(-r)], discarding the last r entries of v */
    4198             : static GEN
    4199        1008 : vecpermute_partial(GEN v, GEN perm, long *r)
    4200             : {
    4201        1008 :   long i, n = lg(v)-1, l = lg(perm);
    4202             :   GEN w;
    4203        1008 :   if (perm[l-1] <= n) { *r = 0; return vecpermute(v,perm); }
    4204          63 :   for (i = 1; i < l; i++)
    4205          63 :     if (perm[i] > n) break;
    4206          21 :   *r = l - i; l = i;
    4207          21 :   w = cgetg(l, typ(v));
    4208          21 :   for (i = 1; i < l; i++) gel(w,i) = gel(v,perm[i]);
    4209          21 :   return w;
    4210             : }
    4211             : 
    4212             : /* given form F, find coeffs of F on mfbasis(mf). If power series, not
    4213             :  * guaranteed correct if precision less than Sturm bound */
    4214             : static GEN
    4215        1022 : mftobasis_i(GEN mf, GEN F)
    4216             : {
    4217             :   GEN v, Mindex, Minv;
    4218        1022 :   if (!MF_get_dim(mf)) return cgetg(1, t_COL);
    4219        1015 :   Mindex = MF_get_Mindex(mf);
    4220        1015 :   Minv = MF_get_Minv(mf);
    4221        1015 :   if (checkmf_i(F))
    4222             :   {
    4223         154 :     long n = Mindex[lg(Mindex)-1];
    4224         154 :     v = vecpermute(mfcoefs_i(F, n, 1), Mindex);
    4225         154 :     return Minv_RgC_mul(Minv, v);
    4226             :   }
    4227             :   else
    4228             :   {
    4229         861 :     GEN A = gel(Minv,1), d = gel(Minv,2);
    4230             :     long r;
    4231         861 :     v = F;
    4232         861 :     switch(typ(F))
    4233             :     {
    4234           0 :       case t_SER: v = sertocol(v);
    4235         861 :       case t_VEC: case t_COL: break;
    4236           0 :       default: pari_err_TYPE("mftobasis", F);
    4237             :     }
    4238         861 :     if (lg(v) == 1) pari_err_TYPE("mftobasis",v);
    4239         861 :     v = vecpermute_partial(v, Mindex, &r);
    4240         861 :     if (!r) return Minv_RgC_mul(Minv, v); /* single solution */
    4241             :     /* affine space of dimension r */
    4242          21 :     v = RgM_RgC_mul(vecslice(A, 1, lg(v)-1), v);
    4243          21 :     if (!equali1(d)) v = RgC_Rg_div(v,d);
    4244          21 :     return mkvec2(v, vecslice(A, lg(A)-r, lg(A)-1));
    4245             :   }
    4246             : }
    4247             : 
    4248             : static GEN
    4249         560 : const_mat(long n, GEN x)
    4250             : {
    4251         560 :   long j, l = n+1;
    4252         560 :   GEN A = cgetg(l,t_MAT);
    4253         560 :   for (j = 1; j < l; j++) gel(A,j) = const_col(n, x);
    4254         560 :   return A;
    4255             : }
    4256             : 
    4257             : /* L is the mftobasis of a form on CUSP space. We allow mf_FULL or mf_CUSP */
    4258             : static GEN
    4259         280 : mftonew_i(GEN mf, GEN L, long *plevel)
    4260             : {
    4261             :   GEN S, listMjd, CHI, res, Aclos, Acoef, D, perm;
    4262         280 :   long N1, LC, lD, i, l, t, level, N = MF_get_N(mf);
    4263             : 
    4264         280 :   if (MF_get_k(mf) == 1) pari_err_IMPL("mftonew in weight 1");
    4265         280 :   listMjd = MFcusp_get_vMjd(mf);
    4266         280 :   CHI = MF_get_CHI(mf); LC = mfcharconductor(CHI);
    4267         280 :   S = MF_get_S(mf);
    4268             : 
    4269         280 :   N1 = N/LC;
    4270         280 :   D = mydivisorsu(N1); lD = lg(D);
    4271         280 :   perm = cgetg(N1+1, t_VECSMALL);
    4272         280 :   for (i = 1; i < lD; i++) perm[D[i]] = i;
    4273         280 :   Aclos = const_mat(lD-1, cgetg(1,t_VEC));
    4274         280 :   Acoef = const_mat(lD-1, cgetg(1,t_VEC));
    4275         280 :   l = lg(listMjd);
    4276        2877 :   for (i = 1; i < l; i++)
    4277             :   {
    4278             :     long M, d;
    4279             :     GEN v;
    4280        2597 :     if (gequal0(gel(L,i))) continue;
    4281         273 :     v = gel(listMjd, i);
    4282         273 :     M = perm[ v[1]/LC ];
    4283         273 :     d = perm[ v[3] ];
    4284         273 :     gcoeff(Aclos,M,d) = vec_append(gcoeff(Aclos,M,d), gel(S,i));
    4285         273 :     gcoeff(Acoef,M,d) = shallowconcat(gcoeff(Acoef,M,d), gel(L,i));
    4286             :   }
    4287         280 :   res = cgetg(l, t_VEC); level = 1;
    4288        2009 :   for (i = t = 1; i < lD; i++)
    4289             :   {
    4290        1729 :     long j, M = D[i]*LC;
    4291        1729 :     GEN gM = utoipos(M);
    4292       15134 :     for (j = 1; j < lD; j++)
    4293             :     {
    4294       13405 :       GEN f = gcoeff(Aclos,i,j), C, NK;
    4295             :       long d;
    4296       13405 :       if (lg(f) == 1) continue;
    4297         245 :       NK = mf_get_NK(gel(f,1));
    4298         245 :       d = D[j];
    4299         245 :       C = gcoeff(Acoef,i,j);
    4300         245 :       level = ulcm(level, M*d);
    4301         245 :       gel(res,t++) = mkvec3(gM, utoipos(d), mflinear_i(NK,f,C));
    4302             :     }
    4303             :   }
    4304         280 :   if (plevel) *plevel = level;
    4305         280 :   setlg(res, t); return res;
    4306             : }
    4307             : GEN
    4308          35 : mftonew(GEN mf, GEN F)
    4309             : {
    4310          35 :   pari_sp av = avma;
    4311             :   GEN ES;
    4312             :   long s;
    4313          35 :   mf = checkMF(mf);
    4314          35 :   s = MF_get_space(mf);
    4315          35 :   if (s != mf_FULL && s != mf_CUSP)
    4316           7 :     pari_err_TYPE("mftonew [not a full or cuspidal space]", mf);
    4317          28 :   ES = mftobasisES(mf,F);
    4318          21 :   if (!gequal0(gel(ES,1)))
    4319           0 :     pari_err_TYPE("mftonew [not a cuspidal form]", F);
    4320          21 :   F = gel(ES,2);
    4321          21 :   return gerepilecopy(av, mftonew_i(mf,F, NULL));
    4322             : }
    4323             : 
    4324             : static GEN mfeisenstein_i(long k, GEN CHI1, GEN CHI2);
    4325             : 
    4326             : /* mfinit(F * Theta) */
    4327             : static GEN
    4328          70 : mf2init(GEN mf)
    4329             : {
    4330          70 :   GEN CHI = MF_get_CHI(mf), gk = gadd(MF_get_gk(mf), ghalf);
    4331          70 :   long N = MF_get_N(mf);
    4332          70 :   return mfinit_Nkchi(N, itou(gk), mfchiadjust(CHI, gk, N), mf_FULL, 0);
    4333             : }
    4334             : 
    4335             : static long
    4336         329 : mfvec_first_cusp(GEN v)
    4337             : {
    4338         329 :   long i, l = lg(v);
    4339         798 :   for (i = 1; i < l; i++)
    4340             :   {
    4341         721 :     GEN F = gel(v,i);
    4342         721 :     long t = mf_get_type(F);
    4343         721 :     if (t == t_MF_BD) { F = gel(F,2); t = mf_get_type(F); }
    4344         721 :     if (t == t_MF_HECKE) { F = gel(F,3); t = mf_get_type(F); }
    4345         721 :     if (t == t_MF_NEWTRACE) break;
    4346             :   }
    4347         329 :   return i;
    4348             : }
    4349             : /* vF a vector of mf F of type DIV(LINEAR(BAS,L), f) in (lcm) level N,
    4350             :  * F[2]=LINEAR(BAS,L), F[2][2]=BAS=fixed basis (Eisentstein or bhn type),
    4351             :  * F[2][3]=L, F[3]=f; mfvectomat(vF, n) */
    4352             : static GEN
    4353         336 : mflineardivtomat(long N, GEN vF, long n)
    4354             : {
    4355             :   GEN F, M, f, fc, V, ME, B, a0;
    4356         336 :   long lM, lF = lg(vF), i, j;
    4357             : 
    4358         336 :   if (lF == 1) return cgetg(1,t_MAT);
    4359         329 :   F = gel(vF,1);
    4360         329 :   M = gmael(F,2,2); /* BAS */
    4361         329 :   lM = lg(M);
    4362         329 :   i = mfvec_first_cusp(M);
    4363         329 :   if (i == 1) ME = NULL;
    4364             :   else
    4365             :   { /* BAS starts by Eisenstein */
    4366         105 :     ME = mfvectomat(vecslice(M,1,i-1), n, 1);
    4367         105 :     M = vecslice(M, i,lM-1);
    4368             :   }
    4369         329 :   M = bhnmat_extend_nocache(NULL, N, n, 1, M);
    4370         329 :   if (ME) M = shallowconcat(ME,M);
    4371             :   /* M = mfcoefs of BAS */
    4372         329 :   f = mfcoefsser(gel(F,3),n);
    4373         329 :   a0 = polcoef_i(f, 0, -1);
    4374         329 :   if (gequal0(a0) || gequal1(a0))
    4375         266 :     a0 = NULL;
    4376             :   else
    4377          63 :     f = gdiv(ser_unscale(f, a0), a0);
    4378         329 :   fc = ginv(f);
    4379         329 :   V = cgetg(lM, t_VEC);
    4380        3402 :   for (i = 1; i < lM; i++)
    4381             :   {
    4382        3073 :     pari_sp av = avma;
    4383        3073 :     GEN LISer = RgV_to_ser_full(gel(M,i)), f;
    4384        3073 :     if (a0) LISer = gdiv(ser_unscale(LISer, a0), a0);
    4385        3073 :     f = gmul(LISer, fc);
    4386        3073 :     if (a0) f = ser_unscale(f, ginv(a0));
    4387        3073 :     f = sertocol(f); setlg(f, n+2);
    4388        3073 :     gel(V,i) = gerepileupto(av,f);
    4389             :   }
    4390         329 :   B = cgetg(lF, t_MAT);
    4391        1568 :   for (j = 1; j < lF; j++)
    4392             :   {
    4393        1239 :     pari_sp av = avma;
    4394        1239 :     GEN S = gen_0, coe;
    4395        1239 :     F = gel(vF, j); /* t_MF_DIV */
    4396        1239 :     coe = gdiv(gmael(F,2,3), gmael(F,2,4));
    4397       17108 :     for (i = 1; i < lM; i++)
    4398             :     {
    4399       15869 :       GEN co = gel(coe, i);
    4400       15869 :       if (!gequal0(co)) S = gadd(S, gmul(co, gel(V, i)));
    4401             :     }
    4402        1239 :     gel(B,j) = gerepileupto(av, S);
    4403             :   }
    4404         329 :   return B;
    4405             : }
    4406             : 
    4407             : static GEN
    4408         105 : mfheckemat_mfcoefs(GEN mf, GEN B, GEN DATA)
    4409             : {
    4410         105 :   GEN Mindex = MF_get_Mindex(mf), Minv = MF_get_Minv(mf);
    4411         105 :   long j, l = lg(B), sb = mfsturm_mf(mf)-1;
    4412         105 :   GEN b = MF_get_basis(mf), Q = cgetg(l, t_VEC);
    4413         371 :   for (j = 1; j < l; j++)
    4414             :   {
    4415         266 :     GEN v = hecke_i(sb, 1, gel(B,j), gel(b,j), DATA); /* Tn b[j] */
    4416         266 :     settyp(v,t_COL); gel(Q,j) = vecpermute(v, Mindex);
    4417             :   }
    4418         105 :   return Minv_RgM_mul(Minv,Q);
    4419             : }
    4420             : /* T_p^2, p prime, 1/2-integral weight; B = mfcoefs(mf,sb*p^2,1) or (mf,sb,p^2)
    4421             :  * if p|N */
    4422             : static GEN
    4423           7 : mfheckemat_mfcoefs_p2(GEN mf, long p, GEN B)
    4424             : {
    4425           7 :   pari_sp av = avma;
    4426           7 :   GEN DATA = heckef2_data(MF_get_N(mf), p*p);
    4427           7 :   return gerepileupto(av, mfheckemat_mfcoefs(mf, B, DATA));
    4428             : }
    4429             : /* convert Mindex from row-index to mfcoef indexation: a(n) is stored in
    4430             :  * mfcoefs()[n+1], so subtract 1 from all indices */
    4431             : static GEN
    4432          49 : Mindex_as_coef(GEN mf)
    4433             : {
    4434          49 :   GEN v, Mindex = MF_get_Mindex(mf);
    4435          49 :   long i, l = lg(Mindex);
    4436          49 :   v = cgetg(l, t_VECSMALL);
    4437          49 :   for (i = 1; i < l; i++) v[i] = Mindex[i]-1;
    4438          49 :   return v;
    4439             : }
    4440             : /* T_p, p prime; B = mfcoefs(mf,sb*p,1) or (mf,sb,p) if p|N; integral weight */
    4441             : static GEN
    4442          35 : mfheckemat_mfcoefs_p(GEN mf, long p, GEN B)
    4443             : {
    4444          35 :   pari_sp av = avma;
    4445          35 :   GEN vm, Q, C, Minv = MF_get_Minv(mf);
    4446          35 :   long lm, k, i, j, l = lg(B), N = MF_get_N(mf);
    4447             : 
    4448          35 :   if (N % p == 0) return Minv_RgM_mul(Minv, rowpermute(B, MF_get_Mindex(mf)));
    4449          21 :   k = MF_get_k(mf);
    4450          21 :   C = gmul(mfchareval_i(MF_get_CHI(mf), p), powuu(p, k-1));
    4451          21 :   vm = Mindex_as_coef(mf); lm = lg(vm);
    4452          21 :   Q = cgetg(l, t_MAT);
    4453          21 :   for (j = 1; j < l; j++) gel(Q,j) = cgetg(lm, t_COL);
    4454         147 :   for (i = 1; i < lm; i++)
    4455             :   {
    4456         126 :     long m = vm[i], mp = m*p;
    4457         126 :     GEN Cm = (m % p) == 0? C : NULL;
    4458        1260 :     for (j = 1; j < l; j++)
    4459             :     {
    4460        1134 :       GEN S = gel(B,j), s = gel(S, mp + 1);
    4461        1134 :       if (Cm) s = gadd(s, gmul(C, gel(S, m/p + 1)));
    4462        1134 :       gcoeff(Q, i, j) = s;
    4463             :     }
    4464             :   }
    4465          21 :   return gerepileupto(av, Minv_RgM_mul(Minv,Q));
    4466             : }
    4467             : /* Matrix of T(p), p prime, dim(mf) > 0 and integral weight */
    4468             : static GEN
    4469          98 : mfheckemat_p(GEN mf, long p)
    4470             : {
    4471          98 :   pari_sp av = avma;
    4472          98 :   long N = MF_get_N(mf), sb = mfsturm_mf(mf)-1;
    4473          98 :   GEN B = (N % p)? mfcoefs_mf(mf, sb * p, 1): mfcoefs_mf(mf, sb, p);
    4474          98 :   return gerepileupto(av, mfheckemat_mfcoefs(mf, B, hecke_data(N,p)));
    4475             : }
    4476             : 
    4477             : /* mf_NEW != (0), weight > 1, p prime. Use
    4478             :  * T(p) T(j) = T(j*p) + p^{k-1} \chi(p) 1_{p | j, p \nmid N} T(j/p) */
    4479             : static GEN
    4480         833 : mfnewmathecke_p(GEN mf, long p)
    4481             : {
    4482         833 :   pari_sp av = avma;
    4483         833 :   GEN tf, vj = MFnew_get_vj(mf), CHI = MF_get_CHI(mf);
    4484         833 :   GEN Mindex = MF_get_Mindex(mf), Minv = MF_get_Minv(mf);
    4485         833 :   long N = MF_get_N(mf), k = MF_get_k(mf);
    4486         833 :   long i, j, lvj = lg(vj), lim = vj[lvj-1] * p;
    4487         833 :   GEN M, perm, V, need = zero_zv(lim);
    4488         833 :   GEN C = (N % p)? gmul(mfchareval_i(CHI,p), powuu(p,k-1)): NULL;
    4489         833 :   tf = mftraceform_new(N, k, CHI);
    4490        3423 :   for (i = 1; i < lvj; i++)
    4491             :   {
    4492        2590 :     j = vj[i]; need[j*p] = 1;
    4493        2590 :     if (N % p && j % p == 0) need[j/p] = 1;
    4494             :   }
    4495         833 :   perm = zero_zv(lim);
    4496         833 :   V = cgetg(lim+1, t_VEC);
    4497       11389 :   for (i = j = 1; i <= lim; i++)
    4498       10556 :     if (need[i]) { gel(V,j) = mfhecke_i(i, N, tf); perm[i] = j; j++; }
    4499         833 :   setlg(V, j);
    4500         833 :   V = bhnmat_extend_nocache(NULL, N, mfsturm_mf(mf)-1, 1, V);
    4501         833 :   V = rowpermute(V, Mindex); /* V[perm[i]] = coeffs(T_i newtrace) */
    4502         833 :   M = cgetg(lvj, t_MAT);
    4503        3423 :   for (i = 1; i < lvj; i++)
    4504             :   {
    4505             :     GEN t;
    4506        2590 :     j = vj[i]; t = gel(V, perm[j*p]);
    4507        2590 :     if (C && j % p == 0) t = RgC_add(t, RgC_Rg_mul(gel(V, perm[j/p]),C));
    4508        2590 :     gel(M,i) = t;
    4509             :   }
    4510         833 :   return gerepileupto(av, Minv_RgM_mul(Minv, M));
    4511             : }
    4512             : 
    4513             : GEN
    4514          77 : mfheckemat(GEN mf, GEN vn)
    4515             : {
    4516          77 :   pari_sp av = avma;
    4517          77 :   long lv, lvP, i, N, dim, nk, dk, p, sb, flint = (typ(vn)==t_INT);
    4518             :   GEN CHI, res, vT, FA, B, vP;
    4519             : 
    4520          77 :   mf = checkMF(mf);
    4521          77 :   if (typ(vn) != t_VECSMALL) vn = gtovecsmall(vn);
    4522          77 :   N = MF_get_N(mf); CHI = MF_get_CHI(mf); Qtoss(MF_get_gk(mf), &nk, &dk);
    4523          77 :   dim = MF_get_dim(mf);
    4524          77 :   lv = lg(vn);
    4525          77 :   res = cgetg(lv, t_VEC);
    4526          77 :   FA = cgetg(lv, t_VEC);
    4527          77 :   vP = cgetg(lv, t_VEC);
    4528          77 :   vT = const_vec(vecsmall_max(vn), NULL);
    4529         182 :   for (i = 1; i < lv; i++)
    4530             :   {
    4531         105 :     ulong n = (ulong)labs(vn[i]);
    4532             :     GEN fa;
    4533         105 :     if (!n) pari_err_TYPE("mfheckemat", vn);
    4534         105 :     if (dk == 1 || uissquareall(n, &n)) fa = myfactoru(n);
    4535           0 :     else { n = 0; fa = myfactoru(1); } /* dummy: T_{vn[i]} = 0 */
    4536         105 :     vn[i] = n;
    4537         105 :     gel(FA,i) = fa;
    4538         105 :     gel(vP,i) = gel(fa,1);
    4539             :   }
    4540          77 :   vP = shallowconcat1(vP); vecsmall_sort(vP);
    4541          77 :   vP = vecsmall_uniq_sorted(vP); /* all primes occurring in vn */
    4542          77 :   lvP = lg(vP); if (lvP == 1) goto END;
    4543          56 :   p = vP[lvP-1];
    4544          56 :   sb = mfsturm_mf(mf)-1;
    4545          56 :   if (dk == 1 && nk != 1 && MF_get_space(mf) == mf_NEW)
    4546          21 :     B = NULL; /* special purpose mfnewmathecke_p is faster */
    4547          35 :   else if (lvP == 2 && N % p == 0)
    4548          21 :     B = mfcoefs_mf(mf, sb, dk==2? p*p: p); /* single prime | N, can optimize */
    4549             :   else
    4550          14 :     B = mfcoefs_mf(mf, sb * (dk==2? p*p: p), 1); /* general initialization */
    4551         126 :   for (i = 1; i < lvP; i++)
    4552             :   {
    4553          70 :     long j, l, q, e = 1;
    4554             :     GEN C, Tp, u1, u0;
    4555          70 :     p = vP[i];
    4556          70 :     for (j = 1; j < lv; j++) e = maxss(e, z_lval(vn[j], p));
    4557          70 :     if (!B)
    4558          28 :       Tp = mfnewmathecke_p(mf, p);
    4559          42 :     else if (dk == 2)
    4560           7 :       Tp = mfheckemat_mfcoefs_p2(mf,p, (lvP==2||N%p)? B: matdeflate(sb,p*p,B));
    4561             :     else
    4562          35 :       Tp = mfheckemat_mfcoefs_p(mf, p, (lvP==2||N%p)? B: matdeflate(sb,p,B));
    4563          70 :     gel(vT, p) = Tp;
    4564          70 :     if (e == 1) continue;
    4565          14 :     u0 = gen_1;
    4566          14 :     if (dk == 2)
    4567             :     {
    4568           0 :       C = N % p? gmul(mfchareval_i(CHI,p*p), powuu(p, nk-2)): NULL;
    4569           0 :       if (e == 2) u0 = sstoQ(p+1,p); /* special case T_{p^4} */
    4570             :     }
    4571             :     else
    4572          14 :       C = N % p? gmul(mfchareval_i(CHI,p),   powuu(p, nk-1)): NULL;
    4573          28 :     for (u1=Tp, q=p, l=2; l <= e; l++)
    4574             :     { /* u0 = T_{p^{l-2}}, u1 = T_{p^{l-1}} for l > 2 */
    4575          14 :       GEN v = gmul(Tp, u1);
    4576          14 :       if (C) v = gsub(v, gmul(C, u0));
    4577             :       /* q = p^l, vT[q] = T_q for k integer else T_{q^2} */
    4578          14 :       q *= p; u0 = u1; gel(vT, q) = u1 = v;
    4579             :     }
    4580             :   }
    4581             : END:
    4582             :   /* vT[p^e] = T_{p^e} for all p^e occurring below */
    4583         182 :   for (i = 1; i < lv; i++)
    4584             :   {
    4585         105 :     long n = vn[i], j, lP;
    4586             :     GEN fa, P, E, M;
    4587         105 :     if (n == 0) { gel(res,i) = zeromat(dim,dim); continue; }
    4588         105 :     if (n == 1) { gel(res,i) = matid(dim); continue; }
    4589          77 :     fa = gel(FA,i);
    4590          77 :     P = gel(fa,1); lP = lg(P);
    4591          77 :     E = gel(fa,2); M = gel(vT, upowuu(P[1], E[1]));
    4592          77 :     for (j = 2; j < lP; j++) M = RgM_mul(M, gel(vT, upowuu(P[j], E[j])));
    4593          77 :     gel(res,i) = M;
    4594             :   }
    4595          77 :   if (flint) res = gel(res,1);
    4596          77 :   return gerepilecopy(av, res);
    4597             : }
    4598             : 
    4599             : 
    4600             : /* f = \sum_i v[i] T_listj[i] (Trace Form) attached to v; replace by f/a_1(f) */
    4601             : static GEN
    4602        1162 : mf_normalize(GEN mf, GEN v)
    4603             : {
    4604        1162 :   GEN c, dc = NULL, M = MF_get_M(mf), Mindex = MF_get_Mindex(mf);
    4605        1162 :   v = Q_primpart(v);
    4606        1162 :   c = RgMrow_RgC_mul(M, v, 2); /* a_1(f) */
    4607        1162 :   if (gequal1(c)) return v;
    4608         707 :   if (typ(c) == t_POL) c = gmodulo(c, mfcharpol(MF_get_CHI(mf)));
    4609         707 :   if (typ(c) == t_POLMOD && varn(gel(c,1)) == 1 && degpol(gel(c,1)) >= 40
    4610           7 :                          && Mindex[1] == 2
    4611           7 :                          && mfcharorder(MF_get_CHI(mf)) <= 2)
    4612           7 :   { /* normalize using expansion at infinity (small coefficients) */
    4613           7 :     GEN w, P = gel(c,1), a1 = gel(c,2);
    4614           7 :     long i, l = lg(Mindex);
    4615           7 :     w = cgetg(l, t_COL);
    4616           7 :     gel(w,1) = gen_1;
    4617         280 :     for (i = 2; i < l; i++)
    4618             :     {
    4619         273 :       c = liftpol_shallow(RgMrow_RgC_mul(M, v, Mindex[i]));
    4620         273 :       gel(w,i) = QXQ_div_ratlift(c, a1, P);
    4621             :     }
    4622             :     /* w = expansion at oo of normalized form */
    4623           7 :     v = Minv_RgC_mul(MF_get_Minv(mf), Q_remove_denom(w, &dc));
    4624           7 :     v = gmodulo(v, P); /* back to mfbasis coefficients */
    4625             :   }
    4626             :   else
    4627             :   {
    4628         700 :     c = ginv(c);
    4629         700 :     if (typ(c) == t_POLMOD) c = Q_remove_denom(c, &dc);
    4630         700 :     v = RgC_Rg_mul(v, c);
    4631             :   }
    4632         707 :   if (dc) v = RgC_Rg_div(v, dc);
    4633         707 :   return v;
    4634             : }
    4635             : static void
    4636         238 : pol_red(GEN NF, GEN *pP, GEN *pa, long flag)
    4637             : {
    4638         238 :   GEN dP, a, P = *pP;
    4639         238 :   long d = degpol(P);
    4640             : 
    4641         238 :   *pa = a = pol_x(varn(P));
    4642         238 :   if (d > 30) return;
    4643             : 
    4644         231 :   dP = RgX_disc(P);
    4645         231 :   if (typ(dP) != t_INT)
    4646          35 :   { dP = gnorm(dP); if (typ(dP) != t_INT) pari_err_BUG("mfnewsplit"); }
    4647         231 :   if (d == 2 || expi(dP) < 62)
    4648             :   {
    4649         217 :     if (expi(dP) < 31)
    4650         217 :       P = NF? rnfpolredabs(NF, P,flag): polredabs0(P,flag);
    4651             :     else
    4652           0 :       P = NF? rnfpolredbest(NF,P,flag): polredbest(P,flag);
    4653         217 :     if (flag)
    4654             :     {
    4655         203 :       a = gel(P,2); if (typ(a) == t_POLMOD) a = gel(a,2);
    4656         203 :       P = gel(P,1);
    4657             :     }
    4658             :   }
    4659         231 :   *pP = P;
    4660         231 :   *pa = a;
    4661             : }
    4662             : 
    4663             : /* Diagonalize and normalize. See mfsplit for meaning of flag. */
    4664             : static GEN
    4665         847 : mfspclean(GEN mf, GEN mf0, GEN NF, long ord, GEN simplesp, long flag)
    4666             : {
    4667         847 :   const long vz = 1;
    4668         847 :   long i, l = lg(simplesp);
    4669         847 :   GEN res = cgetg(l, t_MAT), pols = cgetg(l, t_VEC);
    4670         847 :   GEN zeros = (mf == mf0)? NULL: zerocol(MF_get_dim(mf) - MF_get_dim(mf0));
    4671        2023 :   for (i = 1; i < l; i++)
    4672             :   {
    4673        1176 :     GEN ATP = gel(simplesp, i), A = gel(ATP,1), P = gel(ATP,3);
    4674        1176 :     long d = degpol(P);
    4675        1176 :     GEN a, v = (flag && d > flag)? NULL: gel(A,1);
    4676        1176 :     if (d == 1) P = pol_x(vz);
    4677             :     else
    4678             :     {
    4679         238 :       pol_red(NF, &P, &a, !!v);
    4680         238 :       if (v)
    4681             :       { /* Mod(a,P) root of charpoly(T), K*gpowers(a) = eigenvector of T */
    4682         224 :         GEN K, den, M = cgetg(d+1, t_MAT), T = gel(ATP,2);
    4683             :         long j;
    4684         224 :         T = shallowtrans(T);
    4685         224 :         gel(M,1) = vec_ei(d,1); /* basis of cyclic vectors */
    4686         224 :         for (j = 2; j <= d; j++) gel(M,j) = RgM_RgC_mul(T, gel(M,j-1));
    4687         224 :         M = Q_primpart(M);
    4688         273 :         K = NF? ZabM_inv(liftpol_shallow(M), nf_get_pol(NF), ord, &den)
    4689         273 :               : ZM_inv(M,&den);
    4690         224 :         K = shallowtrans(K);
    4691         224 :         v = gequalX(a)? pol_x_powers(d, vz): RgXQ_powers(a, d-1, P);
    4692         224 :         v = gmodulo(RgM_RgC_mul(A, RgM_RgC_mul(K,v)), P);
    4693             :       }
    4694             :     }
    4695        1176 :     if (v)
    4696             :     {
    4697        1162 :       v = mf_normalize(mf0, v); if (zeros) v = shallowconcat(zeros,v);
    4698        1162 :       gel(res,i) = v; if (flag) setlg(res,i+1);
    4699             :     }
    4700        1176 :     gel(pols,i) = P;
    4701             :   }
    4702         847 :   return mkvec2(res, pols);
    4703             : }
    4704             : 
    4705             : /* return v = v_{X-r}(P), and set Z = P / (X-r)^v */
    4706             : static long
    4707          63 : RgX_valrem_root(GEN P, GEN r, GEN *Z)
    4708             : {
    4709             :   long v;
    4710         126 :   for (v = 0; degpol(P); v++)
    4711             :   {
    4712         126 :     GEN t, Q = RgX_div_by_X_x(P, r, &t);
    4713         126 :     if (!gequal0(t)) break;
    4714          63 :     P = Q;
    4715             :   }
    4716          63 :   *Z = P; return v;
    4717             : }
    4718             : static GEN
    4719         945 : mynffactor(GEN NF, GEN P, long dimlim)
    4720             : {
    4721             :   long i, l, v;
    4722             :   GEN R, E;
    4723         945 :   if (dimlim != 1)
    4724             :   {
    4725         392 :     R = NF? nffactor(NF, P): QX_factor(P);
    4726         392 :     if (!dimlim) return R;
    4727          21 :     E = gel(R,2);
    4728          21 :     R = gel(R,1); l = lg(R);
    4729          98 :     for (i = 1; i < l; i++)
    4730          91 :       if (degpol(gel(R,i)) > dimlim) break;
    4731          21 :     if (i == 1) return NULL;
    4732          21 :     setlg(E,i);
    4733          21 :     setlg(R,i); return mkmat2(R, E);
    4734             :   }
    4735             :   /* dimlim = 1 */
    4736         553 :   R = nfroots(NF, P); l = lg(R);
    4737         553 :   if (l == 1) return NULL;
    4738         490 :   v = varn(P);
    4739         490 :   settyp(R, t_COL);
    4740         490 :   if (degpol(P) == l-1)
    4741         441 :     E = const_vec(l-1, gen_1);
    4742             :   else
    4743             :   {
    4744          49 :     E = cgetg(l, t_COL);
    4745          49 :     for (i = 1; i < l; i++) gel(E,i) = utoi(RgX_valrem_root(P, gel(R,i), &P));
    4746             :   }
    4747         490 :   R = deg1_from_roots(R, v);
    4748         490 :   return mkmat2(R, E);
    4749             : }
    4750             : 
    4751             : /* Let K be a number field attached to NF (Q if NF = NULL). A K-vector
    4752             :  * space of dimension d > 0 is given by a t_MAT A (n x d, full column rank)
    4753             :  * giving a K-basis, X a section (d x n: left pseudo-inverse of A). Return a
    4754             :  * pair (T, fa), where T is an element of the Hecke algebra (a sum of Tp taken
    4755             :  * from vector vTp) acting on A (a d x d t_MAT) and fa is the factorization of
    4756             :  * its characteristic polynomial, limited to factors of degree <= dimlim if
    4757             :  * dimlim != 0 (return NULL if there are no factors of degree <= dimlim) */
    4758             : static GEN
    4759         938 : findbestsplit(GEN NF, GEN vTp, GEN A, GEN X, long dimlim, long vz)
    4760             : {
    4761         938 :   GEN T = NULL, Tkeep = NULL, fakeep = NULL;
    4762         938 :   long lmax = 0, i, lT = lg(vTp);
    4763        2030 :   for (i = 1; i < lT; i++)
    4764             :   {
    4765        1015 :     GEN D, P, E, fa, TpA = gel(vTp,i);
    4766             :     long l;
    4767        1890 :     if (typ(TpA) == t_INT) break;
    4768         945 :     if (lg(TpA) > lg(A)) TpA = RgM_mul(X, RgM_mul(TpA, A)); /* Tp | A */
    4769         945 :     T = T ? RgM_add(T, TpA) : TpA;
    4770         945 :     if (!NF) { P = QM_charpoly_ZX(T); setvarn(P, vz); }
    4771             :     else
    4772             :     {
    4773         112 :       P = charpoly(Q_remove_denom(T, &D), vz);
    4774         112 :       if (D) P = gdiv(RgX_unscale(P, D), powiu(D, degpol(P)));
    4775             :     }
    4776         945 :     fa = mynffactor(NF, P, dimlim);
    4777         945 :     if (!fa) return NULL;
    4778         882 :     E = gel(fa, 2);
    4779             :     /* characteristic polynomial is separable ? */
    4780         882 :     if (isint1(vecmax(E))) { Tkeep = T; fakeep = fa; break; }
    4781          77 :     l = lg(E);
    4782             :     /* characteristic polynomial has more factors than before ? */
    4783          77 :     if (l > lmax) { lmax = l; Tkeep = T; fakeep = fa; }
    4784             :   }
    4785         875 :   return mkvec2(Tkeep, fakeep);
    4786             : }
    4787             : 
    4788             : static GEN
    4789         147 : nfcontent(GEN nf, GEN v)
    4790             : {
    4791         147 :   long i, l = lg(v);
    4792         147 :   GEN c = gel(v,1);
    4793         147 :   for (i = 2; i < l; i++) c = idealadd(nf, c, gel(v,i));
    4794         147 :   if (typ(c) == t_MAT && gequal1(gcoeff(c,1,1))) c = gen_1;
    4795         147 :   return c;
    4796             : }
    4797             : static GEN
    4798         224 : nf_primpart(GEN nf, GEN B)
    4799             : {
    4800         224 :   switch(typ(B))
    4801             :   {
    4802             :     case t_COL:
    4803             :     {
    4804         147 :       GEN A = matalgtobasis(nf, B), c = nfcontent(nf, A);
    4805         147 :       if (typ(c) == t_INT) return B;
    4806          14 :       c = idealred_elt(nf,c);
    4807          14 :       A = Q_primpart( nfC_nf_mul(nf, A, Q_primpart(nfinv(nf,c))) );
    4808          14 :       A = liftpol_shallow( matbasistoalg(nf, A) );
    4809          14 :       if (gexpo(A) > gexpo(B)) A = B;
    4810          14 :       return A;
    4811             :     }
    4812             :     case t_MAT:
    4813             :     {
    4814             :       long i, l;
    4815          77 :       GEN A = cgetg_copy(B, &l);
    4816          77 :       for (i = 1; i < l; i++) gel(A,i) = nf_primpart(nf, gel(B,i));
    4817          77 :       return A;
    4818             :     }
    4819             :     default:
    4820           0 :       pari_err_TYPE("nf_primpart", B);
    4821             :       return NULL; /*LCOV_EXCL_LINE*/
    4822             :   }
    4823             : }
    4824             : 
    4825             : /* rotate entries of v to accomodate new entry 'x' (push out oldest entry) */
    4826             : static void
    4827         903 : vecpush(GEN v, GEN x)
    4828             : {
    4829             :   long i;
    4830         903 :   for (i = lg(v)-1; i > 1; i--) gel(v,i) = gel(v,i-1);
    4831         903 :   gel(v,1) = x;
    4832         903 : }
    4833             : 
    4834             : /* sort t_VEC of vector spaces by increasing dimension */
    4835             : static GEN
    4836         847 : sort_by_dim(GEN v)
    4837             : {
    4838         847 :   long i, l = lg(v);
    4839         847 :   GEN D = cgetg(l, t_VECSMALL);
    4840         847 :   for (i = 1; i < l; i++) D[i] = lg(gmael(v,i,2));
    4841         847 :   return vecpermute(v , vecsmall_indexsort(D));
    4842             : }
    4843             : static GEN
    4844         847 : split_starting_space(GEN mf)
    4845             : {
    4846         847 :   long d = MF_get_dim(mf), d2;
    4847         847 :   GEN id = matid(d);
    4848         847 :   switch(MF_get_space(mf))
    4849             :   {
    4850             :     case mf_NEW:
    4851         840 :     case mf_CUSP: return mkvec2(id, id);
    4852             :   }
    4853           7 :   d2 = lg(MF_get_S(mf))-1;
    4854           7 :   return mkvec2(vecslice(id, d-d2+1,d),
    4855             :                 shallowconcat(zeromat(d2,d-d2),matid(d2)));
    4856             : }
    4857             : /* If dimlim > 0, keep only the dimension <= dimlim eigenspaces.
    4858             :  * See mfsplit for the meaning of flag. */
    4859             : static GEN
    4860        1239 : split_ii(GEN mf, long dimlim, long flag, long *pnewd)
    4861             : {
    4862             :   forprime_t iter;
    4863        1239 :   GEN CHI = MF_get_CHI(mf), empty = cgetg(1, t_VEC), mf0 = mf;
    4864             :   GEN NF, POLCYC, todosp, Tpbigvec, simplesp;
    4865        1239 :   long N = MF_get_N(mf), k = MF_get_k(mf);
    4866        1239 :   long ord, FC, NEWT, dimsimple = 0, newd = -1;
    4867        1239 :   const long NBH = 5, vz = 1;
    4868             :   ulong p;
    4869             : 
    4870        1239 :   switch(MF_get_space(mf))
    4871             :   {
    4872        1127 :     case mf_NEW: break;
    4873             :     case mf_CUSP:
    4874             :     case mf_FULL:
    4875         105 :       if (k > 1) { mf0 = mfinittonew(mf); break; }
    4876          98 :       newd = lg(MF_get_S(mf))-1 - mfolddim(N, k, CHI);
    4877          98 :       break;
    4878           7 :     default: pari_err_TYPE("mfsplit [space does not contain newspace]", mf);
    4879             :       return NULL; /*LCOV_EXCL_LINE*/
    4880             :   }
    4881        1232 :   if (newd < 0) newd = mf0? MF_get_dim(mf0): 0;
    4882        1232 :   *pnewd = newd;
    4883        1232 :   if (!newd) return mkvec2(cgetg(1, t_MAT), empty);
    4884             : 
    4885         847 :   NEWT = (k > 1 && MF_get_space(mf0) == mf_NEW);
    4886         847 :   todosp = mkvec( split_starting_space(mf0) );
    4887         847 :   simplesp = empty;
    4888         847 :   FC = mfcharconductor(CHI);
    4889         847 :   ord = mfcharorder_canon(CHI);
    4890         847 :   if (ord == 1) NF = POLCYC = NULL;
    4891             :   else
    4892             :   {
    4893          77 :     POLCYC = mfcharpol(CHI);
    4894          77 :     NF = nfinit(POLCYC,DEFAULTPREC);
    4895             :   }
    4896         847 :   Tpbigvec = zerovec(NBH);
    4897         847 :   u_forprime_init(&iter, 2, ULONG_MAX);
    4898         847 :   while (dimsimple < newd && (p = u_forprime_next(&iter)))
    4899             :   {
    4900             :     GEN nextsp;
    4901             :     long ind;
    4902        1127 :     if (N % (p*p) == 0 && N/p % FC == 0) continue; /* T_p = 0 in this case */
    4903         903 :     vecpush(Tpbigvec, NEWT? mfnewmathecke_p(mf0,p): mfheckemat_p(mf0,p));
    4904         903 :     nextsp = empty;
    4905        1841 :     for (ind = 1; ind < lg(todosp); ind++)
    4906             :     {
    4907         938 :       GEN tmp = gel(todosp, ind), fa, P, E, D, Tp, DTp;
    4908         938 :       GEN A = gel(tmp, 1);
    4909         938 :       GEN X = gel(tmp, 2);
    4910             :       long lP, i;
    4911         938 :       tmp = findbestsplit(NF, Tpbigvec, A, X, dimlim, vz);
    4912        1575 :       if (!tmp) continue; /* nothing there */
    4913         875 :       Tp = gel(tmp, 1);
    4914         875 :       fa = gel(tmp, 2);
    4915         875 :       P = gel(fa, 1);
    4916         875 :       E = gel(fa, 2); lP = lg(P);
    4917             :       /* lP > 1 */
    4918         875 :       if (DEBUGLEVEL) err_printf("Exponents = %Ps\n", E);
    4919         875 :       if (lP == 2)
    4920             :       {
    4921         616 :         GEN P1 = gel(P,1);
    4922         616 :         long e1 = itos(gel(E,1)), d1 = degpol(P1);
    4923         616 :         if (e1 * d1 == lg(Tp)-1)
    4924             :         {
    4925         574 :           if (e1 > 1) nextsp = vec_append(nextsp, mkvec2(A,X));
    4926             :           else
    4927             :           { /* simple module */
    4928         567 :             simplesp = vec_append(simplesp, mkvec3(A,Tp,P1));
    4929         567 :             dimsimple += d1;
    4930             :           }
    4931         574 :           continue;
    4932             :         }
    4933             :       }
    4934             :       /* Found splitting */
    4935         301 :       DTp = Q_remove_denom(Tp, &D);
    4936        1008 :       for (i = 1; i < lP; i++)
    4937             :       {
    4938         707 :         GEN Ai, Xi, dXi, AAi, v, y, Pi = gel(P,i);
    4939         707 :         Ai = RgX_RgM_eval(D? RgX_rescale(Pi,D): Pi, DTp);
    4940         707 :         Ai = QabM_ker(Ai, POLCYC, ord);
    4941         707 :         if (NF) Ai = nf_primpart(NF, Ai);
    4942             : 
    4943         707 :         AAi = RgM_mul(A, Ai);
    4944             :         /* gives section, works on nonsquare matrices */
    4945         707 :         Xi = QabM_pseudoinv(Ai, POLCYC, ord, &v, &dXi);
    4946         707 :         Xi = RgM_Rg_div(Xi, dXi);
    4947         707 :         y = gel(v,1);
    4948         707 :         if (isint1(gel(E,i)))
    4949             :         {
    4950         609 :           GEN Tpi = RgM_mul(Xi, RgM_mul(rowpermute(Tp,y), Ai));
    4951         609 :           simplesp = vec_append(simplesp, mkvec3(AAi, Tpi, Pi));
    4952         609 :           dimsimple += degpol(Pi);
    4953             :         }
    4954             :         else
    4955             :         {
    4956          98 :           Xi = RgM_mul(Xi, rowpermute(X,y));
    4957          98 :           nextsp = vec_append(nextsp, mkvec2(AAi, Xi));
    4958             :         }
    4959             :       }
    4960             :     }
    4961         903 :     todosp = nextsp; if (lg(todosp) == 1) break;
    4962             :   }
    4963         847 :   if (DEBUGLEVEL) err_printf("end split, need to clean\n");
    4964         847 :   return mfspclean(mf, mf0, NF, ord, sort_by_dim(simplesp), flag);
    4965             : }
    4966             : static GEN
    4967          14 : dim_filter(GEN v, long dim)
    4968             : {
    4969          14 :   GEN P = gel(v,2);
    4970          14 :   long j, l = lg(P);
    4971         112 :   for (j = 1; j < l; j++)
    4972         105 :     if (degpol(gel(P,j)) > dim)
    4973             :     {
    4974           7 :       v = mkvec2(vecslice(gel(v,1),1,j-1), vecslice(P,1,j-1));
    4975           7 :       break;
    4976             :     }
    4977          14 :   return v;
    4978             : }
    4979             : static long
    4980         119 : dim_sum(GEN v)
    4981             : {
    4982         119 :   GEN P = gel(v,2);
    4983         119 :   long j, l = lg(P), d = 0;
    4984         119 :   for (j = 1; j < l; j++) d += degpol(gel(P,j));
    4985         119 :   return d;
    4986             : }
    4987             : static GEN
    4988        1183 : split_i(GEN mf, long dimlim, long flag)
    4989        1183 : { long junk; return split_ii(mf, dimlim, flag, &junk); }
    4990             : /* mf is either already split or output by mfinit. Splitting is done only for
    4991             :  * newspace except in weight 1. If flag = 0 (default) split completely.
    4992             :  * If flag = d > 0, only give the Galois polynomials in degree > d
    4993             :  * Flag is ignored if dimlim = 1. */
    4994             : GEN
    4995          70 : mfsplit(GEN mf0, long dimlim, long flag)
    4996             : {
    4997          70 :   pari_sp av = avma;
    4998          70 :   GEN v, mf = checkMF_i(mf0);
    4999          70 :   if (!mf) pari_err_TYPE("mfsplit", mf0);
    5000          70 :   if ((v = obj_check(mf, MF_SPLIT)))
    5001          14 :   { if (dimlim) v = dim_filter(v, dimlim); }
    5002          56 :   else if (dimlim && (v = obj_check(mf, MF_SPLITN)))
    5003          21 :   { v = (itos(gel(v,1)) >= dimlim)? dim_filter(gel(v,2), dimlim): NULL; }
    5004          70 :   if (!v)
    5005             :   {
    5006             :     long newd;
    5007          56 :     v = split_ii(mf, dimlim, flag, &newd);
    5008          56 :     if (lg(v) == 1) obj_insert(mf, MF_SPLITN, mkvec2(utoi(dimlim), v));
    5009          56 :     else if (!flag)
    5010             :     {
    5011          42 :       if (dim_sum(v) == newd) obj_insert(mf, MF_SPLIT,v);
    5012          21 :       else obj_insert(mf, MF_SPLITN, mkvec2(utoi(dimlim), v));
    5013             :     }
    5014             :   }
    5015          70 :   return gerepilecopy(av, v);
    5016             : }
    5017             : static GEN
    5018         280 : split(GEN mf) { return split_i(mf,0,0); }
    5019             : GEN
    5020         406 : MF_get_newforms(GEN mf) { return gel(obj_checkbuild(mf,MF_SPLIT,&split),1); }
    5021             : GEN
    5022         385 : MF_get_fields(GEN mf) { return gel(obj_checkbuild(mf,MF_SPLIT,&split),2); }
    5023             : 
    5024             : /*************************************************************************/
    5025             : /*                     Modular forms of Weight 1                         */
    5026             : /*************************************************************************/
    5027             : /* S_1(G_0(N)), small N. Return 1 if definitely empty; return 0 if maybe
    5028             :  * non-empty  */
    5029             : static int
    5030       15932 : wt1empty(long N)
    5031             : {
    5032       15932 :   if (N <= 100) switch (N)
    5033             :   { /* non-empty [32/100] */
    5034             :     case 23: case 31: case 39: case 44: case 46:
    5035             :     case 47: case 52: case 55: case 56: case 57:
    5036             :     case 59: case 62: case 63: case 68: case 69:
    5037             :     case 71: case 72: case 76: case 77: case 78:
    5038             :     case 79: case 80: case 83: case 84: case 87:
    5039             :     case 88: case 92: case 93: case 94: case 95:
    5040        5425 :     case 99: case 100: return 0;
    5041        3437 :     default: return 1;
    5042             :   }
    5043        7070 :   if (N <= 600) switch(N)
    5044             :   { /* empty [111/500] */
    5045             :     case 101: case 102: case 105: case 106: case 109:
    5046             :     case 113: case 121: case 122: case 123: case 125:
    5047             :     case 130: case 134: case 137: case 146: case 149:
    5048             :     case 150: case 153: case 157: case 162: case 163:
    5049             :     case 169: case 170: case 173: case 178: case 181:
    5050             :     case 182: case 185: case 187: case 193: case 194:
    5051             :     case 197: case 202: case 205: case 210: case 218:
    5052             :     case 221: case 226: case 233: case 241: case 242:
    5053             :     case 245: case 246: case 250: case 257: case 265:
    5054             :     case 267: case 269: case 274: case 277: case 281:
    5055             :     case 289: case 293: case 298: case 305: case 306:
    5056             :     case 313: case 314: case 317: case 326: case 337:
    5057             :     case 338: case 346: case 349: case 353: case 361:
    5058             :     case 362: case 365: case 369: case 370: case 373:
    5059             :     case 374: case 377: case 386: case 389: case 394:
    5060             :     case 397: case 401: case 409: case 410: case 421:
    5061             :     case 425: case 427: case 433: case 442: case 449:
    5062             :     case 457: case 461: case 466: case 481: case 482:
    5063             :     case 485: case 490: case 493: case 509: case 514:
    5064             :     case 521: case 530: case 533: case 534: case 538:
    5065             :     case 541: case 545: case 554: case 557: case 562:
    5066             :     case 565: case 569: case 577: case 578: case 586:
    5067         336 :     case 593: return 1;
    5068        6720 :     default: return 0;
    5069             :   }
    5070          14 :   return 0;
    5071             : }
    5072             : 
    5073             : static GEN
    5074          28 : initwt1trace(GEN mf)
    5075             : {
    5076          28 :   GEN S = MF_get_S(mf), v, H;
    5077             :   long l, i;
    5078          28 :   if (lg(S) == 1) return mftrivial();
    5079          28 :   H = mfheckemat(mf, Mindex_as_coef(mf));
    5080          28 :   l = lg(H); v = cgetg(l, t_VEC);
    5081          28 :   for (i = 1; i < l; i++) gel(v,i) = gtrace(gel(H,i));
    5082          28 :   v = Minv_RgC_mul(MF_get_Minv(mf), v);
    5083          28 :   return mflineardiv_linear(S, v, 1);
    5084             : }
    5085             : static GEN
    5086          21 : initwt1newtrace(GEN mf)
    5087             : {
    5088          21 :   GEN v, D, S, Mindex, CHI = MF_get_CHI(mf);
    5089          21 :   long FC, lD, i, sb, N1, N2, lM, N = MF_get_N(mf);
    5090          21 :   CHI = mfchartoprimitive(CHI, &FC);
    5091          21 :   if (N % FC || mfcharparity(CHI) == 1) return mftrivial();
    5092          21 :   D = mydivisorsu(N/FC); lD = lg(D);
    5093          21 :   S = MF_get_S(mf);
    5094          21 :   if (lg(S) == 1) return mftrivial();
    5095          21 :   N2 = newd_params2(N);
    5096          21 :   N1 = N / N2;
    5097          21 :   Mindex = MF_get_Mindex(mf);
    5098          21 :   lM = lg(Mindex);
    5099          21 :   sb = Mindex[lM-1];
    5100          21 :   v = zerovec(sb+1);
    5101          42 :   for (i = 1; i < lD; i++)
    5102             :   {
    5103          21 :     long M = FC*D[i], j;
    5104          21 :     GEN tf = initwt1trace(M == N? mf: mfinit_Nkchi(M, 1, CHI, mf_CUSP, 0));
    5105             :     GEN listd, w;
    5106          21 :     if (mf_get_type(tf) == t_MF_CONST) continue;
    5107          21 :     w = mfcoefs_i(tf, sb, 1);
    5108          21 :     if (M == N) { v = gadd(v, w); continue; }
    5109           0 :     listd = mydivisorsu(u_ppo(ugcd(N/M, N1), FC));
    5110           0 :     for (j = 1; j < lg(listd); j++)
    5111             :     {
    5112           0 :       long d = listd[j], d2 = d*d; /* coprime to FC */
    5113           0 :       GEN dk = mfchareval_i(CHI, d);
    5114           0 :       long NMd = N/(M*d), m;
    5115           0 :       for (m = 1; m <= sb/d2; m++)
    5116             :       {
    5117           0 :         long be = mubeta2(NMd, m);
    5118           0 :         if (be)
    5119             :         {
    5120           0 :           GEN c = gmul(dk, gmulsg(be, gel(w, m+1)));
    5121           0 :           long n = m*d2;
    5122           0 :           gel(v, n+1) = gadd(gel(v, n+1), c);
    5123             :         }
    5124             :       }
    5125             :     }
    5126             :   }
    5127          21 :   if (gequal0(gel(v,2))) return mftrivial();
    5128          21 :   v = vecpermute(v,Mindex);
    5129          21 :   v = Minv_RgC_mul(MF_get_Minv(mf), v);
    5130          21 :   return mflineardiv_linear(S, v, 1);
    5131             : }
    5132             : 
    5133             : /* Matrix of T(p), p \nmid N */
    5134             : static GEN
    5135         119 : Tpmat(long p, long lim, GEN CHI)
    5136             : {
    5137         119 :   GEN M = zeromatcopy(lim, p*lim), chip = mfchareval_i(CHI, p); /* != 0 */
    5138             :   long i, j, pi, pj;
    5139         119 :   gcoeff(M, 1, 1) = gaddsg(1, chip);
    5140         119 :   for (i = 1, pi = p; i < lim; i++,  pi += p) gcoeff(M, i+1, pi+1) = gen_1;
    5141         119 :   for (j = 1, pj = p; pj < lim; j++, pj += p) gcoeff(M, pj+1, j+1) = chip;
    5142         119 :   return M;
    5143             : }
    5144             : 
    5145             : /* assume !wt1empty(N), in particular N>25 */
    5146             : /* Returns [[lim,p], mf (weight 2), p*lim x dim matrix] */
    5147             : static GEN
    5148        1722 : mfwt1_pre(long N)
    5149             : {
    5150        1722 :   GEN M, mf = mfinit_Nkchi(N, 2, mfchartrivial(), mf_CUSP, 0);
    5151             :   /*not empty for N>25*/
    5152             :   long p, lim;
    5153        1722 :   if (uisprime(N))
    5154             :   {
    5155         385 :     p = 2; /*N>25 is not 2 */
    5156         385 :     lim = ceilA1(N, 3);
    5157             :   }
    5158             :   else
    5159             :   {
    5160             :     forprime_t S;
    5161        1337 :     u_forprime_init(&S, 2, N);
    5162        1337 :     while ((p = u_forprime_next(&S)))
    5163        2422 :       if (N % p) break;
    5164        1337 :     lim = mfsturm_mf(mf) + 1;
    5165             :   }
    5166             :   /* p = smalllest prime not dividing N */
    5167        1722 :   M = bhnmat_extend_nocache(MF_get_M(mf), N, p*lim-1, 1, MF_get_S(mf));
    5168        1722 :   return mkvec3(mkvecsmall2(lim, p), mf, M);
    5169             : }
    5170             : 
    5171             : /* lg(A) > 1, E a t_POL */
    5172             : static GEN
    5173         833 : mfmatsermul(GEN A, GEN E)
    5174             : {
    5175         833 :   long j, l = lg(A), r = nbrows(A);
    5176         833 :   GEN M = cgetg(l, t_MAT);
    5177         833 :   E = RgXn_red_shallow(E, r+1);
    5178        8925 :   for (j = 1; j < l; j++)
    5179             :   {
    5180        8092 :     GEN c = RgV_to_RgX(gel(A,j), 0);
    5181        8092 :     gel(M, j) = RgX_to_RgC(RgXn_mul(c, E, r+1), r);
    5182             :   }
    5183         833 :   return M;
    5184             : }
    5185             : /* lg(Ap) > 1, Ep an Flxn */
    5186             : static GEN
    5187         441 : mfmatsermul_Fl(GEN Ap, GEN Ep, ulong p)
    5188             : {
    5189         441 :   long j, l = lg(Ap), r = nbrows(Ap);
    5190         441 :   GEN M = cgetg(l, t_MAT);
    5191        6258 :   for (j = 1; j < l; j++)
    5192             :   {
    5193        5817 :     GEN c = Flv_to_Flx(gel(Ap,j), 0);
    5194        5817 :     gel(M,j) = Flx_to_Flv(Flxn_mul(c, Ep, r+1, p), r);
    5195             :   }
    5196         441 :   return M;
    5197             : }
    5198             : 
    5199             : /* CHI mod F | N, return mfchar of modulus N.
    5200             :  * FIXME: wasteful, G should be precomputed  */
    5201             : static GEN
    5202       16212 : mfcharinduce(GEN CHI, long N)
    5203             : {
    5204             :   GEN G, chi;
    5205       16212 :   if (mfcharmodulus(CHI) == N) return CHI;
    5206        2877 :   G = znstar0(utoipos(N), 1);
    5207        2877 :   chi = zncharinduce(gel(CHI,1), gel(CHI,2), G);
    5208        2877 :   CHI = leafcopy(CHI);
    5209        2877 :   gel(CHI,1) = G;
    5210        2877 :   gel(CHI,2) = chi; return CHI;
    5211             : }
    5212             : 
    5213             : static GEN
    5214        3983 : gmfcharno(GEN CHI)
    5215             : {
    5216        3983 :   GEN G = gel(CHI,1), chi = gel(CHI,2);
    5217        3983 :   return mkintmod(znconreyexp(G, chi), znstar_get_N(G));
    5218             : }
    5219             : static long
    5220       12439 : mfcharno(GEN CHI)
    5221             : {
    5222       12439 :   GEN n = znconreyexp(gel(CHI,1), gel(CHI,2));
    5223       12439 :   return itou(n);
    5224             : }
    5225             : 
    5226             : /* return k such that minimal mfcharacter in Galois orbit of CHI is CHI^k */
    5227             : static long
    5228       11151 : mfconreyminimize(GEN CHI)
    5229             : {
    5230       11151 :   GEN G = gel(CHI,1), cyc, chi;
    5231       11151 :   cyc = ZV_to_zv(znstar_get_cyc(G));
    5232       11151 :   chi = ZV_to_zv(znconreychar(G, gel(CHI,2)));
    5233       11151 :   return zv_cyc_minimize(cyc, chi, coprimes_zv(mfcharorder(CHI)));
    5234             : }
    5235             : 
    5236             : /* find scalar c such that first non-0 entry of c*v is 1; return c*v
    5237             :  * (set c = NULL for 1) */
    5238             : static GEN
    5239        1561 : RgV_normalize(GEN v, GEN *pc)
    5240             : {
    5241        1561 :   long i, l = lg(v);
    5242        1561 :   *pc = NULL;
    5243        3668 :   for (i = 1; i < l; i++)
    5244             :   {
    5245        3668 :     GEN c = gel(v,i);
    5246        3668 :     if (!gequal0(c))
    5247             :     {
    5248        1561 :       if (gequal1(c)) { *pc = gen_1; return v; }
    5249         455 :       *pc = ginv(c); return RgV_Rg_mul(v, *pc);
    5250             :     }
    5251             :   }
    5252           0 :   return v;
    5253             : }
    5254             : /* ordchi != 2 mod 4 */
    5255             : static GEN
    5256        2282 : mftreatdihedral(GEN DIH, GEN POLCYC, long ordchi, long biglim, GEN *pS)
    5257             : {
    5258             :   GEN M, Minv, C;
    5259             :   long l, i;
    5260        2282 :   l = lg(DIH); if (l == 1) return NULL;
    5261        2282 :   if (!pS) return DIH;
    5262         728 :   C = cgetg(l, t_VEC);
    5263         728 :   M = cgetg(l, t_MAT);
    5264        2044 :   for (i = 1; i < l; i++)
    5265             :   {
    5266        1316 :     GEN c, v = mfcoefs_i(gel(DIH,i), biglim, 1);
    5267        1316 :     gel(M,i) = RgV_normalize(v, &c);
    5268        1316 :     gel(C,i) = Rg_col_ei(c, l-1, i);
    5269             :   }
    5270         728 :   Minv = gel(mfclean(M,POLCYC,ordchi,0),2);
    5271         728 :   M = RgM_Minv_mul(M, Minv);
    5272         728 :   C = RgM_Minv_mul(C, Minv);
    5273         728 :   *pS = vecmflinear(DIH, C);
    5274         728 :   return M;
    5275             : }
    5276             : 
    5277             : static GEN
    5278         112 : mfstabiter(GEN M, GEN A2, GEN E1inv, long lim, GEN P, long ordchi)
    5279             : {
    5280             :   GEN A, VC, con;
    5281         112 :   E1inv = primitive_part(E1inv, &con);
    5282         112 :   VC = con? ginv(con): gen_1;
    5283         112 :   A = mfmatsermul(A2, E1inv);
    5284             :   while(1)
    5285          56 :   {
    5286         168 :     GEN R = shallowconcat(RgM_mul(M,A), rowslice(A,1,lim));
    5287         168 :     GEN B = QabM_ker(R, P, ordchi);
    5288         168 :     long lA = lg(A), lB = lg(B);
    5289         168 :     if (lB == 1) return NULL;
    5290         168 :     if (lB == lA) return mkvec2(A, VC);
    5291          56 :     B = rowslice(B, 1, lA-1);
    5292          56 :     if (ordchi != 1) B = gmodulo(B, P);
    5293          56 :     A = Q_primitive_part(RgM_mul(A,B), &con);
    5294          56 :     VC = gmul(VC,B); /* first VC is a scalar, then a RgM */
    5295          56 :     if (con) VC = RgM_Rg_div(VC, con);
    5296             :   }
    5297             : }
    5298             : static long
    5299         112 : mfstabitermodp(GEN Mp, GEN Ap, long p, long lim)
    5300             : {
    5301         112 :   GEN VC = NULL;
    5302             :   while (1)
    5303           7 :   {
    5304         119 :     GEN Rp = shallowconcat(Flm_mul(Mp,Ap,p), rowslice(Ap,1,lim));
    5305         119 :     GEN Bp = Flm_ker(Rp, p);
    5306         119 :     long lA = lg(Ap), lB = lg(Bp);
    5307         119 :     if (lB == 1) return 0;
    5308         119 :     if (lB == lA) return lA-1;
    5309           7 :     Bp = rowslice(Bp, 1, lA-1);
    5310           7 :     Ap = Flm_mul(Ap, Bp, p);
    5311           7 :     VC = VC? Flm_mul(VC, Bp, p): Bp;
    5312             :   }
    5313             : }
    5314             : 
    5315             : static GEN
    5316         210 : mfintereis(GEN A, GEN M2, GEN y, GEN den, GEN E2, GEN P, long ordchi)
    5317             : {
    5318         210 :   GEN z, M1 = mfmatsermul(A,E2), M1den = is_pm1(den)? M1: RgM_Rg_mul(M1,den);
    5319         210 :   M2 = RgM_mul(M2, rowpermute(M1, y));
    5320         210 :   z = QabM_ker(RgM_sub(M2,M1den), P, ordchi);
    5321         210 :   if (ordchi != 1) z = gmodulo(z, P);
    5322         210 :   return mkvec2(RgM_mul(A,z), z);
    5323             : }
    5324             : static GEN
    5325         217 : mfintereismodp(GEN A, GEN M2, GEN E2, ulong p)
    5326             : {
    5327         217 :   GEN M1 = mfmatsermul_Fl(A, E2, p), z;
    5328         217 :   long j, lx = lg(A);
    5329         217 :   z = Flm_ker(shallowconcat(M1, M2), p);
    5330         217 :   for (j = lg(z) - 1; j; j--) setlg(z[j], lx);
    5331         217 :   return mkvec2(Flm_mul(A,z,p), z);
    5332             : }
    5333             : 
    5334             : static GEN
    5335         119 : mfcharinv_i(GEN CHI)
    5336             : {
    5337         119 :   GEN G = gel(CHI,1);
    5338         119 :   CHI = leafcopy(CHI); gel(CHI,2) =  zncharconj(G, gel(CHI,2)); return CHI;
    5339             : }
    5340             : 
    5341             : /* upper bound dim S_1(Gamma_0(N),chi) performing the linear algebra mod p */
    5342             : static long
    5343         119 : mfwt1dimmodp(GEN A, GEN ES, GEN M, long ordchi, long dih, long lim)
    5344             : {
    5345             :   GEN Ap, ApF, ES1p, VC;
    5346         119 :   ulong p, r = QabM_init(ordchi, &p);
    5347             : 
    5348         119 :   ApF = Ap = QabM_to_Flm(A, r, p);
    5349         119 :   VC = NULL;
    5350         119 :   ES1p = QabX_to_Flx(gel(ES,1), r, p);
    5351         119 :   if (lg(ES) >= 3)
    5352             :   {
    5353         112 :     GEN M2 = mfmatsermul_Fl(ApF, ES1p, p);
    5354         112 :     pari_sp av = avma;
    5355             :     long i;
    5356         322 :     for (i = 2; i < lg(ES); i++)
    5357             :     {
    5358         217 :       GEN ESip = QabX_to_Flx(gel(ES,i), r, p);
    5359         217 :       GEN C, ApC = mfintereismodp(Ap, M2, ESip, p);
    5360         217 :       Ap = gel(ApC,1);
    5361         217 :       if (lg(Ap)-1 == dih) return dih;
    5362         210 :       C = gel(ApC,2); VC = VC? Flm_mul(VC, C, p): C;
    5363         210 :       gerepileall(av, 2, &Ap,&VC);
    5364             :     }
    5365             :   }
    5366             :   /* intersection of Eisenstein series quotients non empty: use Schaeffer */
    5367         112 :   Ap = mfmatsermul_Fl(Ap, Flxn_inv(ES1p,nbrows(Ap),p), p);
    5368         112 :   return mfstabitermodp(QabM_to_Flm(M,r,p), Ap, p, lim);
    5369             : }
    5370             : 
    5371             : /* Compute the full S_1(\G_0(N),\chi). If pS is NULL, only the dimension
    5372             :  * dim, in the form of a vector having dim components. Otherwise output
    5373             :  * a basis: ptvf contains a pointer to the vector of forms, and the
    5374             :  * program returns the corresponding matrix of Fourier expansions.
    5375             :  * ptdimdih gives the dimension of the subspace generated by dihedral forms;
    5376             :  * TMP is from mfwt1_pre or NULL. */
    5377             : static GEN
    5378       10605 : mfwt1basis(long N, GEN CHI, GEN TMP, GEN *pS, long *ptdimdih)
    5379             : {
    5380             :   GEN ES, mf, A, M, Tp, tmp1, tmp2, den;
    5381             :   GEN S, ESA, VC, C, POLCYC, ES1, ES1INV, DIH, a0, a0i;
    5382             :   long plim, lim, biglim, i, p, dA, dimp, ordchi, dih;
    5383             : 
    5384       10605 :   if (ptdimdih) *ptdimdih = 0;
    5385       10605 :   if (pS) *pS = NULL;
    5386       10605 :   if (wt1empty(N) || mfcharparity(CHI) != -1) return NULL;
    5387       10423 :   ordchi = mfcharorder_canon(CHI);
    5388       10423 :   if (uisprime(N) && ordchi > 4) return NULL;
    5389       10395 :   if (!pS)
    5390             :   {
    5391        7014 :     dih = mfdihedralcuspdim(N, CHI);
    5392        7014 :     DIH = zerovec(dih);
    5393             :   }
    5394             :   else
    5395             :   {
    5396        3381 :     DIH = mfdihedralcusp(N, CHI);
    5397        3381 :     dih = lg(DIH) - 1;
    5398             :   }
    5399       10395 :   POLCYC = (ordchi == 1)? NULL: mfcharpol(CHI);
    5400       10395 :   if (ptdimdih) *ptdimdih = dih;
    5401       10395 :   biglim = mfsturmNk(N, 2);
    5402       10395 :   if (N <= 600) switch(N)
    5403             :   {
    5404             :     long m;
    5405             :     case 219: case 273: case 283: case 331: case 333: case 344: case 416:
    5406             :     case 438: case 468: case 491: case 504: case 546: case 553: case 563:
    5407             :     case 566: case 581: case 592:
    5408          14 :       break; /* one chi with both exotic and dihedral forms */
    5409             :     default: /* only dihedral forms */
    5410        9408 :       if (!dih) return NULL;
    5411             :       /* fall through */
    5412             :     case 124: case 133: case 148: case 171: case 201: case 209: case 224:
    5413             :     case 229: case 248: case 261: case 266: case 288: case 296: case 301:
    5414             :     case 309: case 325: case 342: case 371: case 372: case 380: case 399:
    5415             :     case 402: case 403: case 404: case 408: case 418: case 432: case 444:
    5416             :     case 448: case 451: case 453: case 458: case 496: case 497: case 513:
    5417             :     case 522: case 527: case 532: case 576: case 579:
    5418             :       /* no chi with both exotic and dihedral; one chi with exotic forms */
    5419        3108 :       if (dih) return mftreatdihedral(DIH, POLCYC, ordchi, biglim, pS);
    5420         833 :       CHI = mfcharinduce(CHI,N);
    5421         833 :       m = mfcharno(CHI);
    5422         833 :       if (N == 124 && (m != 67 && m != 87)) return NULL;
    5423         707 :       if (N == 133 && (m != 83 && m !=125)) return NULL;
    5424         413 :       if (N == 148 && (m !=105 && m !=117)) return NULL;
    5425         287 :       if (N == 171 && (m != 94 && m !=151)) return NULL;
    5426         287 :       if (N == 201 && (m != 29 && m !=104)) return NULL;
    5427         287 :       if (N == 209 && (m != 87 && m !=197)) return NULL;
    5428         287 :       if (N == 224 && (m != 95 && m !=191)) return NULL;
    5429         287 :       if (N == 229 && (m !=107 && m !=122)) return NULL;
    5430         287 :       if (N == 248 && (m != 87 && m !=191)) return NULL;
    5431         196 :       if (N == 261 && (m != 46 && m !=244)) return NULL;
    5432         196 :       if (N == 266 && (m != 83 && m !=125)) return NULL;
    5433         196 :       if (N == 288 && (m != 31 && m !=223)) return NULL;
    5434         196 :       if (N == 296 && (m !=105 && m !=265)) return NULL;
    5435             :   }
    5436         119 :   if (!TMP) TMP = mfwt1_pre(N);
    5437         119 :   tmp1= gel(TMP,1); lim = tmp1[1]; p = tmp1[2]; plim = p*lim;
    5438         119 :   mf  = gel(TMP,2);
    5439         119 :   A   = gel(TMP,3); /* p*lim x dim matrix */
    5440         119 :   S = MF_get_S(mf);
    5441         119 :   ESA = mfeisensteinbasis(N, 1, mfcharinv_i(CHI));
    5442         119 :   ES = RgM_to_RgXV(mfvectomat(ESA, plim+1, 1), 0);
    5443         119 :   ES1 = gel(ES,1); /* does not vanish at oo */
    5444         119 :   Tp = Tpmat(p, lim, CHI);
    5445         119 :   dimp = mfwt1dimmodp(A, ES, Tp, ordchi, dih, lim);
    5446         119 :   if (!dimp) return NULL;
    5447         119 :   if (dimp == dih) return mftreatdihedral(DIH, POLCYC, ordchi, biglim, pS);
    5448         112 :   VC = gen_1;
    5449         112 :   if (lg(ES) >= 3)
    5450             :   {
    5451             :     pari_sp btop;
    5452         105 :     long lim2 = (3*lim)/2 + 1;
    5453         105 :     GEN Ash = rowslice(A, 1, lim2), M2 = mfmatsermul(Ash, ES1);
    5454             :     GEN v, y, M2M2I, M2I;
    5455         105 :     M2I = QabM_pseudoinv(M2, POLCYC, ordchi, &v, &den);
    5456         105 :     y = gel(v,1);
    5457         105 :     M2M2I = RgM_mul(M2,M2I);
    5458         105 :     btop = avma;
    5459         315 :     for (i = 2; i < lg(ES); i++)
    5460             :     {
    5461         210 :       GEN APC = mfintereis(Ash, M2M2I, y, den, gel(ES,i), POLCYC,ordchi);
    5462         210 :       Ash = gel(APC,1); if (lg(Ash) == 1) return NULL;
    5463         210 :       VC = gmul(VC, gel(APC,2));
    5464         210 :       if (gc_needed(btop, 1))
    5465             :       {
    5466           6 :         if (DEBUGMEM > 1) pari_warn(warnmem,"mfwt1basis i = %ld", i);
    5467           6 :         gerepileall(btop, 2, &Ash, &VC);
    5468             :       }
    5469             :     }
    5470         105 :     A = RgM_mul(A, vecslice(VC,1, lg(Ash)-1));
    5471             :   }
    5472         112 :   a0 = gel(ES1,2); /* non-zero */
    5473         112 :   if (gequal1(a0)) a0 = a0i = NULL;
    5474             :   else
    5475             :   {
    5476         112 :     a0i = ginv(a0);
    5477         112 :     ES1 = RgX_Rg_mul(RgX_unscale(ES1,a0), a0i);
    5478             :   }
    5479         112 :   ES1INV = RgXn_inv(ES1, plim-1);
    5480         112 :   if (a0) ES1INV = RgX_Rg_mul(RgX_unscale(ES1INV, a0i), a0i);
    5481         112 :   tmp2 = mfstabiter(Tp, A, ES1INV, lim, POLCYC, ordchi);
    5482         112 :   if (!tmp2) return NULL;
    5483         112 :   A = gel(tmp2,1); dA = lg(A);
    5484         112 :   VC = gmul(VC, gel(tmp2,2));
    5485         112 :   C = cgetg(dA, t_VEC);
    5486         112 :   M = cgetg(dA, t_MAT);
    5487         357 :   for (i = 1; i < dA; i++)
    5488             :   {
    5489         245 :     GEN c, v = gel(A,i);
    5490         245 :     gel(M,i) = RgV_normalize(v, &c);
    5491         245 :     gel(C,i) = RgC_Rg_mul(gel(VC,i), c);
    5492             :   }
    5493         112 :   if (pS)
    5494             :   {
    5495          63 :     GEN Minv = gel(mfclean(M, POLCYC, ordchi, 0), 2);
    5496          63 :     M = RgM_Minv_mul(M, Minv);
    5497          63 :     C = RgM_Minv_mul(C, Minv);
    5498          63 :     *pS = vecmflineardiv0(S, C, gel(ESA,1));
    5499             :   }
    5500         112 :   return M;
    5501             : }
    5502             : 
    5503             : static void
    5504         245 : MF_set_space(GEN mf, long x) { gmael(mf,1,4) = utoi(x); }
    5505             : static GEN
    5506          91 : mfwt1_cusptonew(GEN mf)
    5507             : {
    5508          91 :   const long vy = 1;
    5509          91 :   GEN vP, F, S, Snew, vF, v = split(mf);
    5510             :   long i, lP, dSnew, ct;
    5511             : 
    5512          91 :   F = gel(v,1);
    5513          91 :   vP= gel(v,2); lP = lg(vP);
    5514          91 :   if (lP == 1) { obj_insert(mf, MF_SPLIT, v); return NULL; }
    5515          77 :   MF_set_space(mf, mf_NEW);
    5516          77 :   S = MF_get_S(mf);
    5517          77 :   dSnew = dim_sum(v);
    5518          77 :   Snew = cgetg(dSnew + 1, t_VEC); ct = 0;
    5519          77 :   vF = cgetg(lP, t_MAT);
    5520         168 :   for (i = 1; i < lP; i++)
    5521             :   {
    5522          91 :     GEN V, P = gel(vP,i), f = liftpol_shallow(gel(F,i));
    5523          91 :     long j, d = degpol(P);
    5524          91 :     gel(vF,i) = V = zerocol(dSnew);
    5525          91 :     if (d == 1)
    5526             :     {
    5527          56 :       gel(Snew, ct+1) = mflineardiv_linear(S, f, 0);
    5528          56 :       gel(V, ct+1) = gen_1;
    5529             :     }
    5530             :     else
    5531             :     {
    5532          35 :       f = RgXV_to_RgM(f,d);
    5533         112 :       for (j = 1; j <= d; j++)
    5534             :       {
    5535          77 :         gel(Snew, ct+j) = mflineardiv_linear(S, row(f,j), 0);
    5536          77 :         gel(V, ct+j) = mkpolmod(pol_xn(j-1,vy), P);
    5537             :       }
    5538             :     }
    5539          91 :     ct += d;
    5540             :   }
    5541          77 :   obj_insert(mf, MF_SPLIT, mkvec2(vF, vP));
    5542          77 :   gel(mf,3) = Snew; return mf;
    5543             : }
    5544             : static GEN
    5545        3465 : mfwt1init(long N, GEN CHI, GEN TMP, long space, long flraw)
    5546             : {
    5547        3465 :   GEN mf, mf1, S, M = mfwt1basis(N, CHI, TMP, &S, NULL);
    5548        3465 :   if (!M) return NULL;
    5549         791 :   mf1 = mkvec4(stoi(N), gen_1, CHI, utoi(mf_CUSP));
    5550         791 :   mf = mkmf(mf1, cgetg(1,t_VEC), S, gen_0, NULL);
    5551         791 :   if (space == mf_NEW)
    5552             :   {
    5553          91 :     gel(mf,5) = mfcleanCHI(M,CHI, 0);
    5554          91 :     mf = mfwt1_cusptonew(mf); if (!mf) return NULL;
    5555          77 :     if (!flraw) M = mfcoefs_mf(mf, mfsturmNk(N,1)+1, 1);
    5556             :   }
    5557         777 :   gel(mf,5) = flraw? zerovec(3): mfcleanCHI(M, CHI, 0);
    5558         777 :   return mf;
    5559             : }
    5560             : 
    5561             : static GEN
    5562         931 : mfEMPTY(GEN mf1)
    5563             : {
    5564         931 :   GEN Minv = mkMinv(cgetg(1,t_MAT), NULL,NULL,NULL);
    5565         931 :   GEN M = mkvec3(cgetg(1,t_VECSMALL), Minv, cgetg(1,t_MAT));
    5566         931 :   return mkmf(mf1, cgetg(1,t_VEC), cgetg(1,t_VEC), cgetg(1,t_VEC), M);
    5567             : }
    5568             : static GEN
    5569         616 : mfEMPTYall(long N, GEN gk, GEN vCHI, long space)
    5570             : {
    5571             :   long i, l;
    5572             :   GEN v, gN, gs;
    5573         616 :   if (!vCHI) return cgetg(1, t_VEC);
    5574          14 :   gN = utoipos(N); gs = utoi(space);
    5575          14 :   l = lg(vCHI); v = cgetg(l, t_VEC);
    5576          14 :   for (i = 1; i < l; i++) gel(v,i) = mfEMPTY(mkvec4(gN,gk,gel(vCHI,i),gs));
    5577          14 :   return v;
    5578             : }
    5579             : 
    5580             : static GEN
    5581        3983 : fmt_dim(GEN CHI, long d, long dih)
    5582        3983 : { return mkvec4(gmfcharorder(CHI), gmfcharno(CHI), utoi(d), stoi(dih)); }
    5583             : /* merge two vector of fmt_dim's for the same vector of characters. If CHI
    5584             :  * is not NULL, remove dim-0 spaces and add character from CHI */
    5585             : static GEN
    5586           7 : merge_dims(GEN V, GEN W, GEN CHI)
    5587             : {
    5588           7 :   long i, j, id, l = lg(V);
    5589           7 :   GEN A = cgetg(l, t_VEC);
    5590           7 :   if (l == 1) return A;
    5591           7 :   id = CHI? 1: 3;
    5592          21 :   for (i = j = 1; i < l; i++)
    5593             :   {
    5594          14 :     GEN v = gel(V,i), w = gel(W,i);
    5595          14 :     long dv = itou(gel(v,id)), dvh = itou(gel(v,id+1)), d;
    5596          14 :     long dw = itou(gel(w,id)), dwh = itou(gel(w,id+1));
    5597          14 :     d = dv + dw;
    5598          14 :     if (d || CHI)
    5599          42 :       gel(A,j++) = CHI? fmt_dim(gel(CHI,i),d, dvh+dwh)
    5600          28 :                       : mkvec2s(d,dvh+dwh);
    5601             :   }
    5602           7 :   setlg(A, j); return A;
    5603             : }
    5604             : static GEN
    5605        3010 : mfdim0all(GEN w)
    5606             : {
    5607        3010 :   if (w) retconst_vec(lg(w)-1, zerovec(2));
    5608        3003 :   return cgetg(1,t_VEC);
    5609             : }
    5610             : static long
    5611        7140 : mfwt1cuspdim_i(long N, GEN CHI, GEN TMP, long *dih)
    5612             : {
    5613        7140 :   pari_sp av = avma;
    5614        7140 :   GEN b = mfwt1basis(N, CHI, TMP, NULL, dih);
    5615        7140 :   avma = av; return b? lg(b)-1: 0;
    5616             : }
    5617             : static long
    5618         301 : mfwt1cuspdim(long N, GEN CHI) { return mfwt1cuspdim_i(N, CHI, NULL, NULL); }
    5619             : static GEN
    5620        4144 : mfwt1cuspdimall(long N, GEN vCHI)
    5621             : {
    5622             :   GEN z, TMP, w;
    5623             :   long i, j, l;
    5624        4144 :   if (wt1empty(N)) return mfdim0all(vCHI);
    5625        1141 :   w = mfwt1chars(N,vCHI);
    5626        1141 :   l = lg(w); if (l == 1) return cgetg(1,t_VEC);
    5627        1141 :   z = cgetg(l, t_VEC);
    5628        1141 :   TMP = mfwt1_pre(N);
    5629        7861 :   for (i = j = 1; i < l; i++)
    5630             :   {
    5631        6720 :     GEN CHI = gel(w,i);
    5632        6720 :     long dih, d = mfwt1cuspdim_i(N, CHI, TMP, &dih);
    5633        6720 :     if (vCHI)
    5634          42 :       gel(z,j++) = mkvec2s(d, dih);
    5635        6678 :     else if (d)
    5636        1428 :       gel(z,j++) = fmt_dim(CHI, d, dih);
    5637             :   }
    5638        1141 :   setlg(z,j); return z;
    5639             : }
    5640             : 
    5641             : /* dimension of S_1(Gamma_1(N)) */
    5642             : static long
    5643        4123 : mfwt1cuspdimsum(long N)
    5644             : {
    5645        4123 :   pari_sp av = avma;
    5646        4123 :   GEN v = mfwt1cuspdimall(N, NULL);
    5647        4123 :   long i, ct = 0, l = lg(v);
    5648        5544 :   for (i = 1; i < l; i++)
    5649             :   {
    5650        1421 :     GEN w = gel(v,i); /* [ord(CHI),*,dim,*] */
    5651        1421 :     ct += itou(gel(w,3))*myeulerphiu(itou(gel(w,1)));
    5652             :   }
    5653        4123 :   avma = av; return ct;
    5654             : }
    5655             : 
    5656             : static GEN
    5657          56 : mfwt1newdimall(long N, GEN vCHI)
    5658             : {
    5659             :   GEN z, w, vTMP, fa, P, E;
    5660             :   long i, c, l, lw, P1;
    5661          56 :   if (wt1empty(N)) return mfdim0all(vCHI);
    5662          56 :   w = mfwt1chars(N,vCHI);
    5663          56 :   lw = lg(w); if (lw == 1) return cgetg(1,t_VEC);
    5664          56 :   vTMP = const_vec(N, NULL);
    5665          56 :   gel(vTMP,N) = mfwt1_pre(N);
    5666             :   /* if p || N and p \nmid F(CHI), S_1^new(G0(N),chi) = 0 */
    5667          56 :   fa = znstar_get_faN(gmael(w,1,1));
    5668          56 :   P = gel(fa,1); l = lg(P);
    5669          56 :   E = gel(fa,2);
    5670         154 :   for (i = P1 = 1; i < l; i++)
    5671          98 :     if (E[i] == 1) P1 *= itou(gel(P,i));
    5672             :   /* P1 = \prod_{v_p(N) = 1} p */
    5673          56 :   z = cgetg(lw, t_VEC);
    5674         182 :   for (i = c = 1; i < lw; i++)
    5675             :   {
    5676             :     long S, j, l, F, dihnew;
    5677         126 :     GEN D, CHI = gel(w,i), CHIP = mfchartoprimitive(CHI,&F);
    5678             : 
    5679         126 :     S = F % P1? 0: mfwt1cuspdim_i(N, CHI, gel(vTMP,N), &dihnew);
    5680         126 :     if (!S)
    5681             :     {
    5682          56 :       if (vCHI) gel(z, c++) = zerovec(2);
    5683          56 :       continue;
    5684             :     }
    5685          70 :     D = mydivisorsu(N/F); l = lg(D);
    5686          77 :     for (j = l-2; j > 0; j--) /* skip last M = N */
    5687             :     {
    5688           7 :       long M = D[j]*F, m, s, dih;
    5689           7 :       GEN TMP = gel(vTMP,M);
    5690           7 :       if (wt1empty(M) || !(m = mubeta(D[l-j]))) continue; /*m = mubeta(N/M)*/
    5691           7 :       if (!TMP) gel(vTMP,M) = TMP = mfwt1_pre(M);
    5692           7 :       s = mfwt1cuspdim_i(M, CHIP, TMP, &dih);
    5693           7 :       if (s) { S += m * s; dihnew += m * dih; }
    5694             :     }
    5695          70 :     if (vCHI)
    5696          63 :       gel(z,c++) = mkvec2s(S, dihnew);
    5697           7 :     else if (S)
    5698           7 :       gel(z, c++) = fmt_dim(CHI, S, dihnew);
    5699             :   }
    5700          56 :   setlg(z,c); return z;
    5701             : }
    5702             : 
    5703             : static GEN
    5704          28 : mfwt1olddimall(long N, GEN vCHI)
    5705             : {
    5706             :   long i, j, l;
    5707             :   GEN z, w;
    5708          28 :   if (wt1empty(N)) return mfdim0all(vCHI);
    5709          28 :   w = mfwt1chars(N,vCHI);
    5710          28 :   l = lg(w); z = cgetg(l, t_VEC);
    5711          84 :   for (i = j = 1; i < l; i++)
    5712             :   {
    5713          56 :     GEN CHI = gel(w,i);
    5714          56 :     long d = mfolddim(N, 1, CHI);
    5715          56 :     if (vCHI)
    5716          28 :       gel(z,j++) = mkvec2s(d,d?-1:0);
    5717          28 :     else if (d)
    5718           7 :       gel(z, j++) = fmt_dim(CHI, d, -1);
    5719             :   }
    5720          28 :   setlg(z,j); return z;
    5721             : }
    5722             : 
    5723             : static long
    5724         469 : mfwt1olddimsum(long N)
    5725             : {
    5726             :   GEN D;
    5727         469 :   long N2, i, l, S = 0;
    5728         469 :   newd_params(N, &N2); /* will ensure mubeta != 0 */
    5729         469 :   D = mydivisorsu(N/N2); l = lg(D);
    5730        2485 :   for (i = 2; i < l; i++)
    5731             :   {
    5732        2016 :     long M = D[l-i]*N2, d = mfwt1cuspdimsum(M);
    5733        2016 :     if (d) S -= mubeta(D[i]) * d;
    5734             :   }
    5735         469 :   return S;
    5736             : }
    5737             : static long
    5738        1050 : mfwt1newdimsum(long N)
    5739             : {
    5740        1050 :   long S = mfwt1cuspdimsum(N);
    5741        1050 :   return S? S - mfwt1olddimsum(N): 0;
    5742             : }
    5743             : 
    5744             : /* Guess Galois type of wt1 eigenforms. */
    5745             : /* NK can be mf or [N,1,CHI] */
    5746             : static long
    5747          49 : mfisdihedral(GEN F, GEN DIH)
    5748             : {
    5749          49 :   GEN vG = gel(DIH,1), M = gel(DIH,2), v;
    5750             :   long i, l;
    5751          49 :   if (lg(M) == 1) return 0;
    5752          28 :   v = RgM_RgC_invimage(M, mftocol(F, nbrows(M)-1, 1));
    5753          28 :   if (!v) return 0;
    5754          28 :   l = lg(v);
    5755          28 :   for (i = 1; i < l; i++)
    5756          28 :     if (!gequal0(gel(v,i)))
    5757             :     {
    5758          28 :       GEN G = gel(vG,i), bnr = gel(G,2), w = gel(G,3);
    5759          28 :       GEN gen, cyc = bnr_get_cyc(bnr), D = gel(cyc,1);
    5760          28 :       GEN f = bnr_get_mod(bnr), nf = bnr_get_nf(bnr);
    5761          28 :       GEN con = gel(galoisconj(nf,gen_1), 2);
    5762          28 :       GEN f0 = gel(f,1), f0b = galoisapply(nf, con, f0);
    5763          28 :       GEN xin = zv_to_ZV(gel(w,2)); /* xi(bnr.gen[i]) = e(xin[i] / D) */
    5764             :       long e, j, L, n;
    5765          28 :       if (!gequal(f0,f0b))
    5766             :       { /* finite part of conductor not ambiguous */
    5767          14 :         GEN a = idealmul(nf, f0, idealdivexact(nf, f0b, idealadd(nf, f0, f0b)));
    5768          14 :         GEN bnr0 = bnr;
    5769          14 :         bnr = bnrinit0(bnr_get_bnf(bnr), mkvec2(a, gel(f,2)), 1);
    5770          14 :         xin = RgV_RgM_mul(xin, bnrsurjection(bnr, bnr0));
    5771             :         /* still xi(gen[i]) = e(xin[i] / D), for the new generators */
    5772             :       }
    5773          28 :       gen = bnr_get_gen(bnr); L = lg(gen);
    5774          42 :       for (j = 1, e = itou(D); j < L; j++)
    5775             :       {
    5776          35 :         GEN Ng = idealnorm(nf, gel(gen,j));
    5777          35 :         GEN a = shifti(gel(xin,j), 1); /* xi(g_j^2) = e(a/D) */
    5778          35 :         GEN b = FpV_dotproduct(xin, isprincipalray(bnr,Ng), D);
    5779          35 :         GEN m = Fp_sub(a, b, D); /* xi(g_j/\bar{g_j}) = e(m/D) */
    5780          35 :         e = ugcd(e, itou(m)); if (e == 1) break;
    5781             :       }
    5782          28 :       n = itou(D) / e;
    5783          28 :       return n == 1? 4: 2*n;
    5784             :     }
    5785           0 :   return 0;
    5786             : }
    5787             : 
    5788             : static ulong
    5789          21 : radical_u(ulong n)
    5790          21 : { return zv_prod(gel(myfactoru(n),1)); }
    5791             : 
    5792             : /* list of fundamental discriminants unramified outside N, with sign s
    5793             :  * [s = 0 => no sign condition] */
    5794             : static GEN
    5795          21 : mfunram(long N, long s)
    5796             : {
    5797          21 :   long cN = radical_u(N >> vals(N)), p = 1, m = 1, l, c, i;
    5798          21 :   GEN D = mydivisorsu(cN), res;
    5799          21 :   l = lg(D);
    5800          21 :   if (s == 1) m = 0; else if (s == -1) p = 0;
    5801          21 :   res = cgetg(6*l - 5, t_VECSMALL);
    5802          21 :   c = 1;
    5803          21 :   if (!odd(N))
    5804             :   { /* d = 1 */
    5805          14 :     if (p) res[c++] = 8;
    5806          14 :     if (m) { res[c++] =-8; res[c++] =-4; }
    5807             :   }
    5808          56 :   for (i = 2; i < l; i++)
    5809             :   { /* skip d = 1, done above */
    5810          35 :     long d = D[i], d4 = d & 3L; /* d odd, squarefree, d4 = 1 or 3 */
    5811          35 :     if (d4 == 1) { if (p) res[c++] = d; }
    5812          28 :     else         { if (m) res[c++] =-d; }
    5813          35 :     if (!odd(N))
    5814             :     {
    5815          14 :       if (p) { res[c++] = 8*d; if (d4 == 3) res[c++] = 4*d; }
    5816          14 :       if (m) { res[c++] =-8*d; if (d4 == 1) res[c++] =-4*d; }
    5817             :     }
    5818             :   }
    5819          21 :   setlg(res, c); return res;
    5820             : }
    5821             : 
    5822             : /* Return 1 if F is definitely not S4 type; return 0 on failure. */
    5823             : static long
    5824           7 : mfisnotS4(long N, GEN w)
    5825             : {
    5826           7 :   GEN D = mfunram(N, 0);
    5827           7 :   long i, lD = lg(D), lw = lg(w);
    5828          56 :   for (i = 1; i < lD; i++)
    5829             :   {
    5830          49 :     long p, d = D[i], ok = 0;
    5831         154 :     for (p = 2; p < lw; p++)
    5832         154 :       if (w[p] && kross(d,p) == -1) { ok = 1; break; }
    5833          49 :     if (!ok) return 0;
    5834             :   }
    5835           7 :   return 1;
    5836             : }
    5837             : 
    5838             : /* Return 1 if Q(sqrt(5)) \not\subset Q(F), i.e. F is definitely not A5 type;
    5839             :  * return 0 on failure. */
    5840             : static long
    5841           7 : mfisnotA5(GEN F)
    5842             : {
    5843           7 :   GEN CHI = mf_get_CHI(F), P = mfcharpol(CHI), T, Q;
    5844             : 
    5845           7 :   if (mfcharorder(CHI) % 5 == 0) return 0;
    5846           7 :   T = mf_get_field(F); if (degpol(T) == 1) return 1;
    5847           7 :   if (degpol(P) > 1) T = rnfequation(P,T);
    5848           7 :   Q = gsubgs(pol_xn(2,varn(T)), 5);
    5849           7 :   return (typ(nfisincl(Q, T)) == t_INT);
    5850             : }
    5851             : 
    5852             : /* Given x = z + 1/z with z prim. root of unity of order n, find n */
    5853             : static long
    5854         357 : mffindrootof1(GEN u1)
    5855             : {
    5856         357 :   pari_sp av = avma;
    5857         357 :   GEN u0 = gen_2, u1k = u1, u2;
    5858         357 :   long c = 1;
    5859        1379 :   while (!gequalsg(2, liftpol_shallow(u1))) /* u1 = z^c + z^-c */
    5860             :   {
    5861         665 :     u2 = gsub(gmul(u1k, u1), u0);
    5862         665 :     u0 = u1; u1 = u2; c++;
    5863             :   }
    5864         357 :   avma = av; return c;
    5865             : }
    5866             : 
    5867             : /* we known that F is not dihedral */
    5868             : static long
    5869          21 : mfgaloistype_i(long N, GEN CHI, GEN F, long lim)
    5870             : {
    5871             :   forprime_t iter;
    5872          21 :   GEN v = mfcoefs_i(F,lim,1), w = zero_zv(lim);
    5873             :   ulong p;
    5874          21 :   u_forprime_init(&iter, 2, lim);
    5875         406 :   while((p = u_forprime_next(&iter)))
    5876             :   {
    5877             :     GEN u;
    5878             :     long n;
    5879         378 :     if (!(N%p)) continue;
    5880         357 :     u = gdiv(gsqr(gel(v, p+1)), mfchareval_i(CHI, p));
    5881         357 :     n = mffindrootof1(gsubgs(u,2));
    5882         357 :     if (n == 3) w[p] = 1;
    5883         357 :     if (n == 4) return -24; /* S4 */
    5884         350 :     if (n == 5) return -60; /* A5 */
    5885         350 :     if (n > 5) pari_err_DOMAIN("mfgaloistype", "form", "not a",
    5886             :                                strtoGENstr("cuspidal eigenform"), F);
    5887             :   }
    5888           7 :   if (mfisnotS4(N,w) && mfisnotA5(F)) return -12; /* A4 */
    5889           0 :   return 0; /* FAILURE */
    5890             : }
    5891             : 
    5892             : static GEN
    5893          49 : mfgaloistype0(long N, GEN CHI, GEN F, GEN DIH, long lim)
    5894             : {
    5895          49 :   pari_sp av = avma;
    5896          49 :   long t = mfisdihedral(F, DIH);
    5897          49 :   avma = av;
    5898          49 :   if (t) return stoi(t);
    5899             :   for(;;)
    5900             :   {
    5901          21 :     t = mfgaloistype_i(N, CHI, F, lim);
    5902          14 :     avma = av; if (t) return stoi(t);
    5903           0 :     lim += lim >> 1;
    5904             :   }
    5905             : }
    5906             : 
    5907             : /* If f is NULL, give all the galoistypes, otherwise just for f */
    5908             : GEN
    5909          63 : mfgaloistype(GEN NK, GEN f)
    5910             : {
    5911          63 :   pari_sp av = avma;
    5912          63 :   GEN CHI, T, F, DIH, mf = checkMF_i(NK);
    5913             :   long N, k, lL, i, lim, SB;
    5914             : 
    5915          63 :   if (f && !checkmf_i(f)) pari_err_TYPE("mfgaloistype", f);
    5916          56 :   if (mf)
    5917             :   {
    5918          21 :     N = MF_get_N(mf);
    5919          21 :     k = MF_get_k(mf);
    5920          21 :     CHI = MF_get_CHI(mf);
    5921             :   }
    5922             :   else
    5923             :   {
    5924          35 :     checkNK(NK, &N, &k, &CHI, 0);
    5925          35 :     mf = f? NULL: mfinit_i(NK, mf_NEW);
    5926             :   }
    5927          56 :   if (k != 1) pari_err_DOMAIN("mfgaloistype", "k", "!=", gen_1, stoi(k));
    5928          56 :   SB = mfsturmNk(N,1) + 1;
    5929          56 :   lim = maxss(200, 3*SB);
    5930          56 :   DIH = mfdihedralnew(N,CHI);
    5931          56 :   DIH = mkvec2(DIH, mfvectomat(DIH,SB,1));
    5932          56 :   if (f) return gerepileuptoint(av, mfgaloistype0(N,CHI, f, DIH, lim));
    5933          49 :   F = mfeigenbasis(mf); lL = lg(F);
    5934          49 :   T = cgetg(lL, t_VEC);
    5935          49 :   for (i=1; i < lL; i++) gel(T,i) = mfgaloistype0(N,CHI, gel(F,i), DIH, lim);
    5936          49 :   return gerepileupto(av, T);
    5937             : }
    5938             : 
    5939             : /******************************************************************/
    5940             : /*                   Find all dihedral forms.                     */
    5941             : /******************************************************************/
    5942             : /* lim >= 2 */
    5943             : static void
    5944           7 : consttabdihedral(long lim)
    5945           7 : { cache_set(cache_DIH, mfdihedralall(mkvecsmall2(1,lim))); }
    5946             : 
    5947             : /* a ideal coprime to bnr modulus */
    5948             : static long
    5949       77385 : mfdiheval(GEN bnr, GEN w, GEN a)
    5950             : {
    5951       77385 :   GEN L, cycn = gel(w,1), chin = gel(w,2);
    5952       77385 :   long ordmax = cycn[1];
    5953       77385 :   L = ZV_to_Flv(isprincipalray(bnr,a), ordmax);
    5954       77385 :   return Flv_dotproduct(chin, L, ordmax);
    5955             : }
    5956             : 
    5957             : /* A(x^k) mod T */
    5958             : static GEN
    5959       25711 : Galois(GEN A, long k, GEN T)
    5960             : {
    5961       25711 :   if (typ(A) != t_POL) return A;
    5962        9730 :   return gmod(RgX_inflate(A, k), T);
    5963             : }
    5964             : static GEN
    5965         609 : vecGalois(GEN v, long k, GEN T)
    5966             : {
    5967             :   long i, l;
    5968         609 :   GEN w = cgetg_copy(v,&l);
    5969         609 :   for (i = 1; i < l; i++) gel(w,i) = Galois(gel(v,i), k, T);
    5970         609 :   return w;
    5971             : }
    5972             : 
    5973             : static GEN
    5974      154392 : fix_pol(GEN S, GEN Pn, int *trace)
    5975             : {
    5976      154392 :   if (typ(S) != t_POL) return S;
    5977      107884 :   S = RgX_rem(S, Pn);
    5978      107884 :   if (typ(S) == t_POL)
    5979             :   {
    5980      107884 :     switch(lg(S))
    5981             :     {
    5982       37919 :       case 2: return gen_0;
    5983       17094 :       case 3: return gel(S,2);
    5984             :     }
    5985       52871 :     *trace = 1;
    5986             :   }
    5987       52871 :   return S;
    5988             : }
    5989             : 
    5990             : static GEN
    5991       10465 : dihan(GEN bnr, GEN w, GEN k0j, ulong lim)
    5992             : {
    5993       10465 :   GEN nf = bnr_get_nf(bnr), f = bid_get_ideal(bnr_get_bid(bnr));
    5994       10465 :   GEN v = zerovec(lim+1), cycn = gel(w,1), Tinit = gel(w,3);
    5995       10465 :   GEN Pn = gel(Tinit,lg(Tinit)==4? 2: 1);
    5996       10465 :   long j, ordmax = cycn[1], k0 = k0j[1], jdeg = k0j[2];
    5997       10465 :   long D = itos(nf_get_disc(nf)), vt = varn(Pn);
    5998       10465 :   int trace = 0;
    5999             :   ulong p, n;
    6000             :   forprime_t T;
    6001             : 
    6002       10465 :   if (!lim) return v;
    6003       10465 :   gel(v,2) = gen_1;
    6004       10465 :   u_forprime_init(&T, 2, lim);
    6005             :   /* fill in prime powers first */
    6006       10465 :   while ((p = u_forprime_next(&T)))
    6007             :   {
    6008             :     GEN vP, vchiP, S;
    6009             :     long k, lP;
    6010             :     ulong q, qk;
    6011       70980 :     if (kross(D,p) >= 0) q = p;
    6012       29232 :     else if (!(q = umuluu_le(p,p,lim))) continue;
    6013             :     /* q = Norm P */
    6014       47390 :     vP = idealprimedec(nf, utoipos(p));
    6015       47390 :     lP = lg(vP);
    6016       47390 :     vchiP = cgetg(lP, t_VECSMALL);
    6017      128674 :     for (j = k = 1; j < lP; j++)
    6018             :     {
    6019       81284 :       GEN P = gel(vP,j);
    6020       81284 :       if (!idealval(nf, f, P)) vchiP[k++] = mfdiheval(bnr,w,P);
    6021             :     }
    6022       47390 :     if (k == 1) continue;
    6023       45689 :     setlg(vchiP, k); lP = k;
    6024       45689 :     if (lP == 2)
    6025             :     { /* one prime above p not dividing f */
    6026       13993 :       long s, s0 = vchiP[1];
    6027       24738 :       for (qk=q, s = s0;; s = Fl_add(s,s0,ordmax))
    6028             :       {
    6029       35483 :         S = mygmodulo_lift(s, ordmax, gen_1, vt);
    6030       24738 :         gel(v, qk+1) = fix_pol(S, Pn, &trace);
    6031       24738 :         if (!(qk = umuluu_le(qk,q,lim))) break;
    6032             :       }
    6033             :     }
    6034             :     else /* two primes above p not dividing f */
    6035             :     {
    6036       31696 :       long s, s0 = vchiP[1], s1 = vchiP[2];
    6037       46928 :       for (qk=q, k = 1;; k++)
    6038       15232 :       { /* sum over a,b s.t. Norm( P1^a P2^b ) = q^k, i.e. a+b = k */
    6039             :         long a;
    6040       46928 :         GEN S = gen_0;
    6041      163723 :         for (a = 0; a <= k; a++)
    6042             :         {
    6043      116795 :           s = Fl_add(Fl_mul(a, s0, ordmax), Fl_mul(k-a, s1, ordmax), ordmax);
    6044      116795 :           S = gadd(S, mygmodulo_lift(s, ordmax, gen_1, vt));
    6045             :         }
    6046       46928 :         gel(v, qk+1) = fix_pol(S, Pn, &trace);
    6047       46928 :         if (!(qk = umuluu_le(qk,q,lim))) break;
    6048             :       }
    6049             :     }
    6050             :   }
    6051             :   /* complete with non-prime powers */
    6052      200116 :   for (n = 2; n <= lim; n++)
    6053             :   {
    6054      189651 :     GEN S, fa = myfactoru(n), P = gel(fa, 1), E = gel(fa, 2);
    6055             :     long q;
    6056      189651 :     if (lg(P) == 2) continue;
    6057             :     /* not a prime power */
    6058       82726 :     q = upowuu(P[1],E[1]);
    6059       82726 :     S = gmul(gel(v, q + 1), gel(v, n/q + 1));
    6060       82726 :     gel(v, n+1) = fix_pol(S, Pn, &trace);
    6061             :   }
    6062       10465 :   if (trace)
    6063             :   {
    6064        5369 :     if (lg(Tinit) == 4) v = QabV_tracerel(Tinit, jdeg, v);
    6065             :     /* Apply Galois Mod(k0, ordw) */
    6066        5369 :     if (k0 > 1) { GEN Pm = gel(Tinit,1); v = vecGalois(v, k0, Pm); }
    6067             :   }
    6068       10465 :   return v;
    6069             : }
    6070             : 
    6071             : /* as cyc_normalize for t_VECSMALL cyc */
    6072             : static GEN
    6073       13391 : cyc_normalize_zv(GEN cyc)
    6074             : {
    6075       13391 :   long i, o = cyc[1], l = lg(cyc); /* > 1 */
    6076       13391 :   GEN D = cgetg(l, t_VECSMALL);
    6077       13391 :   D[1] = o; for (i = 2; i < l; i++) D[i] = o / cyc[i];
    6078       13391 :   return D;
    6079             : }
    6080             : /* as char_normalize for t_VECSMALLs */
    6081             : static GEN
    6082       58975 : char_normalize_zv(GEN chi, GEN ncyc)
    6083             : {
    6084       58975 :   long i, l = lg(chi);
    6085       58975 :   GEN c = cgetg(l, t_VECSMALL);
    6086       58975 :   if (l > 1) {
    6087       58975 :     c[1] = chi[1];
    6088       58975 :     for (i = 2; i < l; i++) c[i] = chi[i] * ncyc[i];
    6089             :   }
    6090       58975 :   return c;
    6091             : }
    6092             : 
    6093             : static GEN
    6094        6097 : dihan_bnf(long D)
    6095        6097 : { setrand(gen_1); return Buchall(quadpoly(stoi(D)), 0, LOWDEFAULTPREC); }
    6096             : static GEN
    6097       20391 : dihan_bnr(GEN bnf, GEN A)
    6098       20391 : { setrand(gen_1); return bnrinit0(bnf, A, 1); }
    6099             : 
    6100             : /* Hecke xi * (D/.) = Dirichlet chi, return v in Q^r st chi(g_i) = e(v[i]).
    6101             :  * cycn = cyc_normalize_zv(bnr.cyc), chin = char_normalize_zv(chi,cyc) */
    6102             : static GEN
    6103       17206 : bnrchartwist2conrey(GEN chin, GEN cycn, GEN bnrconreyN, GEN kroconreyN)
    6104             : {
    6105       17206 :   long l = lg(bnrconreyN), c1 = cycn[1], i;
    6106       17206 :   GEN v = cgetg(l, t_COL);
    6107       62566 :   for (i = 1; i < l; i++)
    6108             :   {
    6109       45360 :     GEN d = sstoQ(zv_dotproduct(chin, gel(bnrconreyN,i)), c1);
    6110       45360 :     if (kroconreyN[i] < 0) d = gadd(d, ghalf);
    6111       45360 :     gel(v,i) = d;
    6112             :   }
    6113       17206 :   return v;
    6114             : }
    6115             : 
    6116             : /* chi(g_i) = e(v[i]) denormalize wrt Conrey generators orders */
    6117             : static GEN
    6118       17206 : conreydenormalize(GEN znN, GEN v)
    6119             : {
    6120       17206 :   GEN gcyc = znstar_get_conreycyc(znN), w;
    6121       17206 :   long l = lg(v), i;
    6122       17206 :   w = cgetg(l, t_COL);
    6123       62566 :   for (i = 1; i < l; i++)
    6124       45360 :     gel(w,i) = modii(gmul(gel(v,i), gel(gcyc,i)), gel(gcyc,i));
    6125       17206 :   return w;
    6126             : }
    6127             : 
    6128             : static long
    6129       41769 : Miyake(GEN vchi, GEN gb, GEN cycn)
    6130             : {
    6131       41769 :   long i, e = cycn[1], lb = lg(gb);
    6132       41769 :   GEN v = char_normalize_zv(vchi, cycn);
    6133       62132 :   for (i = 1; i < lb; i++)
    6134       49833 :     if ((zv_dotproduct(v, gel(gb,i)) -  v[i]) % e) return 1;
    6135       12299 :   return 0;
    6136             : }
    6137             : 
    6138             : /* list of Hecke characters not induced by a Dirichlet character up to Galois
    6139             :  * conjugation, whose conductor is bnr.cond; cycn = cyc_normalize(bnr.cyc)*/
    6140             : static GEN
    6141       13391 : mklvchi(GEN bnr, GEN con, GEN cycn)
    6142             : {
    6143       13391 :   GEN gb = NULL, cyc = bnr_get_cyc(bnr), cycsmall = ZV_to_zv(cyc);
    6144       13391 :   GEN vchi = cyc2elts(cycsmall);
    6145       13391 :   long ordmax = cycsmall[1], c, i, l;
    6146       13391 :   if (con)
    6147             :   {
    6148        3892 :     GEN g = bnr_get_gen(bnr), nf = bnr_get_nf(bnr);
    6149        3892 :     long lg = lg(g);
    6150        3892 :     gb = cgetg(lg, t_VEC);
    6151        9135 :     for (i = 1; i < lg; i++)
    6152        5243 :       gel(gb,i) = ZV_to_zv(isprincipalray(bnr, galoisapply(nf, con, gel(g,i))));
    6153             :   }
    6154       13391 :   l = lg(vchi);
    6155      151725 :   for (i = c = 1; i < l; i++)
    6156             :   {
    6157      138334 :     GEN chi = gel(vchi,i);
    6158      138334 :     if (!con || Miyake(chi, gb, cycn)) gel(vchi, c++) = Flv_to_ZV(chi);
    6159             :   }
    6160       13391 :   setlg(vchi, c); l = c;
    6161      139426 :   for (i = 1; i < l; i++)
    6162             :   {
    6163      126035 :     GEN chi = gel(vchi,i);
    6164             :     long n;
    6165      126035 :     if (!chi) continue;
    6166      527289 :     for (n = 2; n < ordmax; n++)
    6167      482748 :       if (ugcd(n, ordmax) == 1)
    6168             :       {
    6169      198597 :         GEN tmp = vecmodii(gmulsg(n, chi), cyc);
    6170             :         long j;
    6171     3809050 :         for (j = i+1; j < l; j++)
    6172     3610453 :           if (gel(vchi,j) && gequal(gel(vchi,j), tmp)) gel(vchi,j) = NULL;
    6173             :       }
    6174             :   }
    6175      139426 :   for (i = c = 1; i < l; i++)
    6176             :   {
    6177      126035 :     GEN chi = gel(vchi,i);
    6178      126035 :     if (chi && bnrisconductor(bnr, chi)) gel(vchi, c++) = chi;
    6179             :   }
    6180       13391 :   setlg(vchi, c); return vchi;
    6181             : }
    6182             : 
    6183             : /* con = NULL if D > 0 or if D < 0 and id != idcon. */
    6184             : static GEN
    6185       16835 : mfdihedralcommon(GEN bnf, GEN id, GEN znN, GEN kroconreyN, long N, long D, GEN con)
    6186             : {
    6187             :   GEN bnr, bnrconreyN, cyc, cycn, cycN, Lvchi, res, g, P;
    6188             :   long i, j, ordmax, l, lc, deghecke, degrel;
    6189             : 
    6190       16835 :   bnr = dihan_bnr(bnf, id);
    6191       16835 :   cyc = ZV_to_zv( bnr_get_cyc(bnr) );
    6192       16835 :   lc = lg(cyc); if (lc == 1) return NULL;
    6193             : 
    6194       13391 :   g = znstar_get_conreygen(znN); l = lg(g);
    6195       13391 :   bnrconreyN = cgetg(l, t_VEC);
    6196       50288 :   for (i = 1; i < l; i++)
    6197       36897 :     gel(bnrconreyN,i) = ZV_to_zv(isprincipalray(bnr,gel(g,i)));
    6198             : 
    6199       13391 :   cycn = cyc_normalize_zv(cyc);
    6200       13391 :   cycN = ZV_to_zv(znstar_get_cyc(znN));
    6201       13391 :   ordmax = cyc[1];
    6202       13391 :   P = polcyclo(ord_canon(ordmax), fetch_user_var("t"));
    6203       13391 :   deghecke = myeulerphiu(ordmax);
    6204       13391 :   Lvchi = mklvchi(bnr, con, cycn); l = lg(Lvchi);
    6205       13391 :   if (l == 1) return NULL;
    6206        7917 :   res = cgetg(l, t_VEC);
    6207       25123 :   for (j = 1; j < l; j++)
    6208             :   {
    6209       17206 :     GEN T, Tinit, v, vchi = ZV_to_zv(gel(Lvchi,j));
    6210       17206 :     GEN chi, chin = char_normalize_zv(vchi, cycn);
    6211             :     long ordw, vnum, k0;
    6212       17206 :     v = bnrchartwist2conrey(chin, cycn, bnrconreyN, kroconreyN);
    6213       17206 :     ordw = itou(Q_denom(v));
    6214       17206 :     Tinit = Qab_trace_init(P, ord_canon(ordmax), ord_canon(ordw));
    6215       17206 :     chi = conreydenormalize(znN, v);
    6216       17206 :     vnum = itou(znconreyexp(znN, chi));
    6217       17206 :     chi = ZV_to_zv(znconreychar(znN,chi));
    6218       17206 :     degrel = deghecke / myeulerphiu(ordw);
    6219       17206 :     k0 = zv_cyc_minimize(cycN, chi, coprimes_zv(ordw));
    6220       17206 :     vnum = Fl_powu(vnum, k0, N);
    6221             :     /* encodes degrel forms: jdeg = 0..degrel-1 */
    6222       17206 :     T = mkvecsmalln(6, N, k0, vnum, D, ordmax, degrel);
    6223       17206 :     gel(res,j) = mkvec3(T, id, mkvec3(cycn,chin,Tinit));
    6224             :   }
    6225        7917 :   return res;
    6226             : }
    6227             : 
    6228             : /* Append to v all dihedral weight 1 forms coming from D, if fundamental. */
    6229             : /* B a t_VECSMALL: if #B=1, only that level; if B=[Bmin,Bmax], Bmin <= Bmax:
    6230             :  * between those levels. */
    6231             : static void
    6232        9289 : append_dihedral(GEN v, long D, GEN B)
    6233             : {
    6234        9289 :   long Da = labs(D), no, N, i, numi, ct, min, max;
    6235             :   GEN bnf, con, LI, resall, varch;
    6236             :   pari_sp av;
    6237             : 
    6238        9289 :   if (lg(B) == 2)
    6239             :   {
    6240           0 :     long b = B[1], m = D > 0? 3: 1;
    6241           0 :     min = b / Da;
    6242           0 :     if (b % Da || min < m) return;
    6243           0 :     max = min;
    6244             :   }
    6245             :   else
    6246             :   { /* assume B[1] < B[2] */
    6247        9289 :     min = (B[1] + Da-1)/Da;
    6248        9289 :     max = B[2]/Da;
    6249             :   }
    6250        9289 :   if (!sisfundamental(D)) return;
    6251             : 
    6252        2842 :   av = avma;
    6253        2842 :   bnf = dihan_bnf(D);
    6254        2842 :   con = gel(galoisconj(bnf,gen_1), 2);
    6255        2842 :   LI = ideallist(bnf, max);
    6256        2842 :   numi = 0; for (i = min; i <= max; i++) numi += lg(gel(LI, i)) - 1;
    6257        2842 :   if (D > 0)
    6258             :   {
    6259         707 :     numi <<= 1;
    6260         707 :     varch = mkvec2(mkvec2(gen_1,gen_0), mkvec2(gen_0,gen_1));
    6261             :   }
    6262             :   else
    6263        2135 :     varch = NULL;
    6264        2842 :   resall = cgetg(numi+1, t_VEC); ct = 1;
    6265       27503 :   for (no = min; no <= max; no++)
    6266             :   {
    6267             :     GEN LIs, znN, conreyN, kroconreyN;
    6268             :     long flcond, lgc, lglis;
    6269       24661 :     if (D < 0)
    6270       15043 :       flcond = (no == 2 || no == 3 || (no == 4 && (D&7L)==1));
    6271             :     else
    6272        9618 :       flcond = (no == 4 && (D&7L) != 1);
    6273       24661 :     if (flcond) continue;
    6274       22302 :     LIs = gel(LI, no);
    6275       22302 :     N = Da*no;
    6276       22302 :     znN = znstar0(utoi(N), 1);
    6277       22302 :     conreyN = znstar_get_conreygen(znN); lgc = lg(conreyN);
    6278       22302 :     kroconreyN = cgetg(lgc, t_VECSMALL);
    6279       22302 :     for (i = 1; i < lgc; i++) kroconreyN[i] = krosi(D, gel(conreyN, i));
    6280       22302 :     lglis = lg(LIs);
    6281       43876 :     for (i = 1; i < lglis; i++)
    6282             :     {
    6283       21574 :       GEN id = gel(LIs, i), idcon, conk;
    6284             :       long j, inf, maxinf;
    6285       21574 :       if (typ(id) == t_INT) continue;
    6286       14077 :       idcon = galoisapply(bnf, con, id);
    6287       14077 :       conk = (D < 0 && gequal(idcon, id)) ? con : NULL;
    6288       42294 :       for (j = i; j < lglis; j++)
    6289       28217 :         if (gequal(idcon, gel(LIs, j))) gel(LIs, j) = gen_0;
    6290       14077 :       maxinf = (D < 0 || gequal(idcon,id))? 1: 2;
    6291       30912 :       for (inf = 1; inf <= maxinf; inf++)
    6292             :       {
    6293       16835 :         GEN ide = (D > 0)? mkvec2(id, gel(varch,inf)): id;
    6294       16835 :         GEN res = mfdihedralcommon(bnf, ide, znN, kroconreyN, N, D, conk);
    6295       16835 :         if (res) gel(resall, ct++) = res;
    6296             :       }
    6297             :     }
    6298             :   }
    6299        2842 :   if (ct == 1) avma = av;
    6300             :   else
    6301             :   {
    6302        2394 :     setlg(resall, ct);
    6303        2394 :     vectrunc_append(v, gerepilecopy(av, shallowconcat1(resall)));
    6304             :   }
    6305             : }
    6306             : 
    6307             : static long
    6308       21021 : di_N(GEN a) { return gel(a,1)[1]; }
    6309             : /* All primitive dihedral wt1 forms: LIM a t_VECSMALL with a single component
    6310             :  * (only level LIM) or 2 components [m,M], m < M (between m and M) */
    6311             : static GEN
    6312           7 : mfdihedralall(GEN LIM)
    6313             : {
    6314             :   GEN res, z;
    6315             :   long limD, ct, i, l1, l2;
    6316             : 
    6317           7 :   if (lg(LIM) == 2) l1 = l2 = LIM[1]; else { l1 = LIM[1]; l2 = LIM[2]; }
    6318           7 :   limD = l2;
    6319           7 :   res = vectrunc_init(2*limD);
    6320           7 :   if (l1 == l2)
    6321             :   {
    6322           0 :     GEN D = mydivisorsu(l1);
    6323           0 :     long l = lg(D), j;
    6324           0 :     for (j = 2; j < l; j++)
    6325             :     {
    6326           0 :       long d = D[j];
    6327           0 :       append_dihedral(res, -d, LIM);
    6328           0 :       if (d >= 5 && D[l-j] >= 3) append_dihedral(res, d, LIM);
    6329             :     }
    6330             :   }
    6331             :   else
    6332             :   {
    6333             :     long D;
    6334           7 :     for (D = -3; D >= -limD; D--) append_dihedral(res, D, LIM);
    6335           7 :     limD /= 3;
    6336           7 :     for (D = 5; D <= limD;   D++) append_dihedral(res, D, LIM);
    6337             :   }
    6338           7 :   if (l1 == l2) return gel(res,1); /* single level */
    6339           7 :   ct = lg(res);
    6340           7 :   if (ct > 1)
    6341             :   { /* concat and sort wrt N */
    6342           7 :     res = shallowconcat1(res);
    6343           7 :     res = vecpermute(res, indexvecsort(res, mkvecsmall(1)));
    6344           7 :     ct = lg(res);
    6345             :   }
    6346           7 :   z = const_vec(l2-l1+1, cgetg(1,t_VEC));
    6347        3836 :   for (i = 1; i < ct;)
    6348             :   { /* regroup result sharing the same N */
    6349        3822 :     long n = di_N(gel(res,i)), j = i+1, k;
    6350             :     GEN v;
    6351        3822 :     while (j < ct && di_N(gel(res,j)) == n) j++;
    6352        3822 :     n -= l1-1;
    6353        3822 :     gel(z, n) = v = cgetg(j-i+1, t_VEC);
    6354        3822 :     for (k = 1; i < j; k++,i++) gel(v,k) = gel(res,i);
    6355             :   }
    6356           7 :   return z;
    6357             : }
    6358             : 
    6359             : /* return [vF, index], where vecpermute(vF,index) generates dihedral forms
    6360             :  * for character CHI */
    6361             : static GEN
    6362       23429 : mfdihedralnew_i(long N, GEN CHI)
    6363             : {
    6364             :   GEN bnf, Tinit, Pm, vf, M, V, NK, SP;
    6365             :   long Dold, d, ordw, i, SB, c, l, k0, k1, chino, chinoorig, lv;
    6366             : 
    6367       23429 :   SP = cache_get(cache_DIH, N);
    6368       23429 :   if (!SP) SP = mfdihedralall(mkvecsmall(N));
    6369       23429 :   lv = lg(SP); if (lv == 1) return NULL;
    6370       11151 :   CHI = mfcharinduce(CHI,N);
    6371       11151 :   ordw = mfcharorder(CHI);
    6372       11151 :   chinoorig = mfcharno(CHI);
    6373       11151 :   k0 = mfconreyminimize(CHI);
    6374       11151 :   chino = Fl_powu(chinoorig, k0, N);
    6375       11151 :   k1 = Fl_inv(k0 % ordw, ordw);
    6376       11151 :   V = cgetg(lv, t_VEC);
    6377       11151 :   d = 0;
    6378       34615 :   for (i = l = 1; i < lv; i++)
    6379             :   {
    6380       23464 :     GEN sp = gel(SP,i), T = gel(sp,1);
    6381       23464 :     if (T[3] != chino) continue;
    6382        3556 :     d += T[6];
    6383        3556 :     if (k1 != 1)
    6384             :     {
    6385          77 :       GEN t = leafcopy(T);
    6386          77 :       t[3] = chinoorig;
    6387          77 :       t[2] = (t[2]*k1)%ordw;
    6388          77 :       sp = mkvec4(t, gel(sp,2), gel(sp,3), gel(sp,4));
    6389             :     }
    6390        3556 :     gel(V, l++) = sp;
    6391             :   }
    6392       11151 :   setlg(V, l); /* dihedral forms of level N and character CHI */
    6393       11151 :   if (l == 1) return NULL;
    6394             : 
    6395        2331 :   SB = myeulerphiu(ordw) * mfsturmNk(N,1) + 1;
    6396        2331 :   M = cgetg(d+1, t_MAT);
    6397        2331 :   vf = cgetg(d+1, t_VEC);
    6398        2331 :   NK = mkNK(N, 1, CHI);
    6399        2331 :   bnf = NULL; Dold = 0;
    6400        5887 :   for (i = c = 1; i < l; i++)
    6401             :   { /* T = [N, k0, conreyno, D, ordmax, degrel] */
    6402        3556 :     GEN bnr, Vi = gel(V,i), T = gel(Vi,1), id = gel(Vi,2), w = gel(Vi,3);
    6403        3556 :     long jdeg, k0i = T[2], D = T[4], degrel = T[6];
    6404             : 
    6405        3556 :     if (D != Dold) { Dold = D; bnf = dihan_bnf(D); }
    6406        3556 :     bnr = dihan_bnr(bnf, id);
    6407       10430 :     for (jdeg = 0; jdeg < degrel; jdeg++,c++)
    6408             :     {
    6409        6874 :       GEN k0j = mkvecsmall2(k0i, jdeg), an = dihan(bnr, w, k0j, SB);
    6410        6874 :       settyp(an, t_COL); gel(M,c) = Q_primpart(an);
    6411        6874 :       gel(vf,c) = tag3(t_MF_DIHEDRAL, NK, bnr, w, k0j);
    6412             :     }
    6413             :   }
    6414        2331 :   Tinit = gmael3(V,1,3,3); Pm = gel(Tinit,1);
    6415        2331 :   V = QabM_indexrank(M, degpol(Pm)==1? NULL: Pm, ord_canon(ordw));
    6416        2331 :   return mkvec2(vf,gel(V,2));
    6417             : }
    6418             : static long
    6419       15764 : mfdihedralnewdim(long N, GEN CHI)
    6420             : {
    6421       15764 :   pari_sp av = avma;
    6422       15764 :   GEN S = mfdihedralnew_i(N, CHI);
    6423       15764 :   long d = S ? lg(gel(S,2))-1: 0;
    6424       15764 :   avma = av; return d;
    6425             : }
    6426             : static GEN
    6427        7665 : mfdihedralnew(long N, GEN CHI)
    6428             : {
    6429        7665 :   pari_sp av = avma;
    6430        7665 :   GEN S = mfdihedralnew_i(N, CHI);
    6431        7665 :   if (!S) { avma = av; return cgetg(1, t_VEC); }
    6432         777 :   return vecpermute(gel(S,1), gel(S,2));
    6433             : }
    6434             : 
    6435             : static long
    6436        7014 : mfdihedralcuspdim(long N, GEN CHI)
    6437             : {
    6438        7014 :   pari_sp av = avma;
    6439             :   GEN D, CHIP;
    6440             :   long F, i, lD, dim;
    6441             : 
    6442        7014 :   CHIP = mfchartoprimitive(CHI, &F);
    6443        7014 :   D = mydivisorsu(N/F); lD = lg(D);
    6444        7014 :   dim = mfdihedralnewdim(N, CHI); /* d = 1 */
    6445       15764 :   for (i = 2; i < lD; i++)
    6446             :   {
    6447        8750 :     long d = D[i], M = N/d, a = mfdihedralnewdim(M, CHIP);
    6448        8750 :     if (a) dim += a * mynumdivu(d);
    6449             :   }
    6450        7014 :   avma = av; return dim;
    6451             : }
    6452             : 
    6453             : static GEN
    6454        5446 : mfbdall(GEN E, long N)
    6455             : {
    6456        5446 :   GEN v, D = mydivisorsu(N);
    6457        5446 :   long i, j, nD = lg(D) - 1, nE = lg(E) - 1;
    6458        5446 :   v = cgetg(nD*nE + 1, t_VEC);
    6459        6965 :   for (j = 1; j <= nE; j++)
    6460             :   {
    6461        1519 :     GEN Ej = gel(E, j);
    6462        1519 :     for (i = 0; i < nD; i++) gel(v, i*nE + j) = mfbd_i(Ej, D[i+1]);
    6463             :   }
    6464        5446 :   return v;
    6465             : }
    6466             : static GEN
    6467        3381 : mfdihedralcusp(long N, GEN CHI)
    6468             : {
    6469        3381 :   pari_sp av = avma;
    6470             :   GEN D, CHIP, z;
    6471             :   long F, i, lD;
    6472             : 
    6473        3381 :   CHIP = mfchartoprimitive(CHI, &F);
    6474        3381 :   D = mydivisorsu(N/F); lD = lg(D);
    6475        3381 :   z = cgetg(lD, t_VEC);
    6476        3381 :   gel(z,1) = mfdihedralnew(N, CHI);
    6477        7609 :   for (i = 2; i < lD; i++) /* skip 1 */
    6478             :   {
    6479        4228 :     long d = D[i], M = N / d;
    6480        4228 :     GEN LF = mfdihedralnew(M, mfcharinduce(CHIP, M));
    6481        4228 :     gel(z,i) = mfbdall(LF, d);
    6482             :   }
    6483        3381 :   return gerepilecopy(av, shallowconcat1(z));
    6484             : }
    6485             : 
    6486             : /* used to decide between ratlift and comatrix for ZM_inv; ratlift is better
    6487             :  * when N has many divisors */
    6488             : static int
    6489        2296 : abundant(ulong N) { return mynumdivu(N) >= 8; }
    6490             : 
    6491             : /* CHI an mfchar */
    6492             : static int
    6493         294 : cmp_ord(void *E, GEN a, GEN b)
    6494             : {
    6495         294 :   GEN chia = MF_get_CHI(a), chib = MF_get_CHI(b);
    6496         294 :   (void)E; return cmpii(gmfcharorder(chia), gmfcharorder(chib));
    6497             : }
    6498             : /* mfinit structure.
    6499             : -- mf[1] contains [N,k,CHI,space],
    6500             : -- mf[2] contains vector of closures of Eisenstein series, empty if not
    6501             :    full space.
    6502             : -- mf[3] contains vector of closures, so #mf[3] = dimension of cusp/new space.
    6503             : -- mf[4] contains the corresponding indices: either j for T(j)tf if newspace,
    6504             :    or [M,j,d] for B(d)T(j)tf_M if cuspspace or oldspace.
    6505             : -- mf[5] contains the matrix M of first coefficients of basis, never cleaned.
    6506             :  * NK is either [N,k] or [N,k,CHI].
    6507             :  * mfinit does not do the splitting, only the basis generation. */
    6508             : 
    6509             : /* Set flraw to 1 if do not need mf[5]: no mftobasis etc..., only the
    6510             :    expansions of the basis elements are needed. */
    6511             : 
    6512             : static GEN
    6513        4480 : mfinit_Nkchi(long N, long k, GEN CHI, long space, long flraw)
    6514             : {
    6515        4480 :   GEN M = NULL, mf = NULL, mf1 = mkvec4(utoi(N), stoi(k), CHI, utoi(space));
    6516        4480 :   long sb = mfsturmNk(N, k);
    6517             :   cachenew_t cache;
    6518        4480 :   if (k < 0 || badchar(N, k, CHI)) return mfEMPTY(mf1);
    6519        4445 :   if (k == 0) /*nothing*/;
    6520        4403 :   else if (k == 1)
    6521             :   {
    6522         259 :     switch (space)
    6523             :     {
    6524             :       case mf_NEW:
    6525             :       case mf_FULL:
    6526         231 :       case mf_CUSP: mf = mfwt1init(N, CHI, NULL, space, flraw); break;
    6527          14 :       case mf_EISEN:break;
    6528           7 :       case mf_OLD: pari_err_IMPL("mfinit in weight 1 for old space");
    6529           7 :       default: pari_err_FLAG("mfinit");
    6530             :     }
    6531             :   }
    6532             :   else /* k >= 2 */
    6533             :   {
    6534        4144 :     long ord = mfcharorder_canon(CHI);
    6535        4144 :     GEN z = NULL, P = (ord == 1)? NULL: mfcharpol(CHI);
    6536        4144 :     switch(space)
    6537             :     {
    6538             :       case mf_EISEN:
    6539         105 :         break;
    6540             :       case mf_NEW:
    6541        1169 :         mf = mfnewinit(N, k, CHI, &cache, 1);
    6542        1169 :         if (mf && !flraw) { M = MF_get_M(mf); z = MF_get_Mindex(mf); }
    6543        1169 :         break;
    6544             :       case mf_OLD:
    6545             :       case mf_CUSP:
    6546             :       case mf_FULL:
    6547        2863 :         mf = mfinitcusp(N, k, CHI, &cache, space);
    6548        2863 :         if (mf && !flraw)
    6549             :         {
    6550        2037 :           GEN S = MF_get_S(mf);
    6551        2037 :           M = bhnmat_extend(M, sb+1, 1, S, &cache);
    6552        2037 :           if (space != mf_FULL) gel(mf,5) = mfcleanCHI(M, CHI, abundant(N));
    6553             :         }
    6554        2863 :         dbg_cachenew(&cache);
    6555        2863 :         break;
    6556           7 :       default: pari_err_FLAG("mfinit");
    6557             :     }
    6558        4137 :     if (z) gel(mf,5) = mfclean2(M, z, P, ord);
    6559             :   }
    6560        4424 :   if (!mf) mf = mfEMPTY(mf1);
    6561             :   else
    6562             :   {
    6563        3556 :     gel(mf,1) = mf1;
    6564        3556 :     if (flraw) gel(mf,5) = zerovec(3);
    6565             :   }
    6566        4424 :   if (!space_is_cusp(space))
    6567             :   {
    6568         630 :     GEN E = mfeisensteinbasis(N, k, CHI);
    6569         630 :     gel(mf,2) = E;
    6570         630 :     if (!flraw)
    6571             :     {
    6572         420 :       if (M)
    6573         161 :         M = shallowconcat(mfvectomat(E, sb+1, 1), M);
    6574             :       else
    6575         259 :         M = mfcoefs_mf(mf, sb+1, 1);
    6576         420 :       gel(mf,5) = mfcleanCHI(M, CHI, abundant(N));
    6577             :     }
    6578             :   }
    6579        4424 :   return mf;
    6580             : }
    6581             : 
    6582             : /* mfinit for k = nk/dk */
    6583             : static GEN
    6584        2401 : mfinit_Nndkchi(long N, long nk, long dk, GEN CHI, long space, long flraw)
    6585         210 : { return (dk == 2)? mf2init_Nkchi(N, nk >> 1, CHI, space, flraw)
    6586        2611 :                   : mfinit_Nkchi(N, nk, CHI, space, flraw); }
    6587             : static GEN
    6588        3066 : mfinit_i(GEN NK, long space)
    6589             : {
    6590             :   GEN CHI, mf;
    6591             :   long N, k, dk, joker;
    6592        3066 :   if (checkmf_i(NK))
    6593             :   {
    6594         126 :     N = mf_get_N(NK);
    6595         126 :     Qtoss(mf_get_gk(NK), &k, &dk);
    6596         126 :     CHI = mf_get_CHI(NK);
    6597             :   }
    6598        2940 :   else if ((mf = checkMF_i(NK)))
    6599             :   {
    6600          21 :     long s = MF_get_space(mf);
    6601          21 :     if (s == space) return mf;
    6602          21 :     Qtoss(MF_get_gk(mf), &k, &dk);
    6603          21 :     if (dk == 1 && k > 1 && space == mf_NEW && (s == mf_CUSP || s == mf_FULL))
    6604          21 :       return mfinittonew(mf);
    6605           0 :     N = MF_get_N(mf);
    6606           0 :     CHI = MF_get_CHI(mf);
    6607             :   }
    6608             :   else
    6609        2919 :     checkNK2(NK, &N, &k, &dk, &CHI, 1);
    6610        3024 :   joker = !CHI || typ(CHI) == t_COL;
    6611        3024 :   if (joker)
    6612             :   {
    6613        1141 :     GEN mf, vCHI = CHI;
    6614             :     long i, j, l;
    6615        1141 :     if (CHI && lg(CHI) == 1) return cgetg(1,t_VEC);
    6616        1134 :     if (k < 0) return mfEMPTYall(N, sstoQ(k,dk), CHI, space);
    6617        1120 :     if (k == 1 && dk == 1 && space != mf_EISEN)
    6618         483 :     {
    6619             :       GEN TMP, gN, gs;
    6620        1085 :       if (space != mf_CUSP && space != mf_NEW)
    6621           0 :         pari_err_IMPL("mfinit([N,1,wildcard], space != cusp or new space)");
    6622        1085 :       if (wt1empty(N)) return mfEMPTYall(N, gen_1, CHI, space);
    6623         483 :       vCHI = mfwt1chars(N,vCHI);
    6624         483 :       l = lg(vCHI); mf = cgetg(l, t_VEC); if (l == 1) return mf;
    6625         483 :       TMP = mfwt1_pre(N); gN = utoipos(N); gs = utoi(space);
    6626        3717 :       for (i = j = 1; i < l; i++)
    6627             :       {
    6628        3234 :         GEN c = gel(vCHI,i), z = mfwt1init(N, c, TMP, space, 0);
    6629        3234 :         if (CHI && !z) z = mfEMPTY(mkvec4(gN,gen_1,c,gs));
    6630        3234 :         if (z) gel(mf, j++) = z;
    6631             :       }
    6632             :     }
    6633             :     else
    6634             :     {
    6635          35 :       vCHI = mfchars(N,k,dk,vCHI);
    6636          35 :       l = lg(vCHI); mf = cgetg(l, t_VEC);
    6637         119 :       for (i = j = 1; i < l; i++)
    6638             :       {
    6639          84 :         GEN v = mfinit_Nndkchi(N, k, dk, gel(vCHI,i), space, 0);
    6640          84 :         if (MF_get_dim(v) || CHI) gel(mf, j++) = v;
    6641             :       }
    6642             :     }
    6643         518 :     setlg(mf,j);
    6644         518 :     if (!CHI) gen_sort_inplace(mf, NULL, &cmp_ord, NULL);
    6645         518 :     return mf;
    6646             :   }
    6647        1883 :   return mfinit_Nndkchi(N, k, dk, CHI, space, 0);
    6648             : }
    6649             : GEN
    6650        2114 : mfinit(GEN NK, long space)
    6651             : {
    6652        2114 :   pari_sp av = avma;
    6653        2114 :   return gerepilecopy(av, mfinit_i(NK, space));
    6654             : }
    6655             : 
    6656             : /* UTILITY FUNCTIONS */
    6657             : static void
    6658         357 : cusp_canon(GEN cusp, long N, long *pA, long *pC)
    6659             : {
    6660         357 :   pari_sp av = avma;
    6661             :   long A, C, tc, cg;
    6662         357 :   if (N <= 0) pari_err_DOMAIN("mfcuspwidth","N","<=",gen_0,stoi(N));
    6663         350 :   if (!cusp || (tc = typ(cusp)) == t_INFINITY) { *pA = 1; *pC = N; return; }
    6664         343 :   if (tc != t_INT && tc != t_FRAC) pari_err_TYPE("checkcusp", cusp);
    6665         343 :   Qtoss(cusp, &A,&C);
    6666         343 :   if (N % C)
    6667             :   {
    6668             :     ulong uC;
    6669          14 :     long u = Fl_invgen((C-1)%N + 1, N, &uC);
    6670          14 :     A = Fl_mul(A, u, N);
    6671          14 :     C = (long)uC;
    6672             :   }
    6673         343 :   cg = ugcd(C, N/C);
    6674         343 :   while (ugcd(A, N) > 1) A += cg;
    6675         343 :   *pA = A % N; *pC = C; avma = av;
    6676             : }
    6677             : static long
    6678         798 : mfcuspcanon_width(long N, long C)
    6679         798 : { return (!C || C == N)? 1 : N / ugcd(N, Fl_sqr(umodsu(C,N),N)); }
    6680             : /* v = [a,c] a ZC, width of cusp (a:c) */
    6681             : static long
    6682        6559 : mfZC_width(long N, GEN v)
    6683             : {
    6684        6559 :   ulong C = umodiu(gel(v,2), N);
    6685        6559 :   return (C == 0)? 1: N / ugcd(N, Fl_sqr(C,N));
    6686             : }
    6687             : long
    6688         161 : mfcuspwidth(GEN gN, GEN cusp)
    6689             : {
    6690         161 :   long N = 0, A, C;
    6691             :   GEN mf;
    6692         161 :   if (typ(gN) == t_INT) N = itos(gN);
    6693          42 :   else if ((mf = checkMF_i(gN))) N = MF_get_N(mf);
    6694           0 :   else pari_err_TYPE("mfcuspwidth", gN);
    6695         161 :   cusp_canon(cusp, N, &A, &C);
    6696         154 :   return mfcuspcanon_width(N, C);
    6697             : }
    6698             : 
    6699             : /* Q a t_INT */
    6700             : static GEN
    6701          14 : findq(GEN al, GEN Q)
    6702             : {
    6703             :   long n;
    6704          14 :   if (typ(al) == t_FRAC && cmpii(gel(al,2), Q) <= 0)
    6705           0 :     return mkvec(mkvec2(gel(al,1), gel(al,2)));
    6706          14 :   n = 1 + (long)ceil(2.0781*gtodouble(glog(Q, LOWDEFAULTPREC)));
    6707          14 :   return contfracpnqn(gboundcf(al,n), n);
    6708             : }
    6709             : static GEN
    6710          91 : findqga(long N, GEN z)
    6711             : {
    6712          91 :   GEN Q, LDC, CK = NULL, DK = NULL, ma, x, y = imag_i(z);
    6713             :   long j, l;
    6714          91 :   if (gcmpgs(gmulsg(2*N, y), 1) >= 0) return NULL;
    6715          14 :   x = real_i(z);
    6716          14 :   Q = ground(ginv(gsqrt(gmulsg(N, y), LOWDEFAULTPREC)));
    6717          14 :   LDC = findq(gmulsg(-N,x), Q);
    6718          14 :   ma = gen_1; l = lg(LDC);
    6719          35 :   for (j = 1; j < l; j++)
    6720             :   {
    6721          21 :     GEN D, DC = gel(LDC,j), C1 = gel(DC,2);
    6722          21 :     if (cmpii(C1,Q) > 0) break;
    6723          21 :     D = gel(DC,1);
    6724          21 :     if (ugcdiu(D,N) == 1)
    6725             :     {
    6726           7 :       GEN C = mului(N, C1), den;
    6727           7 :       den = gadd(gsqr(gmul(C,y)), gsqr(gadd(D, gmul(C,x))));
    6728           7 :       if (gcmp(den, ma) < 0) { ma = den; CK = C; DK = D; }
    6729             :     }
    6730             :   }
    6731          14 :   return DK? mkvec2(CK, DK): NULL;
    6732             : }
    6733             : 
    6734             : static long
    6735          70 : valNC2(GEN P, GEN E, long e)
    6736             : {
    6737          70 :   long i, d = 1, l = lg(P);
    6738         168 :   for (i = 1; i < l; i++)
    6739             :   {
    6740          98 :     long v = u_lval(e, P[i]) << 1;
    6741          98 :     if (v == E[i] + 1) v--;
    6742          98 :     d *= upowuu(P[i], v);
    6743             :   }
    6744          70 :   return d;
    6745             : }
    6746             : 
    6747             : static GEN
    6748          28 : findqganew(long N, GEN z)
    6749             : {
    6750          28 :   GEN MI, DI, x = real_i(z), y = imag_i(z), Ck = gen_0, Dk = gen_1, fa, P, E;
    6751             :   long i;
    6752          28 :   MI = ginv(utoi(N));
    6753          28 :   DI = mydivisorsu(mysqrtu(N));
    6754          28 :   fa = myfactoru(N); P = gel(fa,1); E = gel(fa,2);
    6755          98 :   for (i = 1; i < lg(DI); i++)
    6756             :   {
    6757          70 :     long e = DI[i], g;
    6758             :     GEN U, C, D, m;
    6759          70 :     (void)cxredsl2(gmulsg(e, z), &U);
    6760          70 :     C = gcoeff(U,2,1); if (!signe(C)) continue;
    6761          70 :     D = gcoeff(U,2,2);
    6762          70 :     g = ugcdiu(D,e);
    6763          70 :     if (g > 1) { C = muliu(C,e/g); D = diviuexact(D,g); } else C = muliu(C,e);
    6764          70 :     m = gadd(gsqr(gadd(gmul(C, x), D)), gsqr(gmul(C, y)));
    6765          70 :     m = gdivgs(m, valNC2(P, E, e));
    6766          70 :     if (gcmp(m, MI) < 0) { MI = m; Ck = C; Dk = D; }
    6767             :   }
    6768          28 :   return signe(Ck)? mkvec2(Ck, Dk): NULL;
    6769             : }
    6770             : 
    6771             : /* Return z' and U = [a,b;c,d] \in SL_2(Z), z' = U*z,
    6772             :  * Im(z')/width(U.oo) > sqrt(3)/(2N). Set *pczd = c*z+d */
    6773             : static GEN
    6774         154 : cxredga0N(long N, GEN z, GEN *pU, GEN *pczd, long flag)
    6775             : {
    6776         154 :   GEN v = NULL, A, B, C, D;
    6777             :   long e;
    6778         154 :   if (N == 1) return cxredsl2_i(z, pU, pczd);
    6779         119 :   e = gexpo(gel(z,2));
    6780         119 :   if (e < 0) z = gprec_wensure(z, precision(z) + nbits2extraprec(-e));
    6781         119 :   v = flag? findqganew(N,z): findqga(N,z);
    6782         119 :   if (!v) { *pU = matid(2); *pczd = gen_1; return z; }
    6783          35 :   C = gel(v,1);
    6784          35 :   D = gel(v,2);
    6785          35 :   if (!is_pm1(bezout(C,D, &B,&A))) pari_err_BUG("cxredga0N [gcd > 1]");
    6786          35 :   B = negi(B);
    6787          35 :   *pU = mkmat2(mkcol2(A,C), mkcol2(B,D));
    6788          35 :   *pczd = gadd(gmul(C,z), D);
    6789          35 :   return gdiv(gadd(gmul(A,z), B), *pczd);
    6790             : }
    6791             : 
    6792             : static GEN
    6793         147 : lfunthetaall(GEN b, GEN vL, GEN t, long bitprec)
    6794             : {
    6795         147 :   long i, l = lg(vL);
    6796         147 :   GEN v = cgetg(l, t_VEC);
    6797         322 :   for (i = 1; i < l; i++)
    6798             :   {
    6799         175 :     GEN T, L = gel(vL,i), a0 = gel(L,1), ldata = gel(L,2);
    6800         175 :     GEN van = gel(ldata_get_an(ldata),2);
    6801         175 :     if (lg(van) == 1)
    6802             :     {
    6803           0 :       T = gmul(b, a0);
    6804           0 :       if (isexactzero(T)) { GEN z = real_0_bit(-bitprec); T = mkcomplex(z,z); }
    6805             :     }
    6806             :     else
    6807             :     {
    6808         175 :       T = gmul2n(lfuntheta(ldata, t, 0, bitprec), -1);
    6809         175 :       T = gmul(b, gadd(a0, T));
    6810             :     }
    6811         175 :     gel(v,i) = T;
    6812             :   }
    6813         147 :   return l == 2? gel(v,1): v;
    6814             : }
    6815             : 
    6816             : /* P in ZX */
    6817             : static GEN
    6818         154 : ZX_roots(GEN P, long prec)
    6819             : {
    6820         154 :   long d = degpol(P);
    6821         154 :   if (d == 1) return mkvec(gen_0);
    6822         154 :   if (d == 2 && isint1(gel(P,2)) && isintzero(gel(P,3)) && isint1(gel(P,4)))
    6823           7 :     return mkvec2(powIs(3), gen_I()); /* order as polroots */
    6824         147 :   return (ZX_sturm(P) == d)? realroots(P,NULL,prec): QX_complex_roots(P,prec);
    6825             : }
    6826             : /* initializations for RgX_RgV_eval / RgC_embed */
    6827             : static GEN
    6828         189 : rootspowers(GEN v)
    6829             : {
    6830         189 :   long i, l = lg(v);
    6831         189 :   GEN w = cgetg(l, t_VEC);
    6832         189 :   for (i = 1; i < l; i++) gel(w,i) = gpowers(gel(v,i), l-2);
    6833         189 :   return w;
    6834             : }
    6835             : /* mf embeddings attached to Q(chi)/(T), chi attached to cyclotomic P */
    6836             : static GEN
    6837         798 : getembed(GEN P, GEN T, GEN zcyclo, long prec)
    6838             : {
    6839             :   long i, l;
    6840             :   GEN v;
    6841         798 :   if (degpol(P) == 1) P = NULL; /* mfcharpol for quadratic char */
    6842         798 :   if (degpol(T) == 1) T = NULL; /* dim 1 orbit */
    6843         798 :   if (T && P)
    6844          35 :   { /* K(y) / (T(y)), K = Q(t)/(P) cyclotomic */
    6845          35 :     GEN vr = RgX_is_ZX(T)? ZX_roots(T,prec): roots(RgX_embed1(T,zcyclo), prec);
    6846          35 :     v = rootspowers(vr); l = lg(v);
    6847          35 :     for (i = 1; i < l; i++) gel(v,i) = mkcol3(P,zcyclo,gel(v,i));
    6848             :   }
    6849         763 :   else if (T)
    6850             :   { /* Q(y) / (T(y)), T non-cyclotomic */
    6851         154 :     GEN vr = ZX_roots(T, prec);
    6852         154 :     v = rootspowers(vr); l = lg(v);
    6853         154 :     for (i = 1; i < l; i++) gel(v,i) = mkcol2(T, gel(v,i));
    6854             :   }
    6855             :   else /* cyclotomic or rational */
    6856         609 :     v = mkvec(P? mkvec2(P, zcyclo): cgetg(1,t_VEC));
    6857         798 :   return v;
    6858             : }
    6859             : static GEN
    6860         658 : grootsof1_CHI(GEN CHI, long prec)
    6861         658 : { return grootsof1(mfcharorder_canon(CHI), prec); }
    6862             : /* return the [Q(F):Q(chi)] embeddings of F */
    6863             : static GEN
    6864         511 : mfgetembed(GEN F, long prec)
    6865             : {
    6866         511 :   GEN T = mf_get_field(F), CHI = mf_get_CHI(F), P = mfcharpol(CHI);
    6867         511 :   return getembed(P, T, grootsof1_CHI(CHI, prec), prec);
    6868             : }
    6869             : static GEN
    6870           7 : mfchiembed(GEN mf, long prec)
    6871             : {
    6872           7 :   GEN CHI = MF_get_CHI(mf), P = mfcharpol(CHI);
    6873           7 :   return getembed(P, pol_x(0), grootsof1_CHI(CHI, prec), prec);
    6874             : }
    6875             : /* mfgetembed for the successive eigenforms in MF_get_newforms */
    6876             : static GEN
    6877         140 : mfeigenembed(GEN mf, long prec)
    6878             : {
    6879         140 :   GEN vP = MF_get_fields(mf), CHI = MF_get_CHI(mf), P = mfcharpol(CHI);
    6880         140 :   long i, l = lg(vP);
    6881         140 :   GEN zcyclo = grootsof1_CHI(CHI, prec), vE = cgetg(l, t_VEC);
    6882         140 :   for (i = 1; i < l; i++) gel(vE,i) = getembed(P, gel(vP,i), zcyclo, prec);
    6883         140 :   return vE;
    6884             : }
    6885             : 
    6886             : static int
    6887          28 : checkPv(GEN P, GEN v)
    6888          28 : { return typ(P) == t_POL && typ(v) == t_VEC && lg(v)-1 >= degpol(P); }
    6889             : static int
    6890          28 : checkemb_i(GEN E)
    6891             : {
    6892          28 :   long t = typ(E), l = lg(E);
    6893          28 :   if (t == t_VEC) return l == 1 || (l == 3 && checkPv(gel(E,1), gel(E,2)));
    6894          21 :   if (t != t_COL) return 0;
    6895          21 :   if (l == 3) return checkPv(gel(E,1), gel(E,2));
    6896          21 :   return l == 4 && typ(gel(E,2)) == t_VEC && checkPv(gel(E,1), gel(E,3));
    6897             : }
    6898             : static GEN
    6899          28 : anyembed(GEN v, GEN E)
    6900             : {
    6901          28 :   switch(typ(v))
    6902             :   {
    6903          21 :     case t_VEC: case t_COL: return mfvecembed(E, v);
    6904           7 :     case t_MAT: return mfmatembed(E, v);
    6905             :   }
    6906           0 :   return mfembed(E, v);
    6907             : }
    6908             : GEN
    6909          49 : mfembed0(GEN E, GEN v, long prec)
    6910             : {
    6911          49 :   pari_sp av = avma;
    6912          49 :   GEN mf, vE = NULL;
    6913          49 :   if (checkmf_i(E)) vE = mfgetembed(E, prec);
    6914          35 :   else if ((mf = checkMF_i(E))) vE = mfchiembed(mf, prec);
    6915          49 :   if (vE)
    6916             :   {
    6917          21 :     long i, l = lg(vE);
    6918             :     GEN w;
    6919          21 :     if (!v) return gerepilecopy(av, l == 2? gel(vE,1): vE);
    6920           0 :     w = cgetg(l, t_VEC);
    6921           0 :     for (i = 1; i < l; i++) gel(w,i) = anyembed(v, gel(vE,i));
    6922           0 :     return gerepilecopy(av, l == 2? gel(w,1): w);
    6923             :   }
    6924          28 :   if (!checkemb_i(E) || !v) pari_err_TYPE("mfembed", E);
    6925          28 :   return gerepilecopy(av, anyembed(v,E));
    6926             : }
    6927             : 
    6928             : /* dummy lfun create for theta evaluation */
    6929             : static GEN
    6930         700 : mfthetaancreate(GEN van, GEN N, GEN k)
    6931             : {
    6932         700 :   GEN L = zerovec(6);
    6933         700 :   gel(L,1) = lfuntag(t_LFUN_GENERIC, van);
    6934         700 :   gel(L,3) = mkvec2(gen_0, gen_1);
    6935         700 :   gel(L,4) = k;
    6936         700 :   gel(L,5) = N; return L;
    6937             : }
    6938             : /* destroy van and prepare to evaluate theta(sigma(van)), for all sigma in
    6939             :  * embeddings vector vE */
    6940             : static GEN
    6941         287 : van_embedall(GEN van, GEN vE, GEN gN, GEN gk)
    6942             : {
    6943         287 :   GEN a0 = gel(van,1), vL;
    6944         287 :   long i, lE = lg(vE), l = lg(van);
    6945         287 :   van++; van[0] = evaltyp(t_VEC) | evallg(l-1); /* remove a0 */
    6946         287 :   vL = cgetg(lE, t_VEC);
    6947         658 :   for (i = 1; i < lE; i++)
    6948             :   {
    6949         371 :     GEN E = gel(vE,i), v = mfvecembed(E, van);
    6950         371 :     gel(vL,i) = mkvec2(mfembed(E,a0), mfthetaancreate(v, gN, gk));
    6951             :   }
    6952         287 :   return vL;
    6953             : }
    6954             : 
    6955             : static int
    6956         973 : cusp_AC(GEN cusp, long *A, long *C)
    6957             : {
    6958         973 :   switch(typ(cusp))
    6959             :   {
    6960          84 :     case t_INFINITY: *A = 1; *C = 0; break;
    6961         273 :     case t_INT:  *A = itos(cusp); *C = 1; break;
    6962         420 :     case t_FRAC: *A = itos(gel(cusp, 1)); *C = itos(gel(cusp, 2)); break;
    6963             :     case t_REAL: case t_COMPLEX:
    6964         196 :       *A = 0; *C = 0;
    6965         196 :       if (gsigne(imag_i(cusp)) <= 0)
    6966           7 :         pari_err_DOMAIN("mfeval","imag(tau)","<=",gen_0,cusp);
    6967         189 :       return 0;
    6968           0 :     default: pari_err_TYPE("cusp_AC", cusp);
    6969             :   }
    6970         777 :   return 1;
    6971             : }
    6972             : static GEN
    6973         511 : cusp2mat(long A, long C)
    6974             : { long B, D;
    6975         511 :   cbezout(A, C, &D, &B);
    6976         511 :   return mkmat22s(A, -B, C, D);
    6977             : }
    6978             : static GEN
    6979           7 : mkS(void) { return mkmat22s(0,-1,1,0); }
    6980             : 
    6981             : /* if t is a cusp, return F(t), else NULL */
    6982             : static GEN
    6983         343 : evalcusp(GEN mf, GEN F, GEN t, long prec)
    6984             : {
    6985             :   long A, C;
    6986             :   GEN R;
    6987         343 :   if (!cusp_AC(t, &A,&C)) return NULL;
    6988         189 :   if (C % mf_get_N(F) == 0) return gel(mfcoefs_i(F, 0, 1), 1);
    6989         175 :   R = mfgaexpansion(mf, F, cusp2mat(A,C), 0, prec);
    6990         175 :   return gequal0(gel(R,1))? gmael(R,3,1): gen_0;
    6991             : }
    6992             : /* Evaluate an mf closure numerically, i.e., in the usual sense, either for a
    6993             :  * single tau or a vector of tau; for each, return a vector of results
    6994             :  * corresponding to all complex embeddings of F. If flag is non-zero, allow
    6995             :  * replacing F by F | gamma to increase imag(gamma^(-1).tau) [ expensive if
    6996             :  * MF_EISENSPACE not present ] */
    6997             : static GEN
    6998         154 : mfeval_i(GEN mf, GEN F, GEN vtau, long flag, long bitprec)
    6999             : {
    7000             :   GEN L0, vL, vb, sqN, vczd, vTAU, vs, van, vE;
    7001         154 :   long N = mf_get_N(F), N0, ta, lv, i, prec = nbits2prec(bitprec);
    7002         154 :   GEN gN = utoipos(N), gk = mf_get_gk(F), gk1 = gsubgs(gk,1), vgk;
    7003         154 :   long flscal = 0;
    7004             : 
    7005             :   /* gen_0 is ignored, second component assumes Ramanujan-Petersson in
    7006             :    * 1/2-integer weight */
    7007         154 :   vgk = mkvec2(gen_0, mfiscuspidal(mf,F)? gmul2n(gk1,-1): gk1);
    7008         154 :   ta = typ(vtau);
    7009         154 :   if (!is_vec_t(ta)) { flscal = 1; vtau = mkvec(vtau); ta = t_VEC; }
    7010         154 :   lv = lg(vtau);
    7011         154 :   sqN = sqrtr_abs(utor(N, prec));
    7012         154 :   vs = const_vec(lv-1, NULL);
    7013         154 :   vb = const_vec(lv-1, NULL);
    7014         154 :   vL = cgetg(lv, t_VEC);
    7015         154 :   vTAU = cgetg(lv, t_VEC);
    7016         154 :   vczd = cgetg(lv, t_VEC);
    7017         154 :   L0 = mfthetaancreate(NULL, gN, vgk); /* only for thetacost */
    7018         154 :   vE = mfgetembed(F, prec);
    7019         154 :   N0 = 0;
    7020         329 :   for (i = 1; i < lv; i++)
    7021             :   {
    7022         182 :     GEN z = gel(vtau,i), tau, U;
    7023             :     long w, n;
    7024             : 
    7025         182 :     gel(vs,i) = evalcusp(mf, F, z, prec);
    7026         175 :     if (gel(vs,i)) continue;
    7027         147 :     tau = cxredga0N(N, z, &U, &gel(vczd,i), flag);
    7028         147 :     if (!flag) w = 0; else { w = mfZC_width(N, gel(U,1)); tau = gdivgs(tau,w); }
    7029         147 :     gel(vTAU,i) = mulcxmI(gmul(tau, sqN));
    7030         147 :     n = lfunthetacost(L0, real_i(gel(vTAU,i)), 0, bitprec);
    7031         147 :     if (N0 < n) N0 = n;
    7032         147 :     if (flag)
    7033             :     {
    7034          35 :       GEN A, al, v = mfslashexpansion(mf, F, ZM_inv(U,NULL), N0, 0, &A, prec);
    7035          35 :       gel(vL,i) = van_embedall(v, vE, gN, vgk);
    7036          35 :       al = gel(A,1);
    7037          35 :       if (!gequal0(al))
    7038           7 :         gel(vb,i) = gexp(gmul(gmul(gmulsg(w,al),PiI2(prec)), tau), prec);
    7039             :     }
    7040             :   }
    7041         147 :   if (!flag)
    7042             :   {
    7043         112 :     van = mfcoefs_i(F, N0, 1);
    7044         112 :     vL = const_vec(lv-1, van_embedall(van, vE, gN, vgk));
    7045             :   }
    7046         322 :   for (i = 1; i < lv; i++)
    7047             :   {
    7048             :     GEN T;
    7049         175 :     if (gel(vs,i)) continue;
    7050         147 :     T = gpow(gel(vczd,i), gneg(gk), prec);
    7051         147 :     if (flag && gel(vb,i)) T = gmul(T, gel(vb,i));
    7052         147 :     gel(vs,i) = lfunthetaall(T, gel(vL,i), gel(vTAU,i), bitprec);
    7053             :   }
    7054         147 :   return flscal? gel(vs,1): vs;
    7055             : }
    7056             : 
    7057             : static long
    7058        1064 : mfistrivial(GEN F)
    7059             : {
    7060        1064 :   switch(mf_get_type(F))
    7061             :   {
    7062           7 :     case t_MF_CONST: return lg(gel(F,2)) == 1;
    7063         224 :     case t_MF_LINEAR: case t_MF_LINEAR_BHN: return gequal0(gel(F,3));
    7064         833 :     default: return 0;
    7065             :   }
    7066             : }
    7067             : 
    7068             : /* check parameters rigorously, but not coefficients */
    7069             : static long
    7070         889 : mfisinspace_i(GEN mf, GEN F)
    7071             : {
    7072             :   GEN CHI1, CHI2, chi1, chi2, F1, F2, gk;
    7073         889 :   long Nmf, N, space = MF_get_space(mf);
    7074             : 
    7075         889 :   if (mfistrivial(F)) return 1;
    7076         882 :   N = mf_get_N(F);
    7077         882 :   Nmf = MF_get_N(mf);
    7078         882 :   if (space == mf_NEW)
    7079         224 :   { if (N != Nmf) return 0; }
    7080             :   else
    7081         658 :   { if (Nmf % N) return 0; }
    7082         819 :   gk = mf_get_gk(F);
    7083         819 :   if (!gequal(MF_get_gk(mf), gk)) return 0;
    7084         819 :   CHI2 = mf_get_CHI(F);
    7085         819 :   CHI1 = MF_get_CHI(mf);
    7086             :   /* are the primitive chars attached to CHI1 and CHI2 equal ? */
    7087         819 :   F1 = znconreyconductor(gel(CHI1,1), gel(CHI1,2), &chi1);
    7088         819 :   if (typ(F1) == t_VEC) F1 = gel(F1,1);
    7089         819 :   F2 = znconreyconductor(gel(CHI2,1), gel(CHI2,2), &chi2);
    7090         819 :   if (typ(F2) == t_VEC) F2 = gel(F2,1);
    7091         819 :   return equalii(F1,F2) && ZV_equal(chi1,chi2);
    7092             : }
    7093             : static void
    7094           7 : err_space(GEN F)
    7095           7 : { pari_err_DOMAIN("mftobasis", "form", "does not belong to",
    7096           0 :                   strtoGENstr("space"), F); }
    7097             : 
    7098             : static long
    7099         140 : mfcheapeisen(GEN mf)
    7100             : {
    7101         140 :   long k, L, N = MF_get_N(mf);
    7102             :   GEN P;
    7103         140 :   if (N <= 70) return 1;
    7104          84 :   k = itos(gceil(MF_get_gk(mf)));
    7105          84 :   if (odd(k)) k--;
    7106          84 :   switch (k)
    7107             :   {
    7108           0 :     case 2:  L = 190; break;
    7109          14 :     case 4:  L = 162; break;
    7110             :     case 6:
    7111          70 :     case 8:  L = 88; break;
    7112           0 :     case 10: L = 78; break;
    7113           0 :     default: L = 66; break;
    7114             :   }
    7115          84 :   P = gel(myfactoru(N), 1);
    7116          84 :   return P[lg(P)-1] <= L;
    7117             : }
    7118             : 
    7119             : static GEN
    7120         175 : myimag_i(GEN tau)
    7121             : {
    7122         175 :   long tc = typ(tau);
    7123         175 :   if (tc == t_INFINITY || tc == t_INT || tc == t_FRAC)
    7124          28 :     return gen_1;
    7125         147 :   if (tc == t_VEC)
    7126             :   {
    7127             :     long ltau, i;
    7128           7 :     GEN z = cgetg_copy(tau, &ltau);
    7129           7 :     for (i=1; i<ltau; i++) gel(z,i) = myimag_i(gel(tau,i));
    7130           7 :     return z;
    7131             :   }
    7132         140 :   return imag_i(tau);
    7133             : }
    7134             : 
    7135             : static GEN
    7136         140 : mintau(GEN vtau)
    7137             : {
    7138         140 :   if (!is_vec_t(typ(vtau))) return myimag_i(vtau);
    7139           7 :   return (lg(vtau) == 1)? gen_1: vecmin(myimag_i(vtau));
    7140             : }
    7141             : 
    7142             : /* initialization for mfgaexpansion: what does not depend on cusp */
    7143             : static GEN
    7144         819 : mf_eisendec(GEN mf, GEN F, long prec)
    7145             : {
    7146         819 :   GEN B = liftpol_shallow(mfeisensteindec(mf, F)), v = variables_vecsmall(B);
    7147         819 :   GEN Mvecj = obj_check(mf, MF_EISENSPACE);
    7148         819 :   long l = lg(v), i, ord;
    7149         819 :   if (lg(Mvecj) < 5) Mvecj = gel(Mvecj,1);
    7150         819 :   ord = itou(gel(Mvecj,4));
    7151         875 :   for (i = 1; i < l; i++)
    7152         630 :     if (v[i] != 1) { B = gsubst(B, v[i], rootsof1u_cx(ord, prec)); break; }
    7153         819 :   return B;
    7154             : }
    7155             : 
    7156             : GEN
    7157         154 : mfeval(GEN mf0, GEN F, GEN vtau, long bitprec)
    7158             : {
    7159         154 :   pari_sp av = avma;
    7160         154 :   long flnew = 1;
    7161         154 :   GEN mf = checkMF_i(mf0);
    7162         154 :   if (!mf) pari_err_TYPE("mfeval", mf0);
    7163         154 :   if (!checkmf_i(F)) pari_err_TYPE("mfeval", F);
    7164         154 :   if (!mfisinspace_i(mf, F)) err_space(F);
    7165         154 :   if (!obj_check(mf, MF_EISENSPACE)) flnew = mfcheapeisen(mf);
    7166         154 :   if (flnew && gcmpgs(gmulsg(2*MF_get_N(mf), mintau(vtau)), 1) >= 0) flnew = 0;
    7167         154 :   return gerepilecopy(av, mfeval_i(mf, F, vtau, flnew, bitprec));
    7168             : }
    7169             : 
    7170             : static long
    7171         182 : val(GEN v, long bit)
    7172             : {
    7173         182 :   long c, l = lg(v);
    7174         399 :   for (c = 1; c < l; c++)
    7175         385 :     if (gexpo(gel(v,c)) > -bit) return c-1;
    7176          14 :   return -1;
    7177             : }
    7178             : GEN
    7179         196 : mfcuspval(GEN mf, GEN F, GEN cusp, long bitprec)
    7180             : {
    7181         196 :   pari_sp av = avma;
    7182         196 :   long lvE, w, N, sb, n, A, C, prec = nbits2prec(bitprec);
    7183             :   GEN ga, gk, vE;
    7184         196 :   mf = checkMF(mf);
    7185         196 :   if (!checkmf_i(F)) pari_err_TYPE("mfcuspval",F);
    7186         196 :   N = MF_get_N(mf);
    7187         196 :   cusp_canon(cusp, N, &A, &C);
    7188         196 :   gk = mf_get_gk(F);
    7189         196 :   if (typ(gk) != t_INT)
    7190             :   {
    7191          42 :     GEN FT = mfmultheta(F), mf2 = obj_checkbuild(mf, MF_MF2INIT, &mf2init);
    7192          42 :     GEN r = mfcuspval(mf2, FT, cusp, bitprec);
    7193          42 :     if ((C & 3L) == 2)
    7194             :     {
    7195          14 :       GEN z = sstoQ(1,4);
    7196          14 :       r = gsub(r, typ(r) == t_VEC? const_vec(lg(r)-1, z): z);
    7197             :     }
    7198          42 :     return gerepileupto(av, r);
    7199             :   }
    7200         154 :   vE = mfgetembed(F, prec);
    7201         154 :   lvE = lg(vE);
    7202         154 :   w = mfcuspcanon_width(N, C);
    7203         154 :   sb = w * mfsturmNk(N, itos(gk));
    7204         154 :   ga = cusp2mat(A,C);
    7205         161 :   for (n = 8;; n = minss(sb, n << 1))
    7206           7 :   {
    7207         161 :     GEN R = mfgaexpansion(mf, F, ga, n, prec), res = liftpol_shallow(gel(R,3));
    7208         161 :     GEN v = cgetg(lvE-1, t_VECSMALL);
    7209         161 :     long j, ok = 1;
    7210         161 :     res = RgC_embedall(res, vE);
    7211         343 :     for (j = 1; j < lvE; j++)
    7212             :     {
    7213         182 :       v[j] = val(gel(res,j), bitprec/2);
    7214         182 :       if (v[j] < 0) ok = 0;
    7215             :     }
    7216         161 :     if (ok)
    7217             :     {
    7218         147 :       res = cgetg(lvE, t_VEC);
    7219         147 :       for (j = 1; j < lvE; j++) gel(res,j) = gadd(gel(R,1), sstoQ(v[j], w));
    7220         147 :       return gerepilecopy(av, lvE==2? gel(res,1): res);
    7221             :     }
    7222          14 :     if (n == sb) return lvE==2? mkoo(): const_vec(lvE-1, mkoo()); /* 0 */
    7223             :   }
    7224             : }
    7225             : 
    7226             : long
    7227         196 : mfiscuspidal(GEN mf, GEN F)
    7228             : {
    7229         196 :   pari_sp av = avma;
    7230             :   GEN mf2;
    7231         196 :   if (space_is_cusp(MF_get_space(mf))) return 1;
    7232          77 :   if (typ(mf_get_gk(F)) == t_INT)
    7233             :   {
    7234          49 :     GEN v = mftobasis(mf, F, 0);
    7235          49 :     long s = gequal0(vecslice(v, 1, lg(MF_get_E(mf)) - 1));
    7236          49 :     avma = av; return s;
    7237             :   }
    7238          28 :   if (!gequal0(mfak_i(F, 0))) return 0;
    7239          14 :   mf2 = obj_checkbuild(mf, MF_MF2INIT, &mf2init);
    7240          14 :   return mfiscuspidal(mf2, mfmultheta(F));
    7241             : }
    7242             : 
    7243             : /* F = vector of newforms in mftobasis format */
    7244             : static GEN
    7245          70 : mffrickeeigen_i(GEN mf, GEN F, GEN vE, long prec)
    7246             : {
    7247          70 :   GEN M, Z, L0, gN = MF_get_gN(mf), gk = MF_get_gk(mf);
    7248          70 :   long N0, i, lM, bit = prec2nbits(prec), k = itou(gk);
    7249          70 :   long LIM = 5; /* Sturm bound is enough */
    7250             : 
    7251          70 :   L0 = mfthetaancreate(NULL, gN, gk); /* only for thetacost */
    7252             : START:
    7253          70 :   N0 = lfunthetacost(L0, gen_1, LIM, bit);
    7254          70 :   M = mfcoefs_mf(mf, N0, 1);
    7255          70 :   lM = lg(F);
    7256          70 :   Z = cgetg(lM, t_VEC);
    7257         210 :   for (i = 1; i < lM; i++)
    7258             :   { /* expansion of D * F[i] */
    7259         140 :     GEN D, z, van = RgM_RgC_mul(M, Q_remove_denom(gel(F,i), &D));
    7260         140 :     GEN L = van_embedall(van, gel(vE,i), gN, gk);
    7261         140 :     long l = lg(L), j, bit_add = D? expi(D): 0;
    7262         140 :     gel(Z,i) = z = cgetg(l, t_VEC);
    7263         336 :     for (j = 1; j < l; j++)
    7264             :     {
    7265             :       GEN v, C, C0;
    7266             :       long m, e;
    7267         266 :       for (m = 0; m <= LIM; m++)
    7268             :       {
    7269         266 :         v = lfuntheta(gmael(L,j,2), gen_1, m, bit);
    7270         266 :         if (gexpo(v) > bit_add - bit/2) break;
    7271             :       }
    7272         196 :       if (m > LIM) { LIM <<= 1; goto START; }
    7273         196 :       C = mulcxpowIs(gdiv(v,conj_i(v)), 2*m - k);
    7274         196 :       C0 = grndtoi(C, &e); if (e < 5-bit) C = C0;
    7275         196 :       gel(z,j) = C;
    7276             :     }
    7277             :   }
    7278          70 :   return Z;
    7279             : }
    7280             : static GEN
    7281          63 : mffrickeeigen(GEN mf, GEN vE, long prec)
    7282             : {
    7283          63 :   GEN D = obj_check(mf, MF_FRICKE);
    7284          63 :   if (D) { long p = gprecision(D); if (!p || p >= prec) return D; }
    7285          56 :   D = mffrickeeigen_i(mf, MF_get_newforms(mf), vE, prec);
    7286          56 :   return obj_insert(mf, MF_FRICKE, D);
    7287             : }
    7288             : 
    7289             : /* integral weight, new space for primitive quadratic character CHIP;
    7290             :  * MF = vector of embedded eigenforms coefs on mfbasis, by orbit.
    7291             :  * Assume N > Q > 1 and (Q,f(CHIP)) = 1 */
    7292             : static GEN
    7293          56 : mfatkineigenquad(GEN mf, GEN CHIP, long Q, GEN MF, long bitprec)
    7294             : {
    7295             :   GEN L0, la2, S, F, vP, tau, wtau, Z, va, vb, den, coe, sqrtQ, sqrtN;
    7296          56 :   GEN M, gN, gk = MF_get_gk(mf);
    7297          56 :   long N0, x, yq, i, j, lF, dim, muQ, prec = nbits2prec(bitprec);
    7298          56 :   long N = MF_get_N(mf), k = itos(gk), NQ = N / Q;
    7299             : 
    7300             :   /* Q coprime to FC */
    7301          56 :   F = MF_get_newforms(mf);
    7302          56 :   vP = MF_get_fields(mf);
    7303          56 :   lF = lg(F);
    7304          56 :   Z = cgetg(lF, t_VEC);
    7305          56 :   S = MF_get_S(mf); dim = lg(S) - 1;
    7306          56 :   muQ = mymoebiusu(Q);
    7307          56 :   if (muQ)
    7308             :   {
    7309          42 :     GEN SQ = cgetg(dim+1,t_VEC), Qk = gpow(stoi(Q), sstoQ(k-2, 2), prec);
    7310          42 :     long i, bit2 = bitprec >> 1;
    7311          42 :     for (j = 1; j <= dim; j++) gel(SQ,j) = mfak_i(gel(S,j), Q);
    7312          84 :     for (i = 1; i < lF; i++)
    7313             :     {
    7314          42 :       GEN S = RgV_dotproduct(gel(F,i), SQ), T = gel(vP,i);
    7315             :       long e;
    7316          42 :       if (degpol(T) > 1 && typ(S) != t_POLMOD) S = gmodulo(S, T);
    7317          42 :       S = grndtoi(gdiv(conjvec(S, prec), Qk), &e);
    7318          42 :       if (e > -bit2) pari_err_PREC("mfatkineigenquad");
    7319          42 :       if (muQ == -1) S = gneg(S);
    7320          42 :       gel(Z,i) = S;
    7321             :     }
    7322          42 :     return Z;
    7323             :   }
    7324          14 :   la2 = mfchareval_i(CHIP, Q); /* 1 or -1 */
    7325          14 :   (void)cbezout(Q, NQ, &x, &yq);
    7326          14 :   sqrtQ = sqrtr_abs(utor(Q,prec));
    7327          14 :   tau = mkcomplex(gadd(sstoQ(-1, NQ), ginv(utoi(1000))),
    7328             :                   divru(sqrtQ, N));
    7329          14 :   den = gaddgs(gmulsg(NQ, tau), 1);
    7330          14 :   wtau = gdiv(gsub(gmulsg(x, tau), sstoQ(yq, Q)), den);
    7331          14 :   coe = gpowgs(gmul(sqrtQ, den), k);
    7332             : 
    7333          14 :   sqrtN = sqrtr_abs(utor(N,prec));
    7334          14 :   tau  = mulcxmI(gmul(tau,  sqrtN));
    7335          14 :   wtau = mulcxmI(gmul(wtau, sqrtN));
    7336          14 :   gN = utoipos(N);
    7337          14 :   L0 = mfthetaancreate(NULL, gN, gk); /* only for thetacost */
    7338          14 :   N0 = maxss(lfunthetacost(L0,real_i(tau), 0,bitprec),
    7339             :              lfunthetacost(L0,real_i(wtau),0,bitprec));
    7340          14 :   M = mfcoefs_mf(mf, N0, 1);
    7341          14 :   va = cgetg(dim+1, t_VEC);
    7342          14 :   vb = cgetg(dim+1, t_VEC);
    7343         105 :   for (j = 1; j <= dim; j++)
    7344             :   {
    7345          91 :     GEN L, v = vecslice(gel(M,j), 2, N0+1); /* remove a0 */
    7346          91 :     settyp(v, t_VEC); L = mfthetaancreate(v, gN, gk);
    7347          91 :     gel(va,j) = lfuntheta(L, tau,0,bitprec);
    7348          91 :     gel(vb,j) = lfuntheta(L,wtau,0,bitprec);
    7349             :   }
    7350          84 :   for (i = 1; i < lF; i++)
    7351             :   {
    7352          70 :     GEN z, FE = gel(MF,i);
    7353          70 :     long l = lg(FE);
    7354          70 :     z = cgetg(l, t_VEC);
    7355          70 :     for (j = 1; j < l; j++)
    7356             :     {
    7357          70 :       GEN f = gel(FE,j), a = RgV_dotproduct(va,f), b = RgV_dotproduct(vb,f);
    7358          70 :       GEN la = ground( gdiv(b, gmul(a,coe)) );
    7359          70 :       if (!gequal(gsqr(la), la2)) pari_err_PREC("mfatkineigenquad");
    7360          70 :       if (typ(la) == t_INT)
    7361             :       {
    7362          70 :         if (j != 1) pari_err_BUG("mfatkineigenquad");
    7363          70 :         z = const_vec(l-1, la); break;
    7364             :       }
    7365           0 :       gel(z,j) = la;
    7366             :     }
    7367          70 :     gel(Z,i) = z;
    7368             :   }
    7369          14 :   return Z;
    7370             : }
    7371             : 
    7372             : static GEN
    7373          70 : myusqrt(ulong a, long prec)
    7374             : {
    7375          70 :   if (a == 1UL) return gen_1;
    7376          56 :   if (uissquareall(a, &a)) return utoipos(a);
    7377          42 :   return sqrtr_abs(utor(a, prec));
    7378             : }
    7379             : /* Assume mf is a non-trivial new space, rational primitive character CHIP
    7380             :  * and (Q,FC) = 1 */
    7381             : static GEN
    7382          98 : mfatkinmatnewquad(GEN mf, GEN CHIP, long Q, long flag, long PREC)
    7383             : {
    7384          98 :   GEN cM, M, D, MF, den, vE, F = MF_get_newforms(mf);
    7385          98 :   long i, c, e, prec, bitprec, lF = lg(F), N = MF_get_N(mf), k = MF_get_k(mf);
    7386             : 
    7387          98 :   if (Q == 1) return mkvec4(gen_0, matid(MF_get_dim(mf)), gen_1, mf);
    7388          98 :   den = gel(MF_get_Minv(mf), 2);
    7389          98 :   bitprec = expi(den) + 64;
    7390          98 :   if (!flag) bitprec = maxss(bitprec, prec2nbits(PREC));
    7391             : 
    7392             : START:
    7393          98 :   prec = nbits2prec(bitprec);
    7394          98 :   vE = mfeigenembed(mf, prec);
    7395          98 :   M = cgetg(lF, t_VEC);
    7396          98 :   for (i = 1; i < lF; i++) gel(M,i) = RgC_embedall(gel(F,i), gel(vE,i));
    7397          98 :   if (Q != N)
    7398             :   {
    7399          56 :     D = mfatkineigenquad(mf, CHIP, Q, M, bitprec);
    7400          56 :     c = odd(k)? Q: 1;
    7401             :   }
    7402             :   else
    7403             :   {
    7404          42 :     D = mffrickeeigen(mf, vE, DEFAULTPREC);
    7405          42 :     c = mfcharmodulus(CHIP); if (odd(k)) c = -Q/c;
    7406             :   }
    7407          98 :   D = shallowconcat1(D);
    7408          98 :   if (vec_isconst(D)) { MF = diagonal_shallow(D); flag = 0; }
    7409             :   else
    7410             :   {
    7411          63 :     M = shallowconcat1(M);
    7412          63 :     MF = RgM_mul(matmuldiagonal(M,D), ginv(M));
    7413             :   }
    7414          98 :   if (!flag) return mkvec4(gen_0, MF, gen_1, mf);
    7415             : 
    7416          21 :   if (c > 0)
    7417          21 :     cM = myusqrt(c, PREC);
    7418             :   else
    7419             :   {
    7420           0 :     MF = imag_i(MF); c = -c;
    7421           0 :     cM = mkcomplex(gen_0, myusqrt(c,PREC));
    7422             :   }
    7423          21 :   if (c != 1) MF = RgM_Rg_mul(MF, myusqrt(c,prec));
    7424          21 :   MF = grndtoi(RgM_Rg_mul(MF,den), &e);
    7425          21 :   if (e > -32) { bitprec <<= 1; goto START; }
    7426          21 :   MF = RgM_Rg_div(MF, den);
    7427          21 :   if (is_rational_t(typ(cM)) && !isint1(cM))
    7428           0 :   { MF = RgM_Rg_div(MF, cM); cM = gen_1; }
    7429          21 :   return mkvec4(gen_0, MF, cM, mf);
    7430             : }
    7431             : 
    7432             : /* let CHI mod N, Q || N, return \bar{CHI_Q} * CHI_{N/Q} */
    7433             : static GEN
    7434          70 : mfcharAL(GEN CHI, long Q)
    7435             : {
    7436          70 :   GEN G = gel(CHI,1), c = gel(CHI,2), cycc, d, P, E, F;
    7437          70 :   long l = lg(c), N = mfcharmodulus(CHI), i;
    7438          70 :   if (N == Q) return mfcharconj(CHI);
    7439          42 :   if (N == 1) return CHI;
    7440          42 :   CHI = leafcopy(CHI);
    7441          42 :   gel(CHI,2) = d = leafcopy(c);
    7442          42 :   F = znstar_get_faN(G);
    7443          42 :   P = gel(F,1);
    7444          42 :   E = gel(F,2);
    7445          42 :   cycc = znstar_get_conreycyc(G);
    7446          42 :   if (!odd(Q) && equaliu(gel(P,1), 2) && E[1] >= 3)
    7447          14 :     gel(d,2) = Fp_neg(gel(d,2), gel(cycc,2));
    7448          56 :   else for (i = 1; i < l; i++)
    7449          28 :     if (!umodui(Q, gel(P,i))) gel(d,i) = Fp_neg(gel(d,i), gel(cycc,i));
    7450          42 :   return CHI;
    7451             : }
    7452             : static long
    7453         189 : atkin_get_NQ(long N, long Q, const char *f)
    7454             : {
    7455         189 :   long NQ = N / Q;
    7456         189 :   if (N % Q) pari_err_DOMAIN(f,"N % Q","!=",gen_0,utoi(Q));
    7457         189 :   if (ugcd(NQ, Q) > 1) pari_err_DOMAIN(f,"gcd(Q,N/Q)","!=",gen_1,utoi(Q));
    7458         189 :   return NQ;
    7459             : }
    7460             : 
    7461             : /* transform mf to new_NEW if possible */
    7462             : static GEN
    7463        1141 : MF_set_new(GEN mf)
    7464             : {
    7465        1141 :   GEN vMjd, vj, gk = MF_get_gk(mf);
    7466             :   long l, j;
    7467        1141 :   if (MF_get_space(mf) != mf_CUSP
    7468         182 :       || typ(gk) != t_INT || itou(gk) == 1) return mf;
    7469         168 :   vMjd = MFcusp_get_vMjd(mf); l = lg(vMjd);
    7470         168 :   if (l > 1 && gel(vMjd,1)[1] != MF_get_N(mf)) return mf; /* oldspace != 0 */
    7471         168 :   mf = shallowcopy(mf);
    7472         168 :   gel(mf,1) = shallowcopy(gel(mf,1));
    7473         168 :   MF_set_space(mf, mf_NEW);
    7474         168 :   vj = cgetg(l, t_VECSMALL);
    7475         168 :   for (j = 1; j < l; j++) vj[j] = gel(vMjd, j)[2];
    7476         168 :   gel(mf,4) = vj; return mf;
    7477             : }
    7478             : 
    7479             : /* if flag = 1, rationalize, else don't */
    7480             : static GEN
    7481         168 : mfatkininit_i(GEN mf, long Q, long flag, long prec)
    7482             : {
    7483             :   GEN M, B, C, CHI, CHIAL, G, chi, P, z, g, mfB, s, Mindex, Minv;
    7484         168 :   long j, l, lim, ord, FC, NQ, cQ, nk, dk, N = MF_get_N(mf);
    7485             : 
    7486         168 :   B = MF_get_basis(mf); l = lg(B);
    7487         168 :   M = cgetg(l, t_MAT); if (l == 1) return mkvec4(gen_0,M,gen_1,mf);
    7488         168 :   Qtoss(MF_get_gk(mf), &nk,&dk);
    7489         168 :   Q = labs(Q);
    7490         168 :   NQ = atkin_get_NQ(N, Q, "mfatkininit");
    7491         168 :   CHI = MF_get_CHI(mf);
    7492         168 :   CHI = mfchartoprimitive(CHI, &FC);
    7493         168 :   ord = mfcharorder_canon(CHI);
    7494         168 :   mf = MF_set_new(mf);
    7495         168 :   if (MF_get_space(mf) == mf_NEW && ord == 1 && NQ % FC == 0 && dk == 1)
    7496          98 :     return mfatkinmatnewquad(mf, CHI, Q, flag, prec);
    7497             :   /* now flag != 0 */
    7498          70 :   G   = gel(CHI,1);
    7499          70 :   chi = gel(CHI,2);
    7500          70 :   if (Q == N) { g = mkmat22s(0, -1, N, 0); cQ = NQ; } /* Fricke */
    7501             :   else
    7502             :   {
    7503          28 :     GEN F, gQP = utoi(ugcd(Q, FC));
    7504             :     long t, v;
    7505          28 :     chi = znchardecompose(G, chi, gQP);
    7506          28 :     F = znconreyconductor(G, chi, &chi);
    7507          28 :     G = znstar0(F,1);
    7508          28 :     (void)cbezout(Q, NQ, &t, &v);
    7509          28 :     g = mkmat22s(Q*t, 1, -N*v, Q);
    7510          28 :     cQ = -NQ*v;
    7511             :   }
    7512          70 :   C = s = gen_1;
    7513             :   /* N.B. G,chi are G_Q,chi_Q [primitive] at this point */
    7514          70 :   if (lg(chi) != 1) C = ginv( znchargauss(G, chi, gen_1, prec2nbits(prec)) );
    7515          70 :   if (dk == 1)
    7516          63 :   { if (odd(nk)) s = myusqrt(Q,prec); }
    7517             :   else
    7518             :   {
    7519           7 :     long r = nk >> 1; /* k-1/2 */
    7520           7 :     s = gpow(utoipos(Q), mkfracss(odd(r)? 1: 3, 4), prec);
    7521           7 :     if (odd(cQ))
    7522             :     {
    7523           7 :       long t = r + ((cQ-1) >> 1);
    7524           7 :       s = mkcomplex(s, odd(t)? gneg(s): s);
    7525             :     }
    7526             :   }
    7527          70 :   if (!isint1(s)) C = gmul(C, s);
    7528          70 :   CHIAL = mfcharAL(CHI, Q);
    7529          70 :   if (dk == 2)
    7530           7 :     CHIAL = mfcharmul(CHIAL, induce(gel(CHIAL,1), utoipos(odd(Q) ? Q<<2 : Q)));
    7531          70 :   CHIAL = mfchartoprimitive(CHIAL,NULL);
    7532          70 :   mfB = gequal(CHIAL,CHI)? mf: mfinit_Nndkchi(N,nk,dk,CHIAL,MF_get_space(mf),0);
    7533          70 :   Mindex = MF_get_Mindex(mfB);
    7534          70 :   Minv = MF_get_Minv(mfB);
    7535          70 :   P = z = NULL;
    7536          70 :   if (ord != 1) { P = mfcharpol(CHI); z = rootsof1u_cx(ord, prec); }
    7537          70 :   lim = maxss(mfsturm(mfB), mfsturm(mf)) + 1;
    7538         217 :   for (j = 1; j < l; j++)
    7539             :   {
    7540         147 :     GEN v = mfslashexpansion(mf, gel(B,j), g, lim, 0, NULL, prec+1);
    7541             :     long junk;
    7542         147 :     if (!isint1(C)) v = RgV_Rg_mul(v, C);
    7543         147 :     v = bestapprnf(v, P, z, prec);
    7544         147 :     v = vecpermute_partial(v, Mindex, &junk);
    7545         147 :     v = Minv_RgC_mul(Minv, v); /* cf mftobasis_i */
    7546         147 :     gel(M, j) = v;
    7547             :   }
    7548          70 :   if (is_rational_t(typ(C)) && !gequal1(C)) { M = gdiv(M, C); C = gen_1; }
    7549          70 :   if (mfB == mf) mfB = gen_0;
    7550          70 :   return mkvec4(mfB, M, C, mf);
    7551             : }
    7552             : GEN
    7553          77 : mfatkininit(GEN mf, long Q, long prec)
    7554             : {
    7555          77 :   pari_sp av = avma;
    7556          77 :   mf = checkMF(mf); return gerepilecopy(av, mfatkininit_i(mf, Q, 1, prec));
    7557             : }
    7558             : static void
    7559          21 : checkmfa(GEN z)
    7560             : {
    7561          21 :   if (typ(z) != t_VEC || lg(z) != 5 || typ(gel(z,2)) != t_MAT
    7562          21 :       || !checkMF_i(gel(z,4))
    7563          21 :       || (!isintzero(gel(z,1)) && !checkMF_i(gel(z,1))))
    7564           0 :     pari_err_TYPE("mfatkin [please apply mfatkininit()]",z);
    7565          21 : }
    7566             : 
    7567             : /* Apply atkin Q to closure F */
    7568             : GEN
    7569          21 : mfatkin(GEN mfa, GEN F)
    7570             : {
    7571          21 :   pari_sp av = avma;
    7572             :   GEN z, mfB, MQ, mf;
    7573          21 :   checkmfa(mfa);
    7574          21 :   mfB= gel(mfa,1);
    7575          21 :   MQ = gel(mfa,2);
    7576          21 :   mf = gel(mfa,4);
    7577          21 :   if (typ(mfB) == t_INT) mfB = mf;
    7578          21 :   z = RgM_RgC_mul(MQ, mftobasis_i(mf,F));
    7579          21 :   return gerepileupto(av, mflinear(mfB, z));
    7580             : }
    7581             : 
    7582             : GEN
    7583          42 : mfatkineigenvalues(GEN mf, long Q, long prec)
    7584             : {
    7585          42 :   pari_sp av = avma;
    7586             :   GEN vF, L, CHI, M, mfatk, C, MQ, vE, mfB;
    7587             :   long N, NQ, l, i;
    7588             : 
    7589          42 :   mf = checkMF(mf); N = MF_get_N(mf); CHI = MF_get_CHI(mf);
    7590          42 :   vF = MF_get_newforms(mf); l = lg(vF);
    7591          42 :   if (l == 1) { avma = av; return cgetg(1, t_VEC); }
    7592          42 :   L = cgetg(l, t_VEC);
    7593          42 :   if (Q == 1)
    7594             :   {
    7595           7 :     GEN vP = MF_get_fields(mf);
    7596           7 :     for (i = 1; i < l; i++) gel(L,i) = const_vec(degpol(gel(vP,i)), gen_1);
    7597           7 :     return L;
    7598             :   }
    7599          35 :   vE = mfeigenembed(mf,prec);
    7600          35 :   if (Q == N) return gerepileupto(av, mffrickeeigen(mf, vE, prec));
    7601          21 :   Q = labs(Q);
    7602          21 :   NQ = atkin_get_NQ(N, Q, "mfatkineigenvalues");
    7603          21 :   mfatk = mfatkininit(mf, Q, prec);
    7604          21 :   mfB= gel(mfatk,1); if (typ(mfB) != t_VEC) mfB = mf;
    7605          21 :   MQ = gel(mfatk,2);
    7606          21 :   C  = gel(mfatk,3);
    7607          21 :   M = row(mfcoefs_mf(mfB,1,1), 2); /* vec of a_1(b_i) for mfbasis functions */
    7608          56 :   for (i = 1; i < l; i++)
    7609             :   {
    7610          35 :     GEN c = RgV_dotproduct(RgM_RgC_mul(MQ,gel(vF,i)), M); /* C * eigen_i */
    7611          35 :     gel(L,i) = Rg_embedall_i(c, gel(vE,i));
    7612             :   }
    7613          21 :   if (!gequal1(C)) L = gdiv(L, C);
    7614          21 :   mf = MF_set_new(mf);
    7615          21 :   if (MF_get_space(mf) == mf_NEW && mfcharorder(CHI) <= 2
    7616           7 :       && (NQ==1 || NQ % mfcharconductor(CHI) == 0)
    7617           7 :       && typ(MF_get_gk(mf)) == t_INT) L = ground(L);
    7618          21 :   return gerepilecopy(av, L);
    7619             : }
    7620             : 
    7621             : /* expand B_d V, keeping same length */
    7622             : static GEN
    7623        4697 : bdexpand(GEN V, long d)
    7624             : {
    7625             :   GEN W;
    7626             :   long N, n;
    7627        4697 :   if (d == 1) return V;
    7628        1624 :   N = lg(V)-1; W = zerovec(N);
    7629        1624 :   for (n = 0; n <= (N-1)/d; n++) gel(W, n*d+1) = gel(V, n+1);
    7630        1624 :   return W;
    7631             : }
    7632             : /* expand B_d V, increasing length up to lim */
    7633             : static GEN
    7634         252 : bdexpandall(GEN V, long d, long lim)
    7635             : {
    7636             :   GEN W;
    7637             :   long N, n;
    7638         252 :   if (d == 1) return V;
    7639          35 :   N = lg(V)-1; W = zerovec(lim);
    7640          35 :   for (n = 0; n <= N-1 && n*d <= lim; n++) gel(W, n*d+1) = gel(V, n+1);
    7641          35 :   return W;
    7642             : }
    7643             : 
    7644             : static void
    7645        7700 : parse_vecj(GEN T, GEN *E1, GEN *E2)
    7646             : {
    7647        7700 :   if (lg(T)==3) { *E1 = gel(T,1); *E2 = gel(T,2); }
    7648        4151 :   else { *E1 = T; *E2 = NULL; }
    7649        7700 : }
    7650             : 
    7651             : /* g in M_2(Z) ? */
    7652             : static int
    7653        2352 : check_M2Z(GEN g)
    7654        2352 : {  return typ(g) == t_MAT && lg(g) == 3 && lgcols(g) == 3 && RgM_is_ZM(g); }
    7655             : /* g in SL_2(Z) ? */
    7656             : static int
    7657        1449 : check_SL2Z(GEN g) { return check_M2Z(g) && equali1(ZM_det(g)); }
    7658             : 
    7659             : static GEN
    7660        6587 : mfcharcxeval(GEN CHI, long n, long prec)
    7661             : {
    7662             :   GEN ordg;
    7663             :   ulong ord;
    7664        6587 :   if (ugcd(mfcharmodulus(CHI), labs(n)) > 1) return gen_0;
    7665        6587 :   ordg = gmfcharorder(CHI);
    7666        6587 :   ord = itou(ordg);
    7667        6587 :   return rootsof1q_cx(znchareval_i(CHI,n,ordg), ord, prec);
    7668             : }
    7669             : 
    7670             : static GEN
    7671        4361 : RgV_shift(GEN V, GEN gn)
    7672             : {
    7673             :   long i, n, l;
    7674             :   GEN W;
    7675        4361 :   if (typ(gn) != t_INT) pari_err_BUG("RgV_shift [n not integral]");
    7676        4361 :   n = itos(gn);
    7677        4361 :   if (n < 0) pari_err_BUG("RgV_shift [n negative]");
    7678        4361 :   if (!n) return V;
    7679          98 :   W = cgetg_copy(V, &l); if (n > l-1) n = l-1;
    7680          98 :   for (i=1; i <= n; i++) gel(W,i) = gen_0;
    7681          98 :   for (    ; i < l; i++) gel(W,i) = gel(V, i-n);
    7682          98 :   return W;
    7683             : }
    7684             : static GEN
    7685        6685 : hash_eisengacx(hashtable *H, void *E, long w, GEN ga, long n, long prec)
    7686             : {
    7687        6685 :   ulong h = H->hash(E);
    7688        6685 :   hashentry *e = hash_search2(H, E, h);
    7689             :   GEN v;
    7690        6685 :   if (e) v = (GEN)e->val;
    7691             :   else
    7692             :   {
    7693        4340 :     v = mfeisensteingacx((GEN)E, w, ga, n, prec);
    7694        4340 :     hash_insert2(H, E, (void*)v, h);
    7695             :   }
    7696        6685 :   return v;
    7697             : }
    7698             : static GEN
    7699        4361 : vecj_expand(GEN B, hashtable *H, long w, GEN ga, long n, long prec)
    7700             : {
    7701             :   GEN E1, E2, v;
    7702        4361 :   parse_vecj(B, &E1, &E2);
    7703        4361 :   v = hash_eisengacx(H, (void*)E1, w, ga, n, prec);
    7704        4361 :   if (E2)
    7705             :   {
    7706        2310 :     GEN u = hash_eisengacx(H, (void*)E2, w, ga, n, prec);
    7707        2310 :     GEN a = gadd(gel(v,1), gel(u,1));
    7708        2310 :     GEN b = RgV_mul_RgXn(gel(v,2), gel(u,2));
    7709        2310 :     v = mkvec2(a,b);
    7710             :   }
    7711        4361 :   return v;
    7712             : }
    7713             : static GEN
    7714         882 : shift_M(GEN M, GEN Valpha, long w)
    7715             : {
    7716         882 :   long i, l = lg(Valpha);
    7717         882 :   GEN almin = vecmin(Valpha);
    7718        5243 :   for (i = 1; i < l; i++)
    7719             :   {
    7720        4361 :     GEN alpha = gel(Valpha, i), gsh = gmulsg(w, gsub(alpha,almin));
    7721        4361 :     gel(M,i) = RgV_shift(gel(M,i), gsh);
    7722             :   }
    7723         882 :   return almin;
    7724             : }
    7725             : static GEN mfeisensteinspaceinit(GEN NK);
    7726             : #if 0
    7727             : /* ga in M_2^+(Z)), n >= 0 */
    7728             : static GEN
    7729             : mfgaexpansion_init(GEN mf, GEN ga, long n, long prec)
    7730             : {
    7731             :   GEN M, Mvecj, vecj, almin, Valpha;
    7732             :   long i, w, l, N = MF_get_N(mf), c = itos(gcoeff(ga,2,1));
    7733             :   hashtable *H;
    7734             : 
    7735             :   if (c % N == 0)
    7736             :   { /* ga in G_0(N), trivial case; w = 1 */
    7737             :     GEN chid = mfcharcxeval(MF_get_CHI(mf), itos(gcoeff(ga,2,2)), prec);
    7738             :     return mkvec2(chid, utoi(n));
    7739             :   }
    7740             : 
    7741             :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    7742             :   if (lg(Mvecj) < 5) pari_err_IMPL("mfgaexpansion_init in this case");
    7743             :   w = mfcuspcanon_width(N, c);
    7744             :   vecj = gel(Mvecj, 3);
    7745             :   l = lg(vecj);
    7746             :   M = cgetg(l, t_VEC);
    7747             :   Valpha = cgetg(l, t_VEC);
    7748             :   H = hash_create(l, (ulong(*)(void*))&hash_GEN,
    7749             :                      (int(*)(void*,void*))&gidentical, 1);
    7750             :   for (i = 1; i < l; i++)
    7751             :   {
    7752             :     GEN v = vecj_expand(gel(vecj,i), H, w, ga, n, prec);
    7753             :     gel(Valpha,i) = gel(v,1);
    7754             :     gel(M,i) = gel(v,2);
    7755             :   }
    7756             :   almin = shift_M(M, Valpha, w);
    7757             :   return mkvec3(almin, utoi(w), M);
    7758             : }
    7759             : /* half-integer weight not supported; vF = [F,eisendec(F)].
    7760             :  * Minit = mfgaexpansion_init(mf, ga, n, prec) */
    7761             : static GEN
    7762             : mfgaexpansion_with_init(GEN Minit, GEN vF)
    7763             : {
    7764             :   GEN v;
    7765             :   if (lg(Minit) == 3)
    7766             :   { /* ga in G_0(N) */
    7767             :     GEN chid = gel(Minit,1), gn = gel(Minit,2);
    7768             :     v = mfcoefs_i(gel(vF,1), itou(gn), 1);
    7769             :     v = mkvec3(gen_0, gen_1, RgV_Rg_mul(v,chid));
    7770             :   }
    7771             :   else
    7772             :   {
    7773             :     GEN V = RgM_RgC_mul(gel(Minit,3), gel(vF,2));
    7774             :     v = mkvec3(gel(Minit,1), gel(Minit,2), V);
    7775             :   }
    7776             :   return v;
    7777             : }
    7778             : #endif
    7779             : 
    7780             : /* B = mfeisensteindec(F) already embedded, ga in M_2^+(Z)), n >= 0 */
    7781             : static GEN
    7782         882 : mfgaexpansion_i(GEN mf, GEN B0, GEN ga, long n, long prec)
    7783             : {
    7784         882 :   GEN M, Mvecj, vecj, almin, Valpha, B, E = NULL;
    7785         882 :   long i, j, w, nw, l, N = MF_get_N(mf), bit = prec2nbits(prec) / 2;
    7786             :   hashtable *H;
    7787             : 
    7788         882 :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    7789         882 :   if (lg(Mvecj) < 5) { E = gel(Mvecj, 2); Mvecj = gel(Mvecj, 1); }
    7790         882 :   vecj = gel(Mvecj, 3);
    7791         882 :   l = lg(vecj);
    7792         882 :   B = cgetg(l, t_COL);
    7793         882 :   M = cgetg(l, t_VEC);
    7794         882 :   Valpha = cgetg(l, t_VEC);
    7795         882 :   w = mfZC_width(N, gel(ga,1));
    7796         882 :   nw = E ? n + w : n;
    7797         882 :   H = hash_create(l, (ulong(*)(void*))&hash_GEN,
    7798             :                      (int(*)(void*,void*))&gidentical, 1);
    7799        7819 :   for (i = j = 1; i < l; i++)
    7800             :   {
    7801             :     GEN v;
    7802        6937 :     if (gequal0(gel(B0,i))) continue;
    7803        4361 :     v = vecj_expand(gel(vecj,i), H, w, ga, nw, prec);
    7804        4361 :     gel(B,j) = gel(B0,i);
    7805        4361 :     gel(Valpha,j) = gel(v,1);
    7806        4361 :     gel(M,j) = gel(v,2); j++;
    7807             :   }
    7808         882 :   setlg(Valpha, j);
    7809         882 :   setlg(B, j);
    7810         882 :   setlg(M, j); l = j;
    7811         882 :   if (l == 1) return mkvec3(gen_0, utoi(w), zerovec(n+1));
    7812         882 :   almin = shift_M(M, Valpha, w);
    7813         882 :   B = RgM_RgC_mul(M, B); l = lg(B);
    7814      133252 :   for (i = 1; i < l; i++)
    7815      132370 :     if (gexpo(gel(B,i)) < -bit) gel(B,i) = gen_0;
    7816         882 :   settyp(B, t_VEC);
    7817         882 :   if (E)
    7818             :   {
    7819          14 :     GEN v = hash_eisengacx(H, (void*)E, w, ga, n, prec);
    7820          14 :     long ell = 0;
    7821          14 :     almin = gsub(almin, gel(v,1));
    7822          14 :     if (gsigne(almin) < 0)
    7823             :     {
    7824           0 :       GEN gell = gceil(gmulsg(-w, almin));
    7825           0 :       ell = itos(gell);
    7826           0 :       almin = gadd(almin, gdivgs(gell, w));
    7827           0 :       if (nw < ell) pari_err_IMPL("alpha < 0 in mfgaexpansion");
    7828             :     }
    7829          14 :     B = vecslice(B, ell + 1, n + ell + 1);
    7830          14 :     B = RgV_div_RgXn(B, gel(v,2));
    7831             :   }
    7832         882 :   return mkvec3(almin, utoi(w), B);
    7833             : }
    7834             : 
    7835             : /* Theta multiplier: assume 4 | C, (C,D)=1 */
    7836             : static GEN
    7837         238 : mfthetamultiplier(long C, long D)
    7838             : {
    7839         238 :   long s = kross(C, D);
    7840         238 :   if ((D&3L) == 1) return stoi(s);
    7841          49 :   return s > 0 ? powIs(3) : gen_I();
    7842             : }
    7843             : static GEN
    7844         238 : mfthetaexpansion(GEN M, long n)
    7845             : {
    7846         238 :   GEN s, al, sla, V = zerovec(n + 1);
    7847         238 :   long w, lim, la, f, C = itos(gcoeff(M, 2, 1)), D = itos(gcoeff(M, 2, 2));
    7848         238 :   switch (C & 3L)
    7849             :   {
    7850          56 :     case 0: al = gen_0; w = 1;
    7851          56 :       s = mfthetamultiplier(C,D);
    7852          56 :       lim = usqrt(n); gel(V, 1) = s;
    7853          56 :       s = gmul2n(s, 1);
    7854          56 :       for (f = 1; f <= lim; f++) gel(V, f*f + 1) = s;
    7855          56 :       break;
    7856          84 :     case 2: al = sstoQ(1,4); w = 1;
    7857          84 :       s = gmul2n(mfthetamultiplier(C - 2*D, D), 1);
    7858          84 :       lim = (usqrt(n << 2) - 1) >> 1;
    7859          84 :       for (f = 0; f <= lim; f++) gel(V, f*(f+1) + 1) = s;
    7860          84 :       break;
    7861          98 :     default: al = gen_0; w = 4; la = (-D*C) & 3L;
    7862          98 :       s = mfthetamultiplier(-(D + la*C), C);
    7863          98 :       s = gsub(s, mulcxI(s));
    7864          98 :       sla = gmul(s, powIs(-la));
    7865          98 :       lim = usqrt(n); gel(V, 1) = gmul2n(s, -1);
    7866          98 :       for (f = 1; f <= lim; f++) gel(V, f*f + 1) = odd(f) ? sla : s;
    7867          98 :       break;
    7868             :   }
    7869         238 :   return mkvec3(al, stoi(w), V);
    7870             : }
    7871             : 
    7872             : /* F 1/2 integral weight */
    7873             : static GEN
    7874         238 : mf2gaexpansion(GEN mf2, GEN F, GEN ga, long n, long prec)
    7875             : {
    7876         238 :   GEN FT = mfmultheta(F), mf = obj_checkbuild(mf2, MF_MF2INIT, &mf2init);
    7877             :   GEN res, V1, Tres, V2, al, V, gsh;
    7878         238 :   long w2, C = itos(gcoeff(ga,2,1)), w = mfcuspcanon_width(MF_get_N(mf), C);
    7879         238 :   long ext = ((C & 3L) != 2)? 0: (w+3) >> 2;
    7880         238 :   long prec2 = prec + nbits2extraprec((long)M_PI/(2*M_LN2)*sqrt(n + ext));
    7881         238 :   res = mfgaexpansion(mf, FT, ga, n + ext, prec2);
    7882         238 :   Tres = mfthetaexpansion(ga, n + ext);
    7883         238 :   V1 = gel(res,3);
    7884         238 :   V2 = gel(Tres,3);
    7885         238 :   al = gsub(gel(res,1), gel(Tres,1));
    7886         238 :   w2 = itos(gel(Tres,2));
    7887         238 :   if (w != itos(gel(res,2)) || w % w2)
    7888           0 :     pari_err_BUG("mf2gaexpansion [incorrect w2 or w]");
    7889         238 :   if (w2 != w) V2 = bdexpand(V2, w/w2);
    7890         238 :   V = RgV_div_RgXn(V1, V2);
    7891         238 :   gsh = gfloor(gmulsg(w, al));
    7892         238 :   if (!gequal0(gsh))
    7893             :   {
    7894          28 :     al = gsub(al, gdivgs(gsh, w));
    7895          28 :     if (gsigne(gsh) > 0)
    7896             :     {
    7897           0 :       V = RgV_shift(V, gsh);
    7898           0 :       V = vecslice(V, 1, n + 1);
    7899             :     }
    7900             :     else
    7901             :     {
    7902          28 :       long sh = -itos(gsh), i;
    7903          28 :       if (sh > ext) pari_err_BUG("mf2gaexpansion [incorrect sh]");
    7904         119 :       for (i = 1; i <= sh; i++)
    7905          91 :         if (!gequal0(gel(V,i))) pari_err_BUG("mf2gaexpansion [sh too large]");
    7906          28 :       V = vecslice(V, sh+1, n + sh+1);
    7907             :     }
    7908             :   }
    7909         238 :   obj_free(mf); return mkvec3(al, stoi(w), gprec_wtrunc(V, prec));
    7910             : }
    7911             : 
    7912             : static GEN
    7913          70 : mfgaexpansionatkin(GEN mf, GEN F, GEN C, GEN D, long Q, long n, long prec)
    7914             : {
    7915          70 :   GEN mfa = mfatkininit_i(mf, Q, 0, prec), MQ = gel(mfa,2);
    7916          70 :   long i, FC, k = MF_get_k(mf);
    7917          70 :   GEN x, v, V, z, s, CHI = mfchartoprimitive(MF_get_CHI(mf), &FC);
    7918             : 
    7919             :   /* V = mfcoefs(F | w_Q, n), can't use mfatkin because MQ non-rational */
    7920          70 :   V = RgM_RgC_mul(mfcoefs_mf(mf,n,1), RgM_RgC_mul(MQ, mftobasis_i(mf,F)));
    7921          70 :   (void)bezout(utoipos(Q), C, &x, &v);
    7922          70 :   s = mfchareval_i(CHI, (umodiu(x, FC) * umodiu(D, FC)) % FC);
    7923          70 :   s = gdiv(s, gpow(utoipos(Q), sstoQ(k,2), prec));
    7924          70 :   V = RgV_Rg_mul(V, s);
    7925          70 :   z = rootsof1powinit(umodiu(D,Q)*umodiu(v,Q) % Q, Q, prec);
    7926          70 :   for (i = 1; i <= n+1; i++) gel(V,i) = gmul(gel(V,i), rootsof1pow(z, i-1));
    7927          70 :   return mkvec3(gen_0, utoipos(Q), V);
    7928             : }
    7929             : 
    7930             : /* allow F of the form [F, mf_eisendec(F)]~ */
    7931             : static GEN
    7932        1442 : mfgaexpansion(GEN mf, GEN F, GEN ga, long n, long prec)
    7933             : {
    7934        1442 :   GEN v, EF = NULL, res, Mvecj, c, d;
    7935             :   long precnew;
    7936             : 
    7937        1442 :   if (n < 0) pari_err_DOMAIN("mfgaexpansion", "n", "<", gen_0, stoi(n));
    7938        1442 :   if (typ(F) == t_COL && lg(F) == 3) { EF = gel(F,2); F = gel(F,1); }
    7939        1442 :   if (!checkmf_i(F)) pari_err_TYPE("mfgaexpansion", F);
    7940        1442 :   if (!check_SL2Z(ga)) pari_err_TYPE("mfgaexpansion",ga);
    7941        1442 :   if (typ(mf_get_gk(F)) != t_INT) return mf2gaexpansion(mf, F, ga, n, prec);
    7942        1204 :   c = gcoeff(ga,2,1);
    7943        1204 :   d = gcoeff(ga,2,2);
    7944        1204 :   if (!umodiu(c, mf_get_N(F)))
    7945             :   { /* trivial case: ga in Gamma_0(N) */
    7946         252 :     long N = MF_get_N(mf), w = mfcuspcanon_width(N, umodiu(c,N));
    7947         252 :     GEN CHI = mf_get_CHI(F);
    7948         252 :     GEN chid = mfcharcxeval(CHI, umodiu(d,mfcharmodulus(CHI)), prec);
    7949         252 :     v = mfcoefs_i(F, n/w, 1); if (!isint1(chid)) v = RgV_Rg_mul(v,chid);
    7950         252 :     return mkvec3(gen_0, stoi(w), bdexpandall(v,w,n+1));
    7951             :   }
    7952         952 :   mf = MF_set_new(mf);
    7953         952 :   if (MF_get_space(mf) == mf_NEW)
    7954             :   {
    7955         441 :     long N = MF_get_N(mf), cN = umodiu(c,N), g = ugcd(cN,N), Q = N/g;
    7956         441 :     GEN CHI = MF_get_CHI(mf);
    7957         441 :     if (ugcd(cN, Q)==1 && mfcharorder(CHI) <= 2
    7958         217 :                        && g % mfcharconductor(CHI) == 0
    7959         112 :                        && degpol(mf_get_field(F)) == 1)
    7960          70 :       return mfgaexpansionatkin(mf, F, c, d, Q, n, prec);
    7961             :   }
    7962         882 :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    7963         882 :   precnew = (lg(Mvecj) < 5)? prec + nbits2extraprec(n >> 1): prec;
    7964         882 :   if (!EF) EF = mf_eisendec(mf, F, precnew);
    7965         882 :   res = mfgaexpansion_i(mf, EF, ga, n, precnew);
    7966         882 :   return precnew == prec ? res : gprec_wtrunc(res, prec);
    7967             : }
    7968             : 
    7969             : /* parity = -1 or +1 */
    7970             : static GEN
    7971         217 : findd(long N, long parity)
    7972             : {
    7973         217 :   GEN L, D = mydivisorsu(N);
    7974         217 :   long i, j, l = lg(D);
    7975         217 :   L = cgetg(l, t_VEC);
    7976        1218 :   for (i = j = 1; i < l; i++)
    7977             :   {
    7978        1001 :     long d = D[i];
    7979        1001 :     if (parity == -1) d = -d;
    7980        1001 :     if (sisfundamental(d)) gel(L,j++) = stoi(d);
    7981             :   }
    7982         217 :   setlg(L,j); return L;
    7983             : }
    7984             : /* does ND contain a divisor of N ? */
    7985             : static int
    7986         413 : seenD(long N, GEN ND)
    7987             : {
    7988         413 :   long j, l = lg(ND);
    7989         427 :   for (j = 1; j < l; j++)
    7990          14 :     if (N % ND[j] == 0) return 1;
    7991         413 :   return 0;
    7992             : }
    7993             : static GEN
    7994          42 : search_levels(GEN vN, const char *f)
    7995             : {
    7996          42 :   switch(typ(vN))
    7997             :   {
    7998           7 :     case t_INT: vN = mkvecsmall(itos(vN)); break;
    7999          35 :     case t_VEC: case t_COL: vN = ZV_to_zv(vN); break;
    8000           0 :     case t_VECSMALL: vN = leafcopy(vN); break;
    8001           0 :     default: pari_err_TYPE(f, vN);
    8002             :   }
    8003          42 :   vecsmall_sort(vN); return vN;
    8004             : }
    8005             : GEN
    8006          14 : mfsearch(GEN NK, GEN V, long space)
    8007             : {
    8008          14 :   pari_sp av = avma;
    8009             :   GEN F, gk, NbyD, vN;
    8010             :   long n, nk, dk, parity, nV, i, lvN;
    8011             : 
    8012          14 :   if (typ(NK) != t_VEC || lg(NK) != 3) pari_err_TYPE("mfsearch", NK);
    8013          14 :   gk = gel(NK,2);
    8014          14 :   if (typ(gmul2n(gk, 1)) != t_INT) pari_err_TYPE("mfsearch [k]", gk);
    8015          14 :   switch(typ(V))
    8016             :   {
    8017          14 :     case t_VEC: V = shallowtrans(V);
    8018          14 :     case t_COL: break;
    8019           0 :     default: pari_err_TYPE("mfsearch [V]", V);
    8020             :   }
    8021          14 :   vN = search_levels(gel(NK,1), "mfsearch [N]");
    8022          14 :   lvN = lg(vN);
    8023             : 
    8024          14 :   Qtoss(gk, &nk,&dk);
    8025          14 :   parity = (dk == 1 && odd(nk)) ? -1 : 1;
    8026          14 :   nV = lg(V)-2;
    8027          14 :   F = cgetg(1, t_VEC);
    8028          14 :   NbyD = const_vec(vN[lvN-1], cgetg(1,t_VECSMALL));
    8029         231 :   for (n = 1; n < lvN; n++)
    8030             :   {
    8031         217 :     long N = vN[n];
    8032             :     GEN L;
    8033         217 :     if (N <= 0 || (dk == 2 && (N & 3))) continue;
    8034         217 :     L = findd(N, parity);
    8035         630 :     for (i = 1; i < lg(L); i++)
    8036             :     {
    8037         413 :       GEN mf, M, CO, gD = gel(L,i);
    8038         413 :       GEN *ND = (GEN*)NbyD + itou(gD); /* points to NbyD[|D|] */
    8039             : 
    8040         413 :       if (seenD(N, *ND)) continue;
    8041         413 :       mf = mfinit_Nndkchi(N, nk, dk, get_mfchar(gD), space, 1);
    8042         413 :       M = mfcoefs_mf(mf, nV, 1);
    8043         413 :       CO = inverseimage(M, V); if (lg(CO) == 1) continue;
    8044             : 
    8045          42 :       F = vec_append(F, mflinear(mf,CO));
    8046          42 :       *ND = vecsmall_append(*ND, N); /* add to NbyD[|D|] */
    8047             :     }
    8048             :   }
    8049          14 :   return gerepilecopy(av, F);
    8050             : }
    8051             : 
    8052             : static GEN
    8053         882 : search_from_split(GEN mf, GEN vap, GEN vlp)
    8054             : {
    8055         882 :   pari_sp av = avma;
    8056         882 :   long lvlp = lg(vlp), j, jv, l1;
    8057         882 :   GEN v, NK, S1, S, M = NULL;
    8058             : 
    8059         882 :   S1 = gel(split_i(mf, 1, 0), 1); /* rational newforms */
    8060         882 :   l1 = lg(S1);
    8061         882 :   if (l1 == 1) { avma = av; return NULL; }
    8062         448 :   v = cgetg(l1, t_VEC);
    8063         448 :   S = MF_get_S(mf);
    8064         448 :   NK = mf_get_NK(gel(S,1));
    8065         448 :   if (lvlp > 1) M = rowpermute(mfcoefs_mf(mf, vlp[lvlp-1], 1), vlp);
    8066         966 :   for (j = jv = 1; j < l1; j++)
    8067             :   {
    8068         518 :     GEN vF = gel(S1,j);
    8069             :     long t;
    8070         651 :     for (t = lvlp-1; t > 0; t--)
    8071             :     { /* lhs = vlp[j]-th coefficient of eigenform */
    8072         595 :       GEN rhs = gel(vap,t), lhs = RgMrow_RgC_mul(M, vF, t);
    8073         595 :       if (!gequal(lhs, rhs)) break;
    8074             :     }
    8075         518 :     if (!t) gel(v,jv++) = mflinear_i(NK,S,vF);
    8076             :   }
    8077         448 :   if (jv == 1) { avma = av; return NULL; }
    8078          56 :   setlg(v,jv); return v;
    8079             : }
    8080             : GEN
    8081          28 : mfeigensearch(GEN NK, GEN AP)
    8082             : {
    8083          28 :   pari_sp av = avma;
    8084          28 :   GEN k, vN, vap, vlp, vres = cgetg(1, t_VEC), D;
    8085             :   long n, lvN, i, l, even;
    8086             : 
    8087          28 :   if (!AP) l = 1;
    8088             :   else
    8089             :   {
    8090          28 :     l = lg(AP);
    8091          28 :     if (typ(AP) != t_VEC) pari_err_TYPE("mfeigensearch",AP);
    8092             :   }
    8093          28 :   vap = cgetg(l, t_VEC);
    8094          28 :   vlp = cgetg(l, t_VECSMALL);
    8095          28 :   if (l > 1)
    8096             :   {
    8097          28 :     GEN perm = indexvecsort(AP, mkvecsmall(1));
    8098          77 :     for (i = 1; i < l; i++)
    8099             :     {
    8100          49 :       GEN v = gel(AP,perm[i]), gp, ap;
    8101          49 :       if (typ(v) != t_VEC || lg(v) != 3) pari_err_TYPE("mfeigensearch", AP);
    8102          49 :       gp = gel(v,1);
    8103          49 :       ap = gel(v,2);
    8104          49 :       if (typ(gp) != t_INT || (typ(ap) != t_INT && typ(ap) != t_INTMOD))
    8105           0 :         pari_err_TYPE("mfeigensearch", AP);
    8106          49 :       gel(vap,i) = ap;
    8107          49 :       vlp[i] = itos(gp)+1; if (vlp[i] < 0) pari_err_TYPE("mfeigensearch", AP);
    8108             :     }
    8109             :   }
    8110          28 :   l = lg(NK);
    8111          28 :   if (typ(NK) != t_VEC || l != 3) pari_err_TYPE("mfeigensearch",NK);
    8112          28 :   k = gel(NK,2);
    8113          28 :   vN = search_levels(gel(NK,1), "mfeigensearch [N]");
    8114          28 :   lvN = lg(vN);
    8115          28 :   vecsmall_sort(vlp);
    8116          28 :   even = !mpodd(k);
    8117         966 :   for (n = 1; n < lvN; n++)
    8118             :   {
    8119         938 :     pari_sp av2 = avma;
    8120             :     GEN mf, L;
    8121         938 :     long N = vN[n];
    8122         938 :     if (even) D = gen_1;
    8123             :     else
    8124             :     {
    8125         112 :       long r = (N&3L);
    8126         112 :       if (r == 1 || r == 2) continue;
    8127          56 :       D = stoi( corediscs(-N, NULL) ); /* < 0 */
    8128             :     }
    8129         882 :     mf = mfinit_i(mkvec3(utoipos(N), k, D), mf_NEW);
    8130         882 :     L = search_from_split(mf, vap, vlp);
    8131         882 :     if (L) vres = shallowconcat(vres, L); else avma = av2;
    8132             :   }
    8133          28 :   return gerepilecopy(av, vres);
    8134             : }
    8135             : 
    8136             : /* tf_{N,k}(n) */
    8137             : static GEN
    8138     2192393 : mfnewtracecache(long N, long k, long n, cachenew_t *cache)
    8139             : {
    8140     2192393 :   GEN C = NULL, S;
    8141             :   long lcache;
    8142     2192393 :   if (!n) return gen_0;
    8143     2115022 :   S = gel(cache->vnew,N);
    8144     2115022 :   lcache = lg(S);
    8145     2115022 :   if (n < lcache) C = gel(S, n);
    8146     2115022 :   if (C) cache->newHIT++;
    8147     1272439 :   else C = mfnewtrace_i(N,k,n,cache);
    8148     2115022 :   cache->newTOTAL++;
    8149     2115022 :   if (n < lcache) gel(S,n) = C;
    8150     2115022 :   return C;
    8151             : }
    8152             : 
    8153             : static long
    8154        1386 : mfdim_Nkchi(long N, long k, GEN CHI, long space)
    8155             : {
    8156        1386 :   if (k < 0 || badchar(N,k,CHI)) return 0;
    8157        1085 :   if (k == 0)
    8158          35 :     return mfcharistrivial(CHI) && !space_is_cusp(space)? 1: 0;
    8159        1050 :   switch(space)
    8160             :   {
    8161         238 :     case mf_NEW: return mfnewdim(N,k,CHI);
    8162         196 :     case mf_CUSP:return mfcuspdim(N,k,CHI);
    8163         168 :     case mf_OLD: return mfolddim(N,k,CHI);
    8164         217 :     case mf_FULL:return mffulldim(N,k,CHI);
    8165         231 :     case mf_EISEN: return mfeisensteindim(N,k,CHI);
    8166           0 :     default: pari_err_FLAG("mfdim");
    8167             :   }
    8168             :   return 0;/*LCOV_EXCL_LINE*/
    8169             : }
    8170             : static long
    8171        2114 : mfwt1dimsum(long N, long space)
    8172             : {
    8173        2114 :   switch(space)
    8174             :   {
    8175        1050 :     case mf_NEW:  return mfwt1newdimsum(N);
    8176        1057 :     case mf_CUSP: return mfwt1cuspdimsum(N);
    8177           7 :     case mf_OLD:  return mfwt1olddimsum(N);
    8178             :   }
    8179           0 :   pari_err_FLAG("mfdim");
    8180             :   return 0; /*LCOV_EXCL_LINE*/
    8181             : }
    8182             : /* mfdim for k = nk/dk */
    8183             : static long
    8184       44744 : mfdim_Nndkchi(long N, long nk, long dk, GEN CHI, long space)
    8185       43463 : { return (dk == 2)? mf2dim_Nkchi(N, nk >> 1, CHI, space)
    8186       88207 :                   : mfdim_Nkchi(N, nk, CHI, space); }
    8187             : /* FIXME: use direct dim Gamma1(N) formula, don't compute individual spaces */
    8188             : static long
    8189         252 : mfwtkdimsum(long N, long k, long dk, long space)
    8190             : {
    8191         252 :   GEN w = mfchars(N, k, dk, NULL);
    8192         252 :   long i, j, D = 0, l = lg(w);
    8193        1239 :   for (i = j = 1; i < l; i++)
    8194             :   {
    8195         987 :     GEN CHI = gel(w,i);
    8196         987 :     long d = mfdim_Nndkchi(N,k,dk,CHI,space);
    8197         987 :     if (d) D += d * myeulerphiu(mfcharorder(CHI));
    8198             :   }
    8199         252 :   return D;
    8200             : }
    8201             : static GEN
    8202         105 : mfwt1dims(long N, GEN vCHI, long space)
    8203             : {
    8204         105 :   GEN D = NULL;
    8205         105 :   switch(space)
    8206             :   {
    8207          56 :     case mf_NEW: D = mfwt1newdimall(N, vCHI); break;
    8208          21 :     case mf_CUSP:D = mfwt1cuspdimall(N, vCHI); break;
    8209          28 :     case mf_OLD: D = mfwt1olddimall(N, vCHI); break;
    8210           0 :     default: pari_err_FLAG("mfdim");
    8211             :   }
    8212         105 :   return D;
    8213             : }
    8214             : static GEN
    8215        2961 : mfwtkdims(long N, long k, long dk, GEN vCHI, long space)
    8216             : {
    8217        2961 :   GEN D, w = mfchars(N, k, dk, vCHI);
    8218        2961 :   long i, j, l = lg(w);
    8219        2961 :   D = cgetg(l, t_VEC);
    8220       46592 :   for (i = j = 1; i < l; i++)
    8221             :   {
    8222       43631 :     GEN CHI = gel(w,i);
    8223       43631 :     long d = mfdim_Nndkchi(N,k,dk,CHI,space);
    8224       43631 :     if (vCHI)
    8225         574 :       gel(D, j++) = mkvec2s(d, 0);
    8226       43057 :     else if (d)
    8227        2520 :       gel(D, j++) = fmt_dim(CHI, d, 0);
    8228             :   }
    8229        2961 :   setlg(D,j); return D;
    8230             : }
    8231             : GEN
    8232        5719 : mfdim(GEN NK, long space)
    8233             : {
    8234        5719 :   pari_sp av = avma;
    8235             :   long N, k, dk, joker;
    8236             :   GEN CHI, mf;
    8237        5719 :   if ((mf = checkMF_i(NK))) return utoi(MF_get_dim(mf));
    8238        5586 :   checkNK2(NK, &N, &k, &dk, &CHI, 2);
    8239        5586 :   if (!CHI) joker = 1;
    8240             :   else
    8241        2611 :     switch(typ(CHI))
    8242             :     {
    8243        2373 :       case t_INT: joker = 2; break;
    8244         112 :       case t_COL: joker = 3; break;
    8245         126 :       default: joker = 0; break;
    8246             :     }
    8247        5586 :   if (joker)
    8248             :   {
    8249             :     long d;
    8250             :     GEN D;
    8251        5460 :     if (k < 0) switch(joker)
    8252             :     {
    8253           0 :       case 1: return cgetg(1,t_VEC);
    8254           7 :       case 2: return gen_0;
    8255           0 :       case 3: return mfdim0all(CHI);
    8256             :     }
    8257        5453 :     if (k == 0)
    8258             :     {
    8259          28 :       if (space_is_cusp(space)) switch(joker)
    8260             :       {
    8261           7 :         case 1: return cgetg(1,t_VEC);
    8262           0 :         case 2: return gen_0;
    8263           7 :         case 3: return mfdim0all(CHI);
    8264             :       }
    8265          14 :       switch(joker)
    8266             :       {
    8267             :         long i, l;
    8268           7 :         case 1: retmkvec(fmt_dim(mfchartrivial(),0,0));
    8269           0 :         case 2: return gen_1;
    8270           7 :         case 3: l = lg(CHI); D = cgetg(l,t_VEC);
    8271          35 :                 for (i = 1; i < l; i++)
    8272             :                 {
    8273          28 :                   long t = mfcharistrivial(gel(CHI,i));
    8274          28 :                   gel(D,i) = mkvec2(t? gen_1: gen_0, gen_0);
    8275             :                 }
    8276           7 :                 return D;
    8277             :       }
    8278             :     }
    8279        5425 :     if (dk == 1 && k == 1 && space != mf_EISEN)
    8280         105 :     {
    8281        2219 :       long fix = 0, space0 = space;
    8282        2219 :       if (space == mf_FULL) space = mf_CUSP; /* remove Eisenstein part */
    8283        2219 :       if (joker == 2)
    8284             :       {
    8285        2114 :         d = mfwt1dimsum(N, space);
    8286        2114 :         if (space0 == mf_FULL) d += mfwtkdimsum(N,k,dk,mf_EISEN);/*add it back*/
    8287        2114 :         avma = av; return utoi(d);
    8288             :       }
    8289             :       /* must initialize explicitly: trivial spaces for E_k/S_k differ */
    8290         105 :       if (space0 == mf_FULL)
    8291             :       {
    8292           7 :         if (!CHI) fix = 1; /* must remove 0 spaces */
    8293           7 :         CHI = mfchars(N, k, dk, CHI);
    8294             :       }
    8295         105 :       D = mfwt1dims(N, CHI, space);
    8296         105 :       if (space0 == mf_FULL)
    8297             :       {
    8298           7 :         GEN D2 = mfwtkdims(N, k, dk, CHI, mf_EISEN);
    8299           7 :         D = merge_dims(D, D2, fix? CHI: NULL);
    8300             :       }
    8301             :     }
    8302             :     else
    8303             :     {
    8304        3206 :       if (joker==2) { d = mfwtkdimsum(N,k,dk,space); avma=av; return utoi(d); }
    8305        2954 :       D = mfwtkdims(N, k, dk, CHI, space);
    8306             :     }
    8307        3059 :     if (!CHI) return gerepileupto(av, vecsort(D, mkvecsmall(1)));
    8308         105 :     return gerepilecopy(av, D);
    8309             :   }
    8310         126 :   return utoi( mfdim_Nndkchi(N, k, dk, CHI, space) );
    8311             : }
    8312             : 
    8313             : GEN
    8314         301 : mfbasis(GEN NK, long space)
    8315             : {
    8316         301 :   pari_sp av = avma;
    8317             :   long N, k, dk;
    8318             :   GEN mf, CHI;
    8319         301 :   if ((mf = checkMF_i(NK))) return concat(gel(mf,2), gel(mf,3));
    8320           7 :   checkNK2(NK, &N, &k, &dk, &CHI, 0);
    8321           7 :   if (dk == 2) return gerepilecopy(av, mf2basis(N, k>>1, CHI, space));
    8322           7 :   mf = mfinit_Nkchi(N, k, CHI, space, 1);
    8323           7 :   return gerepilecopy(av, MF_get_basis(mf));
    8324             : }
    8325             : 
    8326             : static GEN
    8327          28 : deg1ser_shallow(GEN a1, GEN a0, long v, long e)
    8328          28 : { return RgX_to_ser(deg1pol_shallow(a1, a0, v), e+2); }
    8329             : /* r / x + O(1) */
    8330             : static GEN
    8331          28 : simple_pole(GEN r)
    8332             : {
    8333          28 :   GEN S = deg1ser_shallow(gen_0, r, 0, 1);
    8334          28 :   setvalp(S, -1); return S;
    8335             : }
    8336             : 
    8337             : /* F form, E embedding; mfa = mfatkininit or root number (eigenform case) */
    8338             : static GEN
    8339         105 : mflfuncreate(GEN mfa, GEN F, GEN E, GEN N, GEN gk)
    8340             : {
    8341         105 :   GEN LF = cgetg(8,t_VEC), polar = cgetg(1,t_COL), eps;
    8342         105 :   long k = itou(gk);
    8343         105 :   gel(LF,1) = lfuntag(t_LFUN_MFCLOS, mkvec3(F,E,gen_1));
    8344         105 :   if (typ(mfa) != t_VEC)
    8345          84 :     eps = mfa; /* cuspidal eigenform: root number; no poles */
    8346             :   else
    8347             :   { /* mfatkininit */
    8348          21 :     GEN a0, b0, vF, vG, G = NULL, M = gdiv(gel(mfa,2), gel(mfa,3)), mf = gel(mfa,4);
    8349          21 :     vF = mftobasis_i(mf, F);
    8350          21 :     vG = RgM_RgC_mul(M, vF);
    8351          21 :     if (gequal(vF,vG)) eps = gen_1;
    8352           7 :     else if (gequal(vF,gneg(vG))) eps = gen_m1;
    8353             :     else
    8354             :     { /* not self-dual */
    8355           7 :       eps = NULL;
    8356           7 :       G = mfatkin(mfa, F);
    8357           7 :       gel(LF,2) = lfuntag(t_LFUN_MFCLOS, mkvec3(G,E,ginv(gel(mfa,3))));
    8358           7 :       gel(LF,6) = powIs(k);
    8359             :     }
    8360             :     /* polar part */
    8361          21 :     a0 = mfcoef(F,0);
    8362          21 :     b0 = eps? gmul(eps,a0): mfcoef(G,0);
    8363          21 :     if (!gequal0(b0))
    8364             :     {
    8365          14 :       b0 = mulcxpowIs(gmul2n(b0,1), k);
    8366          14 :       polar = vec_append(polar, mkvec2(gk, simple_pole(b0)));
    8367             :     }
    8368          21 :     if (!gequal0(a0))
    8369             :     {
    8370          14 :       a0 = gneg(gmul2n(a0,1));
    8371          14 :       polar = vec_append(polar, mkvec2(gen_0, simple_pole(a0)));
    8372             :     }
    8373             :   }
    8374         105 :   if (eps) /* self-dual */
    8375             :   {
    8376          98 :     gel(LF,2) = mfcharorder(mf_get_CHI(F)) <= 2? gen_0: gen_1;
    8377          98 :     gel(LF,6) = mulcxpowIs(eps,k);
    8378             :   }
    8379         105 :   gel(LF,3) = mkvec2(gen_0, gen_1);
    8380         105 :   gel(LF,4) = gk;
    8381         105 :   gel(LF,5) = N;
    8382         105 :   if (lg(polar) == 1) setlg(LF,7); else gel(LF,7) = polar;
    8383         105 :   return LF;
    8384             : }
    8385             : static GEN
    8386          91 : mflfuncreateall(long sd, GEN mfa, GEN F, GEN vE, GEN gN, GEN gk)
    8387             : {
    8388          91 :   long i, l = lg(vE);
    8389          91 :   GEN L = cgetg(l, t_VEC);
    8390         196 :   for (i = 1; i < l; i++)
    8391         105 :     gel(L,i) = mflfuncreate(sd? gel(mfa,i): mfa, F, gel(vE,i), gN, gk);
    8392          91 :   return L;
    8393             : }
    8394             : GEN
    8395          42 : lfunmf(GEN mf, GEN F, long bitprec)
    8396             : {
    8397          42 :   pari_sp av = avma;
    8398          42 :   long i, l, prec = nbits2prec(bitprec);
    8399             :   GEN L, gk, gN;
    8400          42 :   mf = checkMF(mf);
    8401          42 :   gk = MF_get_gk(mf);
    8402          42 :   gN = MF_get_gN(mf);
    8403          42 :   if (F)
    8404             :   {
    8405             :     GEN v;
    8406          35 :     long s = MF_get_space(mf);
    8407          35 :     if (!checkmf_i(F)) pari_err_TYPE("lfunmf", F);
    8408          35 :     if (!mfisinspace_i(mf, F)) err_space(F);
    8409          35 :     L = NULL;
    8410          35 :     if ((s == mf_NEW || s == mf_CUSP || s == mf_FULL)
    8411          21 :         && gequal(mfcoefs_i(F,1,1), mkvec2(gen_0,gen_1)))
    8412             :     { /* check if eigenform */
    8413          14 :       GEN vP, vF, b = mftobasis_i(mf, F);
    8414          14 :       long lF, d = degpol(mf_get_field(F));
    8415          14 :       v = mfsplit(mf, d, 0);
    8416          14 :       vF = gel(v,1);
    8417          14 :       vP = gel(v,2); lF = lg(vF);
    8418          14 :       for (i = 1; i < lF; i++)
    8419          14 :         if (degpol(gel(vP,i)) == d && gequal(gel(vF,i), b))
    8420             :         {
    8421          14 :           GEN vE = mfgetembed(F, prec);
    8422          14 :           GEN Z = mffrickeeigen_i(mf, mkvec(b), mkvec(vE), prec);
    8423          14 :           L = mflfuncreateall(1, gel(Z,1), F, vE, gN, gk);
    8424          14 :           break;
    8425             :         }
    8426             :     }
    8427          35 :     if (!L)
    8428             :     { /* not an eigenform: costly general case */
    8429          21 :       GEN mfa = mfatkininit_i(mf, itou(gN), 1, prec);
    8430          21 :       L = mflfuncreateall(0,mfa, F, mfgetembed(F,prec), gN, gk);
    8431             :     }
    8432          35 :     if (lg(L) == 2) L = gel(L,1);
    8433             :   }
    8434             :   else
    8435             :   {
    8436           7 :     GEN M = mfeigenbasis(mf), vE = mfeigenembed(mf, prec);
    8437           7 :     GEN v = mffrickeeigen(mf, vE, prec);
    8438           7 :     l = lg(vE); L = cgetg(l, t_VEC);
    8439          63 :     for (i = 1; i < l; i++)
    8440          56 :       gel(L,i) = mflfuncreateall(1,gel(v,i), gel(M,i), gel(vE,i), gN, gk);
    8441             :   }
    8442          42 :   return gerepilecopy(av, L);
    8443             : }
    8444             : 
    8445             : GEN
    8446          21 : mffromell(GEN E)
    8447             : {
    8448          21 :   pari_sp av = avma;
    8449             :   GEN mf, F, z, v, S;
    8450             :   long N, i, l;
    8451             : 
    8452          21 :   checkell(E);
    8453          21 :   if (ell_get_type(E) != t_ELL_Q) pari_err_TYPE("mfffromell [E not over Q]", E);
    8454          21 :   N = itos(ellQ_get_N(E));
    8455          21 :   mf = mfinit_i(mkvec2(utoi(N), gen_2), mf_NEW);
    8456          21 :   v = split_i(mf, 1, 0);
    8457          21 :   S = gel(v,1); l = lg(S); /* rational newforms */
    8458          21 :   F = tag(t_MF_ELL, mkNK(N,2,mfchartrivial()), E);
    8459          21 :   z = mftobasis_i(mf, F);
    8460          21 :   for(i = 1; i < l; i++)
    8461          21 :     if (gequal(z, gel(S,i))) break;
    8462          21 :   if (i == l) pari_err_BUG("mffromell [E is not modular]");
    8463          21 :   return gerepilecopy(av, mkvec3(mf, F, z));
    8464             : }
    8465             : 
    8466             : /* returns -1 if not, degree otherwise */
    8467             : long
    8468         105 : polishomogeneous(GEN P)
    8469             : {
    8470             :   long i, D, l;
    8471         105 :   if (typ(P) != t_POL) return 0;
    8472          49 :   D = -1; l = lg(P);
    8473         231 :   for (i = 2; i < l; i++)
    8474             :   {
    8475         182 :     GEN c = gel(P,i);
    8476             :     long d;
    8477         182 :     if (gequal0(c)) continue;
    8478          84 :     d = polishomogeneous(c);
    8479          84 :     if (d < 0) return -1;
    8480          84 :     if (D < 0) D = d + i-2; else if (D != d + i-2) return -1;
    8481             :   }
    8482          49 :   return D;
    8483             : }
    8484             : 
    8485             : /* 1 if spherical, 0 otherwise */
    8486             : static long
    8487          21 : polisspherical(GEN Qi, GEN P)
    8488             : {
    8489          21 :   pari_sp av = avma;
    8490             :   GEN va, S;
    8491             :   long lva, i, j, r;
    8492          21 :   if (gequal0(P) || degpol(P) <= 1) return 1;
    8493          14 :   va = variables_vecsmall(P); lva = lg(va);
    8494          14 :   if (lva > lg(Qi)) pari_err(e_MISC, "too many variables in mffromqf");
    8495          14 :   S = gen_0;
    8496          42 :   for (j = 1; j < lva; j++)
    8497             :   {
    8498          28 :     GEN col = gel(Qi, j), Pj = deriv(P, va[j]);
    8499          70 :     for (i = 1; i <= j; i++)
    8500             :     {
    8501          42 :       GEN coe = gel(col, i);
    8502          42 :       if (i != j) coe = gmul2n(coe, 1);
    8503          42 :       if (!gequal0(coe)) S = gadd(S, gmul(coe, deriv(Pj, va[i])));
    8504             :     }
    8505             :   }
    8506          14 :   r = gequal0(S); avma = av; return r;
    8507             : }
    8508             : 
    8509             : static GEN
    8510          28 : c_QFsimple_i(long n, GEN Q, GEN P)
    8511             : {
    8512          28 :   pari_sp av = avma;
    8513          28 :   GEN V, v = qfrep0(Q, utoi(n), 1);
    8514          28 :   long i, l = lg(v);
    8515          28 :   V = cgetg(l+1, t_VEC);
    8516          49 :   if (!P || equali1(P))
    8517             :   {
    8518          21 :     gel(V,1) = gen_1;
    8519          21 :     for (i = 2; i <= l; i++) gel(V,i) = utoi(v[i-1] << 1);
    8520             :   }
    8521             :   else
    8522             :   {
    8523           7 :     gel(V,1) = gcopy(P);
    8524           7 :     for (i = 2; i <= l; i++) gel(V,i) = gmulgs(P, v[i-1] << 1);
    8525             :   }
    8526          28 :   return gerepileupto(av, V);
    8527             : }
    8528             : static GEN
    8529          35 : c_QF_i(long n, GEN Q, GEN P)
    8530             : {
    8531          35 :   pari_sp av = avma;
    8532             :   GEN V, v, va;
    8533             :   long i, lva, lq, l;
    8534          35 :   if (!P || typ(P) != t_POL) return c_QFsimple_i(n, Q, P);
    8535           7 :   v = gel(minim(Q, utoi(2*n), NULL), 3);
    8536           7 :   va = variables_vec(P); lq = lg(Q) - 1; lva = lg(va) - 1;
    8537           7 :   V = zerovec(n + 1); l = lg(v);
    8538          35 :   for (i = 1; i < l; i++)
    8539             :   {
    8540          28 :     GEN X = gel(v,i);
    8541          28 :     long ind = (itos(qfeval0(Q, X, NULL)) >> 1) + 1;
    8542          28 :     if (lq > lva) X = vecslice(X, 1, lva);
    8543          28 :     gel(V, ind) = gadd(gel(V, ind), gsubstvec(P, va, X));
    8544             :   }
    8545           7 :   return gerepilecopy(av, gmul2n(V, 1));
    8546             : }
    8547             : 
    8548             : GEN
    8549          42 : mffromqf(GEN Q, GEN P)
    8550             : {
    8551          42 :   pari_sp av = avma;
    8552             :   GEN G, Qi, F, D, N, mf, v, gk, gwt, chi;
    8553             :   long m, d, space;
    8554          42 :   if (typ(Q) != t_MAT) pari_err_TYPE("mffromqf", Q);
    8555          42 :   if (!RgM_is_ZM(Q) || !qf_iseven(Q))
    8556           0 :     pari_err_TYPE("mffromqf [not integral or even]", Q);
    8557          42 :   m = lg(Q)-1;
    8558          42 :   gk = sstoQ(m, 2);
    8559          42 :   Qi = ZM_inv(Q, &N);
    8560          42 :   if (!qf_iseven(Qi)) N = shifti(N, 1);
    8561          42 :   if (!P || gequal1(P)) { d = 0; P = NULL; }
    8562             :   else
    8563             :   {
    8564          21 :     d = polishomogeneous(P);
    8565          21 :     if (d < 0) pari_err_TYPE("mffromqf [not homogeneous t_POL]", P);
    8566          21 :     if (!polisspherical(Qi, P))
    8567           0 :       pari_err_TYPE("mffromqf [not a spherical t_POL]", P);
    8568          21 :     if (d == 0) P = simplify_shallow(P);
    8569             :   }
    8570          42 :   D = ZM_det(Q);
    8571          42 :   if (typ(gk) == t_INT) { if (mpodd(gk)) D = negi(D); } else D = shifti(D, 1);
    8572          42 :   space = d > 0 ? mf_CUSP : mf_FULL;
    8573          42 :   G = znstar0(N,1);
    8574          42 :   chi = mkvec2(G, znchar_quad(G,D));
    8575          42 :   gwt = gaddgs(gk, d);
    8576          42 :   mf = mfinit(mkvec3(N, gwt, chi), space);
    8577          42 :   if (odd(d))
    8578             :   {
    8579           7 :     F = mftrivial();
    8580           7 :     v = zerocol(MF_get_dim(mf));
    8581             :   }
    8582             :   else
    8583             :   {
    8584          35 :     F = c_QF_i(mfsturm(mf), Q, P);
    8585          35 :     v = mftobasis_i(mf, F);
    8586          35 :     F = mflinear(mf, v);
    8587             :   }
    8588          42 :   return gerepilecopy(av, mkvec3(mf, F, v));
    8589             : }
    8590             : 
    8591             : /***********************************************************************/
    8592             : /*                          Eisenstein Series                          */
    8593             : /***********************************************************************/
    8594             : /* \sigma_{k-1}(\chi,n) */
    8595             : static GEN
    8596       22057 : sigchi(long k, GEN CHI, long n)
    8597             : {
    8598       22057 :   pari_sp av = avma;
    8599       22057 :   GEN S = gen_1, D = mydivisorsu(u_ppo(n,mfcharmodulus(CHI)));
    8600       22057 :   long i, l = lg(D), ord = mfcharorder(CHI), vt = varn(mfcharpol(CHI));
    8601       77742 :   for (i = 2; i < l; i++) /* skip D[1] = 1 */
    8602             :   {
    8603       55685 :     long d = D[i], a = mfcharevalord(CHI, d, ord);
    8604       55685 :     S = gadd(S, mygmodulo_lift(a, ord, powuu(d, k-1), vt));
    8605             :   }
    8606       22057 :   return gerepileupto(av,S);
    8607             : }
    8608             : 
    8609             : /* write n = n0*n1*n2, (n0,N1*N2) = 1, n1 | N1^oo, n2 | N2^oo;
    8610             :  * return NULL if (n,N1,N2) > 1, else return factoru(n0) */
    8611             : static GEN
    8612      194516 : sigchi2_dec(long n, long N1, long N2, long *pn1, long *pn2)
    8613             : {
    8614      194516 :   GEN P0, E0, P, E, fa = myfactoru(n);
    8615             :   long i, j, l;
    8616      194516 :   *pn1 = 1;
    8617      194516 :   *pn2 = 1;
    8618      194516 :   if (N1 == 1 && N2 == 1) return fa;
    8619      181251 :   P = gel(fa,1); l = lg(P);
    8620      181251 :   E = gel(fa,2);
    8621      181251 :   P0 = cgetg(l, t_VECSMALL);
    8622      181251 :   E0 = cgetg(l, t_VECSMALL);
    8623      394079 :   for (i = j = 1; i < l; i++)
    8624             :   {
    8625      234654 :     long p = P[i], e = E[i];
    8626      234654 :     if (N1 % p == 0)
    8627             :     {
    8628       37086 :       if (N2 % p == 0) return NULL;
    8629       15260 :       *pn1 *= upowuu(p,e);
    8630             :     }
    8631      197568 :     else if (N2 % p == 0)
    8632       39872 :       *pn2 *= upowuu(p,e);
    8633      157696 :     else { P0[j] = p; E0[j] = e; j++; }
    8634             :   }
    8635      159425 :   setlg(P0, j);
    8636      159425 :   setlg(E0, j); return mkvec2(P0,E0);
    8637             : }
    8638             : 
    8639             : /* sigma_{k-1}(\chi_1,\chi_2,n), ord multiple of lcm(ord(CHI1),ord(CHI2)) */
    8640             : static GEN
    8641      149632 : sigchi2(long k, GEN CHI1, GEN CHI2, long n, long ord)
    8642             : {
    8643      149632 :   pari_sp av = avma;
    8644      149632 :   GEN S = gen_0, D;
    8645      149632 :   long i, l, n1, n2, vt, N1 = mfcharmodulus(CHI1), N2 = mfcharmodulus(CHI2);
    8646      149632 :   D = sigchi2_dec(n, N1, N2, &n1, &n2); if (!D) { avma = av; return S; }
    8647      132174 :   D = divisorsu_fact(D); l = lg(D);
    8648      132174 :   vt = varn(mfcharpol(CHI1));
    8649      472374 :   for (i = 1; i < l; i++)
    8650             :   { /* S += d^(k-1)*chi1(d)*chi2(n/d) */
    8651      340200 :     long a, d = n2*D[i], nd = n1*D[l-i]; /* (d,N1)=1; (n/d,N2) = 1 */
    8652      340200 :     a = mfcharevalord(CHI1, d, ord) + mfcharevalord(CHI2, nd, ord);
    8653      340200 :     if (a >= ord) a -= ord;
    8654      340200 :     S = gadd(S, mygmodulo_lift(a, ord, powuu(d, k-1), vt));
    8655             :   }
    8656      132174 :   return gerepileupto(av, S);
    8657             : }
    8658             : 
    8659             : /**************************************************************************/
    8660             : /**           Dirichlet characters with precomputed values               **/
    8661             : /**************************************************************************/
    8662             : /* CHI mfchar */
    8663             : static GEN
    8664       11116 : mfcharcxinit(GEN CHI, long prec)
    8665             : {
    8666       11116 :   GEN G = gel(CHI,1), chi = gel(CHI,2), z, V;
    8667       11116 :   GEN v = ncharvecexpo(G, znconrey_normalized(G,chi));
    8668       11116 :   long n, l = lg(v), o = mfcharorder(CHI);
    8669       11116 :   V = cgetg(l, t_VEC);
    8670       11116 :   z = grootsof1(o, prec); /* Mod(t, Phi_o(t)) -> e(1/o) */
    8671       11116 :   for (n = 1; n < l; n++) gel(V,n) = v[n] < 0? gen_0: gel(z, v[n]+1);
    8672       11116 :   return mkvecn(6, G, chi, gmfcharorder(CHI), v, V, mfcharpol(CHI));
    8673             : }
    8674             : /* v a "CHIvec" */
    8675             : static long
    8676    18600708 : CHIvec_N(GEN v) { return itou(znstar_get_N(gel(v,1))); }
    8677             : static GEN
    8678       10752 : CHIvec_CHI(GEN v)
    8679       10752 : { return mkvec4(gel(v,1), gel(v,2), gel(v,3), gel(v,6)); }
    8680             : /* character order */
    8681             : static long
    8682       31269 : CHIvec_ord(GEN v) { return itou(gel(v,3)); }
    8683             : /* character exponents, i.e. t such that chi(n) = e(t) */
    8684             : static GEN
    8685      301168 : CHIvec_expo(GEN v) { return gel(v,4); }
    8686             : /* character values chi(n) */
    8687             : static GEN
    8688    18145358 : CHIvec_val(GEN v) { return gel(v,5); }
    8689             : /* CHI(n) */
    8690             : static GEN
    8691    18132576 : mychareval(GEN v, long n)
    8692             : {
    8693    18132576 :   long N = CHIvec_N(v), ind = n%N;
    8694    18132576 :   if (ind <= 0) ind += N;
    8695    18132576 :   return gel(CHIvec_val(v), ind);
    8696             : }
    8697             : /* return c such that CHI(n) = e(c / ordz) or -1 if (n,N) > 1 */
    8698             : static long
    8699      301168 : mycharexpo(GEN v, long n)
    8700             : {
    8701      301168 :   long N = CHIvec_N(v), ind = n%N;
    8702      301168 :   if (ind <= 0) ind += N;
    8703      301168 :   return CHIvec_expo(v)[ind];
    8704             : }
    8705             : /* faster than mfcharparity */
    8706             : static long
    8707       32102 : CHIvec_parity(GEN v) { return mycharexpo(v,-1) ? -1: 1; }
    8708             : /**************************************************************************/
    8709             : 
    8710             : static ulong
    8711       44884 : sigchi2_Fl(long k, GEN CHI1vec, GEN CHI2vec, long n, GEN vz, ulong p)
    8712             : {
    8713       44884 :   pari_sp av = avma;
    8714       44884 :   long ordz = lg(vz)-2, i, l, n1, n2;
    8715       44884 :   ulong S = 0;
    8716       44884 :   GEN D = sigchi2_dec(n, CHIvec_N(CHI1vec), CHIvec_N(CHI2vec), &n1, &n2);
    8717       44884 :   if (!D) { avma = av; return S; }
    8718       40516 :   D = divisorsu_fact(D);
    8719       40516 :   l = lg(D);
    8720      136402 :   for (i = 1; i < l; i++)
    8721             :   { /* S += d^(k-1)*chi1(d)*chi2(n/d) */
    8722       95886 :     long a, d = n2*D[i], nd = n1*D[l-i]; /* (d,N1)=1, (n/d,N2)=1 */
    8723       95886 :     a = mycharexpo(CHI2vec, nd) + mycharexpo(CHI1vec, d);
    8724       95886 :     if (a >= ordz) a -= ordz;
    8725       95886 :     S = Fl_add(S, mygmodulo_Fl(a, vz, Fl_powu(d,k-1,p), p), p);
    8726             :   }
    8727       40516 :   avma = av; return S;
    8728             : }
    8729             : 
    8730             : /**********************************************************************/
    8731             : /* Fourier expansions of Eisenstein series                            */
    8732             : /**********************************************************************/
    8733             : /* L(CHI,0) / 2, order(CHI) | ord != 0 */
    8734             : static GEN
    8735        1575 : charLFwt1(GEN CHI, long ord)
    8736             : {
    8737             :   GEN S;
    8738        1575 :   long r, vt, m = mfcharmodulus(CHI);
    8739             : 
    8740        1575 :   if (m == 1) return mkfrac(gen_m1,stoi(4));
    8741        1575 :   S = gen_0; vt = varn(mfcharpol(CHI));
    8742       40656 :   for (r = 1; r < m; r++)
    8743             :   { /* S += r*chi(r) */
    8744             :     long a;
    8745       39081 :     if (ugcd(m,r) != 1) continue;
    8746       29638 :     a = mfcharevalord(CHI,r,ord);
    8747       29638 :     S = gadd(S, mygmodulo_lift(a, ord, utoi(r), vt));
    8748             :   }
    8749        1575 :   return gdivgs(S, -2*m);
    8750             : }
    8751             : /* L(CHI,0) / 2, mod p */
    8752             : static ulong
    8753        1043 : charLFwt1_Fl(GEN CHIvec, GEN vz, ulong p)
    8754             : {
    8755        1043 :   long r, m = CHIvec_N(CHIvec);
    8756             :   ulong S;
    8757        1043 :   if (m == 1) return Rg_to_Fl(mkfrac(gen_m1,stoi(4)), p);
    8758        1043 :   S = 0;
    8759       54299 :   for (r = 1; r < m; r++)
    8760             :   { /* S += r*chi(r) */
    8761       53256 :     long a = mycharexpo(CHIvec,r);
    8762       53256 :     if (a < 0) continue;
    8763       51086 :     S = Fl_add(S, mygmodulo_Fl(a, vz, r, p), p);
    8764             :   }
    8765        1043 :   return Fl_div(Fl_neg(S,p), 2*m, p);
    8766             : }
    8767             : /* L(CHI,1-k) / 2, order(CHI) | ord != 0 */
    8768             : static GEN
    8769        1365 : charLFwtk(long k, GEN CHI, long ord)
    8770             : {
    8771             :   GEN S, P, dS;
    8772             :   long r, m, vt;
    8773             : 
    8774        1365 :   if (k == 1) return charLFwt1(CHI, ord);
    8775        1358 :   m = mfcharmodulus(CHI);
    8776        1358 :   if (m == 1) return gdivgs(bernfrac(k),-2*k);
    8777         770 :   S = gen_0; vt = varn(mfcharpol(CHI));
    8778         770 :   P = ZX_rescale(Q_remove_denom(bernpol(k,0), &dS), utoi(m));
    8779         770 :   dS = mul_denom(dS, stoi(-2*m*k));
    8780        9394 :   for (r = 1; r < m; r++)
    8781             :   { /* S += P(r)*chi(r) */
    8782             :     long a;
    8783        8624 :     if (ugcd(r,m) != 1) continue;
    8784        6846 :     a = mfcharevalord(CHI,r,ord);
    8785        6846 :     S = gadd(S, mygmodulo_lift(a, ord, poleval(P, utoi(r)), vt));
    8786             :   }
    8787         770 :   return gdiv(S, dS);
    8788             : }
    8789             : /* L(CHI,1-k) / 2, mod p */
    8790             : static ulong
    8791        1666 : charLFwtk_Fl(long k, GEN CHIvec, GEN vz, ulong p)
    8792             : {
    8793             :   GEN P;
    8794             :   long r, m;
    8795             :   ulong S;
    8796        1666 :   if (k == 1) return charLFwt1_Fl(CHIvec, vz, p);
    8797         623 :   m = CHIvec_N(CHIvec);
    8798         623 :   if (m == 1) return Rg_to_Fl(gdivgs(bernfrac(k),-2*k), p);
    8799         371 :   S = 0;
    8800         371 :   P = RgX_to_Flx(RgX_rescale(bernpol(k,0), utoi(m)), p);
    8801        7049 :   for (r = 1; r < m; r++)
    8802             :   { /* S += P(r)*chi(r) */
    8803        6678 :     long a = mycharexpo(CHIvec,r);
    8804        6678 :     if (a < 0) continue;
    8805        5558 :     S = Fl_add(S, mygmodulo_Fl(a, vz, Flx_eval(P,r,p), p), p);
    8806             :   }
    8807         371 :   return Fl_div(Fl_neg(S,p), 2*k*m, p);
    8808             : }
    8809             : 
    8810             : static GEN
    8811        5509 : mfeisenstein2_0(long k, GEN CHI1, GEN CHI2, long ord)
    8812             : {
    8813        5509 :   if (k == 1 && mfcharistrivial(CHI1))
    8814        1568 :     return charLFwt1(CHI2, ord);
    8815        3941 :   else if (mfcharistrivial(CHI2))
    8816        1211 :     return charLFwtk(k, CHI1, ord);
    8817        2730 :   else return gen_0;
    8818             : }
    8819             : static ulong
    8820        2940 : mfeisenstein2_0_Fl(long k, GEN CHI1vec, GEN CHI2vec, GEN vz, ulong p)
    8821             : {
    8822        2940 :   if (k == 1 && CHIvec_ord(CHI1vec) == 1)
    8823        1043 :     return charLFwtk_Fl(k, CHI2vec, vz, p);
    8824        1897 :   else if (CHIvec_ord(CHI2vec) == 1)
    8825         623 :     return charLFwtk_Fl(k, CHI1vec, vz, p);
    8826        1274 :   else return 0;
    8827             : }
    8828             : static GEN
    8829          63 : NK_eisen2(long k, GEN CHI1, GEN CHI2)
    8830             : {
    8831          63 :   long N = mfcharmodulus(CHI1)*mfcharmodulus(CHI2);
    8832          63 :   return mkNK(N, k, mfcharmul(CHI1,CHI2));
    8833             : }
    8834             : static GEN
    8835         245 : mfeisenstein_i(long k, GEN CHI1, GEN CHI2)
    8836             : {
    8837         245 :   long s = 1, ord, vt;
    8838             :   GEN E0, NK, vchi, CHI, T;
    8839         245 :   if (CHI2) { CHI2 = get_mfchar(CHI2); if (mfcharparity(CHI2) < 0) s = -s; }
    8840         245 :   if (CHI1) { CHI1 = get_mfchar(CHI1); if (mfcharparity(CHI1) < 0) s = -s; }
    8841         231 :   if (s != m1pk(k)) return mftrivial();
    8842         210 :   if (!CHI1)
    8843         133 :     CHI = CHI2? CHI2: mfchartrivial();
    8844          77 :   else if (!CHI2)
    8845          14 :     CHI = CHI1;
    8846             :   else
    8847          63 :     CHI = NULL;
    8848         210 :   if (CHI)
    8849             :   { /* E_k(chi) */
    8850         147 :     vt = varn(mfcharpol(CHI));
    8851         147 :     ord = mfcharorder(CHI);
    8852         147 :     NK = mkNK(mfcharmodulus(CHI), k, CHI);
    8853         147 :     E0 = charLFwtk(k, CHI, ord);
    8854         147 :     vchi = mkvec3(E0, mkvec(mfcharpol(CHI)), CHI);
    8855         147 :     return tag(t_MF_EISEN, NK, vchi);
    8856             :   }
    8857             :   /* E_k(chi1,chi2) */
    8858          63 :   vt = varn(mfcharpol(CHI1));
    8859          63 :   NK = NK_eisen2(k, CHI1, CHI2);
    8860          63 :   ord = ulcm(mfcharorder(CHI1), mfcharorder(CHI2));
    8861          63 :   E0 = mfeisenstein2_0(k, CHI1, CHI2, ord);
    8862          63 :   T = mkvec(polcyclo(ord_canon(ord), vt));
    8863          63 :   vchi = mkvec4(E0, T, CHI1, CHI2);
    8864          63 :   return tag2(t_MF_EISEN, NK, vchi, mkvecsmall2(ord,0));
    8865             : }
    8866             : GEN
    8867         245 : mfeisenstein(long k, GEN CHI1, GEN CHI2)
    8868             : {
    8869         245 :   pari_sp av = avma;
    8870         245 :   if (k < 1) pari_err_DOMAIN("mfeisenstein", "k", "<", gen_1, stoi(k));
    8871         245 :   return gerepilecopy(av, mfeisenstein_i(k, CHI1, CHI2));
    8872             : }
    8873             : 
    8874             : static GEN
    8875        1218 : mfeisenstein2all(long N0, GEN NK, long k, GEN CHI1, GEN CHI2, GEN T, long o)
    8876             : {
    8877        1218 :   GEN E, E0 = mfeisenstein2_0(k, CHI1,CHI2, o), vchi = mkvec4(E0, T, CHI1,CHI2);
    8878        1218 :   long j, d = (lg(T)==4)? itou(gmael(T,3,1)): 1;
    8879        1218 :   E = cgetg(d+1, t_VEC);
    8880        1218 :   for (j=1; j<=d; j++) gel(E,j) = tag2(t_MF_EISEN, NK,vchi,mkvecsmall2(o,j-1));
    8881        1218 :   return mfbdall(E, N0 / mf_get_N(gel(E,1)));
    8882             : }
    8883             : 
    8884             : static GEN
    8885         532 : zncharsG(GEN G)
    8886             : {
    8887         532 :   long i, l, N = itou(znstar_get_N(G));
    8888             :   GEN vCHI, V;
    8889         532 :   if (N == 1) return mkvec2(gen_1,cgetg(1,t_COL));
    8890         532 :   vCHI = const_vec(N,NULL);
    8891         532 :   V = cyc2elts(znstar_get_conreycyc(G));
    8892         532 :   l = lg(V);
    8893       15848 :   for (i = 1; i < l; i++)
    8894             :   {
    8895       15316 :     GEN chi0, chi = zc_to_ZC(gel(V,i)), n, F;
    8896       15316 :     F = znconreyconductor(G, chi, &chi0);
    8897       15316 :     if (typ(F) != t_INT) F = gel(F,1);
    8898       15316 :     n = znconreyexp(G, chi);
    8899       15316 :     gel(vCHI, itos(n)) = mkvec2(F, chi0);
    8900             :   }
    8901         532 :   return vCHI;
    8902             : }
    8903             : 
    8904             : /* CHI primitive, f(CHI) | N. Return pairs (CHI1,CHI2) both primitive
    8905             :  * such that f(CHI1)*f(CHI2) | N and CHI1 * CHI2 = CHI;
    8906             :  * if k = 1, CHI1 is even; if k = 2, omit (1,1) if CHI = 1 */
    8907             : static GEN
    8908         721 : mfeisensteinbasis_i(long N0, long k, GEN CHI)
    8909             : {
    8910         721 :   GEN G = gel(CHI,1), chi = gel(CHI,2), vT = const_vec(myeulerphiu(N0), NULL);
    8911             :   GEN CHI0, GN, chiN, Lchi, LG, V, RES, NK, T;
    8912         721 :   long i, j, l, n, n1, N, ord = mfcharorder(CHI), OC = ord_canon(ord);
    8913         721 :   long F = mfcharmodulus(CHI), vt = varn(mfcharpol(CHI));
    8914             : 
    8915         721 :   CHI0 = (F == 1)? CHI: mfchartrivial();
    8916         721 :   j = 1; RES = cgetg(N0+1, t_VEC);
    8917         721 :   T = gel(vT,OC) = Qab_trace_init(polcyclo(OC,vt), OC, OC);
    8918         721 :   if (F != 1 || k != 2)
    8919             :   { /* N1 = 1 */
    8920         602 :     NK = mkNK(F, k, CHI);
    8921         602 :     gel(RES, j++) = mfeisenstein2all(N0, NK, k, CHI0, CHI, T, ord);
    8922         602 :     if (F != 1 && k != 1)
    8923         203 :       gel(RES, j++) = mfeisenstein2all(N0, NK, k, CHI, CHI0, T, ord);
    8924             :   }
    8925         721 :   if (N0 == 1) { setlg(RES,j); return RES; }
    8926         651 :   GN = G; chiN = chi;
    8927         651 :   if (F == N0) N = N0;
    8928             :   else
    8929             :   {
    8930         406 :     GEN faN = myfactoru(N0), P = gel(faN,1), E = gel(faN,2);
    8931         406 :     long lP = lg(P);
    8932        1036 :     for (i = N = 1; i < lP; i++)
    8933             :     {
    8934         630 :       long p = P[i];
    8935         630 :       N *= upowuu(p, maxuu(E[i]/2, z_lval(F,p)));
    8936             :     }
    8937         406 :     if ((N & 3) == 2) N >>= 1;
    8938         406 :     if (N == 1) { setlg(RES,j); return RES; }
    8939         287 :     if (F != N)
    8940             :     {
    8941          91 :       GN = znstar0(utoipos(N),1);
    8942          91 :       chiN = zncharinduce(G, chi, GN);
    8943             :     }
    8944             :   }
    8945         532 :   LG = const_vec(N, NULL); /* LG[d] = znstar(d,1) or NULL */
    8946         532 :   gel(LG,1) = gel(CHI0,1);
    8947         532 :   gel(LG,F) = G;
    8948         532 :   gel(LG,N) = GN;
    8949         532 :   Lchi = coprimes_zv(N);
    8950         532 :   n = itou(znconreyexp(GN,chiN));
    8951         532 :   V = zncharsG(GN); l = lg(V);
    8952       20342 :   for (n1 = 2; n1 < l; n1++) /* skip 1 (trivial char) */
    8953             :   {
    8954       19810 :     GEN v = gel(V,n1), w, chi1, chi2, G1, G2, CHI1, CHI2;
    8955             :     long N12, N1, N2, no, oc, o12, t, m;
    8956       19810 :     if (!Lchi[n1]) continue;
    8957       14735 :     chi1 = gel(v,2); N1 = itou(gel(v,1)); /* conductor of chi1 */
    8958       14735 :     w = gel(V, Fl_div(n,n1,N));
    8959       14735 :     chi2 = gel(w,2); N2 = itou(gel(w,1)); /* conductor of chi2 */
    8960       14735 :     N12 = N1 * N2;
    8961       14735 :     if (N2 == 1 || N0 % N12) continue;
    8962             : 
    8963         546 :     G1 = gel(LG,N1); if (!G1) gel(LG,N1) = G1 = znstar0(utoipos(N1), 1);
    8964         546 :     if (k == 1 && zncharisodd(G1,chi1)) continue;
    8965         413 :     G2 = gel(LG,N2); if (!G2) gel(LG,N2) = G2 = znstar0(utoipos(N2), 1);
    8966         413 :     CHI1 = mfcharGL(G1, chi1);
    8967         413 :     CHI2 = mfcharGL(G2, chi2);
    8968         413 :     o12 = ulcm(mfcharorder(CHI1), mfcharorder(CHI2));
    8969             :     /* remove Galois orbit: same trace */
    8970         413 :     no = Fl_powu(n1, ord, N);
    8971         707 :     for (t = 1+ord, m = n1; t <= o12; t += ord)
    8972             :     { /* m <-> CHI1^t, if t in Gal(Q(chi1,chi2)/Q), omit (CHI1^t,CHI2^t) */
    8973         294 :       m = Fl_mul(m, no, N); if (!m) break;
    8974         294 :       if (ugcd(t, o12) == 1) Lchi[m] = 0;
    8975             :     }
    8976         413 :     oc = ord_canon(o12); T = gel(vT,oc);
    8977         413 :     if (!T) T = gel(vT,oc) = Qab_trace_init(polcyclo(oc,vt), oc, OC);
    8978         413 :     NK = mkNK(N12, k, CHI);
    8979         413 :     gel(RES, j++) = mfeisenstein2all(N0, NK, k, CHI1, CHI2, T, o12);
    8980             :   }
    8981         532 :   setlg(RES,j); return RES;
    8982             : }
    8983             : 
    8984             : static GEN
    8985         609 : mfbd_E2(GEN E2, long d, GEN CHI)
    8986             : {
    8987         609 :   GEN E2d = mfbd_i(E2, d);
    8988         609 :   GEN F = mkvec2(E2, E2d), L = mkvec2(gen_1, utoineg(d));
    8989             :   /* cannot use mflinear_i: E2 and E2d do not have the same level */
    8990         609 :   return tag3(t_MF_LINEAR, mkNK(d,2,CHI), F, L, gen_1);
    8991             : }
    8992             : /* C-basis of E_k(Gamma_0(N),chi). If k = 1, the first basis element must not
    8993             :  * vanish at oo [used in mfwt1basis]. Here E_1(CHI), whose q^0 coefficient
    8994             :  * does not vanish (since L(CHI,0) does not) *if* CHI is not trivial; which
    8995             :  * must be the case in weight 1.
    8996             :  *
    8997             :  * (k>=3): In weight k >= 3, basis is B(d) E(CHI1,(CHI/CHI1)_prim), where
    8998             :  * CHI1 is primitive modulo N1, and if N2 is the conductor of CHI/CHI1
    8999             :  * then d*N1*N2 | N.
    9000             :  * (k=2): In weight k=2, same if CHI is nontrivial. If CHI is trivial, must
    9001             :  * not take CHI1 trivial, and must add E_2(tau)-dE_2(d tau)), where
    9002             :  * d|N, d > 1.
    9003             :  * (k=1): In weight k=1, same as k >= 3 except that we restrict to CHI1 even */
    9004             : static GEN
    9005         749 : mfeisensteinbasis(long N, long k, GEN CHI)
    9006             : {
    9007             :   long i, F;
    9008             :   GEN L;
    9009         749 :   if (badchar(N, k, CHI)) return cgetg(1, t_VEC);
    9010         749 :   if (k == 0) return mfcharistrivial(CHI)? mkvec(mf1()): cgetg(1, t_VEC);
    9011         721 :   CHI = mfchartoprimitive(CHI, &F);
    9012         721 :   L = mfeisensteinbasis_i(N, k, CHI);
    9013         721 :   if (F == 1 && k == 2)
    9014             :   {
    9015         119 :     GEN v, E2 = mfeisenstein(2, NULL, NULL), D = mydivisorsu(N);
    9016         119 :     long nD = lg(D)-1;
    9017         119 :     v = cgetg(nD, t_VEC); L = vec_append(L,v);
    9018         119 :     for (i = 1; i < nD; i++) gel(v,i) = mfbd_E2(E2, D[i+1], CHI);
    9019             :   }
    9020         721 :   return lg(L) == 1? L: shallowconcat1(L);
    9021             : }
    9022             : 
    9023             : /* when flag set, do not return error message */
    9024             : GEN
    9025         791 : mftobasis(GEN mf, GEN F, long flag)
    9026             : {
    9027         791 :   pari_sp av2, av = avma;
    9028             :   GEN G, v, y;
    9029         791 :   long B, ismf = checkmf_i(F);
    9030             : 
    9031         791 :   mf = checkMF(mf);
    9032         791 :   if (ismf && !mfisinspace_i(mf, F))
    9033             :   {
    9034          63 :     if (flag) return cgetg(1, t_COL);
    9035           0 :     err_space(F);
    9036             :   }
    9037             :   /* at least the parameters are right */
    9038         728 :   B = mfsturmNgk(MF_get_N(mf), MF_get_gk(mf)) + 1;
    9039         728 :   if (ismf) v = mfcoefs_i(F,B,1);
    9040             :   else
    9041             :   {
    9042          91 :     switch(typ(F))
    9043             :     { /* F(0),...,F(lg(v)-2) */
    9044          63 :       case t_SER: v = sertocol(F); settyp(v,t_VEC); break;
    9045          14 :       case t_VEC: v = F; break;
    9046           7 :       case t_COL: v = shallowtrans(F); break;
    9047           7 :       default: pari_err_TYPE("mftobasis",F);
    9048             :                v = NULL;/*LCOV_EXCL_LINE*/
    9049             :     }
    9050          84 :     if (flag) B = minss(B, lg(v)-2);
    9051             :   }
    9052         721 :   y = mftobasis_i(mf, v);
    9053         721 :   if (typ(y) == t_VEC)
    9054             :   {
    9055          21 :     if (flag) return gerepilecopy(av, y);
    9056           0 :     pari_err(e_MISC, "not enough coefficients in mftobasis");
    9057             :   }
    9058         700 :   av2 = avma;
    9059         700 :   if (MF_get_space(mf) == mf_FULL || mfsturm(mf)+1 == B) return y;
    9060         182 :   G = mflinear(mf, y);
    9061         182 :   if (!gequal(v, mfcoefs_i(G, lg(v)-2,1))) y = NULL;
    9062         182 :   avma = av2;
    9063         182 :   if (!y)
    9064             :   {
    9065           7 :     if (flag) { avma = av; return cgetg(1, t_COL); }
    9066           7 :     err_space(F);
    9067             :   }
    9068         175 :   return gerepileupto(av, y);
    9069             : }
    9070             : 
    9071             : /* assume N > 0; first cusp is always 0 */
    9072             : static GEN
    9073          49 : mfcusps_i(long N)
    9074             : {
    9075             :   long i, c, l;
    9076             :   GEN D, v;
    9077             : 
    9078          49 :   if (N == 1) return mkvec(gen_0);
    9079          49 :   D = mydivisorsu(N); l = lg(D); /* left on stack */
    9080          49 :   c = mfnumcuspsu_fact(myfactoru(N));
    9081          49 :   v = cgetg(c + 1, t_VEC);
    9082         350 :   for (i = c = 1; i < l; i++)
    9083             :   {
    9084         301 :     long C = D[i], NC = D[l-i], lima = ugcd(C, NC), A0, A;
    9085         889 :     for (A0 = 0; A0 < lima; A0++)
    9086         588 :       if (ugcd(A0, lima) == 1)
    9087             :       {
    9088         392 :         A = A0; while (ugcd(A,C) > 1) A += lima;
    9089         392 :         gel(v, c++) = sstoQ(A, C);
    9090             :       }
    9091             :   }
    9092          49 :   return v;
    9093             : }
    9094             : /* List of cusps of Gamma_0(N) */
    9095             : GEN
    9096          28 : mfcusps(GEN gN)
    9097             : {
    9098             :   long N;
    9099             :   GEN mf;
    9100          28 :   if (typ(gN) == t_INT) N = itos(gN);
    9101          14 :   else if ((mf = checkMF_i(gN))) N = MF_get_N(mf);
    9102           0 :   else { pari_err_TYPE("mfcusps", gN); N = 0; }
    9103          28 :   if (N <= 0) pari_err_DOMAIN("mfcusps", "N", "<=", gen_0, stoi(N));
    9104          28 :   return mfcusps_i(N);
    9105             : }
    9106             : 
    9107             : long
    9108         315 : mfcuspisregular(GEN NK, GEN cusp)
    9109             : {
    9110             :   long v, N, dk, nk, t, o;
    9111             :   GEN mf, CHI, go, A, C, g, c, d;
    9112         315 :   if ((mf = checkMF_i(NK)))
    9113             :   {
    9114          49 :     GEN gk = MF_get_gk(mf);
    9115          49 :     N = MF_get_N(mf);
    9116          49 :     CHI = MF_get_CHI(mf);
    9117          49 :     Qtoss(gk, &nk, &dk);
    9118             :   }
    9119             :   else
    9120         266 :     checkNK2(NK, &N, &nk, &dk, &CHI, 0);
    9121         315 :   if (typ(cusp) == t_INFINITY) return 1;
    9122         315 :   if (typ(cusp) == t_FRAC) { A = gel(cusp,1); C = gel(cusp,2); }
    9123          28 :   else { A = cusp; C = gen_1; }
    9124         315 :   g = diviuexact(mului(N,C), ugcd(N, Fl_sqr(umodiu(C,N), N)));
    9125         315 :   c = mulii(negi(C),g);
    9126         315 :   d = addiu(mulii(A,g), 1);
    9127         315 :   if (!CHI) return 1;
    9128         315 :   go = gmfcharorder(CHI);
    9129         315 :   v = vali(go); if (v < 2) go = shifti(go, 2-v);
    9130         315 :   t = itou( znchareval(gel(CHI,1), gel(CHI,2), d, go) );
    9131         315 :   if (dk == 1) return t == 0;
    9132         154 :   o = itou(go);
    9133         154 :   if (kronecker(c,d) < 0) t = Fl_add(t, o/2, o);
    9134         154 :   if (Mod4(d) == 1) return t == 0;
    9135          14 :   t = Fl_sub(t, Fl_mul(o/4, nk, o), o);
    9136          14 :   return t == 0;
    9137             : }
    9138             : 
    9139             : /* Some useful closures */
    9140             : 
    9141             : /* sum_{d|n} d^k */
    9142             : static GEN
    9143       16261 : mysumdivku(ulong n, ulong k)
    9144             : {
    9145       16261 :   GEN fa = myfactoru(n);
    9146       16261 :   return k == 1? usumdiv_fact(fa): usumdivk_fact(fa,k);
    9147             : }
    9148             : static GEN
    9149         658 : c_Ek(long n, long d, GEN F)
    9150             : {
    9151         658 :   GEN E = cgetg(n + 2, t_VEC), C = gel(F,2);
    9152         658 :   long i, k = mf_get_k(F);
    9153         658 :   gel (E, 1) = gen_1;
    9154        8260 :   for (i = 1; i <= n; i++)
    9155             :   {
    9156        7602 :     pari_sp av = avma;
    9157        7602 :     gel(E, i+1) = gerepileupto(av, gmul(C, mysumdivku(i*d, k-1)));
    9158             :   }
    9159         658 :   return E;
    9160             : }
    9161             : 
    9162             : GEN
    9163         189 : mfEk(long k)
    9164             : {
    9165         189 :   pari_sp av = avma;
    9166             :   GEN E0, NK;
    9167         189 :   if (k < 0 || odd(k)) pari_err_TYPE("mfEk [incorrect k]", stoi(k));
    9168         189 :   if (!k) return mf1();
    9169         182 :   E0 = gdivsg(-2*k, bernfrac(k));
    9170         182 :   NK = mkNK(1,k,mfchartrivial());
    9171         182 :   return gerepilecopy(av, tag(t_MF_Ek, NK, E0));
    9172             : }
    9173             : 
    9174             : GEN
    9175          49 : mfDelta(void)
    9176             : {
    9177          49 :   pari_sp av = avma;
    9178          49 :   return gerepilecopy(av, tag0(t_MF_DELTA, mkNK(1,12,mfchartrivial())));
    9179             : }
    9180             : 
    9181             : GEN
    9182         504 : mfTheta(GEN psi)
    9183             : {
    9184         504 :   pari_sp av = avma;
    9185             :   GEN N, gk, psi2;
    9186             :   long par;
    9187         504 :   if (!psi) { psi = mfchartrivial(); N = utoipos(4); par = 1; }
    9188             :   else
    9189             :   {
    9190             :     long FC;
    9191          21 :     psi = get_mfchar(psi);
    9192          21 :     FC = mfcharconductor(psi);
    9193          21 :     if (mfcharmodulus(psi) != FC)
    9194           0 :       pari_err_TYPE("mfTheta [nonprimitive character]", psi);
    9195          21 :     par = mfcharparity(psi);
    9196          21 :     N = shifti(sqru(FC),2);
    9197             :   }
    9198         504 :   if (par > 0) { gk = ghalf; psi2 = psi; }
    9199           7 :   else { gk = gsubsg(2, ghalf); psi2 = mfcharmul(psi, get_mfchar(stoi(-4))); }
    9200         504 :   return gerepilecopy(av, tag(t_MF_THETA, mkgNK(N, gk, psi2, pol_x(1)), psi));
    9201             : }
    9202             : 
    9203             : /* Output 0 if not desired eta product: if flag=0 (default) require
    9204             :  * holomorphic at cusps. If flag set, accept meromorphic, but sill in some
    9205             :  * modular function space */
    9206             : GEN
    9207         133 : mffrometaquo(GEN eta, long flag)
    9208             : {
    9209         133 :   pari_sp av = avma;
    9210             :   GEN NK, N, k, BR, P;
    9211         133 :   long v, cusp = 0;
    9212         133 :   if (!etaquotype(&eta, &N,&k,&P, &v, NULL, flag? NULL: &cusp) || cusp < 0)
    9213             :   {
    9214          14 :     avma = av; return gen_0;
    9215             :   }
    9216         119 :   if (lg(gel(eta,1)) == 1) { avma = av; return mf1(); }
    9217         112 :   BR = mkvec2(ZV_to_zv(gel(eta,1)), ZV_to_zv(gel(eta,2)));
    9218         112 :   if (v < 0) v = 0;
    9219         112 :   NK = mkgNK(N, k, get_mfchar(P), pol_x(1));
    9220         112 :   return gerepilecopy(av, tag2(t_MF_ETAQUO, NK, BR, utoi(v)));
    9221             : }
    9222             : 
    9223             : #if 0
    9224             : /* number of primitive characters modulo N */
    9225             : static ulong
    9226             : numprimchars(ulong N)
    9227             : {
    9228             :   GEN fa, P, E;
    9229             :   long i, l;
    9230             :   ulong n;
    9231             :   if ((N & 3) == 2) return 0;
    9232             :   fa = myfactoru(N);
    9233             :   P = gel(fa,1); l = lg(P);
    9234             :   E = gel(fa,2);
    9235             :   for (i = n = 1; i < l; i++)
    9236             :   {
    9237             :     ulong p = P[i], e = E[i];
    9238             :     if (e == 2) n *= p-2; else n *= (p-1)*(p-1)*upowuu(p,e-2);
    9239             :   }
    9240             :   return n;
    9241             : }
    9242             : #endif
    9243             : 
    9244             : /* Space generated by products of two Eisenstein series */
    9245             : 
    9246             : INLINE int
    9247      102060 : cmp_small(long a, long b) { return a>b? 1: (a<b? -1: 0); }
    9248             : static int
    9249       60767 : cmp_small_priority(void *E, GEN a, GEN b)
    9250             : {
    9251       60767 :   GEN prio = (GEN)E;
    9252       60767 :   return cmp_small(prio[(long)a], prio[(long)b]);
    9253             : }
    9254             : static long
    9255         910 : znstar_get_expo(GEN G)
    9256             : {
    9257         910 :   GEN cyc = znstar_get_cyc(G);
    9258         910 :   return (lg(cyc) == 1)? 1: itou(gel(cyc,1));
    9259             : }
    9260             : 
    9261             : /* Return [vchi, bymod, vG]:
    9262             :  * vG[f] = znstar(f,1) for f a conductor of (at least) a char mod N; else NULL
    9263             :  * bymod[f] = vecsmall of conrey indexes of chars modulo f | N; else NULL
    9264             :  * vchi[n] = a list of CHIvec [G0,chi0,o,ncharvecexpo(G0,nchi0),...]:
    9265             :  *   chi0 = primitive char attached to Conrey Mod(n,N)
    9266             :  * (resp. NULL if (n,N) > 1) */
    9267             : static GEN
    9268         455 : charsmodN(long N)
    9269             : {
    9270         455 :   GEN D, G, prio, phio, dummy = cgetg(1,t_VEC);
    9271         455 :   GEN vP, vG = const_vec(N,NULL), vCHI  = const_vec(N,NULL);
    9272         455 :   GEN bymod = const_vec(N,NULL);
    9273         455 :   long pn, i, l, vt = fetch_user_var("t");
    9274         455 :   D = mydivisorsu(N); l = lg(D);
    9275        3017 :   for (i = 1; i < l; i++)
    9276        2562 :     gel(bymod, D[i]) = vecsmalltrunc_init(myeulerphiu(D[i])+1);
    9277         455 :   gel(vG,N) = G = znstar0(utoipos(N),1);
    9278         455 :   pn = znstar_get_expo(G);  /* exponent(Z/NZ)^* */
    9279         455 :   vP = const_vec(pn,NULL);
    9280       21924 :   for (i = 1; i <= N; i++)
    9281             :   {
    9282             :     GEN P, gF, G0, chi0, nchi0, chi, v, go;
    9283             :     long j, F, o;
    9284       21469 :     if (ugcd(i,N) != 1) continue;
    9285       10563 :     chi = znconreylog(G, utoipos(i));
    9286       10563 :     gF = znconreyconductor(G, chi, &chi0);
    9287       10563 :     F = (typ(gF) == t_INT)? itou(gF): itou(gel(gF,1));
    9288       10563 :     G0 = gel(vG, F); if (!G0) G0 = gel(vG,F) = znstar0(gF, 1);
    9289       10563 :     nchi0 = znconreylog_normalize(G0,chi0);
    9290       10563 :     go = gel(nchi0,1); o = itou(go); /* order(chi0) */
    9291       10563 :     v = ncharvecexpo(G0, nchi0);
    9292       10563 :     if (!equaliu(go, pn)) v = zv_z_mul(v, pn / o);
    9293       10563 :     P = gel(vP, o); if (!P) P = gel(vP,o) = polcyclo(o,vt);
    9294             :     /* mfcharcxinit with dummy complex powers */
    9295       10563 :     gel(vCHI,i) = mkvecn(6, G0, chi0, go, v, dummy, P);
    9296       10563 :     D = mydivisorsu(N / F); l = lg(D);
    9297       10563 :     for (j = 1; j < l; j++) vecsmalltrunc_append(gel(bymod, F*D[j]), i);
    9298             :   }
    9299         455 :   phio = zero_zv(pn); l = lg(vCHI); prio = cgetg(l, t_VEC);
    9300       21924 :   for (i = 1; i < l; i++)
    9301             :   {
    9302       21469 :     GEN CHI = gel(vCHI,i);
    9303             :     long o;
    9304       21469 :     if (!CHI) continue;
    9305       10563 :     o = CHIvec_ord(CHI);
    9306       10563 :     if (!phio[o]) phio[o] = myeulerphiu(o);
    9307       10563 :     prio[i] = phio[o];
    9308             :   }
    9309         455 :   l = lg(bymod);
    9310             :   /* sort characters by increasing value of phi(order) */
    9311       21924 :   for (i = 1; i < l; i++)
    9312             :   {
    9313       21469 :     GEN z = gel(bymod,i);
    9314       21469 :     if (z) gen_sort_inplace(z, (void*)prio, &cmp_small_priority, NULL);
    9315             :   }
    9316         455 :   return mkvec3(vCHI, bymod, vG);
    9317             : }
    9318             : 
    9319             : static GEN
    9320        4228 : mfeisenstein2pure(long k, GEN CHI1, GEN CHI2, long ord, GEN P, long lim)
    9321             : {
    9322        4228 :   GEN c, V = cgetg(lim+2, t_COL);
    9323             :   long n;
    9324        4228 :   c = mfeisenstein2_0(k, CHI1, CHI2, ord);
    9325        4228 :   if (P) c = grem(c, P);
    9326        4228 :   gel(V,1) = c;
    9327       91420 :   for (n=1; n <= lim; n++)
    9328             :   {
    9329       87192 :     c = sigchi2(k, CHI1, CHI2, n, ord);
    9330       87192 :     if (P) c = grem(c, P);
    9331       87192 :     gel(V,n+1) = c;
    9332             :   }
    9333        4228 :   return V;
    9334             : }
    9335             : static GEN
    9336        2940 : mfeisenstein2pure_Fl(long k, GEN CHI1vec, GEN CHI2vec, GEN vz, ulong p, long lim)
    9337             : {
    9338        2940 :   GEN V = cgetg(lim+2, t_VECSMALL);
    9339             :   long n;
    9340        2940 :   V[1] = mfeisenstein2_0_Fl(k, CHI1vec, CHI2vec, vz, p);
    9341        2940 :   for (n=1; n <= lim; n++) V[n+1] = sigchi2_Fl(k, CHI1vec, CHI2vec, n, vz, p);
    9342        2940 :   return V;
    9343             : }
    9344             : 
    9345             : static GEN
    9346         161 : getcolswt2(GEN M, GEN D, ulong p)
    9347             : {
    9348         161 :   GEN R, v = gel(M,1);
    9349         161 :   long i, l = lg(M) - 1;
    9350         161 :   R = cgetg(l, t_MAT); /* skip D[1] = 1 */
    9351         588 :   for (i = 1; i < l; i++)
    9352             :   {
    9353         427 :     GEN w = Flv_Fl_mul(gel(M,i+1), D[i+1], p);
    9354         427 :     gel(R,i) = Flv_sub(v, w, p);
    9355             :   }
    9356         161 :   return R;
    9357             : }
    9358             : static GEN
    9359        4228 : expandbd(GEN V, long d)
    9360             : {
    9361             :   long L, n, nd;
    9362             :   GEN W;
    9363        4228 :   if (d == 1) return V;
    9364        1575 :   L = lg(V)-1; W = zerocol(L); /* nd = n/d */
    9365        1575 :   for (n = nd = 0; n < L; n += d, nd++) gel(W, n+1) = gel(V, nd+1);
    9366        1575 :   return W;
    9367             : }
    9368             : static GEN
    9369        4858 : expandbd_Fl(GEN V, long d)
    9370             : {
    9371             :   long L, n, nd;
    9372             :   GEN W;
    9373        4858 :   if (d == 1) return V;
    9374        1918 :   L = lg(V)-1; W = zero_Flv(L); /* nd = n/d */
    9375        1918 :   for (n = nd = 0; n < L; n += d, nd++) W[n+1] = V[nd+1];
    9376        1918 :   return W;
    9377             : }
    9378             : static void
    9379        2940 : getcols_i(GEN *pM, GEN *pvj, GEN gk, GEN CHI1vec, GEN CHI2vec, long NN1, GEN vz,
    9380             :           ulong p, long lim)
    9381             : {
    9382        2940 :   GEN CHI1 = CHIvec_CHI(CHI1vec), CHI2 = CHIvec_CHI(CHI2vec);
    9383        2940 :   long N2 = CHIvec_N(CHI2vec);
    9384        2940 :   GEN vj, M, D = mydivisorsu(NN1/N2);
    9385        2940 :   long i, l = lg(D), k = gk[2];
    9386        2940 :   GEN V = mfeisenstein2pure_Fl(k, CHI1vec, CHI2vec, vz, p, lim);
    9387        2940 :   M = cgetg(l, t_MAT);
    9388        2940 :   for (i = 1; i < l; i++) gel(M,i) = expandbd_Fl(V, D[i]);
    9389        2940 :   if (k == 2 && N2 == 1 && CHIvec_N(CHI1vec) == 1)
    9390             :   {
    9391         161 :     M = getcolswt2(M, D, p); l--;
    9392         161 :     D = vecslice(D, 2, l);
    9393             :   }
    9394        2940 :   *pM = M;
    9395        2940 :   *pvj = vj = cgetg(l, t_VEC);
    9396        2940 :   for (i = 1; i < l; i++) gel(vj,i) = mkvec4(gk, CHI1, CHI2, utoipos(D[i]));
    9397        2940 : }
    9398             : 
    9399             : /* find all CHI1, CHI2 mod N such that CHI1*CHI2 = CHI, f(CHI1)*f(CHI2) | N.
    9400             :  * set M = mfcoefs(B_e E(CHI1,CHI2), lim), vj = [e,i1,i2] */
    9401             : static void
    9402        1099 : getcols(GEN *pM, GEN *pv, long k, long nCHI, GEN allN, GEN vz, ulong p,
    9403             :         long lim)
    9404             : {
    9405        1099 :   GEN vCHI = gel(allN,1), gk = utoi(k);
    9406        1099 :   GEN M = cgetg(1,t_MAT), v = cgetg(1,t_VEC);
    9407        1099 :   long i1, N = lg(vCHI)-1;
    9408       56938 :   for (i1 = 1; i1 <= N; i1++)
    9409             :   {
    9410       55839 :     GEN CHI1vec = gel(vCHI, i1), CHI2vec, M1, v1;
    9411             :     long NN1, i2;
    9412      109382 :     if (!CHI1vec) continue;
    9413       40670 :     if (k == 1 && CHIvec_parity(CHI1vec) == -1) continue;
    9414       25802 :     NN1 = N/CHIvec_N(CHI1vec); /* N/f(chi1) */;
    9415       25802 :     i2 = Fl_div(nCHI,i1, N);
    9416       25802 :     if (!i2) i2 = 1;
    9417       25802 :     CHI2vec = gel(vCHI,i2);
    9418       25802 :     if (NN1 % CHIvec_N(CHI2vec)) continue; /* f(chi1)f(chi2) | N ? */
    9419        2296 :     getcols_i(&M1, &v1, gk, CHI1vec, CHI2vec, NN1, vz, p, lim);
    9420        2296 :     M = shallowconcat(M, M1);
    9421        2296 :     v = shallowconcat(v, v1);
    9422             :   }
    9423        1099 :   *pM = M;
    9424        1099 :   *pv = v;
    9425        1099 : }
    9426             : 
    9427             : static void
    9428         700 : update_Mj(GEN *M, GEN *vecj, GEN *pz, ulong p)
    9429             : {
    9430             :   GEN perm;
    9431         700 :   *pz = Flm_indexrank(*M, p); perm = gel(*pz,2);
    9432         700 :   *M = vecpermute(*M, perm);
    9433         700 :   *vecj = vecpermute(*vecj, perm);
    9434         700 : }
    9435             : static int
    9436         287 : getcolsgen(long dim, GEN *pM, GEN *pvj, GEN *pz, long k, long ell, long nCHI,
    9437             :            GEN allN, GEN vz, ulong p, long lim)
    9438             : {
    9439         287 :   GEN vCHI = gel(allN,1), bymod = gel(allN,2), gell = utoi(ell);
    9440         287 :   long i1, N = lg(vCHI)-1;
    9441         287 :   long L = lim+1;
    9442         287 :   if (lg(*pvj)-1 >= dim) update_Mj(pM, pvj, pz, p);
    9443         287 :   if (lg(*pvj)-1 == dim) return 1;
    9444         826 :   for (i1 = 1; i1 <= N; i1++)
    9445             :   {
    9446         819 :     GEN CHI1vec = gel(vCHI, i1), T;
    9447             :     long par1, j, l, N1, NN1;
    9448             : 
    9449         819 :     if (!CHI1vec) continue;
    9450         812 :     par1 = CHIvec_parity(CHI1vec);
    9451         812 :     if (ell == 1 && par1 == -1) continue;
    9452         539 :     if (odd(ell)) par1 = -par1;
    9453         539 :     N1 = CHIvec_N(CHI1vec);
    9454         539 :     NN1 = N/N1;
    9455         539 :     T = gel(bymod, NN1); l = lg(T);
    9456        1827 :     for (j = 1; j < l; j++)
    9457             :     {
    9458        1554 :       long i2 = T[j], l1, l2, j1, s, nC;
    9459        1554 :       GEN M, M1, M2, vj, vj1, vj2, CHI2vec = gel(vCHI, i2);
    9460        2464 :       if (CHIvec_parity(CHI2vec) != par1) continue;
    9461         644 :       nC = Fl_div(nCHI, Fl_mul(i1,i2,N), N);
    9462         644 :       getcols(&M2, &vj2, k-ell, nC, allN, vz, p, lim);
    9463         644 :       l2 = lg(M2); if (l2 == 1) continue;
    9464         644 :       getcols_i(&M1, &vj1, gell, CHI1vec, CHI2vec, NN1, vz, p, lim);
    9465         644 :       l1 = lg(M1);
    9466         644 :       M1 = Flm_to_FlxV(M1, 0);
    9467         644 :       M2 = Flm_to_FlxV(M2, 0);
    9468         644 :       M  = cgetg((l1-1)*(l2-1) + 1, t_MAT);
    9469         644 :       vj = cgetg((l1-1)*(l2-1) + 1, t_VEC);
    9470        1687 :       for (j1 = s = 1; j1 < l1; j1++)
    9471             :       {
    9472        1043 :         GEN E = gel(M1,j1), v = gel(vj1,j1);
    9473             :         long j2;
    9474        4837 :         for (j2 = 1; j2 < l2; j2++, s++)
    9475             :         {
    9476        3794 :           GEN c = Flx_to_Flv(Flxn_mul(E, gel(M2,j2), L, p), L);
    9477        3794 :           gel(M,s) = c;
    9478        3794 :           gel(vj,s) = mkvec2(v, gel(vj2,j2));
    9479             :         }
    9480             :       }
    9481         644 :       *pM = shallowconcat(*pM, M);
    9482         644 :       *pvj = shallowconcat(*pvj, vj);
    9483         644 :       if (lg(*pvj)-1 >= dim) update_Mj(pM, pvj, pz, p);
    9484         644 :       if (lg(*pvj)-1 == dim) return 1;
    9485             :     }
    9486             :   }
    9487           7 :   if (ell == 1)
    9488             :   {
    9489           7 :     update_Mj(pM, pvj, pz, p);
    9490           7 :     return (lg(*pvj)-1 == dim);
    9491             :   }
    9492           0 :   return 0;
    9493             : }
    9494             : 
    9495             : static GEN
    9496         903 : mkF2bd(long d, long lim)
    9497             : {
    9498         903 :   GEN V = zerovec(lim + 1);
    9499             :   long n;
    9500         903 :   gel(V, 1) = ginv(stoi(-24));
    9501         903 :   for (n = 1; n <= lim/d; n++) gel(V, n*d + 1) = mysumdivku(n, 1);
    9502         903 :   return V;
    9503             : }
    9504             : 
    9505             : static GEN
    9506        4578 : mkeisen(GEN E, long ord, GEN P, long lim)
    9507             : {
    9508        4578 :   long k = itou(gel(E,1)), e = itou(gel(E,4));
    9509        4578 :   GEN CHI1 = gel(E,2), CHI2 = gel(E,3);
    9510        4578 :   if (k == 2 && mfcharistrivial(CHI1) && mfcharistrivial(CHI2))
    9511         350 :     return gsub(mkF2bd(1,lim), gmulgs(mkF2bd(e,lim), e));
    9512             :   else
    9513             :   {
    9514        4228 :     GEN V = mfeisenstein2pure(k, CHI1, CHI2, ord, P, lim);
    9515        4228 :     return expandbd(V, e);
    9516             :   }
    9517             : }
    9518             : static GEN
    9519         434 : mkM(GEN vj, long pn, GEN P, long lim)
    9520             : {
    9521         434 :   long j, l = lg(vj), L = lim+1;
    9522         434 :   GEN M = cgetg(l, t_MAT);
    9523        3773 :   for (j = 1; j < l; j++)
    9524             :   {
    9525             :     GEN E1, E2;
    9526        3339 :     parse_vecj(gel(vj,j), &E1,&E2);
    9527        3339 :     E1 = RgV_to_RgX(mkeisen(E1, pn, P, lim), 0);
    9528        3339 :     if (E2)
    9529             :     {
    9530        1239 :       E2 = RgV_to_RgX(mkeisen(E2, pn, P, lim), 0);
    9531        1239 :       E1 = RgXn_mul(E1, E2, L);
    9532             :     }
    9533        3339 :     E1 = RgX_to_RgC(E1, L);
    9534        3339 :     if (P && E2) E1 = RgXQV_red(E1, P);
    9535        3339 :     gel(M,j) = E1;
    9536             :   }
    9537         434 :   return M;
    9538             : }
    9539             : 
    9540             : /* assume N > 2 */
    9541             : static GEN
    9542          14 : mffindeisen1(long N)
    9543             : {
    9544          14 :   GEN G = znstar0(utoipos(N), 1), L = chargalois(G, NULL), chi0 = NULL;
    9545          14 :   long j, m = N, l = lg(L);
    9546          84 :   for (j = 1; j < l; j++)
    9547             :   {
    9548          84 :     GEN chi = gel(L,j);
    9549          84 :     long r = myeulerphiu(itou(zncharorder(G,chi)));
    9550          84 :     if (r >= m) continue;
    9551          56 :     chi = znconreyfromchar(G, chi);
    9552          56 :     if (zncharisodd(G,chi)) { m = r; chi0 = chi; if (r == 1) break; }
    9553             :   }
    9554          14 :   if (!chi0) pari_err_BUG("mffindeisen1 [no Eisenstein series found]");
    9555          14 :   chi0 = znchartoprimitive(G,chi0);
    9556          14 :   return mfcharGL(gel(chi0,1), gel(chi0,2));
    9557             : }
    9558             : 
    9559             : static GEN
    9560         455 : mfeisensteinspaceinit_i(long N, long k, GEN CHI)
    9561             : {
    9562         455 :   GEN M, Minv, vj, vG, GN, allN, P, vz, z = NULL;
    9563         455 :   long nCHI, lim, ell, ord, pn, dim = mffulldim(N, k, CHI);
    9564             :   ulong r, p;
    9565             : 
    9566         455 :   if (!dim) retmkvec3(cgetg(1,t_VECSMALL),
    9567             :                       mkvec2(cgetg(1,t_MAT),gen_1),cgetg(1,t_VEC));
    9568         455 :   lim = mfsturmNk(N, k) + 1;
    9569         455 :   allN = charsmodN(N);
    9570         455 :   vG = gel(allN,3);
    9571         455 :   GN = gel(vG,N);
    9572         455 :   pn = znstar_get_expo(GN);
    9573         455 :   ord = ord_canon(pn);
    9574         455 :   P = ord == 1? NULL: polcyclo(ord, varn(mfcharpol(CHI)));
    9575         455 :   CHI = induce(GN, CHI); /* lift CHI mod N before mfcharno*/
    9576         455 :   nCHI = mfcharno(CHI);
    9577         455 :   r = QabM_init(ord, &p);
    9578         455 :   vz = Fl_powers(r, pn, p);
    9579         455 :   getcols(&M, &vj, k, nCHI, allN, vz, p, lim);
    9580         462 :   for (ell = k>>1; ell >= 1; ell--)
    9581         287 :     if (getcolsgen(dim, &M, &vj, &z, k, ell, nCHI, allN, vz, p, lim)) break;
    9582         455 :   if (!z) update_Mj(&M, &vj, &z, p);
    9583         455 :   if (lg(vj) - 1 < dim) return NULL;
    9584         434 :   M = mkM(vj, pn, P, lim);
    9585         434 :   Minv = QabM_Minv(rowpermute(M, gel(z,1)), P, ord);
    9586         434 :   return mkvec4(gel(z,1), Minv, vj, utoi(ord));
    9587             : }
    9588             : /* true mf */
    9589             : static GEN
    9590         434 : mfeisensteinspaceinit(GEN mf)
    9591             : {
    9592         434 :   pari_sp av = avma;
    9593         434 :   GEN z, CHI = MF_get_CHI(mf);
    9594         434 :   long N = MF_get_N(mf), k = MF_get_k(mf);
    9595         434 :   if (!CHI) CHI = mfchartrivial();
    9596         434 :   z = mfeisensteinspaceinit_i(N, k, CHI);
    9597         434 :   if (!z)
    9598             :   {
    9599          14 :     GEN E, CHIN = mffindeisen1(N), CHI0 = mfchartrivial();
    9600          14 :     z = mfeisensteinspaceinit_i(N, k+1, mfcharmul(CHI, CHIN));
    9601          14 :     if (z) E = mkvec4(gen_1, CHI0, CHIN, gen_1);
    9602             :     else
    9603             :     {
    9604           7 :       z = mfeisensteinspaceinit_i(N, k+2, CHI);
    9605           7 :       E = mkvec4(gen_2, CHI0, CHI0, utoipos(N));
    9606             :     }
    9607          14 :     z = mkvec2(z, E);
    9608             :   }
    9609         434 :   return gerepilecopy(av, z);
    9610             : }
    9611             : 
    9612             : /* decomposition of modular form on eisenspace */
    9613             : static GEN
    9614         819 : mfeisensteindec(GEN mf, GEN F)
    9615             : {
    9616         819 :   pari_sp av = avma;
    9617             :   GEN M, Mindex, Mvecj, V, B, CHI;
    9618             :   long o, ord;
    9619             : 
    9620         819 :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    9621         819 :   if (lg(Mvecj) < 5)
    9622             :   {
    9623          14 :     GEN E, e = gel(Mvecj,2), gkE = gel(e,1);
    9624          14 :     long dE = itou(gel(e,4));
    9625          14 :     Mvecj = gel(Mvecj,1);
    9626          14 :     E = mfeisenstein(itou(gkE), NULL, gel(e,3));
    9627          14 :     if (dE != 1) E = mfbd_E2(E, dE, gel(e,2)); /* here k = 2 */
    9628          14 :     F = mfmul(F, E);
    9629             :   }
    9630         819 :   M = gel(Mvecj, 2);
    9631         819 :   if (lg(M) == 1) return cgetg(1, t_VEC);
    9632         819 :   Mindex = gel(Mvecj, 1);
    9633         819 :   ord = itou(gel(Mvecj,4));
    9634         819 :   V = mfcoefs(F, Mindex[lg(Mindex)-1]-1, 1); settyp(V, t_COL);
    9635         819 :   CHI = mf_get_CHI(F);
    9636         819 :   o = mfcharorder_canon(CHI);
    9637         819 :   if (o > 1 && o != ord)
    9638             :   { /* convert Mod(.,polcyclo(o)) to Mod(., polcyclo(N)) for o | N,
    9639             :      * o and N both != 2 (mod 4) */
    9640          42 :     GEN z, P = gel(M,4); /* polcyclo(ord) */
    9641          42 :     long vt = varn(P);
    9642          42 :     z = gmodulo(pol_xn(ord/o, vt), P);
    9643          42 :     if (ord % o) pari_err_TYPE("mfeisensteindec", V);
    9644          42 :     V = gsubst(liftpol_shallow(V), vt, z);
    9645             :   }
    9646         819 :   B = Minv_RgC_mul(M, vecpermute(V, Mindex));
    9647         819 :   return gerepileupto(av, B);
    9648             : }
    9649             : 
    9650             : /*********************************************************************/
    9651             : /*                        END EISENSPACE                             */
    9652             : /*********************************************************************/
    9653             : 
    9654             : static GEN
    9655          70 : sertocol2(GEN S, long l)
    9656             : {
    9657          70 :   GEN C = cgetg(l + 2, t_COL);
    9658             :   long i;
    9659          70 :   for (i = 0; i <= l; i++) gel(C, i+1) = polcoef_i(S, i, -1);
    9660          70 :   return C;
    9661             : }
    9662             : 
    9663             : /* Compute polynomial P0 such that F=E4^(k/4)P0(E6/E4^(3/2)). */
    9664             : static GEN
    9665          14 : mfcanfindp0(GEN F, long k)
    9666             : {
    9667          14 :   pari_sp ltop = avma;
    9668             :   GEN E4, E6, V, V1, Q, W, res, M, B;
    9669             :   long l, j;
    9670          14 :   l = k/6 + 2;
    9671          14 :   V = mfcoefsser(F,l);
    9672          14 :   E4 = mfcoefsser(mfEk(4),l);
    9673          14 :   E6 = mfcoefsser(mfEk(6),l);
    9674          14 :   V1 = gdiv(V, gpow(E4, sstoQ(k,4), 0));
    9675          14 :   Q = gdiv(E6, gpow(E4, sstoQ(3,2), 0));
    9676          14 :   W = gpowers(Q, l - 1);
    9677          14 :   M = cgetg(l + 1, t_MAT);
    9678          14 :   for (j = 1; j <= l; j++) gel(M,j) = sertocol2(gel(W,j), l);
    9679          14 :   B = sertocol2(V1, l);
    9680          14 :   res = inverseimage(M, B);
    9681          14 :   if (lg(res) == 1) err_space(F);
    9682          14 :   return gerepilecopy(ltop, gtopolyrev(res, 0));
    9683             : }
    9684             : 
    9685             : /* Compute the first n+1 Taylor coeffs at tau=I of a modular form
    9686             :  * on SL_2(Z). */
    9687             : GEN
    9688          14 : mftaylor(GEN F, long n, long flreal, long prec)
    9689             : {
    9690          14 :   pari_sp ltop = avma;
    9691          14 :   GEN P0, Pm1 = gen_0, v;
    9692          14 :   GEN X2 = mkpoln(3, ghalf,gen_0,gneg(ghalf)); /* (x^2-1) / 2 */
    9693             :   long k, m;
    9694          14 :   if (!checkmf_i(F)) pari_err_TYPE("mftaylor",F);
    9695          14 :   k = mf_get_k(F);
    9696          14 :   if (mf_get_N(F) != 1 || k < 0) pari_err_IMPL("mftaylor for this form");
    9697          14 :   P0 = mfcanfindp0(F, k);
    9698          14 :   v = cgetg(n+2, t_VEC); gel(v, 1) = RgX_coeff(P0,0);
    9699         154 :   for (m = 0; m < n; m++)
    9700             :   {
    9701         140 :     GEN P1 = gdivgs(gmulsg(-(k + 2*m), RgX_shift(P0,1)), 12);
    9702         140 :     P1 = gadd(P1, gmul(X2, RgX_deriv(P0)));
    9703         140 :     if (m) P1 = gsub(P1, gdivgs(gmulsg(m*(m+k-1), Pm1), 144));
    9704         140 :     Pm1 = P0; P0 = P1;
    9705         140 :     gel(v, m+2) = RgX_coeff(P0, 0);
    9706             :   }
    9707          14 :   if (flreal)
    9708             :   {
    9709           7 :     GEN pi2 = Pi2n(1, prec), pim4 = gmulsg(-2, pi2), VPC;
    9710           7 :     GEN C = gmulsg(3, gdiv(gpowgs(ggamma(ginv(utoi(4)), prec), 8), gpowgs(pi2, 6)));
    9711             :     /* E_4(i): */
    9712           7 :     GEN facn = gen_1;
    9713           7 :     VPC = gpowers(gmul(pim4, gsqrt(C, prec)), n);
    9714           7 :     C = gpow(C, sstoQ(k,4), prec);
    9715          84 :     for (m = 0; m <= n; m++)
    9716             :     {
    9717          77 :       gel(v, m+1) = gdiv(gmul(C, gmul(gel(v, m+1), gel(VPC, m+1))), facn);
    9718          77 :       facn = gmulgs(facn, m+1);
    9719             :     }
    9720             :   }
    9721          14 :   return gerepilecopy(ltop, v);
    9722             : }
    9723             : 
    9724             : #if 0
    9725             : /* To be used in mfeigensearch() */
    9726             : GEN
    9727             : mfreadratfile()
    9728             : {
    9729             :   GEN eqn;
    9730             :   pariFILE *F = pari_fopengz("rateigen300.gp");
    9731             :   eqn = gp_readvec_stream(F->file);
    9732             :   pari_fclose(F);
    9733             :   return eqn;
    9734             : }
    9735             : #endif
    9736             :  /*****************************************************************/
    9737             : /*           EISENSTEIN CUSPS: COMPLEX DIRECTLY: one F_k         */
    9738             : /*****************************************************************/
    9739             : 
    9740             : /* CHIvec = charinit(CHI); data = [N1g/g1,N2g/g2,g1/g,g2/g,C/g1,C/g2,
    9741             :  * (N1g/g1)^{-1},(N2g/g2)^{-1}] */
    9742             : 
    9743             : /* nm = n/m;
    9744             :  * z1 = powers of \z_{C/g}^{(Ae/g)^{-1}},
    9745             :  * z2 = powers of \z_N^{A^{-1}(g1g2/C)}]
    9746             :  * N.B. : we compute value and conjugate at the end, so it is (Ae/g)^{-1}
    9747             :  * and not -(Ae/g)^{-1} */
    9748             : static GEN
    9749     5039776 : eiscnm(long nm, long m, GEN CHI1vec, GEN CHI2vec, GEN data, GEN z1)
    9750             : {
    9751     5039776 :   long Cg1 = data[5], s10 = (nm*data[7]) % Cg1, r10 = (nm - data[1]*s10) / Cg1;
    9752     5039776 :   long Cg2 = data[6], s20 = (m *data[8]) % Cg2, r20 = (m  - data[2]*s20) / Cg2;
    9753             :   long j1, r1, s1;
    9754     5039776 :   GEN T = gen_0;
    9755    13200250 :   for (j1 = 0, r1 = r10, s1 = s10; j1 < data[3]; j1++, r1 -= data[1], s1 += Cg1)
    9756             :   {
    9757     8160474 :     GEN c1 = mychareval(CHI1vec, r1);
    9758     8160474 :     if (!gequal0(c1))
    9759             :     {
    9760             :       long j2, r2, s2;
    9761     5311656 :       GEN S = gen_0;
    9762    15281196 :       for (j2 = 0, r2 = r20, s2 = s20; j2 < data[4]; j2++, r2 -= data[2], s2 += Cg2)
    9763             :       {
    9764     9969540 :         GEN c2 = mychareval(CHI2vec, r2);
    9765     9969540 :         if (!gequal0(c2)) S = gadd(S, gmul(c2, rootsof1pow(z1, s1*s2)));
    9766             :       }
    9767     5311656 :       T = gadd(T, gmul(c1, S));
    9768             :     }
    9769             :   }
    9770     5039776 :   return conj_i(T);
    9771             : }
    9772             : 
    9773             : static GEN
    9774      398622 : fg1g2n(long n, long k, GEN CHI1vec, GEN CHI2vec, GEN data, GEN z1, GEN z2)
    9775             : {
    9776      398622 :   pari_sp av = avma;
    9777      398622 :   GEN S = gen_0, D = mydivisorsu(n);
    9778      398622 :   long i, l = lg(D);
    9779     2918510 :   for (i = 1; i < l; i++)
    9780             :   {
    9781     2519888 :     long m = D[i], nm = D[l-i]; /* n/m */
    9782     2519888 :     GEN u = eiscnm( nm,  m, CHI1vec, CHI2vec, data, z1);
    9783     2519888 :     GEN v = eiscnm(-nm, -m, CHI1vec, CHI2vec, data, z1);
    9784     2519888 :     GEN w = odd(k) ? gsub(u, v) : gadd(u, v);
    9785     2519888 :     S = gadd(S, gmul(powuu(m, k-1), w));
    9786             :   }
    9787      398622 :   return gerepileupto(av, gmul(S, rootsof1pow(z2, n)));
    9788             : }
    9789             : 
    9790             : static GEN
    9791       11116 : gausssumcx(GEN CHIvec, long prec)
    9792             : {
    9793       11116 :   GEN z, S, V = CHIvec_val(CHIvec);
    9794       11116 :   long m, N = CHIvec_N(CHIvec);
    9795       11116 :   z = rootsof1u_cx(N, prec);
    9796       11116 :   S = gmul(z, gel(V, N));
    9797       11116 :   for (m = N-1; m >= 1; m--) S = gmul(z, gadd(gel(V, m), S));
    9798       11116 :   return S;
    9799             : }
    9800             : 
    9801             : /* Computation of Q_k(\z_N^s) as a polynomial in \z_N^s. FIXME: explicit
    9802             :  * formula ? */
    9803             : static GEN
    9804        1666 : mfqk(long k, long N)
    9805             : {
    9806        1666 :   GEN X = pol_x(0), P = gsubgs(gpowgs(X,N), 1), ZI, Q, Xm1, invden;
    9807             :   long i;
    9808        1666 :   ZI = cgetg(N, t_VEC);
    9809        1666 :   for (i = 1; i < N; i++) gel(ZI, i) = utoi(i);
    9810        1666 :   ZI = gdivgs(gmul(X, gtopolyrev(ZI, 0)), N);
    9811        1666 :   if (k == 1) return ZI;
    9812        1050 :   invden = RgXQ_powu(ZI, k, P);
    9813        1050 :   Q = gneg(X); Xm1 = gsubgs(X, 1);
    9814        2695 :   for (i = 2; i < k; i++)
    9815        1645 :     Q = gmul(X, ZX_add(gmul(Xm1, ZX_deriv(Q)), gmulsg(-i, Q)));
    9816        1050 :   return RgXQ_mul(Q, invden, P);
    9817             : }
    9818             : /* CHI mfchar */
    9819             : /* Warning: M is a multiple of the conductor of CHI, but is NOT
    9820             :    necessarily its modulus */
    9821             : 
    9822             : static GEN
    9823        2436 : mfskcx(long k, GEN CHI, long M, long prec)
    9824             : {
    9825             :   GEN S, CHIvec, P;
    9826             :   long F, m, i, l;
    9827        2436 :   CHI = mfchartoprimitive(CHI, &F);
    9828        2436 :   CHIvec = mfcharcxinit(CHI, prec);
    9829        2436 :   if (F == 1) S = gdivgs(bernfrac(k), k);
    9830             :   else
    9831             :   {
    9832        1666 :     GEN Q = mfqk(k, F), V = CHIvec_val(CHIvec);
    9833        1666 :     S = gmul(gel(V, F), RgX_coeff(Q, 0));
    9834        1666 :     for (m = 1; m < F; m++) S = gadd(S, gmul(gel(V, m), RgX_coeff(Q, m)));
    9835        1666 :     S = conj_i(S);
    9836             :   }
    9837             :   /* prime divisors of M not dividing f(chi) */
    9838        2436 :   P = gel(myfactoru(u_ppo(M/F,F)), 1); l = lg(P);
    9839        2562 :   for (i = 1; i < l; i++)
    9840             :   {
    9841         126 :     long p = P[i];
    9842         126 :     S = gmul(S, gsubsg(1, gdiv(mychareval(CHIvec, p), powuu(p, k))));
    9843             :   }
    9844        2436 :   return gmul(gmul(gausssumcx(CHIvec, prec), S), powuu(M/F, k));
    9845             : }
    9846             : 
    9847             : static GEN
    9848        4501 : f00_i(long k, GEN CHI1vec, GEN CHI2vec, GEN G2, GEN S, long prec)
    9849             : {
    9850             :   GEN c, a;
    9851        4501 :   long N1 = CHIvec_N(CHI1vec), N2 = CHIvec_N(CHI2vec);
    9852        4501 :   if (S[2] != N1) return gen_0;
    9853        2436 :   c = mychareval(CHI1vec, S[3]);
    9854        2436 :   if (isintzero(c)) return gen_0;
    9855        2436 :   a = mfskcx(k, mfchardiv(CHIvec_CHI(CHI2vec), CHIvec_CHI(CHI1vec)), N1*N2, prec);
    9856        2436 :   a = gmul(a, conj_i(gmul(c,G2)));
    9857        2436 :   return gdiv(a, mulsi(-N2, powuu(S[1], k-1)));
    9858             : }
    9859             : 
    9860             : static GEN
    9861        3766 : f00(long k, GEN CHI1vec,GEN CHI2vec, GEN G1,GEN G2, GEN data, long prec)
    9862             : {
    9863             :   GEN T1, T2;
    9864        3766 :   T2 = f00_i(k, CHI1vec, CHI2vec, G2, data, prec);
    9865        3766 :   if (k > 1) return T2;
    9866         735 :   T1 = f00_i(k, CHI2vec, CHI1vec, G1, data, prec);
    9867         735 :   return gadd(T1, T2);
    9868             : }
    9869             : 
    9870             : /* ga in SL_2(Z), find beta [a,b;c,d] in Gamma_0(N) and mu in Z such that
    9871             :  * beta * ga * T^u = [A',B';C',D'] with C' | N and N | B', C' > 0 */
    9872             : static void
    9873        4340 : mfgatogap(GEN ga, long N, long *pA, long *pC, long *pD, long *pd, long *pmu)
    9874             : {
    9875        4340 :   GEN A = gcoeff(ga,1,1), B = gcoeff(ga,1,2);
    9876        4340 :   GEN C = gcoeff(ga,2,1), D = gcoeff(ga,2,2), a, b, c, d;
    9877             :   long t, Ap, Cp, B1, D1, mu;
    9878        4340 :   Cp = itou(bezout(muliu(A,N), C, &c, &d)); /* divides N */
    9879        4340 :   t = 0;
    9880        4340 :   if (Cp > 1)
    9881             :   { /* (d, N/Cp) = 1, find t such that (d - t*(A*N/Cp), N) = 1 */
    9882        2345 :     long dN = umodiu(d,Cp), Q = (N/Cp * umodiu(A,Cp)) % Cp;
    9883        2345 :     while (ugcd(dN, Cp) > 1) { t++; dN = Fl_sub(dN, Q, Cp); }
    9884             :   }
    9885        4340 :   if (t)
    9886             :   {
    9887         371 :     c = addii(c, mului(t, diviuexact(C,Cp)));
    9888         371 :     d = subii(d, mului(t, muliu(A, N/Cp))); /* (d,N) = 1 */
    9889             :   }
    9890        4340 :   D1 = umodiu(mulii(d,D), N);
    9891        4340 :   (void)bezout(d, mulis(c,-N), &a, &b); /* = 1 */
    9892        4340 :   t = 0; Ap = umodiu(addii(mulii(a,A), mulii(b,C)), N); /* (Ap,Cp) = 1 */
    9893        4340 :   while (ugcd(Ap, N) > 1) { t++; Ap = Fl_add(Ap, Cp, N); }
    9894        4340 :   B1 = umodiu(a,N)*umodiu(B,N) + umodiu(b,N)*umodiu(D,N) + t*D1;
    9895        4340 :   B1 %= N;
    9896        4340 :   *pmu = mu = Fl_neg(Fl_div(B1, Ap, N), N);
    9897             :   /* A', D' and d only needed modulo N */
    9898        4340 :   *pd = umodiu(d, N);
    9899        4340 :   *pA = Ap;
    9900        4340 :   *pC = Cp; *pD = (D1 + Cp*mu) % N;
    9901        4340 : }
    9902             : 
    9903             : #if 0
    9904             : /* CHI is a mfchar, return alpha(CHI) */
    9905             : static long
    9906             : mfalchi(GEN CHI, long AN, long cg)
    9907             : {
    9908             :   GEN G = gel(CHI,1), chi = gel(CHI,2), go = gmfcharorder(CHI);
    9909             :   long o = itou(go), a = itos( znchareval(G, chi, stoi(1 + AN/cg), go) );
    9910             :   if (a < 0 || (cg * a) % o) pari_err_BUG("mfalchi");
    9911             :   return (cg * a) / o;
    9912             : }
    9913             : #endif
    9914             : /* return A such that CHI1(t) * CHI2(t) = e(A) or NULL if (t,N1*N2) > 1 */
    9915             : static GEN
    9916        8680 : mfcharmuleval(GEN CHI1vec, GEN CHI2vec, long t)
    9917             : {
    9918        8680 :   long a1 = mycharexpo(CHI1vec, t), o1 = CHIvec_ord(CHI1vec);
    9919        8680 :   long a2 = mycharexpo(CHI2vec, t), o2 = CHIvec_ord(CHI2vec);;
    9920        8680 :   if (a1 < 0 || a2 < 0) return NULL;
    9921        8680 :   return sstoQ(a1*o2 + a2*o1, o1*o2);
    9922             : }
    9923             : static GEN
    9924        4340 : mfcharmulcxeval(GEN CHI1vec, GEN CHI2vec, long t, long prec)
    9925             : {
    9926        4340 :   GEN A = mfcharmuleval(CHI1vec, CHI2vec, t);
    9927             :   long n, d;
    9928        4340 :   if (!A) return gen_0;
    9929        4340 :   Qtoss(A, &n,&d); return rootsof1q_cx(n, d, prec);
    9930             : }
    9931             : /* alpha(CHI1 * CHI2) */
    9932             : static long
    9933        4340 : mfalchi2(GEN CHI1vec, GEN CHI2vec, long AN, long cg)
    9934             : {
    9935        4340 :   GEN A = mfcharmuleval(CHI1vec, CHI2vec, 1 + AN/cg);
    9936             :   long a;
    9937        4340 :   if (!A) pari_err_BUG("mfalchi2");
    9938        4340 :   A = gmulsg(cg, A);
    9939        4340 :   if (typ(A) != t_INT) pari_err_BUG("mfalchi2");
    9940        4340 :   a = itos(A) % cg; if (a < 0) a += cg;
    9941        4340 :   return a;
    9942             : }
    9943             : 
    9944             : /* return g = (a,b), set u >= 0 s.t. g = a * u (mod b) */
    9945             : static long
    9946       17360 : mybezout(long a, long b, long *pu)
    9947             : {
    9948       17360 :   long junk, g = cbezout(a, b, pu, &junk);
    9949       17360 :   if (*pu < 0) *pu += b/g;
    9950       17360 :   return g;
    9951             : }
    9952             : 
    9953             : /* E = [k, CHI1,CHI2, e], CHI1 and CHI2 primitive mfchars such that,
    9954             :  * CHI1(-1)*CHI2(-1) = (-1)^k; expansion of (B_e (E_k(CHI1,CHI2))) | ga.
    9955             :  * w is the width for the space of the calling function. */
    9956             : static GEN
    9957        4340 : mfeisensteingacx(GEN E, long w, GEN ga, long lim, long prec)
    9958             : {
    9959        4340 :   GEN CHI1vec, CHI2vec, CHI1 = gel(E,2), CHI2 = gel(E,3), v, S, ALPHA;
    9960             :   GEN G1, G2, z1, z2, data;
    9961        4340 :   long k = itou(gel(E,1)), e = itou(gel(E,4));
    9962        4340 :   long N1 = mfcharmodulus(CHI1);
    9963        4340 :   long N2 = mfcharmodulus(CHI2), N = e * N1 * N2;
    9964             :   long NsurC, cg, wN, A, C, Ai, d, mu, alchi, na, da;
    9965             :   long eg, g, gH, U, u0, u1, u2, Aig, H, m, n, t, Cg, NC1, NC2;
    9966             : 
    9967        4340 :   mfgatogap(ga, N, &A, &C, &Ai, &d, &mu);
    9968        4340 :   CHI1vec = mfcharcxinit(CHI1, prec);
    9969        4340 :   CHI2vec = mfcharcxinit(CHI2, prec);
    9970        4340 :   NsurC = N/C; cg  = ugcd(C, NsurC); wN = NsurC / cg;
    9971        4340 :   if (w%wN) pari_err_BUG("mfeisensteingacx [wN does not divide w]");
    9972        4340 :   alchi = mfalchi2(CHI1vec, CHI2vec, A*N, cg);
    9973        4340 :   ALPHA = sstoQ(alchi, NsurC);
    9974             : 
    9975        4340 :   g = mybezout(A*e, C, &u0); Cg = C/g; eg = e/g;
    9976        4340 :   NC1 = mybezout(N1, Cg, &u1);
    9977        4340 :   NC2 = mybezout(N2, Cg, &u2);
    9978        4340 :   H = (NC1*NC2*g)/Cg;
    9979        4340 :   Aig = (Ai*H)%N; if (Aig < 0) Aig += N;
    9980        4340 :   z1 = rootsof1powinit(u0, Cg, prec);
    9981        4340 :   z2 = rootsof1powinit(Aig, N, prec);
    9982        4340 :   data = mkvecsmalln(8, N1/NC1, N2/NC2, NC1, NC2, Cg/NC1, Cg/NC2, u1, u2);
    9983        4340 :   v = zerovec(lim + 1);
    9984             :   /* need n*H = alchi (mod cg) */
    9985        4340 :   gH = mybezout(H, cg, &U);
    9986        4340 :   if (gH > 1)
    9987             :   {
    9988         357 :     if (alchi % gH) return mkvec2(gen_0, v);
    9989         357 :     alchi /= gH; cg /= gH; H /= gH;
    9990             :   }
    9991        4340 :   G1 = gausssumcx(CHI1vec, prec);
    9992        4340 :   G2 = gausssumcx(CHI2vec, prec);
    9993        4340 :   if (!alchi)
    9994        3766 :     gel(v,1) = f00(k, CHI1vec,CHI2vec,G1,G2, mkvecsmall3(NC2,Cg,A*eg), prec);
    9995        4340 :   n = Fl_mul(alchi,U,cg); if (!n) n = cg;
    9996        4340 :   m = (n*H - alchi) / cg; /* positive, exact division */
    9997      402962 :   for (; m <= lim; n+=cg, m+=H)
    9998      398622 :     gel(v, m+1) = fg1g2n(n, k, CHI1vec, CHI2vec, data, z1,z2);
    9999        4340 :   t = (2*e)/g; if (odd(k)) t = -t;
   10000        4340 :   v = gdiv(v, gmul(conj_i(gmul(G1,G2)), mulsi(t, powuu(eg*N2/NC2, k-1))));
   10001        4340 :   if (k == 2 && N1 == 1 && N2 == 1) v = gsub(mkF2bd(wN,lim), gmulsg(e,v));
   10002             : 
   10003        4340 :   Qtoss(ALPHA, &na,&da);
   10004        4340 :   S = conj_i( mfcharmulcxeval(CHI1vec,CHI2vec,d,prec) ); /* CHI(1/d) */
   10005        4340 :   if (wN > 1)
   10006             :   {
   10007        3199 :     GEN z = rootsof1powinit(-mu, wN, prec);
   10008        3199 :     long i, l = lg(v);
   10009        3199 :     for (i = 1; i < l; i++) gel(v,i) = gmul(gel(v,i), rootsof1pow(z,i-1));
   10010             :   }
   10011        4340 :   v = RgV_Rg_mul(v, gmul(S, rootsof1q_cx(-mu*na, da, prec)));
   10012        4340 :   return mkvec2(ALPHA, bdexpand(v, w/wN));
   10013             : }
   10014             : 
   10015             : /*****************************************************************/
   10016             : /*                       END EISENSTEIN CUSPS                    */
   10017             : /*****************************************************************/
   10018             : 
   10019             : static GEN
   10020        1582 : mfchisimpl(GEN CHI)
   10021             : {
   10022             :   GEN G, chi;
   10023        1582 :   if (typ(CHI) == t_INT) return CHI;
   10024        1582 :   G = gel(CHI, 1); chi = gel(CHI, 2);
   10025        1582 :   switch(mfcharorder(CHI))
   10026             :   {
   10027        1134 :     case 1: chi = gen_1; break;
   10028         427 :     case 2: chi = znchartokronecker(G,chi,1); break;
   10029          21 :     default:chi = mkintmod(znconreyexp(G,chi), znstar_get_N(G)); break;
   10030             :   }
   10031        1582 :   return chi;
   10032             : }
   10033             : 
   10034             : GEN
   10035         700 : mfparams(GEN F)
   10036             : {
   10037         700 :   pari_sp av = avma;
   10038             :   GEN z, mf;
   10039         700 :   if ((mf = checkMF_i(F)))
   10040             :   {
   10041          14 :     long N = MF_get_N(mf);
   10042          14 :     GEN gk = MF_get_gk(mf);
   10043          14 :     z = mkvec4(utoi(N), gk, MF_get_CHI(mf), utoi(MF_get_space(mf)));
   10044             :   }
   10045             :   else
   10046             :   {
   10047         686 :     if (!checkmf_i(F)) pari_err_TYPE("mfparams", F);
   10048         686 :     z = shallowcopy( mf_get_NK(F) );
   10049             :   }
   10050         700 :   gel(z,3) = mfchisimpl(gel(z,3));
   10051         700 :   return gerepilecopy(av, z);
   10052             : }
   10053             : 
   10054             : GEN
   10055          14 : mfisCM(GEN F)
   10056             : {
   10057          14 :   pari_sp av = avma;
   10058             :   forprime_t S;
   10059             :   GEN D, v;
   10060             :   long N, k, lD, sb, p, i;
   10061          14 :   if (!checkmf_i(F)) pari_err_TYPE("mfisCM", F);
   10062          14 :   N = mf_get_N(F);
   10063          14 :   k = mf_get_k(F); if (N < 0 || k < 0) pari_err_IMPL("mfisCM for this F");
   10064          14 :   D = mfunram(N, -1);
   10065          14 :   lD = lg(D);
   10066          14 :   sb = maxss(mfsturmNk(N, k), 4*N);
   10067          14 :   v = mfcoefs_i(F, sb, 1);
   10068          14 :   u_forprime_init(&S, 2, sb);
   10069         518 :   while ((p = u_forprime_next(&S)))
   10070             :   {
   10071         490 :     GEN ap = gel(v, p+1);
   10072         490 :     if (!gequal0(ap))
   10073         406 :       for (i = 1; i < lD; i++)
   10074         245 :         if (kross(D[i], p) == -1) { D = vecsplice(D, i); lD--; }
   10075             :   }
   10076          14 :   if (lD == 1) { avma = av; return gen_0; }
   10077          14 :   if (lD == 2) { avma = av; return stoi(D[1]); }
   10078           7 :   if (k > 1) pari_err_BUG("mfisCM");
   10079           7 :   return gerepileupto(av, zv_to_ZV(D));
   10080             : }
   10081             : 
   10082             : static long
   10083         287 : mfspace_i(GEN mf, GEN F)
   10084             : {
   10085             :   GEN v, vF, gk;
   10086             :   long n, nE, i, l, s, N;
   10087             : 
   10088         287 :   mf = checkMF(mf); s = MF_get_space(mf);
   10089         287 :   if (!F) return s;
   10090         287 :   if (!checkmf_i(F)) pari_err_TYPE("mfspace",F);
   10091         287 :   v = mftobasis(mf, F, 1);
   10092         287 :   n = lg(v)-1; if (!n) return -1;
   10093         231 :   nE = lg(MF_get_E(mf))-1;
   10094         231 :   switch(s)
   10095             :   {
   10096          56 :     case mf_NEW: case mf_OLD: case mf_EISEN: return s;
   10097             :     case mf_FULL:
   10098         140 :       if (mf_get_type(F) == t_MF_THETA) return mf_EISEN;
   10099         133 :       if (!gequal0(vecslice(v,1,nE)))
   10100          63 :         return gequal0(vecslice(v,nE+1,n))? mf_EISEN: mf_FULL;
   10101             :   }
   10102             :   /* mf is mf_CUSP or mf_FULL, F a cusp form */
   10103         105 :   gk = mf_get_gk(F);
   10104         105 :   if (typ(gk) == t_FRAC || equali1(gk)) return mf_CUSP;
   10105          91 :   vF = mftonew_i(mf, vecslice(v, nE+1, n), &N);
   10106          91 :   if (N != MF_get_N(mf)) return mf_OLD;
   10107          63 :   l = lg(vF);
   10108         105 :   for (i = 1; i < l; i++)
   10109          63 :     if (itos(gmael(vF,i,1)) != N) return mf_CUSP;
   10110          42 :   return mf_NEW;
   10111             : }
   10112             : long
   10113         287 : mfspace(GEN mf, GEN F)
   10114             : {
   10115         287 :   pari_sp av = avma;
   10116         287 :   long s = mfspace_i(mf,F);
   10117         287 :   avma = av; return s;
   10118             : }
   10119             : static GEN
   10120           7 : lfunfindchi(GEN ldata, GEN van, long prec)
   10121             : {
   10122           7 :   GEN gN = ldata_get_conductor(ldata), G = znstar0(gN,1), L, go, vz;
   10123           7 :   long k = ldata_get_k(ldata), N = itou(gN), bit = 10 - prec2nbits(prec);
   10124           7 :   long i, j, o, l, odd = k & 1, B0 = 2, B = lg(van)-1;
   10125             : 
   10126           7 :   van = shallowcopy(van);
   10127           7 :   L = cyc2elts(znstar_get_conreycyc(G));
   10128           7 :   l = lg(L);
   10129          21 :   for (i = j = 1; i < l; i++)
   10130             :   {
   10131          14 :     GEN chi = zc_to_ZC(gel(L,i));
   10132          14 :     if (zncharisodd(G,chi) == odd) gel(L,j++) = mfcharGL(G,chi);
   10133             :   }
   10134           7 :   setlg(L,j); l = j;
   10135           7 :   if (l <= 2) return gel(L,1);
   10136           0 :   o = znstar_get_expo(G); go = utoi(o);
   10137           0 :   vz = grootsof1(o, prec);
   10138             :   for (;;)
   10139           0 :   {
   10140             :     long n;
   10141           0 :     for (n = B0; n <= B; n++)
   10142             :     {
   10143           0 :       GEN an = gel(van,n), r;
   10144             :       long j;
   10145           0 :       if (ugcd(n, N) != 1 || gexpo(an) < bit) continue;
   10146           0 :       r = gdiv(an, conj_i(an));
   10147           0 :       for (i = 1; i < l; i++)
   10148             :       {
   10149           0 :         GEN CHI = gel(L,i);
   10150           0 :         if (gexpo(gsub(r, gel(vz, znchareval_i(CHI,n,go)+1))) > bit)
   10151           0 :           gel(L,i) = NULL;
   10152             :       }
   10153           0 :       for (i = j = 1; i < l; i++)
   10154           0 :         if (gel(L,i)) gel(L,j++) = gel(L,i);
   10155           0 :       l = j; setlg(L,l);
   10156           0 :       if (l == 2) return gel(L,1);
   10157             :     }
   10158           0 :     B0 = B+1; B <<= 1;
   10159           0 :     van = ldata_vecan(ldata_get_an(ldata), B, prec);
   10160             :   }
   10161             : }
   10162             : 
   10163             : GEN
   10164           7 : mffromlfun(GEN L, long prec)
   10165             : {
   10166           7 :   pari_sp av = avma;
   10167           7 :   GEN ldata = lfunmisc_to_ldata_shallow(L), Vga = ldata_get_gammavec(ldata);
   10168             :   GEN van, a0, CHI, NK;
   10169             :   long k, N, space;
   10170           7 :   if (!gequal(Vga, mkvec2(gen_0, gen_1))) pari_err_TYPE("mffromlfun", L);
   10171           7 :   k = ldata_get_k(ldata);
   10172           7 :   N = itou(ldata_get_conductor(ldata));
   10173           7 :   van = ldata_vecan(ldata_get_an(ldata), mfsturmNk(N,k) + 2, prec);
   10174           7 :   CHI = lfunfindchi(ldata, van, prec);
   10175           7 :   space = (lg(ldata) == 7)? mf_CUSP: mf_FULL;
   10176           7 :   a0 = (space == mf_CUSP)? gen_0: gneg(lfun(L, gen_0, prec2nbits(prec)));
   10177           7 :   NK = mkvec3(utoi(N), utoi(k), mfchisimpl(CHI));
   10178           7 :   return gerepilecopy(av, mkvec3(NK, utoi(space), shallowconcat(a0, van)));
   10179             : }
   10180             : /*******************************************************************/
   10181             : /*                                                                 */
   10182             : /*                       HALF-INTEGRAL WEIGHT                      */
   10183             : /*                                                                 */
   10184             : /*******************************************************************/
   10185             : /* We use the prefix mf2; k represents the weight -1/2, so e.g.
   10186             :    k = 2 is weight 5/2. N is the level, so 4\mid N, and CHI is the
   10187             :    character, always even. */
   10188             : 
   10189             : static long
   10190        3360 : lamCO(long r, long s, long p)
   10191             : {
   10192        3360 :   if ((s << 1) <= r)
   10193             :   {
   10194        1232 :     long rp = r >> 1;
   10195        1232 :     if (odd(r)) return upowuu(p, rp) << 1;
   10196         336 :     else return (p + 1)*upowuu(p, rp - 1);
   10197             :   }
   10198        2128 :   else return upowuu(p, r - s) << 1;
   10199             : }
   10200             : 
   10201             : static int
   10202        1568 : condC(GEN faN, GEN valF)
   10203             : {
   10204        1568 :   GEN P = gel(faN, 1), E = gel(faN, 2);
   10205        1568 :   long l = lg(P), i;
   10206        3696 :   for (i = 1; i < l; i++)
   10207        3024 :     if ((P[i] & 3L) == 3)
   10208             :     {
   10209        1120 :       long r = E[i];
   10210        1120 :       if (odd(r) || r < (valF[i] << 1)) return 1;
   10211             :     }
   10212         672 :   return 0;
   10213             : }
   10214             : 
   10215             : /* returns 2*zetaCO; weight is k + 1/2 */
   10216             : static long
   10217        3696 : zeta2CO(GEN faN, GEN valF, long r2, long s2, long k)
   10218             : {
   10219        3696 :   if (r2 >= 4) return lamCO(r2, s2, 2) << 1;
   10220        2912 :   if (r2 == 3) return 6;
   10221        1568 :   if (condC(faN, valF)) return 4;
   10222         672 :   if (odd(k)) return s2 ? 3 : 5; else return s2 ? 5: 3;
   10223             : }
   10224             : 
   10225             : /* returns 4 times last term in formula */
   10226             : static long
   10227        3696 : dim22(long N, long F, long k)
   10228             : {
   10229        3696 :   pari_sp av = avma;
   10230        3696 :   GEN vF, faN = myfactoru(N), P = gel(faN, 1), E = gel(faN, 2);
   10231        3696 :   long i, D, l = lg(P);
   10232        3696 :   vF = cgetg(l, t_VECSMALL);
   10233        3696 :   for (i = 1; i < l; i++) vF[i] = u_lval(F, P[i]);
   10234        3696 :   D = zeta2CO(faN, vF, E[1], vF[1], k);
   10235        3696 :   for (i = 2; i < l; i++) D *= lamCO(E[i], vF[i], P[i]);
   10236        3696 :   avma = av; return D;
   10237             : }
   10238             : 
   10239             : /* PSI not necessarily primitive, of conductor F */
   10240             : static int
   10241       13846 : charistotallyeven(GEN PSI, long F)
   10242             : {
   10243       13846 :   pari_sp av = avma;
   10244       13846 :   GEN P = gel(myfactoru(F), 1);
   10245       13846 :   GEN G = gel(PSI,1), psi = gel(PSI,2);
   10246             :   long i;
   10247       14350 :   for (i = 1; i < lg(P); i++)
   10248             :   {
   10249         532 :     GEN psip = znchardecompose(G, psi, utoipos(P[i]));
   10250         532 :     if (zncharisodd(G, psip)) { avma = av; return 0; }
   10251             :   }
   10252       13818 :   avma = av; return 1;
   10253             : }
   10254             : 
   10255             : static GEN
   10256      299775 : get_PSI(GEN CHI, long t)
   10257             : {
   10258      299775 :   long r = t & 3L, t2 = (r == 2 || r == 3) ? t << 2 : t;
   10259      299775 :   return mfcharmul_i(CHI, induce(gel(CHI,1), utoipos(t2)));
   10260             : }
   10261             : /* space = mf_CUSP, mf_EISEN or mf_FULL, weight k + 1/2 */
   10262             : static long
   10263       41363 : mf2dimwt12(long N, GEN CHI, long space)
   10264             : {
   10265       41363 :   pari_sp av = avma;
   10266       41363 :   GEN D = mydivisorsu(N >> 2);
   10267       41363 :   long i, l = lg(D), dim3 = 0, dim4 = 0;
   10268             : 
   10269       41363 :   CHI = induceN(N, CHI);
   10270      341138 :   for (i = 1; i < l; i++)
   10271             :   {
   10272      299775 :     long rp, t = D[i], Mt = D[l-i];
   10273      299775 :     GEN PSI = get_PSI(CHI,t);
   10274      299775 :     rp = mfcharconductor(PSI);
   10275      299775 :     if (Mt % (rp*rp) == 0) { dim4++; if (charistotallyeven(PSI,rp)) dim3++; }
   10276             :   }
   10277       41363 :   avma = av;
   10278       41363 :   switch (space)
   10279             :   {
   10280       40439 :     case mf_CUSP: return dim4 - dim3;
   10281         462 :     case mf_EISEN:return dim3;
   10282         462 :     case mf_FULL: return dim4;
   10283             :   }
   10284             :   return 0; /*LCOV_EXCL_LINE*/
   10285             : }
   10286             : 
   10287             : static long
   10288         693 : mf2dimwt32(long N, GEN CHI, long F, long space)
   10289             : {
   10290             :   long D;
   10291         693 :   switch(space)
   10292             :   {
   10293         231 :     case mf_CUSP: D = mypsiu(N) - 6*dim22(N, F, 1);
   10294         231 :       if (D%24) pari_err_BUG("mfdim");
   10295         231 :       return D/24 + mf2dimwt12(N, CHI, 4);
   10296         231 :     case mf_FULL: D = mypsiu(N) + 6*dim22(N, F, 0);
   10297         231 :       if (D%24) pari_err_BUG("mfdim");
   10298         231 :       return D/24 + mf2dimwt12(N, CHI, 1);
   10299         231 :     case mf_EISEN: D = dim22(N, F, 0) + dim22(N, F, 1);
   10300         231 :       if (D & 3L) pari_err_BUG("mfdim");
   10301         231 :       return (D >> 2) - mf2dimwt12(N, CHI, 3);
   10302             :   }
   10303             :   return 0; /*LCOV_EXCL_LINE*/
   10304             : }
   10305             : 
   10306             : /* F = conductor(CHI), weight k = r+1/2 */
   10307             : static long
   10308       43673 : checkmf2(long N, long r, GEN CHI, long F, long space)
   10309             : {
   10310       43673 :   switch(space)
   10311             :   {
   10312       43652 :     case mf_FULL: case mf_CUSP: case mf_EISEN: break;
   10313             :     case mf_NEW: case mf_OLD:
   10314          14 :       pari_err_TYPE("half-integral weight [new/old spaces]", utoi(space));
   10315             :     default:
   10316           7 :       pari_err_TYPE("half-integral weight [incorrect space]",utoi(space));
   10317             :   }
   10318       43652 :   if (N & 3L)
   10319           0 :     pari_err_DOMAIN("half-integral weight", "N % 4", "!=", gen_0, stoi(N));
   10320       43652 :   return r >= 0 && mfcharparity(CHI) == 1 && N % F == 0;
   10321             : }
   10322             : 
   10323             : /* weight k = r + 1/2 */
   10324             : static long
   10325       43463 : mf2dim_Nkchi(long N, long r, GEN CHI, ulong space)
   10326             : {
   10327       43463 :   long D, D2, F = mfcharconductor(CHI);
   10328       43463 :   if (!checkmf2(N, r, CHI, F, space)) return 0;
   10329       43442 :   if (r == 0) return mf2dimwt12(N, CHI, space);
   10330        2772 :   if (r == 1) return mf2dimwt32(N, CHI, F, space);
   10331        2079 :   if (space == mf_EISEN)
   10332             :   {
   10333         693 :     D = dim22(N, F, r) + dim22(N, F, 1-r);
   10334         693 :     if (D & 3L) pari_err_BUG("mfdim");
   10335         693 :     return D >> 2;
   10336             :   }
   10337        1386 :   D2 = space == mf_FULL? dim22(N, F, 1-r): -dim22(N, F, r);
   10338        1386 :   D = (2*r-1)*mypsiu(N) + 6*D2;
   10339        1386 :   if (D%24) pari_err_BUG("mfdim");
   10340        1386 :   return D/24;
   10341             : }
   10342             : 
   10343             : /* weight k=r+1/2 */
   10344             : static GEN
   10345         210 : mf2init_Nkchi(long N, long r, GEN CHI, long space, long flraw)
   10346             : {
   10347         210 :   GEN Minv, Minvmat, B, M, gk = gaddsg(r,ghalf);
   10348         210 :   GEN mf1 = mkvec4(utoi(N),gk,CHI,utoi(space));
   10349             :   long L;
   10350         210 :   if (!checkmf2(N, r, CHI, mfcharconductor(CHI), space)) return mfEMPTY(mf1);
   10351         210 :   if (space==mf_EISEN) pari_err_IMPL("half-integral weight Eisenstein space");
   10352         210 :   L = mfsturmNgk(N, gk) + 1;
   10353         210 :   B = mf2basis(N, r, CHI, space);
   10354         210 :   M = mflineardivtomat(N,B,L);
   10355         210 :   if (flraw) M = mkvec3(gen_0,gen_0,M);
   10356             :   else
   10357             :   {
   10358         210 :     M = mfcleanCHI(M, CHI, 0);
   10359         210 :     Minv = gel(M,2);
   10360         210 :     Minvmat = RgM_Minv_mul(NULL, Minv);
   10361         210 :     B = vecmflineardiv_linear(B, Minvmat);
   10362         210 :     gel(M,3) = RgM_Minv_mul(gel(M,3), Minv);
   10363         210 :     gel(M,2) = mkMinv(matid(lg(B)-1), NULL,NULL,NULL);
   10364             :   }
   10365         210 :   return mkmf(mf1, cgetg(1,t_VEC), B, gen_0, M);
   10366             : }
   10367             : 
   10368             : /**************************************************************************/
   10369             : /*                          Kohnen + space                                */
   10370             : /**************************************************************************/
   10371             : 
   10372             : static GEN
   10373          21 : mfkohnenbasis_i(GEN mf, GEN CHIP, long eps, long sb)
   10374             : {
   10375          21 :   GEN M = shallowtrans(mfcoefs_mf(mf, sb, 1)), ME;
   10376             :   long c, i, n;
   10377          21 :   ME = cgetg(sb + 2, t_MAT);
   10378         784 :   for (i = 0, c = 1; i <= sb; i++)
   10379             :   {
   10380         763 :     long j = i & 3L;
   10381         763 :     if (j == 2 || j == 2 + eps) gel(ME, c++) = gel(M, i+1);
   10382             :   }
   10383          21 :   setlg(ME, c); ME = shallowtrans(Q_primpart(ME));
   10384          21 :   n = mfcharorder_canon(CHIP);
   10385          21 :   return n == 1? ZM_ker(ME): ZabM_ker(liftpol_shallow(ME), mfcharpol(CHIP), n);
   10386             : }
   10387             : GEN
   10388          21 : mfkohnenbasis(GEN mf)
   10389             : {
   10390          21 :   pari_sp av = avma;
   10391             :   GEN gk, CHI, CHIP, K;
   10392             :   long N4, r, eps, sb;
   10393          21 :   mf = checkMF(mf);
   10394          21 :   if (MF_get_space(mf) != mf_CUSP)
   10395           0 :     pari_err_TYPE("mfkohnenbasis [not a cuspidal space", mf);
   10396          21 :   if (!MF_get_dim(mf)) return cgetg(1, t_MAT);
   10397          21 :   N4 = MF_get_N(mf) >> 2; gk = MF_get_gk(mf); CHI = MF_get_CHI(mf);
   10398          21 :   if (typ(gk) == t_INT) pari_err_TYPE("mfkohnenbasis", gk);
   10399          21 :   r = MF_get_r(mf);
   10400          21 :   CHIP = mfcharchiliftprim(CHI, N4);
   10401          21 :   eps = CHIP==CHI? 1: -1;
   10402          21 :   if (!CHIP) pari_err_TYPE("mfkohnenbasis [incorrect CHI]", CHI);
   10403          21 :   if (odd(r)) eps = -eps;
   10404          21 :   if (uissquarefree(N4))
   10405             :   {
   10406          14 :     long d = mfdim_Nkchi(N4, 2*r, mfcharpow(CHI, gen_2), mf_CUSP);
   10407          14 :     sb = mfsturmNgk(N4 << 2, gk) + 1;
   10408          14 :     K = mfkohnenbasis_i(mf, CHIP, eps, sb);
   10409          14 :     if (lg(K) - 1 == d) return gerepilecopy(av, K);
   10410             :   }
   10411           7 :   sb = mfsturmNgk(N4 << 4, gk) + 1;
   10412           7 :   K = mfkohnenbasis_i(mf, CHIP, eps, sb);
   10413           7 :   return gerepilecopy(av, K);
   10414             : }
   10415             : 
   10416             : /* return [mf3, bijection, mfkohnenbasis, codeshi] */
   10417             : static GEN
   10418          14 : mfkohnenbijection_i(GEN mf)
   10419             : {
   10420          14 :   GEN vB, mf3, K, SHI, P, CHI = MF_get_CHI(mf);
   10421          14 :   long n, lK, i, dim, m, lw, sb3, N4 = MF_get_N(mf)>>2, r = MF_get_r(mf);
   10422          14 :   long Dp[] = {1, 5, 8, 12, 13, 17, 21, 24};
   10423          14 :   long Dm[] = {-3, -4, -7, -8, -11, -15, -19, -20}, *D = odd(r)? Dm: Dp;
   10424          14 :   const long nbD = 8, MAXm = 6560; /* #D, 3^#D - 1 */
   10425             : 
   10426          14 :   K = mfkohnenbasis(mf); lK = lg(K);
   10427          14 :   mf3 = mfinit_Nkchi(N4, r<<1, mfcharpow(CHI,gen_2), mf_CUSP, 0);
   10428          14 :   if (MF_get_dim(mf3) != lK - 1)
   10429           0 :     pari_err_BUG("mfkohnenbijection [different dimensions]");
   10430          14 :   if (lK == 1) return mkvec4(mf3, cgetg(1, t_MAT), K, cgetg(1, t_VEC));
   10431          14 :   CHI = mfcharchiliftprim(CHI, N4);
   10432          14 :   if (!CHI) pari_err_TYPE("mfkohnenbijection [incorrect CHI]", CHI);
   10433          14 :   n = mfcharorder_canon(CHI);
   10434          14 :   P = n==1? NULL: mfcharpol(CHI);
   10435          14 :   SHI = cgetg(nbD+1, t_VEC);
   10436          14 :   sb3 = mfsturm(mf3);
   10437          14 :   vB = RgM_mul(mfcoefs_mf(mf, labs(D[nbD-1])*sb3*sb3, 1), K);
   10438          14 :   dim = MF_get_dim(mf3);
   10439          35 :   for (m = 1, lw = 0; m <= MAXm; m += (m%3)? 2: 1)
   10440             :   {
   10441             :     pari_sp av;
   10442          35 :     ulong m1, y, v = u_lvalrem(m, 3, &y);
   10443             :     GEN z, M;
   10444             :     long j;
   10445          35 :     if (y == 1)
   10446             :     {
   10447          28 :       long d = D[v];
   10448          28 :       GEN a = cgetg(lK, t_MAT);
   10449          98 :       for (i = 1; i < lK; i++)
   10450             :       {
   10451          70 :         pari_sp av2 = avma;
   10452          70 :         GEN f = c_deflate(sb3*sb3, labs(d), gel(vB,i));
   10453          70 :         f = mftobasis_i(mf3, RgV_shimura(f, sb3, d, N4, r, CHI));
   10454          70 :         gel(a,i) = gerepileupto(av2, f);
   10455             :       }
   10456          28 :       lw++; gel(SHI,v+1) = a;
   10457             :     }
   10458          35 :     av = avma; M = NULL;
   10459          91 :     for (j = 1, m1 = m; j <= lw; j++, m1/=3)
   10460             :     {
   10461          56 :       long s = m1%3;
   10462          56 :       if (s)
   10463             :       {
   10464          42 :         GEN t = gel(SHI,j);
   10465          42 :         if (M) M = (s == 2)? RgM_sub(M, t): RgM_add(M, t);
   10466          35 :         else   M = (s == 2)? RgM_neg(t): t;
   10467             :       }
   10468             :     }
   10469          35 :     z = QabM_indexrank(M,P,n);
   10470          35 :     if (lg(gel(z,2)) > dim)
   10471             :     {
   10472          14 :       GEN d = ZV_to_zv( digits(utoipos(m), utoipos(3)) );
   10473          14 :       GEN mres, dMi, Mi = QabM_pseudoinv(M,P,n, NULL,&dMi);
   10474          14 :       long ld = lg(d), c = 1;
   10475          14 :       if (DEBUGLEVEL>1)
   10476           0 :         err_printf("mfkohnenbijection: used %ld discriminants\n",lw);
   10477          14 :       mres = cgetg(ld, t_VEC);
   10478          42 :       for (j = ld-1; j >= 1; j--)
   10479          28 :         if (d[j]) gel(mres,c++) = mkvec2s(D[ld-j-1], d[j]);
   10480          14 :       setlg(mres,c); return mkvec4(mf3, RgM_Rg_div(Mi,dMi), K, mres);
   10481             :     }
   10482          21 :     avma = av;
   10483             :   }
   10484           0 :   pari_err_BUG("mfkohnenbijection");
   10485             :   return NULL; /*LCOV_EXCL_LINE*/
   10486             : }
   10487             : GEN
   10488          14 : mfkohnenbijection(GEN mf)
   10489             : {
   10490          14 :   pari_sp av = avma;
   10491             :   long N;
   10492          14 :   mf = checkMF(mf); N = MF_get_N(mf);
   10493          14 :   if (!uissquarefree(N >> 2))
   10494           0 :     pari_err_TYPE("mfkohnenbijection [N/4 not squarefree]", utoi(N));
   10495          14 :   if (MF_get_space(mf) != mf_CUSP || MF_get_r(mf) == 0 || !mfshimura_space_cusp(mf))
   10496           0 :     pari_err_TYPE("mfkohnenbijection [incorrect mf for Kohnen]", mf);
   10497          14 :   return gerepilecopy(av, mfkohnenbijection_i(mf));
   10498             : }
   10499             : 
   10500             : static int
   10501           7 : checkbij_i(GEN b)
   10502             : {
   10503          14 :   return typ(b) == t_VEC && lg(b) == 5 && checkMF_i(gel(b,1))
   10504           7 :          && typ(gel(b,2)) == t_MAT
   10505           7 :          && typ(gel(b,3)) == t_MAT
   10506          14 :          && typ(gel(b,4)) == t_VEC;
   10507             : }
   10508             : 
   10509             : /* bij is the output of mfkohnenbijection */
   10510             : GEN
   10511           7 : mfkohneneigenbasis(GEN mf, GEN bij)
   10512             : {
   10513           7 :   pari_sp av = avma;
   10514             :   GEN mf3, mf30, B, KM, M, k;
   10515             :   long r, i, l, N4;
   10516           7 :   mf = checkMF(mf);
   10517           7 :   if (!checkbij_i(bij))
   10518           0 :     pari_err_TYPE("mfkohneneigenbasis [bijection]", bij);
   10519           7 :   if (MF_get_space(mf) != mf_CUSP)
   10520           0 :     pari_err_TYPE("mfkohneneigenbasis [not a cuspidal space]", mf);
   10521           7 :   if (!MF_get_dim(mf))
   10522           0 :     retmkvec3(cgetg(1, t_VEC), cgetg(1, t_VEC), cgetg(1, t_VEC));
   10523           7 :   N4 = MF_get_N(mf) >> 2; k = MF_get_gk(mf);
   10524           7 :   if (typ(k) == t_INT) pari_err_TYPE("mfkohneneigenbasis", k);
   10525           7 :   if (!uissquarefree(N4))
   10526           0 :     pari_err_TYPE("mfkohneneigenbasis [N not squarefree]", utoipos(N4));
   10527           7 :   r = MF_get_r(mf);
   10528           7 :   KM = RgM_mul(gel(bij,3), gel(bij,2));
   10529           7 :   mf3 = gel(bij,1);
   10530           7 :   mf30 = mfinit_Nkchi(N4, 2*r, MF_get_CHI(mf3), mf_NEW, 0);
   10531           7 :   B = mfcoefs_mf(mf30, mfsturm_mf(mf3), 1); l = lg(B);
   10532           7 :   M = cgetg(l, t_MAT);
   10533           7 :   for (i=1; i<l; i++) gel(M,i) = RgM_RgC_mul(KM, mftobasis_i(mf3, gel(B,i)));
   10534           7 :   return gerepilecopy(av, mkvec3(mf30, M, RgM_mul(M, MF_get_newforms(mf30))));
   10535             : }
   10536             : /*************************** End Kohnen ************************************/
   10537             : /***************************************************************************/
   10538             : 
   10539             : static GEN desc(GEN F);
   10540             : static GEN
   10541         504 : desc_mfeisen(GEN F)
   10542             : {
   10543         504 :   GEN R, gk = mf_get_gk(F);
   10544         504 :   if (typ(gk) == t_FRAC)
   10545           7 :     R = gsprintf("H_{%Ps}", gk);
   10546             :   else
   10547             :   {
   10548         497 :     GEN vchi = gel(F, 2), CHI = mfchisimpl(gel(vchi, 3));
   10549         497 :     long k = itou(gk);
   10550         497 :     if (lg(vchi) < 5) R = gsprintf("F_%ld(%Ps)", k, CHI);
   10551             :     else
   10552             :     {
   10553         294 :       GEN CHI2 = mfchisimpl(gel(vchi, 4));
   10554         294 :       R = gsprintf("F_%ld(%Ps, %Ps)", k, CHI, CHI2);
   10555             :     }
   10556             :   }
   10557         504 :   return R;
   10558             : }
   10559             : static GEN
   10560          35 : desc_hecke(GEN F)
   10561             : {
   10562             :   long n, N;
   10563          35 :   GEN D = gel(F,2);
   10564          35 :   if (typ(D) == t_VECSMALL) { N = D[3]; n = D[1]; }
   10565          14 :   else { GEN nN = gel(D,2); n = nN[1]; N = nN[2]; } /* half integer */
   10566          35 :   return gsprintf("T_%ld(%ld)(%Ps)", N, n, desc(gel(F,3)));
   10567             : }
   10568             : static GEN
   10569          98 : desc_linear(GEN FLD, GEN dL)
   10570             : {
   10571          98 :   GEN F = gel(FLD,2), L = gel(FLD,3), R = strtoGENstr("LIN([");
   10572          98 :   long n = lg(F) - 1, i;
   10573         168 :   for (i = 1; i <= n; i++)
   10574             :   {
   10575         168 :     R = shallowconcat(R, desc(gel(F,i))); if (i == n) break;
   10576          70 :     R = shallowconcat(R, strtoGENstr(", "));
   10577             :   }
   10578          98 :   return shallowconcat(R, gsprintf("], %Ps)", gdiv(L, dL)));
   10579             : }
   10580             : static GEN
   10581          21 : desc_dihedral(GEN F)
   10582             : {
   10583          21 :   GEN bnr = gel(F,2), D = nf_get_disc(bnr_get_nf(bnr)), f = bnr_get_mod(bnr);
   10584          21 :   GEN cyc = bnr_get_cyc(bnr);
   10585          21 :   GEN w = gel(F,3), chin = zv_to_ZV(gel(w,2)), o = utoi(gel(w,1)[1]);
   10586          21 :   GEN chi = char_denormalize(cyc, o, chin);
   10587          21 :   if (lg(gel(f,2)) == 1) f = gel(f,1);
   10588          21 :   return gsprintf("DIH(%Ps, %Ps, %Ps, %Ps)",D,f,cyc,chi);
   10589             : }
   10590             : 
   10591             : static void
   10592        1043 : unpack0(GEN *U)
   10593        1043 : { if (U) *U = mkvec2(cgetg(1, t_VEC), cgetg(1, t_VEC)); }
   10594             : static void
   10595          42 : unpack2(GEN F, GEN *U)
   10596          42 : { if (U) *U = mkvec2(mkvec2(gel(F,2), gel(F,3)), cgetg(1, t_VEC)); }
   10597             : static void
   10