Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - mftrace.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.14.0 lcov report (development 26712-590d837a1c) Lines: 7540 7726 97.6 %
Date: 2021-06-22 07:13:04 Functions: 766 771 99.4 %
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; either version 2 of the License, or (at your option) any later
       8             : version. It is distributed in the hope that it will be useful, but WITHOUT
       9             : ANY WARRANTY WHATSOEVER.
      10             : 
      11             : Check the License for details. You should have received a copy of it, along
      12             : with the package; see the file 'COPYING'. If not, write to the Free Software
      13             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      14             : 
      15             : /*************************************************************************/
      16             : /*                                                                       */
      17             : /*              Modular forms package based on trace formulas            */
      18             : /*                                                                       */
      19             : /*************************************************************************/
      20             : #include "pari.h"
      21             : #include "paripriv.h"
      22             : 
      23             : #define DEBUGLEVEL DEBUGLEVEL_mf
      24             : 
      25             : enum {
      26             :   MF_SPLIT = 1,
      27             :   MF_EISENSPACE,
      28             :   MF_FRICKE,
      29             :   MF_MF2INIT,
      30             :   MF_SPLITN
      31             : };
      32             : 
      33             : typedef struct {
      34             :   GEN vnew, vfull, DATA, VCHIP;
      35             :   long n, newHIT, newTOTAL, cuspHIT, cuspTOTAL;
      36             : } cachenew_t;
      37             : 
      38             : static void init_cachenew(cachenew_t *c, long n, long N, GEN f);
      39             : static long mf1cuspdim_i(long N, GEN CHI, GEN TMP, GEN vSP, long *dih);
      40             : static GEN mfinit_i(GEN NK, long space);
      41             : static GEN mfinit_Nkchi(long N, long k, GEN CHI, long space, long flraw);
      42             : static GEN mf2init_Nkchi(long N, long k, GEN CHI, long space, long flraw);
      43             : static GEN mf2basis(long N, long r, GEN CHI, GEN *pCHI1, long space);
      44             : static GEN mfeisensteinbasis(long N, long k, GEN CHI);
      45             : static GEN mfeisensteindec(GEN mf, GEN F);
      46             : static GEN initwt1newtrace(GEN mf);
      47             : static GEN initwt1trace(GEN mf);
      48             : static GEN myfactoru(long N);
      49             : static GEN mydivisorsu(long N);
      50             : static GEN Qab_Czeta(long k, long ord, GEN C, long vt);
      51             : static GEN mfcoefs_i(GEN F, long n, long d);
      52             : static GEN bhnmat_extend(GEN M, long m,long l, GEN S, cachenew_t *cache);
      53             : static GEN initnewtrace(long N, GEN CHI);
      54             : static void dbg_cachenew(cachenew_t *C);
      55             : static GEN hecke_i(long m, long l, GEN V, GEN F, GEN DATA);
      56             : static GEN c_Ek(long n, long d, GEN F);
      57             : static GEN RgV_heckef2(long n, long d, GEN V, GEN F, GEN DATA);
      58             : static GEN mfcusptrace_i(long N, long k, long n, GEN Dn, GEN TDATA);
      59             : static GEN mfnewtracecache(long N, long k, long n, cachenew_t *cache);
      60             : static GEN colnewtrace(long n0, long n, long d, long N, long k, cachenew_t *c);
      61             : static GEN dihan(GEN bnr, GEN w, GEN k0j, long m, ulong n);
      62             : static GEN sigchi(long k, GEN CHI, long n);
      63             : static GEN sigchi2(long k, GEN CHI1, GEN CHI2, long n, long ord);
      64             : static GEN mflineardivtomat(long N, GEN vF, long n);
      65             : static GEN mfdihedralcusp(long N, GEN CHI, GEN vSP);
      66             : static long mfdihedralcuspdim(long N, GEN CHI, GEN vSP);
      67             : static GEN mfdihedralnew(long N, GEN CHI, GEN SP);
      68             : static GEN mfdihedral(long N);
      69             : static GEN mfdihedralall(long N);
      70             : static long mf1cuspdim(long N, GEN CHI, GEN vSP);
      71             : static long mf2dim_Nkchi(long N, long k, GEN CHI, ulong space);
      72             : static long mfdim_Nkchi(long N, long k, GEN CHI, long space);
      73             : static GEN charLFwtk(long N, long k, GEN CHI, long ord, long t);
      74             : static GEN mfeisensteingacx(GEN E,long w,GEN ga,long n,long prec);
      75             : static GEN mfgaexpansion(GEN mf, GEN F, GEN gamma, long n, long prec);
      76             : static GEN mfEHmat(long n, long r);
      77             : static GEN mfEHcoef(long r, long N);
      78             : static GEN mftobasis_i(GEN mf, GEN F);
      79             : 
      80             : static GEN
      81       35126 : mkgNK(GEN N, GEN k, GEN CHI, GEN P) { return mkvec4(N, k, CHI, P); }
      82             : static GEN
      83       14567 : mkNK(long N, long k, GEN CHI) { return mkgNK(stoi(N), stoi(k), CHI, pol_x(1)); }
      84             : GEN
      85        7749 : MF_get_CHI(GEN mf) { return gmael(mf,1,3); }
      86             : GEN
      87       18942 : MF_get_gN(GEN mf) { return gmael(mf,1,1); }
      88             : long
      89       18053 : MF_get_N(GEN mf) { return itou(MF_get_gN(mf)); }
      90             : GEN
      91       13069 : MF_get_gk(GEN mf) { return gmael(mf,1,2); }
      92             : long
      93        6496 : MF_get_k(GEN mf)
      94             : {
      95        6496 :   GEN gk = MF_get_gk(mf);
      96        6496 :   if (typ(gk)!=t_INT) pari_err_IMPL("half-integral weight");
      97        6496 :   return itou(gk);
      98             : }
      99             : long
     100         245 : MF_get_r(GEN mf)
     101             : {
     102         245 :   GEN gk = MF_get_gk(mf);
     103         245 :   if (typ(gk) == t_INT) pari_err_IMPL("integral weight");
     104         245 :   return itou(gel(gk, 1)) >> 1;
     105             : }
     106             : long
     107       13552 : MF_get_space(GEN mf) { return itos(gmael(mf,1,4)); }
     108             : GEN
     109        4088 : MF_get_E(GEN mf) { return gel(mf,2); }
     110             : GEN
     111       19999 : MF_get_S(GEN mf) { return gel(mf,3); }
     112             : GEN
     113        1561 : MF_get_basis(GEN mf) { return shallowconcat(gel(mf,2), gel(mf,3)); }
     114             : long
     115        5243 : MF_get_dim(GEN mf)
     116             : {
     117        5243 :   switch(MF_get_space(mf))
     118             :   {
     119         693 :     case mf_FULL:
     120         693 :       return lg(MF_get_S(mf)) - 1 + lg(MF_get_E(mf))-1;
     121         140 :     case mf_EISEN:
     122         140 :       return lg(MF_get_E(mf))-1;
     123        4410 :     default: /* mf_NEW, mf_CUSP, mf_OLD */
     124        4410 :       return lg(MF_get_S(mf)) - 1;
     125             :   }
     126             : }
     127             : GEN
     128        6958 : MFnew_get_vj(GEN mf) { return gel(mf,4); }
     129             : GEN
     130         490 : MFcusp_get_vMjd(GEN mf) { return gel(mf,4); }
     131             : GEN
     132        6615 : MF_get_M(GEN mf) { return gmael(mf,5,3); }
     133             : GEN
     134        4494 : MF_get_Minv(GEN mf) { return gmael(mf,5,2); }
     135             : GEN
     136        9758 : MF_get_Mindex(GEN mf) { return gmael(mf,5,1); }
     137             : 
     138             : /* ordinary gtocol forgets about initial 0s */
     139             : GEN
     140        2324 : sertocol(GEN S) { return gtocol0(S, -(lg(S) - 2 + valp(S))); }
     141             : /*******************************************************************/
     142             : /*     Linear algebra in cyclotomic fields (TODO: export this)     */
     143             : /*******************************************************************/
     144             : /* return r and split prime p giving projection Q(zeta_n) -> Fp, zeta -> r */
     145             : static ulong
     146        1155 : QabM_init(long n, ulong *p)
     147             : {
     148        1155 :   ulong pinit = 1000000007;
     149             :   forprime_t T;
     150        1155 :   if (n <= 1) { *p = pinit; return 0; }
     151        1148 :   u_forprime_arith_init(&T, pinit, ULONG_MAX, 1, n);
     152        1148 :   *p = u_forprime_next(&T);
     153        1148 :   return Flx_oneroot(ZX_to_Flx(polcyclo(n, 0), *p), *p);
     154             : }
     155             : static ulong
     156     8261337 : Qab_to_Fl(GEN P, ulong r, ulong p)
     157             : {
     158             :   ulong t;
     159             :   GEN den;
     160     8261337 :   P = Q_remove_denom(liftpol_shallow(P), &den);
     161     8261337 :   if (typ(P) == t_POL) { GEN Pp = ZX_to_Flx(P, p); t = Flx_eval(Pp, r, p); }
     162     8130521 :   else t = umodiu(P, p);
     163     8261337 :   if (den) t = Fl_div(t, umodiu(den, p), p);
     164     8261337 :   return t;
     165             : }
     166             : static GEN
     167       36820 : QabC_to_Flc(GEN C, ulong r, ulong p)
     168             : {
     169       36820 :   long i, l = lg(C);
     170       36820 :   GEN A = cgetg(l, t_VECSMALL);
     171     8074731 :   for (i = 1; i < l; i++) uel(A,i) = Qab_to_Fl(gel(C,i), r, p);
     172       36820 :   return A;
     173             : }
     174             : static GEN
     175         581 : QabM_to_Flm(GEN M, ulong r, ulong p)
     176             : {
     177             :   long i, l;
     178         581 :   GEN A = cgetg_copy(M, &l);
     179       37401 :   for (i = 1; i < l; i++)
     180       36820 :     gel(A, i) = QabC_to_Flc(gel(M, i), r, p);
     181         581 :   return A;
     182             : }
     183             : /* A a t_POL */
     184             : static GEN
     185        1442 : QabX_to_Flx(GEN A, ulong r, ulong p)
     186             : {
     187        1442 :   long i, l = lg(A);
     188        1442 :   GEN a = cgetg(l, t_VECSMALL);
     189        1442 :   a[1] = ((ulong)A[1])&VARNBITS;
     190      224616 :   for (i = 2; i < l; i++) uel(a,i) = Qab_to_Fl(gel(A,i), r, p);
     191        1442 :   return Flx_renormalize(a, l);
     192             : }
     193             : 
     194             : /* FIXME: remove */
     195             : static GEN
     196        1071 : ZabM_pseudoinv_i(GEN M, GEN P, long n, GEN *pv, GEN *den, int ratlift)
     197             : {
     198        1071 :   GEN v = ZabM_indexrank(M, P, n);
     199        1071 :   if (pv) *pv = v;
     200        1071 :   M = shallowmatextract(M,gel(v,1),gel(v,2));
     201        1071 :   return ratlift? ZabM_inv_ratlift(M, P, n, den): ZabM_inv(M, P, n, den);
     202             : }
     203             : 
     204             : /* M matrix with coeff in Q(\chi)), where Q(\chi) = Q(X)/(P) for
     205             :  * P = cyclotomic Phi_n. Assume M rational if n <= 2 */
     206             : static GEN
     207        1498 : QabM_ker(GEN M, GEN P, long n)
     208             : {
     209        1498 :   if (n <= 2) return QM_ker(M);
     210         378 :   return ZabM_ker(row_Q_primpart(liftpol_shallow(M)), P, n);
     211             : }
     212             : /* pseudo-inverse of M. FIXME: should replace QabM_pseudoinv */
     213             : static GEN
     214        1211 : QabM_pseudoinv_i(GEN M, GEN P, long n, GEN *pv, GEN *pden)
     215             : {
     216             :   GEN cM, Mi;
     217        1211 :   if (n <= 2)
     218             :   {
     219        1071 :     M = Q_primitive_part(M, &cM);
     220        1071 :     Mi = ZM_pseudoinv(M, pv, pden); /* M^(-1) = Mi / (cM * den) */
     221             :   }
     222             :   else
     223             :   {
     224         140 :     M = Q_primitive_part(liftpol_shallow(M), &cM);
     225         140 :     Mi = ZabM_pseudoinv(M, P, n, pv, pden);
     226             :   }
     227        1211 :   *pden = mul_content(*pden, cM);
     228        1211 :   return Mi;
     229             : }
     230             : /* FIXME: delete */
     231             : static GEN
     232         973 : QabM_pseudoinv(GEN M, GEN P, long n, GEN *pv, GEN *pden)
     233             : {
     234         973 :   GEN Mi = QabM_pseudoinv_i(M, P, n, pv, pden);
     235         973 :   return P? gmodulo(Mi, P): Mi;
     236             : }
     237             : 
     238             : static GEN
     239       10087 : QabM_indexrank(GEN M, GEN P, long n)
     240             : {
     241             :   GEN z;
     242       10087 :   if (n <= 2)
     243             :   {
     244        8946 :     M = vec_Q_primpart(M);
     245        8946 :     z = ZM_indexrank(M); /* M^(-1) = Mi / (cM * den) */
     246             :   }
     247             :   else
     248             :   {
     249        1141 :     M = vec_Q_primpart(liftpol_shallow(M));
     250        1141 :     z = ZabM_indexrank(M, P, n);
     251             :   }
     252       10087 :   return z;
     253             : }
     254             : 
     255             : /*********************************************************************/
     256             : /*                    Simple arithmetic functions                    */
     257             : /*********************************************************************/
     258             : /* TODO: most of these should be exported and used in ifactor1.c */
     259             : /* phi(n) */
     260             : static ulong
     261      105287 : myeulerphiu(ulong n)
     262             : {
     263             :   pari_sp av;
     264      105287 :   if (n == 1) return 1;
     265       87003 :   av = avma; return gc_ulong(av, eulerphiu_fact(myfactoru(n)));
     266             : }
     267             : static long
     268       65688 : mymoebiusu(ulong n)
     269             : {
     270             :   pari_sp av;
     271       65688 :   if (n == 1) return 1;
     272       54173 :   av = avma; return gc_long(av, moebiusu_fact(myfactoru(n)));
     273             : }
     274             : 
     275             : static long
     276        2891 : mynumdivu(long N)
     277             : {
     278             :   pari_sp av;
     279        2891 :   if (N == 1) return 1;
     280        2786 :   av = avma; return gc_long(av, numdivu_fact(myfactoru(N)));
     281             : }
     282             : 
     283             : /* N\prod_{p|N} (1+1/p) */
     284             : static long
     285      365491 : mypsiu(ulong N)
     286             : {
     287             :   pari_sp av;
     288             :   GEN P;
     289             :   long j, l, a;
     290      365491 :   if (N == 1) return 1;
     291      286489 :   av = avma; P = gel(myfactoru(N), 1); l = lg(P);
     292      680295 :   for (a = N, j = 1; j < l; j++) a += a / P[j];
     293      286489 :   return gc_long(av, a);
     294             : }
     295             : /* write n = mf^2. Return m, set f. */
     296             : static ulong
     297         274 : mycore(ulong n, long *pf)
     298             : {
     299         274 :   pari_sp av = avma;
     300         274 :   GEN fa = myfactoru(n), P = gel(fa,1), E = gel(fa,2);
     301         274 :   long i, l = lg(P), m = 1, f = 1;
     302        1127 :   for (i = 1; i < l; i++)
     303             :   {
     304         853 :     long j, p = P[i], e = E[i];
     305         853 :     if (e & 1) m *= p;
     306        1601 :     for (j = 2; j <= e; j+=2) f *= p;
     307             :   }
     308         274 :   *pf = f; return gc_long(av,m);
     309             : }
     310             : 
     311             : /* fa = factorization of -D > 0, return -D0 > 0 (where D0 is fundamental) */
     312             : static long
     313     9928564 : corediscs_fact(GEN fa)
     314             : {
     315     9928564 :   GEN P = gel(fa,1), E = gel(fa,2);
     316     9928564 :   long i, l = lg(P), m = 1;
     317    32817387 :   for (i = 1; i < l; i++)
     318             :   {
     319    22888823 :     long p = P[i], e = E[i];
     320    22888823 :     if (e & 1) m *= p;
     321             :   }
     322     9928564 :   if ((m&3L) != 3) m <<= 2;
     323     9928564 :   return m;
     324             : }
     325             : static long
     326        6748 : mubeta(long n)
     327             : {
     328        6748 :   pari_sp av = avma;
     329        6748 :   GEN E = gel(myfactoru(n), 2);
     330        6748 :   long i, s = 1, l = lg(E);
     331       13986 :   for (i = 1; i < l; i++)
     332             :   {
     333        7238 :     long e = E[i];
     334        7238 :     if (e >= 3) return gc_long(av,0);
     335        7238 :     if (e == 1) s *= -2;
     336             :   }
     337        6748 :   return gc_long(av,s);
     338             : }
     339             : 
     340             : /* n = n1*n2, n1 = ppo(n, m); return mubeta(n1)*moebiusu(n2).
     341             :  * N.B. If n from newt_params we, in fact, never return 0 */
     342             : static long
     343     6620248 : mubeta2(long n, long m)
     344             : {
     345     6620248 :   pari_sp av = avma;
     346     6620248 :   GEN fa = myfactoru(n), P = gel(fa,1), E = gel(fa,2);
     347     6620248 :   long i, s = 1, l = lg(P);
     348    12948900 :   for (i = 1; i < l; i++)
     349             :   {
     350     6328652 :     long p = P[i], e = E[i];
     351     6328652 :     if (m % p)
     352             :     { /* p^e in n1 */
     353     5361707 :       if (e >= 3) return gc_long(av,0);
     354     5361707 :       if (e == 1) s *= -2;
     355             :     }
     356             :     else
     357             :     { /* in n2 */
     358      966945 :       if (e >= 2) return gc_long(av,0);
     359      966945 :       s = -s;
     360             :     }
     361             :   }
     362     6620248 :   return gc_long(av,s);
     363             : }
     364             : 
     365             : /* write N = prod p^{ep} and n = df^2, d squarefree.
     366             :  * set g  = ppo(gcd(sqfpart(N), f), FC)
     367             :  *     N2 = prod p^if(e==1 || p|n, ep-1, ep-2) */
     368             : static void
     369     1730467 : newt_params(long N, long n, long FC, long *pg, long *pN2)
     370             : {
     371     1730467 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
     372     1730467 :   long i, g = 1, N2 = 1, l = lg(P);
     373     4534090 :   for (i = 1; i < l; i++)
     374             :   {
     375     2803623 :     long p = P[i], e = E[i];
     376     2803623 :     if (e == 1)
     377     2491083 :     { if (FC % p && n % (p*p) == 0) g *= p; }
     378             :     else
     379      312540 :       N2 *= upowuu(p,(n % p)? e-2: e-1);
     380             :   }
     381     1730467 :   *pg = g; *pN2 = N2;
     382     1730467 : }
     383             : /* simplified version of newt_params for n = 1 (newdim) */
     384             : static void
     385       39095 : newd_params(long N, long *pN2)
     386             : {
     387       39095 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
     388       39095 :   long i, N2 = 1, l = lg(P);
     389       98028 :   for (i = 1; i < l; i++)
     390             :   {
     391       58933 :     long p = P[i], e = E[i];
     392       58933 :     if (e > 2) N2 *= upowuu(p, e-2);
     393             :   }
     394       39095 :   *pN2 = N2;
     395       39095 : }
     396             : 
     397             : static long
     398          21 : newd_params2(long N)
     399             : {
     400          21 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
     401          21 :   long i, N2 = 1, l = lg(P);
     402          56 :   for (i = 1; i < l; i++)
     403             :   {
     404          35 :     long p = P[i], e = E[i];
     405          35 :     if (e >= 2) N2 *= upowuu(p, e);
     406             :   }
     407          21 :   return N2;
     408             : }
     409             : 
     410             : /*******************************************************************/
     411             : /*   Relative trace between cyclotomic fields (TODO: export this)  */
     412             : /*******************************************************************/
     413             : /* g>=1; return g * prod_{p | g, (p,q) = 1} (1-1/p) */
     414             : static long
     415       36862 : phipart(long g, long q)
     416             : {
     417       36862 :   if (g > 1)
     418             :   {
     419       19663 :     GEN P = gel(myfactoru(g), 1);
     420       19663 :     long i, l = lg(P);
     421       40180 :     for (i = 1; i < l; i++) { long p = P[i]; if (q % p) g -= g / p; }
     422             :   }
     423       36862 :   return g;
     424             : }
     425             : /* Set s,v s.t. Trace(zeta_N^k) from Q(zeta_N) to Q(\zeta_N) = s * zeta_M^v
     426             :  * With k > 0, N = M*d and N, M != 2 mod 4 */
     427             : static long
     428       84735 : tracerelz(long *pv, long d, long M, long k)
     429             : {
     430             :   long s, g, q, muq;
     431       84735 :   if (d == 1) { *pv = k; return 1; }
     432       65597 :   *pv = 0; g = ugcd(k, d); q = d / g;
     433       65597 :   muq = mymoebiusu(q); if (!muq) return 0;
     434       47166 :   if (M != 1)
     435             :   {
     436       37821 :     long v = Fl_invsafe(q % M, M);
     437       37821 :     if (!v) return 0;
     438       27517 :     *pv = (v * (k/g)) % M;
     439             :   }
     440       36862 :   s = phipart(g, M*q); if (muq < 0) s = -s;
     441       36862 :   return s;
     442             : }
     443             : /* Pi = polcyclo(i), i = m or n. Let Ki = Q(zeta_i), initialize Tr_{Kn/Km} */
     444             : GEN
     445       33922 : Qab_trace_init(long n, long m, GEN Pn, GEN Pm)
     446             : {
     447             :   long a, i, j, N, M, vt, d, D;
     448             :   GEN T, G;
     449             : 
     450       33922 :   if (m == n || n <= 2) return mkvec(Pm);
     451       16548 :   vt = varn(Pn);
     452       16548 :   d = degpol(Pn);
     453             :   /* if (N != n) zeta_N = zeta_n^2 and zeta_n = - zeta_N^{(N+1)/2} */
     454       16548 :   N = ((n & 3) == 2)? n >> 1: n;
     455       16548 :   M = ((m & 3) == 2)? m >> 1: m; /* M | N | n */
     456       16548 :   a = N / M;
     457       16548 :   T = const_vec(d, NULL);
     458       16548 :   D = d / degpol(Pm); /* relative degree */
     459       16548 :   if (D == 1) G = NULL;
     460             :   else
     461             :   { /* zeta_M = zeta_n^A; s_j(zeta_M) = zeta_M <=> j = 1 (mod J) */
     462       15274 :     long lG, A = (N == n)? a: (a << 1), J = n / ugcd(n, A);
     463       15274 :     G = coprimes_zv(n);
     464      150241 :     for (j = lG = 1; j < n; j += J)
     465      134967 :       if (G[j]) G[lG++] = j;
     466       15274 :     setlg(G, lG); /* Gal(Q(zeta_n) / Q(zeta_m)) */
     467             :   }
     468       16548 :   T = const_vec(d, NULL);
     469       16548 :   gel(T,1) = utoipos(D); /* Tr 1 */
     470      140112 :   for (i = 1; i < d; i++)
     471             :   { /* if n = 2N, zeta_n^i = (-1)^i zeta_N^k */
     472             :     long s, v, k;
     473             :     GEN t;
     474             : 
     475      123564 :     if (gel(T, i+1)) continue;
     476       84735 :     k = (N == n)? i: ((odd(i)? i + N: i) >> 1);
     477       84735 :     if ((s = tracerelz(&v, a, M, k)))
     478             :     {
     479       56000 :       if (m != M) v *= 2;/* Tr = s * zeta_m^v */
     480       56000 :       if (n != N && odd(i)) s = -s;
     481       56000 :       t = Qab_Czeta(v, m, stoi(s), vt);
     482             :     }
     483             :     else
     484       28735 :       t = gen_0;
     485             :     /* t = Tr_{Kn/Km} zeta_n^i; fill using Galois action */
     486       84735 :     if (!G)
     487       19138 :       gel(T, i + 1) = t;
     488             :     else
     489      370811 :       for (j = 1; j <= D; j++)
     490             :       {
     491      305214 :         long z = Fl_mul(i,G[j], n);
     492      305214 :         if (z < d) gel(T, z + 1) = t;
     493             :       }
     494             :   }
     495       16548 :   return mkvec3(Pm, Pn, T);
     496             : }
     497             : /* x a t_POL modulo Phi_n */
     498             : static GEN
     499       70931 : tracerel_i(GEN T, GEN x)
     500             : {
     501       70931 :   long k, l = lg(x);
     502             :   GEN S;
     503       70931 :   if (l == 2) return gen_0;
     504       70931 :   S = gmul(gel(T,1), gel(x,2));
     505      257390 :   for (k = 3; k < l; k++) S = gadd(S, gmul(gel(T,k-1), gel(x,k)));
     506       70931 :   return S;
     507             : }
     508             : static GEN
     509      213745 : tracerel(GEN a, GEN v, GEN z)
     510             : {
     511      213745 :   a = liftpol_shallow(a);
     512      213745 :   a = simplify_shallow(z? gmul(z,a): a);
     513      213745 :   if (typ(a) == t_POL)
     514             :   {
     515       70931 :     GEN T = gel(v,3);
     516       70931 :     long degrel = itou(gel(T,1));
     517       70931 :     a = tracerel_i(T, RgX_rem(a, gel(v,2)));
     518       70931 :     if (degrel != 1) a = gdivgs(a, degrel);
     519       70931 :     if (typ(a) == t_POL) a = RgX_rem(a, gel(v,1));
     520             :   }
     521      213745 :   return a;
     522             : }
     523             : static GEN
     524        6461 : tracerel_z(GEN v, long t)
     525             : {
     526        6461 :   GEN Pn = gel(v,2);
     527        6461 :   return t? pol_xn(t, varn(Pn)): NULL;
     528             : }
     529             : /* v = Qab_trace_init(n,m); x is a t_VEC of polmodulo Phi_n; Kn = Q(zeta_n)
     530             :  * [Kn:Km]^(-1) Tr_{Kn/Km} (zeta_n^t * x); 0 <= t < [Kn:Km] */
     531             : GEN
     532           0 : Qab_tracerel(GEN v, long t, GEN a)
     533             : {
     534           0 :   if (lg(v) != 4) return a; /* => t = 0 */
     535           0 :   return tracerel(a, v, tracerel_z(v, t));
     536             : }
     537             : GEN
     538       15211 : QabV_tracerel(GEN v, long t, GEN x)
     539             : {
     540             :   GEN z;
     541       15211 :   if (lg(v) != 4) return x; /* => t = 0 */
     542        6461 :   z = tracerel_z(v, t);
     543      220206 :   pari_APPLY_same(tracerel(gel(x,i), v, z));
     544             : }
     545             : GEN
     546         133 : QabM_tracerel(GEN v, long t, GEN x)
     547             : {
     548         133 :   if (lg(v) != 4) return x;
     549         105 :   pari_APPLY_same(QabV_tracerel(v, t, gel(x,i)));
     550             : }
     551             : 
     552             : /* C*zeta_o^k mod X^o - 1 */
     553             : static GEN
     554     2114847 : Qab_Czeta(long k, long o, GEN C, long vt)
     555             : {
     556     2114847 :   if (!k) return C;
     557     1404564 :   if (!odd(o))
     558             :   { /* optimization: reduce max degree by a factor 2 for free */
     559     1357286 :     o >>= 1;
     560     1357286 :     if (k >= o) { k -= o; C = gneg(C); if (!k) return C; }
     561             :   }
     562     1072631 :   return monomial(C, k, vt);
     563             : }
     564             : /* zeta_o^k */
     565             : static GEN
     566      165004 : Qab_zeta(long k, long o, long vt) { return Qab_Czeta(k, o, gen_1, vt); }
     567             : 
     568             : /*              Operations on Dirichlet characters                       */
     569             : 
     570             : /* A Dirichlet character can be given in GP in different formats, but in this
     571             :  * package, it will be a vector CHI=[G,chi,ord], where G is the (Z/MZ)^* to
     572             :  * which the character belongs, chi is the character in Conrey format, ord is
     573             :  * the order */
     574             : 
     575             : static GEN
     576     3642282 : gmfcharorder(GEN CHI) { return gel(CHI, 3); }
     577             : long
     578     3605714 : mfcharorder(GEN CHI) { return itou(gmfcharorder(CHI)); }
     579             : static long
     580        2590 : mfcharistrivial(GEN CHI) { return !CHI || mfcharorder(CHI) == 1; }
     581             : static GEN
     582     1519833 : gmfcharmodulus(GEN CHI) { return gmael3(CHI, 1, 1, 1); }
     583             : long
     584     1519833 : mfcharmodulus(GEN CHI) { return itou(gmfcharmodulus(CHI)); }
     585             : GEN
     586      551047 : mfcharpol(GEN CHI) { return gel(CHI,4); }
     587             : 
     588             : /* vz[i+1] = image of (zeta_o)^i in Fp */
     589             : static ulong
     590      219611 : Qab_Czeta_Fl(long k, GEN vz, ulong C, ulong p)
     591             : {
     592             :   long o;
     593      219611 :   if (!k) return C;
     594      148484 :   o = lg(vz)-2;
     595      148484 :   if ((k << 1) == o) return Fl_neg(C,p);
     596      123123 :   return Fl_mul(C, vz[k+1], p);
     597             : }
     598             : 
     599             : static long
     600     2449265 : znchareval_i(GEN CHI, long n, GEN ord)
     601     2449265 : { return itos(znchareval(gel(CHI,1), gel(CHI,2), stoi(n), ord)); }
     602             : 
     603             : /* n coprime with the modulus of CHI */
     604             : static GEN
     605       13468 : mfchareval(GEN CHI, long n)
     606             : {
     607       13468 :   GEN Pn, C, go = gmfcharorder(CHI);
     608       13468 :   long k, o = go[2];
     609       13468 :   if (o == 1) return gen_1;
     610        6958 :   k = znchareval_i(CHI, n, go);
     611        6958 :   Pn = mfcharpol(CHI);
     612        6958 :   C = Qab_zeta(k, o, varn(Pn));
     613        6958 :   if (typ(C) != t_POL) return C;
     614        5299 :   return gmodulo(C, Pn);
     615             : }
     616             : /* d a multiple of ord(CHI); n coprime with char modulus;
     617             :  * return x s.t. CHI(n) = \zeta_d^x] */
     618             : static long
     619     3487554 : mfcharevalord(GEN CHI, long n, long d)
     620             : {
     621     3487554 :   if (mfcharorder(CHI) == 1) return 0;
     622     2438611 :   return znchareval_i(CHI, n, utoi(d));
     623             : }
     624             : 
     625             : /* G a znstar, L a Conrey log: return a 'mfchar' */
     626             : static GEN
     627      372946 : mfcharGL(GEN G, GEN L)
     628             : {
     629      372946 :   GEN o = zncharorder(G,L);
     630      372946 :   long ord = itou(o), vt = fetch_user_var("t");
     631      372946 :   return mkvec4(G, L, o, polcyclo(ord,vt));
     632             : }
     633             : static GEN
     634        5495 : mfchartrivial()
     635        5495 : { return mfcharGL(znstar0(gen_1,1), cgetg(1,t_COL)); }
     636             : /* convert a generic character into an 'mfchar' */
     637             : static GEN
     638        3969 : get_mfchar(GEN CHI)
     639             : {
     640             :   GEN G, L;
     641        3969 :   if (typ(CHI) != t_VEC) CHI = znchar(CHI);
     642             :   else
     643             :   {
     644         889 :     long l = lg(CHI);
     645         889 :     if ((l != 3 && l != 5) || !checkznstar_i(gel(CHI,1)))
     646           7 :       pari_err_TYPE("checkNF [chi]", CHI);
     647         882 :     if (l == 5) return CHI;
     648             :   }
     649        3899 :   G = gel(CHI,1);
     650        3899 :   L = gel(CHI,2); if (typ(L) != t_COL) L = znconreylog(G,L);
     651        3899 :   return mfcharGL(G, L);
     652             : }
     653             : 
     654             : /* parse [N], [N,k], [N,k,CHI]. If 'joker' is set, allow wildcard for CHI */
     655             : static GEN
     656        9107 : checkCHI(GEN NK, long N, int joker)
     657             : {
     658             :   GEN CHI;
     659        9107 :   if (lg(NK) == 3)
     660         637 :     CHI = mfchartrivial();
     661             :   else
     662             :   {
     663             :     long i, l;
     664        8470 :     CHI = gel(NK,3); l = lg(CHI);
     665        8470 :     if (isintzero(CHI) && joker)
     666        4116 :       CHI = NULL; /* all character orbits */
     667        4354 :     else if (isintm1(CHI) && joker > 1)
     668        2373 :       CHI = gen_m1; /* sum over all character orbits */
     669        2114 :     else if ((typ(CHI) == t_VEC &&
     670         217 :              (l == 1 || l != 3 || !checkznstar_i(gel(CHI,1)))) && joker)
     671             :     {
     672         133 :       CHI = shallowtrans(CHI); /* list of characters */
     673         952 :       for (i = 1; i < l; i++) gel(CHI,i) = get_mfchar(gel(CHI,i));
     674             :     }
     675             :     else
     676             :     {
     677        1848 :       CHI = get_mfchar(CHI); /* single char */
     678        1848 :       if (N % mfcharmodulus(CHI)) pari_err_TYPE("checkNF [chi]", NK);
     679             :     }
     680             :   }
     681        9093 :   return CHI;
     682             : }
     683             : /* support half-integral weight */
     684             : static void
     685        9114 : checkNK2(GEN NK, long *N, long *nk, long *dk, GEN *CHI, int joker)
     686             : {
     687        9114 :   long l = lg(NK);
     688             :   GEN T;
     689        9114 :   if (typ(NK) != t_VEC || l < 3 || l > 4) pari_err_TYPE("checkNK", NK);
     690        9114 :   T = gel(NK,1); if (typ(T) != t_INT) pari_err_TYPE("checkNF [N]", NK);
     691        9114 :   *N = itos(T); if (*N <= 0) pari_err_TYPE("checkNF [N <= 0]", NK);
     692        9114 :   T = gel(NK,2);
     693        9114 :   switch(typ(T))
     694             :   {
     695        5740 :     case t_INT:  *nk = itos(T); *dk = 1; break;
     696        3367 :     case t_FRAC:
     697        3367 :       *nk = itos(gel(T,1));
     698        3367 :       *dk = itou(gel(T,2)); if (*dk == 2) break;
     699           7 :     default: pari_err_TYPE("checkNF [k]", NK);
     700             :   }
     701        9107 :   *CHI = checkCHI(NK, *N, joker);
     702        9093 : }
     703             : /* don't support half-integral weight */
     704             : static void
     705         133 : checkNK(GEN NK, long *N, long *k, GEN *CHI, int joker)
     706             : {
     707             :   long d;
     708         133 :   checkNK2(NK, N, k, &d, CHI, joker);
     709         133 :   if (d != 1) pari_err_TYPE("checkNF [k]", NK);
     710         133 : }
     711             : 
     712             : static GEN
     713        4872 : mfchargalois(long N, int odd, GEN flagorder)
     714             : {
     715        4872 :   GEN G = znstar0(utoi(N), 1), L = chargalois(G, flagorder);
     716        4872 :   long l = lg(L), i, j;
     717      113526 :   for (i = j = 1; i < l; i++)
     718             :   {
     719      108654 :     GEN chi = znconreyfromchar(G, gel(L,i));
     720      108654 :     if (zncharisodd(G,chi) == odd) gel(L,j++) = mfcharGL(G,chi);
     721             :   }
     722        4872 :   setlg(L, j); return L;
     723             : }
     724             : /* possible characters for nontrivial S_1(N, chi) */
     725             : static GEN
     726        1729 : mf1chars(long N, GEN vCHI)
     727             : {
     728        1729 :   if (vCHI) return vCHI; /*do not filter, user knows best*/
     729             :   /* Tate's theorem */
     730        1659 :   return mfchargalois(N, 1, uisprime(N)? mkvecsmall2(2,4): NULL);
     731             : }
     732             : static GEN
     733        3255 : mfchars(long N, long k, long dk, GEN vCHI)
     734        3255 : { return vCHI? vCHI: mfchargalois(N, (dk == 2)? 0: (k & 1), NULL); }
     735             : 
     736             : /* wrappers from mfchar to znchar */
     737             : static long
     738       67900 : mfcharparity(GEN CHI)
     739             : {
     740       67900 :   if (!CHI) return 1;
     741       67900 :   return zncharisodd(gel(CHI,1), gel(CHI,2)) ? -1 : 1;
     742             : }
     743             : /* if CHI is primitive, return CHI itself, not a copy */
     744             : static GEN
     745       72933 : mfchartoprimitive(GEN CHI, long *pF)
     746             : {
     747             :   pari_sp av;
     748             :   GEN chi, F;
     749       72933 :   if (!CHI) { if (pF) *pF = 1; return mfchartrivial(); }
     750       72933 :   av = avma; F = znconreyconductor(gel(CHI,1), gel(CHI,2), &chi);
     751       72933 :   if (typ(F) == t_INT) set_avma(av);
     752             :   else
     753             :   {
     754        7812 :     CHI = leafcopy(CHI);
     755        7812 :     gel(CHI,1) = znstar0(F, 1);
     756        7812 :     gel(CHI,2) = chi;
     757             :   }
     758       72933 :   if (pF) *pF = mfcharmodulus(CHI);
     759       72933 :   return CHI;
     760             : }
     761             : static long
     762      395542 : mfcharconductor(GEN CHI)
     763             : {
     764      395542 :   pari_sp av = avma;
     765      395542 :   GEN res = znconreyconductor(gel(CHI,1), gel(CHI,2), NULL);
     766      395542 :   if (typ(res) == t_VEC) res = gel(res, 1);
     767      395542 :   return gc_long(av, itos(res));
     768             : }
     769             : 
     770             : /*                      Operations on mf closures                    */
     771             : static GEN
     772       59353 : tagparams(long t, GEN NK) { return mkvec2(mkvecsmall(t), NK); }
     773             : static GEN
     774        1127 : lfuntag(long t, GEN x) { return mkvec2(mkvecsmall(t), x); }
     775             : static GEN
     776          56 : tag0(long t, GEN NK) { retmkvec(tagparams(t,NK)); }
     777             : static GEN
     778        9856 : tag(long t, GEN NK, GEN x) { retmkvec2(tagparams(t,NK), x); }
     779             : static GEN
     780       34230 : tag2(long t, GEN NK, GEN x, GEN y) { retmkvec3(tagparams(t,NK), x,y); }
     781             : static GEN
     782       15085 : tag3(long t, GEN NK, GEN x,GEN y,GEN z) { retmkvec4(tagparams(t,NK), x,y,z); }
     783             : static GEN
     784           0 : tag4(long t, GEN NK, GEN x,GEN y,GEN z,GEN a)
     785           0 : { retmkvec5(tagparams(t,NK), x,y,z,a); }
     786             : /* is F a "modular form" ? */
     787             : int
     788       16737 : checkmf_i(GEN F)
     789       16737 : { return typ(F) == t_VEC
     790       16100 :     && lg(F) > 1 && typ(gel(F,1)) == t_VEC
     791       11851 :     && lg(gel(F,1)) == 3
     792       11690 :     && typ(gmael(F,1,1)) == t_VECSMALL
     793       32837 :     && typ(gmael(F,1,2)) == t_VEC; }
     794      220731 : long mf_get_type(GEN F) { return gmael(F,1,1)[1]; }
     795      176428 : GEN mf_get_gN(GEN F) { return gmael3(F,1,2,1); }
     796      132244 : GEN mf_get_gk(GEN F) { return gmael3(F,1,2,2); }
     797             : /* k - 1/2, assume k in 1/2 + Z */
     798         413 : long mf_get_r(GEN F) { return itou(gel(mf_get_gk(F),1)) >> 1; }
     799      113169 : long mf_get_N(GEN F) { return itou(mf_get_gN(F)); }
     800       67935 : long mf_get_k(GEN F)
     801             : {
     802       67935 :   GEN gk = mf_get_gk(F);
     803       67935 :   if (typ(gk)!=t_INT) pari_err_IMPL("half-integral weight");
     804       67935 :   return itou(gk);
     805             : }
     806       58751 : GEN mf_get_CHI(GEN F) { return gmael3(F,1,2,3); }
     807       22981 : GEN mf_get_field(GEN F) { return gmael3(F,1,2,4); }
     808       17815 : GEN mf_get_NK(GEN F) { return gmael(F,1,2); }
     809             : static void
     810         504 : mf_setfield(GEN f, GEN P)
     811             : {
     812         504 :   gel(f,1) = leafcopy(gel(f,1));
     813         504 :   gmael(f,1,2) = leafcopy(gmael(f,1,2));
     814         504 :   gmael3(f,1,2,4) = P;
     815         504 : }
     816             : 
     817             : /* UTILITY FUNCTIONS */
     818             : GEN
     819        8183 : mftocol(GEN F, long lim, long d)
     820        8183 : { GEN c = mfcoefs_i(F, lim, d); settyp(c,t_COL); return c; }
     821             : GEN
     822        1932 : mfvectomat(GEN vF, long lim, long d)
     823             : {
     824        1932 :   long j, l = lg(vF);
     825        1932 :   GEN M = cgetg(l, t_MAT);
     826        9324 :   for (j = 1; j < l; j++) gel(M,j) = mftocol(gel(vF,j), lim, d);
     827        1932 :   return M;
     828             : }
     829             : 
     830             : static GEN
     831        4571 : RgV_to_ser_full(GEN x) { return RgV_to_ser(x, 0, lg(x)+1); }
     832             : /* TODO: delete */
     833             : static GEN
     834         644 : mfcoefsser(GEN F, long n) { return RgV_to_ser_full(mfcoefs_i(F,n,1)); }
     835             : static GEN
     836         833 : sertovecslice(GEN S, long n)
     837             : {
     838         833 :   GEN v = gtovec0(S, -(lg(S) - 2 + valp(S)));
     839         833 :   long l = lg(v), n2 = n + 2;
     840         833 :   if (l < n2) pari_err_BUG("sertovecslice [n too large]");
     841         833 :   return (l == n2)? v: vecslice(v, 1, n2-1);
     842             : }
     843             : 
     844             : /* a, b two RgV of the same length, multiply as truncated power series */
     845             : static GEN
     846        3318 : RgV_mul_RgXn(GEN a, GEN b)
     847             : {
     848        3318 :   long n = lg(a)-1;
     849             :   GEN c;
     850        3318 :   a = RgV_to_RgX(a,0);
     851        3318 :   b = RgV_to_RgX(b,0); c = RgXn_mul(a, b, n);
     852        3318 :   c = RgX_to_RgC(c,n); settyp(c,t_VEC); return c;
     853             : }
     854             : /* divide as truncated power series */
     855             : static GEN
     856         357 : RgV_div_RgXn(GEN a, GEN b)
     857             : {
     858         357 :   long n = lg(a)-1;
     859             :   GEN c;
     860         357 :   a = RgV_to_RgX(a,0);
     861         357 :   b = RgV_to_RgX(b,0); c = RgXn_mul(a, RgXn_inv(b,n), n);
     862         357 :   c = RgX_to_RgC(c,n); settyp(c,t_VEC); return c;
     863             : }
     864             : /* a^b */
     865             : static GEN
     866         105 : RgV_pows_RgXn(GEN a, long b)
     867             : {
     868         105 :   long n = lg(a)-1;
     869             :   GEN c;
     870         105 :   a = RgV_to_RgX(a,0);
     871         105 :   if (b < 0) { a = RgXn_inv(a, n); b = -b; }
     872         105 :   c = RgXn_powu_i(a,b,n);
     873         105 :   c = RgX_to_RgC(c,n); settyp(c,t_VEC); return c;
     874             : }
     875             : 
     876             : /* assume lg(V) >= n*d + 2 */
     877             : static GEN
     878        7938 : c_deflate(long n, long d, GEN v)
     879             : {
     880        7938 :   long i, id, l = n+2;
     881             :   GEN w;
     882        7938 :   if (d == 1) return lg(v) == l ? v: vecslice(v, 1, l-1);
     883         427 :   w = cgetg(l, typ(v));
     884       10654 :   for (i = id = 1; i < l; i++, id += d) gel(w, i) = gel(v, id);
     885         427 :   return w;
     886             : }
     887             : 
     888             : static void
     889          14 : err_cyclo(void)
     890          14 : { pari_err_IMPL("changing cyclotomic fields in mf"); }
     891             : /* Q(zeta_a) = Q(zeta_b) ? */
     892             : static int
     893         616 : same_cyc(long a, long b)
     894         616 : { return (a == b) || (odd(a) && b == (a<<1)) || (odd(b) && a == (b<<1)); }
     895             : /* need to combine elements in Q(CHI1) and Q(CHI2) with result in Q(CHI),
     896             :  * CHI = CHI1 * CHI2 or CHI / CHI2 times some character of order 2 */
     897             : static GEN
     898        2632 : chicompat(GEN CHI, GEN CHI1, GEN CHI2)
     899             : {
     900        2632 :   long o1 = mfcharorder(CHI1);
     901        2632 :   long o2 = mfcharorder(CHI2), O, o;
     902             :   GEN T1, T2, P, Po;
     903        2632 :   if (o1 <= 2 && o2 <= 2) return NULL;
     904         623 :   o = mfcharorder(CHI);
     905         623 :   Po = mfcharpol(CHI);
     906         623 :   P = mfcharpol(CHI1);
     907         623 :   if (o1 == o2)
     908             :   {
     909          21 :     if (o1 == o) return NULL;
     910          14 :     if (!same_cyc(o1,o)) err_cyclo();
     911           0 :     return mkvec4(P, gen_1,gen_1, Qab_trace_init(o1, o, P, Po));
     912             :   }
     913         602 :   O = ulcm(o1, o2);
     914         602 :   if (!same_cyc(O,o)) err_cyclo();
     915         602 :   if (O != o1) P = (O == o2)? mfcharpol(CHI2): polcyclo(O, varn(P));
     916         602 :   T1 = o1 <= 2? gen_1: utoipos(O / o1);
     917         602 :   T2 = o2 <= 2? gen_1: utoipos(O / o2);
     918         602 :   return mkvec4(P, T1, T2, O == o? gen_1: Qab_trace_init(O, o, P, Po));
     919             : }
     920             : /* *F a vector of cyclotomic numbers */
     921             : static void
     922           7 : compatlift(GEN *F, long o, GEN P)
     923             : {
     924             :   long i, l;
     925           7 :   GEN f = *F, g = cgetg_copy(f,&l);
     926          56 :   for (i = 1; i < l; i++)
     927             :   {
     928          49 :     GEN fi = lift_shallow(gel(f,i));
     929          49 :     gel(g,i) = gmodulo(typ(fi)==t_POL? RgX_inflate(fi,o): fi, P);
     930             :   }
     931           7 :   *F = g;
     932           7 : }
     933             : static void
     934         651 : chicompatlift(GEN T, GEN *F, GEN *G)
     935             : {
     936         651 :   long o1 = itou(gel(T,2)), o2 = itou(gel(T,3));
     937         651 :   GEN P = gel(T,1);
     938         651 :   if (o1 != 1) compatlift(F, o1, P);
     939         651 :   if (o2 != 1 && G) compatlift(G, o2, P);
     940         651 : }
     941             : static GEN
     942         651 : chicompatfix(GEN T, GEN F)
     943             : {
     944         651 :   GEN V = gel(T,4);
     945         651 :   if (typ(V) == t_VEC) F = gmodulo(QabV_tracerel(V, 0, F), gel(V,1));
     946         651 :   return F;
     947             : }
     948             : 
     949             : static GEN
     950         637 : c_mul(long n, long d, GEN S)
     951             : {
     952         637 :   pari_sp av = avma;
     953         637 :   long nd = n*d;
     954         637 :   GEN F = gel(S,2), G = gel(S,3);
     955         637 :   F = mfcoefs_i(F, nd, 1);
     956         637 :   G = mfcoefs_i(G, nd, 1);
     957         637 :   if (lg(S) == 5) chicompatlift(gel(S,4),&F,&G);
     958         637 :   F = c_deflate(n, d, RgV_mul_RgXn(F,G));
     959         637 :   if (lg(S) == 5) F = chicompatfix(gel(S,4), F);
     960         637 :   return gerepilecopy(av, F);
     961             : }
     962             : static GEN
     963         105 : c_pow(long n, long d, GEN S)
     964             : {
     965         105 :   pari_sp av = avma;
     966         105 :   long nd = n*d;
     967         105 :   GEN F = gel(S,2), a = gel(S,3), f = mfcoefs_i(F,nd,1);
     968         105 :   if (lg(S) == 5) chicompatlift(gel(S,4),&F, NULL);
     969         105 :   f = RgV_pows_RgXn(f, itos(a));
     970         105 :   f = c_deflate(n, d, f);
     971         105 :   if (lg(S) == 5) f = chicompatfix(gel(S,4), f);
     972         105 :   return gerepilecopy(av, f);
     973             : }
     974             : 
     975             : /* F * Theta */
     976             : static GEN
     977         406 : mfmultheta(GEN F)
     978             : {
     979         406 :   if (typ(mf_get_gk(F)) == t_FRAC && mf_get_type(F) == t_MF_DIV)
     980             :   {
     981         112 :     GEN T = gel(F,3); /* hopefully mfTheta() */
     982         112 :     if (mf_get_type(T) == t_MF_THETA && mf_get_N(T) == 4) return gel(F,2);
     983             :   }
     984         294 :   return mfmul(F, mfTheta(NULL));
     985             : }
     986             : 
     987             : static GEN
     988          42 : c_bracket(long n, long d, GEN S)
     989             : {
     990          42 :   pari_sp av = avma;
     991          42 :   long i, nd = n*d;
     992          42 :   GEN F = gel(S,2), G = gel(S,3), tF, tG, C, mpow, res, gk, gl;
     993          42 :   GEN VF = mfcoefs_i(F, nd, 1);
     994          42 :   GEN VG = mfcoefs_i(G, nd, 1);
     995          42 :   ulong j, m = itou(gel(S,4));
     996             : 
     997          42 :   if (!n)
     998             :   {
     999          14 :     if (m > 0) { set_avma(av); return mkvec(gen_0); }
    1000           7 :     return gerepilecopy(av, mkvec(gmul(gel(VF, 1), gel(VG, 1))));
    1001             :   }
    1002          28 :   tF = cgetg(nd+2, t_VEC);
    1003          28 :   tG = cgetg(nd+2, t_VEC);
    1004          28 :   res = NULL; gk = mf_get_gk(F); gl = mf_get_gk(G);
    1005             :   /* pow[i,j+1] = i^j */
    1006          28 :   if (lg(S) == 6) chicompatlift(gel(S,5),&VF,&VG);
    1007          28 :   mpow = cgetg(m+2, t_MAT);
    1008          28 :   gel(mpow,1) = const_col(nd, gen_1);
    1009          56 :   for (j = 1; j <= m; j++)
    1010             :   {
    1011          28 :     GEN c = cgetg(nd+1, t_COL);
    1012          28 :     gel(mpow,j+1) = c;
    1013         245 :     for (i = 1; i <= nd; i++) gel(c,i) = muliu(gcoeff(mpow,i,j), i);
    1014             :   }
    1015          28 :   C = binomial(gaddgs(gk, m-1), m);
    1016          28 :   if (odd(m)) C = gneg(C);
    1017          84 :   for (j = 0; j <= m; j++)
    1018             :   { /* C = (-1)^(m-j) binom(m+l-1, j) binom(m+k-1,m-j) */
    1019             :     GEN c;
    1020          56 :     gel(tF,1) = j == 0? gel(VF,1): gen_0;
    1021          56 :     gel(tG,1) = j == m? gel(VG,1): gen_0;
    1022          56 :     gel(tF,2) = gel(VF,2); /* assume nd >= 1 */
    1023          56 :     gel(tG,2) = gel(VG,2);
    1024         518 :     for (i = 2; i <= nd; i++)
    1025             :     {
    1026         462 :       gel(tF, i+1) = gmul(gcoeff(mpow,i,j+1),   gel(VF, i+1));
    1027         462 :       gel(tG, i+1) = gmul(gcoeff(mpow,i,m-j+1), gel(VG, i+1));
    1028             :     }
    1029          56 :     c = gmul(C, c_deflate(n, d, RgV_mul_RgXn(tF, tG)));
    1030          56 :     res = res? gadd(res, c): c;
    1031          56 :     if (j < m)
    1032          56 :       C = gdiv(gmul(C, gmulsg(m-j, gaddgs(gl,m-j-1))),
    1033          28 :                gmulsg(-(j+1), gaddgs(gk,j)));
    1034             :   }
    1035          28 :   if (lg(S) == 6) res = chicompatfix(gel(S,5), res);
    1036          28 :   return gerepileupto(av, res);
    1037             : }
    1038             : /* linear combination \sum L[j] vecF[j] */
    1039             : static GEN
    1040        2905 : c_linear(long n, long d, GEN F, GEN L, GEN dL)
    1041             : {
    1042        2905 :   pari_sp av = avma;
    1043        2905 :   long j, l = lg(L);
    1044        2905 :   GEN S = NULL;
    1045       10283 :   for (j = 1; j < l; j++)
    1046             :   {
    1047        7378 :     GEN c = gel(L,j);
    1048        7378 :     if (gequal0(c)) continue;
    1049        6741 :     c = gmul(c, mfcoefs_i(gel(F,j), n, d));
    1050        6741 :     S = S? gadd(S,c): c;
    1051             :   }
    1052        2905 :   if (!S) return zerovec(n+1);
    1053        2905 :   if (!is_pm1(dL)) S = gdiv(S, dL);
    1054        2905 :   return gerepileupto(av, S);
    1055             : }
    1056             : 
    1057             : /* B_d(T_j Trace^new) as t_MF_BD(t_MF_HECKE(t_MF_NEWTRACE)) or
    1058             :  * t_MF_HECKE(t_MF_NEWTRACE)
    1059             :  * or t_MF_NEWTRACE in level N. Set d and j, return t_MF_NEWTRACE component*/
    1060             : static GEN
    1061       78939 : bhn_parse(GEN f, long *d, long *j)
    1062             : {
    1063       78939 :   long t = mf_get_type(f);
    1064       78939 :   *d = *j = 1;
    1065       78939 :   if (t == t_MF_BD) { *d = itos(gel(f,3)); f = gel(f,2); t = mf_get_type(f); }
    1066       78939 :   if (t == t_MF_HECKE) { *j = gel(f,2)[1]; f = gel(f,3); }
    1067       78939 :   return f;
    1068             : }
    1069             : /* f as above, return the t_MF_NEWTRACE component */
    1070             : static GEN
    1071       31507 : bhn_newtrace(GEN f)
    1072             : {
    1073       31507 :   long t = mf_get_type(f);
    1074       31507 :   if (t == t_MF_BD) { f = gel(f,2); t = mf_get_type(f); }
    1075       31507 :   if (t == t_MF_HECKE) f = gel(f,3);
    1076       31507 :   return f;
    1077             : }
    1078             : static int
    1079        3640 : ok_bhn_linear(GEN vf)
    1080             : {
    1081        3640 :   long i, N0 = 0, l = lg(vf);
    1082             :   GEN CHI, gk;
    1083        3640 :   if (l == 1) return 1;
    1084        3640 :   gk = mf_get_gk(gel(vf,1));
    1085        3640 :   CHI = mf_get_CHI(gel(vf,1));
    1086       26803 :   for (i = 1; i < l; i++)
    1087             :   {
    1088       25340 :     GEN f = bhn_newtrace(gel(vf,i));
    1089       25340 :     long N = mf_get_N(f);
    1090       25340 :     if (mf_get_type(f) != t_MF_NEWTRACE) return 0;
    1091       23163 :     if (N < N0) return 0; /* largest level must come last */
    1092       23163 :     N0 = N;
    1093       23163 :     if (!gequal(gk,mf_get_gk(f))) return 0; /* same k */
    1094       23163 :     if (!gequal(gel(mf_get_CHI(f),2), gel(CHI,2))) return 0; /* same CHI */
    1095             :   }
    1096        1463 :   return 1;
    1097             : }
    1098             : 
    1099             : /* vF not empty, same hypotheses as bhnmat_extend */
    1100             : static GEN
    1101        6258 : bhnmat_extend_nocache(GEN M, long N, long n, long d, GEN vF)
    1102             : {
    1103             :   cachenew_t cache;
    1104        6258 :   long l = lg(vF);
    1105             :   GEN f;
    1106        6258 :   if (l == 1) return M? M: cgetg(1, t_MAT);
    1107        6167 :   f = bhn_newtrace(gel(vF,1)); /* N.B. mf_get_N(f) divides N */
    1108        6167 :   init_cachenew(&cache, n*d, N, f);
    1109        6167 :   M = bhnmat_extend(M, n, d, vF, &cache);
    1110        6167 :   dbg_cachenew(&cache); return M;
    1111             : }
    1112             : /* c_linear of "bhn" mf closures, same hypotheses as bhnmat_extend */
    1113             : static GEN
    1114        1652 : c_linear_bhn(long n, long d, GEN F)
    1115             : {
    1116             :   pari_sp av;
    1117        1652 :   GEN M, v, vF = gel(F,2), L = gel(F,3), dL = gel(F,4);
    1118        1652 :   if (lg(L) == 1) return zerovec(n+1);
    1119        1652 :   av = avma;
    1120        1652 :   M = bhnmat_extend_nocache(NULL, mf_get_N(F), n, d, vF);
    1121        1652 :   v = RgM_RgC_mul(M,L); settyp(v, t_VEC);
    1122        1652 :   if (!is_pm1(dL)) v = gdiv(v, dL);
    1123        1652 :   return gerepileupto(av, v);
    1124             : }
    1125             : 
    1126             : /* c in K, K := Q[X]/(T) vz = vector of consecutive powers of root z of T
    1127             :  * attached to an embedding s: K -> C. Return s(c) in C */
    1128             : static GEN
    1129       84282 : Rg_embed1(GEN c, GEN vz)
    1130             : {
    1131       84282 :   long t = typ(c);
    1132       84282 :   if (t == t_POLMOD) { c = gel(c,2); t = typ(c); }
    1133       84282 :   if (t == t_POL) c = RgX_RgV_eval(c, vz);
    1134       84282 :   return c;
    1135             : }
    1136             : /* return s(P) in C[X] */
    1137             : static GEN
    1138         882 : RgX_embed1(GEN P, GEN vz)
    1139             : {
    1140             :   long i, l;
    1141         882 :   GEN Q = cgetg_copy(P, &l);
    1142         882 :   Q[1] = P[1];
    1143        2317 :   for (i = 2; i < l; i++) gel(Q,i) = Rg_embed1(gel(P,i), vz);
    1144         882 :   return normalizepol_lg(Q,l); /* normally a no-op */
    1145             : }
    1146             : /* return s(P) in C^n */
    1147             : static GEN
    1148         798 : vecembed1(GEN P, GEN vz)
    1149             : {
    1150             :   long i, l;
    1151         798 :   GEN Q = cgetg_copy(P, &l);
    1152       39510 :   for (i = 1; i < l; i++) gel(Q,i) = Rg_embed1(gel(P,i), vz);
    1153         798 :   return Q;
    1154             : }
    1155             : /* P in L = K[X]/(U), K = Q[t]/T; s an embedding of K -> C attached
    1156             :  * to a root of T, extended to an embedding of L -> C attached to a root
    1157             :  * of s(U); vT powers of the root of T, vU powers of the root of s(U).
    1158             :  * Return s(P) in C^n */
    1159             : static GEN
    1160       13328 : Rg_embed2(GEN P, long vt, GEN vT, GEN vU)
    1161             : {
    1162             :   long i, l;
    1163             :   GEN Q;
    1164       13328 :   P = liftpol_shallow(P);
    1165       13328 :   if (typ(P) != t_POL) return P;
    1166       13300 :   if (varn(P) == vt) return Rg_embed1(P, vT);
    1167             :   /* varn(P) == vx */
    1168       13293 :   Q = cgetg_copy(P, &l); Q[1] = P[1];
    1169       39669 :   for (i = 2; i < l; i++) gel(Q,i) = Rg_embed1(gel(P,i), vT);
    1170       13293 :   return Rg_embed1(Q, vU);
    1171             : }
    1172             : static GEN
    1173          42 : vecembed2(GEN P, long vt, GEN vT, GEN vU)
    1174             : {
    1175             :   long i, l;
    1176          42 :   GEN Q = cgetg_copy(P, &l);
    1177        1050 :   for (i = 1; i < l; i++) gel(Q,i) = Rg_embed2(gel(P,i), vt, vT, vU);
    1178          42 :   return Q;
    1179             : }
    1180             : static GEN
    1181         532 : RgX_embed2(GEN P, long vt, GEN vT, GEN vU)
    1182             : {
    1183             :   long i, l;
    1184         532 :   GEN Q = cgetg_copy(P, &l);
    1185        3724 :   for (i = 2; i < l; i++) gel(Q,i) = Rg_embed2(gel(P,i), vt, vT, vU);
    1186         532 :   Q[1] = P[1]; return normalizepol_lg(Q,l);
    1187             : }
    1188             : /* embed polynomial f in variable 0 [ may be a scalar ], E from getembed */
    1189             : static GEN
    1190        1617 : RgX_embed(GEN f, GEN E)
    1191             : {
    1192             :   GEN vT;
    1193        1617 :   if (typ(f) != t_POL || varn(f) != 0) return mfembed(E, f);
    1194        1575 :   if (lg(E) == 1) return f;
    1195        1379 :   vT = gel(E,2);
    1196        1379 :   if (lg(E) == 3)
    1197         847 :     f = RgX_embed1(f, vT);
    1198             :   else
    1199         532 :     f = RgX_embed2(f, varn(gel(E,1)), vT, gel(E,3));
    1200        1379 :   return f;
    1201             : }
    1202             : /* embed vector, E from getembed */
    1203             : GEN
    1204        1694 : mfvecembed(GEN E, GEN v)
    1205             : {
    1206             :   GEN vT;
    1207        1694 :   if (lg(E) == 1) return v;
    1208         840 :   vT = gel(E,2);
    1209         840 :   if (lg(E) == 3)
    1210         798 :     v = vecembed1(v, vT);
    1211             :   else
    1212          42 :     v = vecembed2(v, varn(gel(E,1)), vT, gel(E,3));
    1213         840 :   return v;
    1214             : }
    1215             : GEN
    1216          70 : mfmatembed(GEN E, GEN f)
    1217             : {
    1218             :   long i, l;
    1219             :   GEN g;
    1220          70 :   if (lg(E) == 1) return f;
    1221          42 :   g = cgetg_copy(f, &l);
    1222         168 :   for (i = 1; i < l; i++) gel(g,i) = mfvecembed(E, gel(f,i));
    1223          42 :   return g;
    1224             : }
    1225             : /* embed vector of polynomials in var 0 */
    1226             : static GEN
    1227          98 : RgXV_embed(GEN f, GEN E)
    1228             : {
    1229             :   long i, l;
    1230             :   GEN v;
    1231          98 :   if (lg(E) == 1) return f;
    1232          70 :   v = cgetg_copy(f, &l);
    1233        1358 :   for (i = 1; i < l; i++) gel(v,i) = RgX_embed(gel(f,i), E);
    1234          70 :   return v;
    1235             : }
    1236             : 
    1237             : /* embed scalar */
    1238             : GEN
    1239       96643 : mfembed(GEN E, GEN f)
    1240             : {
    1241             :   GEN vT;
    1242       96643 :   if (lg(E) == 1) return f;
    1243       13587 :   vT = gel(E,2);
    1244       13587 :   if (lg(E) == 3)
    1245        4459 :     f = Rg_embed1(f, vT);
    1246             :   else
    1247        9128 :     f = Rg_embed2(f, varn(gel(E,1)), vT, gel(E,3));
    1248       13587 :   return f;
    1249             : }
    1250             : /* vector of the sigma(f), sigma in vE */
    1251             : static GEN
    1252         308 : RgX_embedall(GEN f, GEN vE)
    1253             : {
    1254         308 :   long i, l = lg(vE);
    1255             :   GEN v;
    1256         308 :   if (l == 2) return RgX_embed(f, gel(vE,1));
    1257          21 :   v = cgetg(l, t_VEC);
    1258          63 :   for (i = 1; i < l; i++) gel(v,i) = RgX_embed(f, gel(vE,i));
    1259          21 :   return v;
    1260             : }
    1261             : /* matrix whose colums are the sigma(v), sigma in vE */
    1262             : static GEN
    1263         343 : RgC_embedall(GEN v, GEN vE)
    1264             : {
    1265         343 :   long j, l = lg(vE);
    1266         343 :   GEN M = cgetg(l, t_MAT);
    1267         861 :   for (j = 1; j < l; j++) gel(M,j) = mfvecembed(gel(vE,j), v);
    1268         343 :   return M;
    1269             : }
    1270             : /* vector of the sigma(v), sigma in vE */
    1271             : static GEN
    1272        4907 : Rg_embedall_i(GEN v, GEN vE)
    1273             : {
    1274        4907 :   long j, l = lg(vE);
    1275        4907 :   GEN M = cgetg(l, t_VEC);
    1276       14735 :   for (j = 1; j < l; j++) gel(M,j) = mfembed(gel(vE,j), v);
    1277        4907 :   return M;
    1278             : }
    1279             : /* vector of the sigma(v), sigma in vE; if #vE == 1, return v */
    1280             : static GEN
    1281       90980 : Rg_embedall(GEN v, GEN vE)
    1282       90980 : { return (lg(vE) == 2)? mfembed(gel(vE,1), v): Rg_embedall_i(v, vE); }
    1283             : 
    1284             : static GEN
    1285         833 : c_div_i(long n, GEN S)
    1286             : {
    1287         833 :   GEN F = gel(S,2), G = gel(S,3);
    1288             :   GEN a0, a0i, H;
    1289         833 :   F = mfcoefs_i(F, n, 1);
    1290         833 :   G = mfcoefs_i(G, n, 1);
    1291         833 :   if (lg(S) == 5) chicompatlift(gel(S,4),&F,&G);
    1292         833 :   F = RgV_to_ser_full(F);
    1293         833 :   G = RgV_to_ser_full(G);
    1294         833 :   a0 = polcoef_i(G, 0, -1); /* != 0 */
    1295         833 :   if (gequal1(a0)) a0 = a0i = NULL;
    1296             :   else
    1297             :   {
    1298         602 :     a0i = ginv(a0);
    1299         602 :     G = gmul(ser_unscale(G,a0), a0i);
    1300         602 :     F = gmul(ser_unscale(F,a0), a0i);
    1301             :   }
    1302         833 :   H = gdiv(F, G);
    1303         833 :   if (a0) H = ser_unscale(H,a0i);
    1304         833 :   H = sertovecslice(H, n);
    1305         833 :   if (lg(S) == 5) H = chicompatfix(gel(S,4), H);
    1306         833 :   return H;
    1307             : }
    1308             : static GEN
    1309         833 : c_div(long n, long d, GEN S)
    1310             : {
    1311         833 :   pari_sp av = avma;
    1312         833 :   GEN D = (d==1)? c_div_i(n, S): c_deflate(n, d, c_div_i(n*d, S));
    1313         833 :   return gerepilecopy(av, D);
    1314             : }
    1315             : 
    1316             : static GEN
    1317          35 : c_shift(long n, long d, GEN F, GEN gsh)
    1318             : {
    1319          35 :   pari_sp av = avma;
    1320             :   GEN vF;
    1321          35 :   long sh = itos(gsh), n1 = n*d + sh;
    1322          35 :   if (n1 < 0) return zerovec(n+1);
    1323          35 :   vF = mfcoefs_i(F, n1, 1);
    1324          35 :   if (sh < 0) vF = shallowconcat(zerovec(-sh), vF);
    1325          35 :   else vF = vecslice(vF, sh+1, n1+1);
    1326          35 :   return gerepilecopy(av, c_deflate(n, d, vF));
    1327             : }
    1328             : 
    1329             : static GEN
    1330         147 : c_deriv(long n, long d, GEN F, GEN gm)
    1331             : {
    1332         147 :   pari_sp av = avma;
    1333         147 :   GEN V = mfcoefs_i(F, n, d), res;
    1334         147 :   long i, m = itos(gm);
    1335         147 :   if (!m) return V;
    1336         147 :   res = cgetg(n+2, t_VEC); gel(res,1) = gen_0;
    1337         147 :   if (m < 0)
    1338          49 :   { for (i=1; i <= n; i++) gel(res, i+1) = gdiv(gel(V, i+1), powuu(i,-m)); }
    1339             :   else
    1340        1953 :   { for (i=1; i <= n; i++) gel(res, i+1) = gmul(gel(V,i+1), powuu(i,m)); }
    1341         147 :   return gerepileupto(av, res);
    1342             : }
    1343             : 
    1344             : static GEN
    1345          14 : c_derivE2(long n, long d, GEN F, GEN gm)
    1346             : {
    1347          14 :   pari_sp av = avma;
    1348             :   GEN VF, VE, res, tmp, gk;
    1349          14 :   long i, m = itos(gm), nd;
    1350          14 :   if (m == 0) return mfcoefs_i(F, n, d);
    1351          14 :   nd = n*d;
    1352          14 :   VF = mfcoefs_i(F, nd, 1); VE = mfcoefs_i(mfEk(2), nd, 1);
    1353          14 :   gk = mf_get_gk(F);
    1354          14 :   if (m == 1)
    1355             :   {
    1356           7 :     res = cgetg(n+2, t_VEC);
    1357          56 :     for (i = 0; i <= n; i++) gel(res, i+1) = gmulsg(i, gel(VF, i*d+1));
    1358           7 :     tmp = c_deflate(n, d, RgV_mul_RgXn(VF, VE));
    1359           7 :     return gerepileupto(av, gsub(res, gmul(gdivgs(gk, 12), tmp)));
    1360             :   }
    1361             :   else
    1362             :   {
    1363             :     long j;
    1364          35 :     for (j = 1; j <= m; j++)
    1365             :     {
    1366          28 :       tmp = RgV_mul_RgXn(VF, VE);
    1367         140 :       for (i = 0; i <= nd; i++) gel(VF, i+1) = gmulsg(i, gel(VF, i+1));
    1368          28 :       VF = gsub(VF, gmul(gdivgs(gaddgs(gk, 2*(j-1)), 12), tmp));
    1369             :     }
    1370           7 :     return gerepilecopy(av, c_deflate(n, d, VF));
    1371             :   }
    1372             : }
    1373             : 
    1374             : /* Twist by the character (D/.) */
    1375             : static GEN
    1376           7 : c_twist(long n, long d, GEN F, GEN D)
    1377             : {
    1378           7 :   pari_sp av = avma;
    1379           7 :   GEN V = mfcoefs_i(F, n, d), res = cgetg(n+2, t_VEC);
    1380             :   long i;
    1381         119 :   for (i = 0; i <= n; i++)
    1382         112 :     gel(res, i + 1) = gmulsg(krois(D, i), gel(V, i+1));
    1383           7 :   return gerepileupto(av, res);
    1384             : }
    1385             : 
    1386             : /* form F given by closure, compute T(n)(F) as closure */
    1387             : static GEN
    1388         994 : c_hecke(long m, long l, GEN DATA, GEN F)
    1389             : {
    1390         994 :   pari_sp av = avma;
    1391         994 :   return gerepilecopy(av, hecke_i(m, l, NULL, F, DATA));
    1392             : }
    1393             : static GEN
    1394         140 : c_const(long n, long d, GEN C)
    1395             : {
    1396         140 :   GEN V = zerovec(n+1);
    1397         140 :   long i, j, l = lg(C);
    1398         140 :   if (l > d*n+2) l = d*n+2;
    1399         189 :   for (i = j = 1; i < l; i+=d, j++) gel(V, j) = gcopy(gel(C,i));
    1400         140 :   return V;
    1401             : }
    1402             : 
    1403             : /* m > 0 */
    1404             : static GEN
    1405         469 : eta3_ZXn(long m)
    1406             : {
    1407         469 :   long l = m+2, n, k;
    1408         469 :   GEN P = cgetg(l,t_POL);
    1409         469 :   P[1] = evalsigne(1)|evalvarn(0);
    1410        6489 :   for (n = 2; n < l; n++) gel(P,n) = gen_0;
    1411         469 :   for (n = k = 0;; n++)
    1412             :   {
    1413        2611 :     if (k + n >= m) { setlg(P, k+3); return P; }
    1414        2142 :     k += n;
    1415             :     /* now k = n(n+1) / 2 */
    1416        2142 :     gel(P, k+2) = odd(n)? utoineg(2*n+1): utoipos(2*n+1);
    1417             :   }
    1418             : }
    1419             : 
    1420             : static GEN
    1421         476 : c_delta(long n, long d)
    1422             : {
    1423         476 :   pari_sp ltop = avma;
    1424         476 :   long N = n*d;
    1425             :   GEN e;
    1426         476 :   if (!N) return mkvec(gen_0);
    1427         469 :   e = eta3_ZXn(N);
    1428         469 :   e = ZXn_sqr(e,N);
    1429         469 :   e = ZXn_sqr(e,N);
    1430         469 :   e = ZXn_sqr(e,N); /* eta(x)^24 */
    1431         469 :   settyp(e, t_VEC);
    1432         469 :   gel(e,1) = gen_0; /* Delta(x) = x*eta(x)^24 as a t_VEC */
    1433         469 :   return gerepilecopy(ltop, c_deflate(n, d, e));
    1434             : }
    1435             : 
    1436             : /* return s(d) such that s|f <=> d | f^2 */
    1437             : static long
    1438          49 : mysqrtu(ulong d)
    1439             : {
    1440          49 :   GEN fa = myfactoru(d), P = gel(fa,1), E = gel(fa,2);
    1441          49 :   long l = lg(P), i, s = 1;
    1442         126 :   for (i = 1; i < l; i++) s *= upowuu(P[i], (E[i]+1)>>1);
    1443          49 :   return s;
    1444             : }
    1445             : static GEN
    1446        1813 : c_theta(long n, long d, GEN psi)
    1447             : {
    1448        1813 :   long lim = usqrt(n*d), F = mfcharmodulus(psi), par = mfcharparity(psi);
    1449        1813 :   long f, d2 = d == 1? 1: mysqrtu(d);
    1450        1813 :   GEN V = zerovec(n + 1);
    1451        8008 :   for (f = d2; f <= lim; f += d2)
    1452        6195 :     if (ugcd(F, f) == 1)
    1453             :     {
    1454        6188 :       pari_sp av = avma;
    1455        6188 :       GEN c = mfchareval(psi, f);
    1456        6188 :       gel(V, f*f/d + 1) = gerepileupto(av, par < 0 ? gmulgs(c,2*f) : gmul2n(c,1));
    1457             :     }
    1458        1813 :   if (F == 1) gel(V, 1) = gen_1;
    1459        1813 :   return V; /* no gerepile needed */
    1460             : }
    1461             : 
    1462             : static GEN
    1463         203 : c_etaquo(long n, long d, GEN eta, GEN gs)
    1464             : {
    1465         203 :   pari_sp av = avma;
    1466         203 :   long s = itos(gs), nd = n*d, nds = nd - s + 1;
    1467             :   GEN c;
    1468         203 :   if (nds <= 0) return zerovec(n+1);
    1469         182 :   c = RgX_to_RgC(eta_product_ZXn(eta, nds), nds); settyp(c, t_VEC);
    1470         182 :   if (s > 0) c = shallowconcat(zerovec(s), c);
    1471         182 :   return gerepilecopy(av, c_deflate(n, d, c));
    1472             : }
    1473             : 
    1474             : static GEN
    1475          77 : c_ell(long n, long d, GEN E)
    1476             : {
    1477          77 :   pari_sp av = avma;
    1478             :   GEN v;
    1479          77 :   if (d == 1) return gconcat(gen_0, ellan(E, n));
    1480           7 :   v = vec_prepend(ellan(E, n*d), gen_0);
    1481           7 :   return gerepilecopy(av, c_deflate(n, d, v));
    1482             : }
    1483             : 
    1484             : static GEN
    1485          21 : c_cusptrace(long n, long d, GEN F)
    1486             : {
    1487          21 :   pari_sp av = avma;
    1488          21 :   GEN D = gel(F,2), res = cgetg(n+2, t_VEC);
    1489          21 :   long i, N = mf_get_N(F), k = mf_get_k(F);
    1490          21 :   gel(res, 1) = gen_0;
    1491         140 :   for (i = 1; i <= n; i++)
    1492         119 :     gel(res, i+1) = mfcusptrace_i(N, k, i*d, mydivisorsu(i*d), D);
    1493          21 :   return gerepilecopy(av, res);
    1494             : }
    1495             : 
    1496             : static GEN
    1497        1561 : c_newtrace(long n, long d, GEN F)
    1498             : {
    1499        1561 :   pari_sp av = avma;
    1500             :   cachenew_t cache;
    1501        1561 :   long N = mf_get_N(F);
    1502             :   GEN v;
    1503        1561 :   init_cachenew(&cache, n == 1? 1: n*d, N, F);
    1504        1561 :   v = colnewtrace(0, n, d, N, mf_get_k(F), &cache);
    1505        1561 :   settyp(v, t_VEC); return gerepilecopy(av, v);
    1506             : }
    1507             : 
    1508             : static GEN
    1509        6769 : c_Bd(long n, long d, GEN F, GEN A)
    1510             : {
    1511        6769 :   pari_sp av = avma;
    1512        6769 :   long a = itou(A), ad = ugcd(a,d), aad = a/ad, i, j;
    1513        6769 :   GEN w, v = mfcoefs_i(F, n/aad, d/ad);
    1514        6769 :   if (a == 1) return v;
    1515        6769 :   n++; w = zerovec(n);
    1516      196021 :   for (i = j = 1; j <= n; i++, j += aad) gel(w,j) = gcopy(gel(v,i));
    1517        6769 :   return gerepileupto(av, w);
    1518             : }
    1519             : 
    1520             : static GEN
    1521        4879 : c_dihedral(long n, long d, GEN F)
    1522             : {
    1523        4879 :   pari_sp av = avma;
    1524        4879 :   GEN CHI = mf_get_CHI(F);
    1525        4879 :   GEN w = gel(F,3), V = dihan(gel(F,2), w, gel(F,4), mfcharorder(CHI), n*d);
    1526        4879 :   GEN Tinit = gel(w,3), Pm = gel(Tinit,1);
    1527        4879 :   GEN A = c_deflate(n, d, V);
    1528        4879 :   if (degpol(Pm) == 1 || RgV_is_ZV(A)) return gerepilecopy(av, A);
    1529         973 :   return gerepileupto(av, gmodulo(A, Pm));
    1530             : }
    1531             : 
    1532             : static GEN
    1533         315 : c_mfEH(long n, long d, GEN F)
    1534             : {
    1535         315 :   pari_sp av = avma;
    1536             :   GEN v, M, A;
    1537         315 :   long i, r = mf_get_r(F);
    1538         315 :   if (n == 1)
    1539          14 :     return gerepilecopy(av, mkvec2(mfEHcoef(r,0),mfEHcoef(r,d)));
    1540             :   /* speedup mfcoef */
    1541         301 :   if (r == 1)
    1542             :   {
    1543          70 :     v = cgetg(n+2, t_VEC);
    1544          70 :     gel(v,1) = sstoQ(-1,12);
    1545       83258 :     for (i = 1; i <= n; i++)
    1546             :     {
    1547       83188 :       long id = i*d, a = id & 3;
    1548       83188 :       gel(v,i+1) = (a==1 || a==2)? gen_0: sstoQ(hclassno6u(id), 6);
    1549             :     }
    1550          70 :     return v; /* no gerepile needed */
    1551             :   }
    1552         231 :   M = mfEHmat(n*d+1,r);
    1553         231 :   if (d > 1)
    1554             :   {
    1555          35 :     long l = lg(M);
    1556         119 :     for (i = 1; i < l; i++) gel(M,i) = c_deflate(n, d, gel(M,i));
    1557             :   }
    1558         231 :   A = gel(F,2); /* [num(B), den(B)] */
    1559         231 :   v = RgC_Rg_div(RgM_RgC_mul(M, gel(A,1)), gel(A,2));
    1560         231 :   settyp(v,t_VEC); return gerepileupto(av, v);
    1561             : }
    1562             : 
    1563             : static GEN
    1564       10822 : c_mfeisen(long n, long d, GEN F)
    1565             : {
    1566       10822 :   pari_sp av = avma;
    1567       10822 :   GEN v, vchi, E0, P, T, CHI, gk = mf_get_gk(F);
    1568             :   long i, k;
    1569       10822 :   if (typ(gk) != t_INT) return c_mfEH(n, d, F);
    1570       10507 :   k = itou(gk);
    1571       10507 :   vchi = gel(F,2);
    1572       10507 :   E0 = gel(vchi,1);
    1573       10507 :   T = gel(vchi,2);
    1574       10507 :   P = gel(T,1);
    1575       10507 :   CHI = gel(vchi,3);
    1576       10507 :   v = cgetg(n+2, t_VEC);
    1577       10507 :   gel(v, 1) = gcopy(E0); /* E(0) */
    1578       10507 :   if (lg(vchi) == 5)
    1579             :   { /* E_k(chi1,chi2) */
    1580        8421 :     GEN CHI2 = gel(vchi,4), F3 = gel(F,3);
    1581        8421 :     long ord = F3[1], j = F3[2];
    1582      497840 :     for (i = 1; i <= n; i++) gel(v, i+1) = sigchi2(k, CHI, CHI2, i*d, ord);
    1583        8421 :     v = QabV_tracerel(T, j, v);
    1584             :   }
    1585             :   else
    1586             :   { /* E_k(chi) */
    1587       26278 :     for (i = 1; i <= n; i++) gel(v, i+1) = sigchi(k, CHI, i*d);
    1588             :   }
    1589       10507 :   if (degpol(P) != 1 && !RgV_is_QV(v)) return gerepileupto(av, gmodulo(v, P));
    1590        7602 :   return gerepilecopy(av, v);
    1591             : }
    1592             : 
    1593             : /* L(chi_D, 1-k) */
    1594             : static GEN
    1595          28 : lfunquadneg_naive(long D, long k)
    1596             : {
    1597          28 :   GEN B, dS, S = gen_0;
    1598          28 :   long r, N = labs(D);
    1599             :   pari_sp av;
    1600          28 :   if (k == 1 && N == 1) return gneg(ghalf);
    1601             :   /* B = N^k * denom(B) * B(x/N) */
    1602          28 :   B = ZX_rescale(Q_remove_denom(bernpol(k, 0), &dS), utoi(N));
    1603          28 :   dS = mul_denom(dS, stoi(-N*k));
    1604          28 :   av = avma;
    1605        7175 :   for (r = 0; r < N; r++)
    1606             :   {
    1607        7147 :     long c = kross(D, r);
    1608        7147 :     if (c)
    1609             :     {
    1610        5152 :       GEN t = ZX_Z_eval(B, utoi(r));
    1611        5152 :       S = c > 0 ? addii(S, t) : subii(S, t);
    1612        5152 :       S = gerepileuptoint(av, S);
    1613             :     }
    1614             :   }
    1615          28 :   return gdiv(S, dS);
    1616             : }
    1617             : 
    1618             : /* Returns vector of coeffs from F[0], F[d], ..., F[d*n] */
    1619             : static GEN
    1620       34706 : mfcoefs_i(GEN F, long n, long d)
    1621             : {
    1622       34706 :   if (n < 0) return gen_0;
    1623       34706 :   switch(mf_get_type(F))
    1624             :   {
    1625         140 :     case t_MF_CONST: return c_const(n, d, gel(F,2));
    1626       10822 :     case t_MF_EISEN: return c_mfeisen(n, d, F);
    1627         812 :     case t_MF_Ek: return c_Ek(n, d, F);
    1628         476 :     case t_MF_DELTA: return c_delta(n, d);
    1629        1575 :     case t_MF_THETA: return c_theta(n, d, gel(F,2));
    1630         203 :     case t_MF_ETAQUO: return c_etaquo(n, d, gel(F,2), gel(F,3));
    1631          77 :     case t_MF_ELL: return c_ell(n, d, gel(F,2));
    1632         637 :     case t_MF_MUL: return c_mul(n, d, F);
    1633         105 :     case t_MF_POW: return c_pow(n, d, F);
    1634          42 :     case t_MF_BRACKET: return c_bracket(n, d, F);
    1635        2905 :     case t_MF_LINEAR: return c_linear(n, d, gel(F,2), gel(F,3), gel(F,4));
    1636        1652 :     case t_MF_LINEAR_BHN: return c_linear_bhn(n, d, F);
    1637         833 :     case t_MF_DIV: return c_div(n, d, F);
    1638          35 :     case t_MF_SHIFT: return c_shift(n, d, gel(F,2), gel(F,3));
    1639         147 :     case t_MF_DERIV: return c_deriv(n, d, gel(F,2), gel(F,3));
    1640          14 :     case t_MF_DERIVE2: return c_derivE2(n, d, gel(F,2), gel(F,3));
    1641           7 :     case t_MF_TWIST: return c_twist(n, d, gel(F,2), gel(F,3));
    1642         994 :     case t_MF_HECKE: return c_hecke(n, d, gel(F,2), gel(F,3));
    1643        6769 :     case t_MF_BD: return c_Bd(n, d, gel(F,2), gel(F,3));
    1644          21 :     case t_MF_TRACE: return c_cusptrace(n, d, F);
    1645        1561 :     case t_MF_NEWTRACE: return c_newtrace(n, d, F);
    1646        4879 :     case t_MF_DIHEDRAL: return c_dihedral(n, d, F);
    1647             :     default: pari_err_TYPE("mfcoefs",F); return NULL;/*LCOV_EXCL_LINE*/
    1648             :   }
    1649             : }
    1650             : 
    1651             : static GEN
    1652         385 : matdeflate(long n, long d, GEN M)
    1653             : {
    1654             :   long i, l;
    1655             :   GEN A;
    1656             :   /*  if (d == 1) return M; */
    1657         385 :   A = cgetg_copy(M,&l);
    1658        1575 :   for (i = 1; i < l; i++) gel(A,i) = c_deflate(n,d,gel(M,i));
    1659         385 :   return A;
    1660             : }
    1661             : static int
    1662        5663 : space_is_cusp(long space) { return space != mf_FULL && space != mf_EISEN; }
    1663             : /* safe with flraw mf */
    1664             : static GEN
    1665        2471 : mfcoefs_mf(GEN mf, long n, long d)
    1666             : {
    1667        2471 :   GEN MS, ME, E = MF_get_E(mf), S = MF_get_S(mf), M = MF_get_M(mf);
    1668        2471 :   long lE = lg(E), lS = lg(S), l = lE+lS-1;
    1669             : 
    1670        2471 :   if (l == 1) return cgetg(1, t_MAT);
    1671        2359 :   if (typ(M) == t_MAT && lg(M) != 1 && (n+1)*d < nbrows(M))
    1672          21 :     return matdeflate(n, d, M); /*cached; lg = 1 is possible from mfinit */
    1673        2338 :   ME = (lE == 1)? cgetg(1, t_MAT): mfvectomat(E, n, d);
    1674        2338 :   if (lS == 1)
    1675         420 :     MS = cgetg(1, t_MAT);
    1676        1918 :   else if (mf_get_type(gel(S,1)) == t_MF_DIV) /*k 1/2-integer or k=1 (exotic)*/
    1677         364 :     MS = matdeflate(n,d, mflineardivtomat(MF_get_N(mf), S, n*d));
    1678        1554 :   else if (MF_get_k(mf) == 1) /* k = 1 (dihedral) */
    1679             :   {
    1680         252 :     GEN M = mfvectomat(gmael(S,1,2), n, d);
    1681             :     long i;
    1682         252 :     MS = cgetg(lS, t_MAT);
    1683        1085 :     for (i = 1; i < lS; i++)
    1684             :     {
    1685         833 :       GEN f = gel(S,i), dc = gel(f,4), c = RgM_RgC_mul(M, gel(f,3));
    1686         833 :       if (!equali1(dc)) c = RgC_Rg_div(c,dc);
    1687         833 :       gel(MS,i) = c;
    1688             :     }
    1689             :   }
    1690             :   else /* k >= 2 integer */
    1691        1302 :     MS = bhnmat_extend_nocache(NULL, MF_get_N(mf), n, d, S);
    1692        2338 :   return shallowconcat(ME,MS);
    1693             : }
    1694             : GEN
    1695        3745 : mfcoefs(GEN F, long n, long d)
    1696             : {
    1697        3745 :   if (!checkmf_i(F))
    1698             :   {
    1699          42 :     pari_sp av = avma;
    1700          42 :     GEN mf = checkMF_i(F); if (!mf) pari_err_TYPE("mfcoefs", F);
    1701          42 :     return gerepilecopy(av, mfcoefs_mf(mf,n,d));
    1702             :   }
    1703        3703 :   if (d <= 0) pari_err_DOMAIN("mfcoefs", "d", "<=", gen_0, stoi(d));
    1704        3703 :   if (n < 0) return cgetg(1, t_VEC);
    1705        3703 :   return mfcoefs_i(F, n, d);
    1706             : }
    1707             : 
    1708             : /* assume k >= 0 */
    1709             : static GEN
    1710         301 : mfak_i(GEN F, long k)
    1711             : {
    1712         301 :   if (!k) return gel(mfcoefs_i(F,0,1), 1);
    1713         154 :   return gel(mfcoefs_i(F,1,k), 2);
    1714             : }
    1715             : GEN
    1716         147 : mfcoef(GEN F, long n)
    1717             : {
    1718         147 :   pari_sp av = avma;
    1719         147 :   if (!checkmf_i(F)) pari_err_TYPE("mfcoef",F);
    1720         147 :   return n < 0? gen_0: gerepilecopy(av, mfak_i(F, n));
    1721             : }
    1722             : 
    1723             : static GEN
    1724         126 : paramconst() { return tagparams(t_MF_CONST, mkNK(1,0,mfchartrivial())); }
    1725             : static GEN
    1726          84 : mftrivial(void) { retmkvec2(paramconst(), cgetg(1,t_VEC)); }
    1727             : static GEN
    1728          42 : mf1(void) { retmkvec2(paramconst(), mkvec(gen_1)); }
    1729             : 
    1730             : /* induce mfchar CHI to G */
    1731             : static GEN
    1732      307370 : induce(GEN G, GEN CHI)
    1733             : {
    1734             :   GEN o, chi;
    1735      307370 :   if (typ(CHI) == t_INT) /* Kronecker */
    1736             :   {
    1737      300664 :     chi = znchar_quad(G, CHI);
    1738      300664 :     o = ZV_equal0(chi)? gen_1: gen_2;
    1739      300664 :     CHI = mkvec4(G,chi,o,cgetg(1,t_VEC));
    1740             :   }
    1741             :   else
    1742             :   {
    1743        6706 :     if (mfcharmodulus(CHI) == itos(znstar_get_N(G))) return CHI;
    1744        6132 :     CHI = leafcopy(CHI);
    1745        6132 :     chi = zncharinduce(gel(CHI,1), gel(CHI,2), G);
    1746        6132 :     gel(CHI,1) = G;
    1747        6132 :     gel(CHI,2) = chi;
    1748             :   }
    1749      306796 :   return CHI;
    1750             : }
    1751             : /* induce mfchar CHI to znstar(G) */
    1752             : static GEN
    1753       42245 : induceN(long N, GEN CHI)
    1754             : {
    1755       42245 :   if (mfcharmodulus(CHI) != N) CHI = induce(znstar0(utoipos(N),1), CHI);
    1756       42245 :   return CHI;
    1757             : }
    1758             : /* *pCHI1 and *pCHI2 are mfchar, induce to common modulus */
    1759             : static void
    1760        6195 : char2(GEN *pCHI1, GEN *pCHI2)
    1761             : {
    1762        6195 :   GEN CHI1 = *pCHI1, G1 = gel(CHI1,1), N1 = znstar_get_N(G1);
    1763        6195 :   GEN CHI2 = *pCHI2, G2 = gel(CHI2,1), N2 = znstar_get_N(G2);
    1764        6195 :   if (!equalii(N1,N2))
    1765             :   {
    1766        4669 :     GEN G, d = gcdii(N1,N2);
    1767        4669 :     if      (equalii(N2,d)) *pCHI2 = induce(G1, CHI2);
    1768        1512 :     else if (equalii(N1,d)) *pCHI1 = induce(G2, CHI1);
    1769             :     else
    1770             :     {
    1771         154 :       if (!equali1(d)) N2 = diviiexact(N2,d);
    1772         154 :       G = znstar0(mulii(N1,N2), 1);
    1773         154 :       *pCHI1 = induce(G, CHI1);
    1774         154 :       *pCHI2 = induce(G, CHI2);
    1775             :     }
    1776             :   }
    1777        6195 : }
    1778             : /* mfchar or charinit wrt same modulus; outputs a mfchar */
    1779             : static GEN
    1780      301707 : mfcharmul_i(GEN CHI1, GEN CHI2)
    1781             : {
    1782      301707 :   GEN G = gel(CHI1,1), chi3 = zncharmul(G, gel(CHI1,2), gel(CHI2,2));
    1783      301707 :   return mfcharGL(G, chi3);
    1784             : }
    1785             : /* mfchar or charinit; outputs a mfchar */
    1786             : static GEN
    1787        1050 : mfcharmul(GEN CHI1, GEN CHI2)
    1788             : {
    1789        1050 :   char2(&CHI1, &CHI2); return mfcharmul_i(CHI1,CHI2);
    1790             : }
    1791             : /* mfchar or charinit; outputs a mfchar */
    1792             : static GEN
    1793         140 : mfcharpow(GEN CHI, GEN n)
    1794             : {
    1795             :   GEN G, chi;
    1796         140 :   G = gel(CHI,1); chi = zncharpow(G, gel(CHI,2), n);
    1797         140 :   return mfchartoprimitive(mfcharGL(G, chi), NULL);
    1798             : }
    1799             : /* mfchar or charinit wrt same modulus; outputs a mfchar */
    1800             : static GEN
    1801        5145 : mfchardiv_i(GEN CHI1, GEN CHI2)
    1802             : {
    1803        5145 :   GEN G = gel(CHI1,1), chi3 = znchardiv(G, gel(CHI1,2), gel(CHI2,2));
    1804        5145 :   return mfcharGL(G, chi3);
    1805             : }
    1806             : /* mfchar or charinit; outputs a mfchar */
    1807             : static GEN
    1808        5145 : mfchardiv(GEN CHI1, GEN CHI2)
    1809             : {
    1810        5145 :   char2(&CHI1, &CHI2); return mfchardiv_i(CHI1,CHI2);
    1811             : }
    1812             : static GEN
    1813          49 : mfcharconj(GEN CHI)
    1814             : {
    1815          49 :   CHI = leafcopy(CHI);
    1816          49 :   gel(CHI,2) = zncharconj(gel(CHI,1), gel(CHI,2));
    1817          49 :   return CHI;
    1818             : }
    1819             : 
    1820             : /* CHI mfchar, assume 4 | N. Multiply CHI by \chi_{-4} */
    1821             : static GEN
    1822         882 : mfchilift(GEN CHI, long N)
    1823             : {
    1824         882 :   CHI = induceN(N, CHI);
    1825         882 :   return mfcharmul_i(CHI, induce(gel(CHI,1), stoi(-4)));
    1826             : }
    1827             : /* CHI defined mod N, N4 = N/4;
    1828             :  * if CHI is defined mod N4 return CHI;
    1829             :  * else if CHI' = CHI*(-4,.) is defined mod N4, return CHI' (primitive)
    1830             :  * else error */
    1831             : static GEN
    1832          35 : mfcharchiliftprim(GEN CHI, long N4)
    1833             : {
    1834          35 :   long FC = mfcharconductor(CHI);
    1835             :   GEN CHIP;
    1836          35 :   if (N4 % FC == 0) return CHI;
    1837          14 :   CHIP = mfchartoprimitive(mfchilift(CHI, N4 << 2), &FC);
    1838          14 :   if (N4 % FC) pari_err_TYPE("mfkohnenbasis [incorrect CHI]", CHI);
    1839          14 :   return CHIP;
    1840             : }
    1841             : /* ensure CHI(-1) = (-1)^k [k integer] or 1 [half-integer], by multiplying
    1842             :  * by (-4/.) if needed */
    1843             : static GEN
    1844        2716 : mfchiadjust(GEN CHI, GEN gk, long N)
    1845             : {
    1846        2716 :   long par = mfcharparity(CHI);
    1847        2716 :   if (typ(gk) == t_INT &&  mpodd(gk)) par = -par;
    1848        2716 :   return par == 1 ? CHI : mfchilift(CHI, N);
    1849             : }
    1850             : 
    1851             : static GEN
    1852        3794 : mfsamefield(GEN T, GEN P, GEN Q)
    1853             : {
    1854        3794 :   if (degpol(P) == 1) return Q;
    1855         602 :   if (degpol(Q) == 1) return P;
    1856         511 :   if (!gequal(P,Q)) pari_err_TYPE("mfsamefield [different fields]",mkvec2(P,Q));
    1857         504 :   if (T) err_cyclo();
    1858         504 :   return P;
    1859             : }
    1860             : 
    1861             : GEN
    1862         455 : mfmul(GEN f, GEN g)
    1863             : {
    1864         455 :   pari_sp av = avma;
    1865             :   GEN T, N, K, NK, CHI, CHIf, CHIg;
    1866         455 :   if (!checkmf_i(f)) pari_err_TYPE("mfmul",f);
    1867         455 :   if (!checkmf_i(g)) pari_err_TYPE("mfmul",g);
    1868         455 :   N = lcmii(mf_get_gN(f), mf_get_gN(g));
    1869         455 :   K = gadd(mf_get_gk(f), mf_get_gk(g));
    1870         455 :   CHIf = mf_get_CHI(f);
    1871         455 :   CHIg = mf_get_CHI(g);
    1872         455 :   CHI = mfchiadjust(mfcharmul(CHIf,CHIg), K, itos(N));
    1873         455 :   T = chicompat(CHI, CHIf, CHIg);
    1874         455 :   NK = mkgNK(N, K, CHI, mfsamefield(T, mf_get_field(f), mf_get_field(g)));
    1875         448 :   return gerepilecopy(av, T? tag3(t_MF_MUL,NK,f,g,T): tag2(t_MF_MUL,NK,f,g));
    1876             : }
    1877             : GEN
    1878          70 : mfpow(GEN f, long n)
    1879             : {
    1880          70 :   pari_sp av = avma;
    1881             :   GEN T, KK, NK, gn, CHI, CHIf;
    1882          70 :   if (!checkmf_i(f)) pari_err_TYPE("mfpow",f);
    1883          70 :   if (!n) return mf1();
    1884          70 :   if (n == 1) return gcopy(f);
    1885          70 :   KK = gmulsg(n,mf_get_gk(f));
    1886          70 :   gn = stoi(n);
    1887          70 :   CHIf = mf_get_CHI(f);
    1888          70 :   CHI = mfchiadjust(mfcharpow(CHIf,gn), KK, mf_get_N(f));
    1889          70 :   T = chicompat(CHI, CHIf, CHIf);
    1890          63 :   NK = mkgNK(mf_get_gN(f), KK, CHI, mf_get_field(f));
    1891          63 :   return gerepilecopy(av, T? tag3(t_MF_POW,NK,f,gn,T): tag2(t_MF_POW,NK,f,gn));
    1892             : }
    1893             : GEN
    1894          28 : mfbracket(GEN f, GEN g, long m)
    1895             : {
    1896          28 :   pari_sp av = avma;
    1897             :   GEN T, N, K, NK, CHI, CHIf, CHIg;
    1898          28 :   if (!checkmf_i(f)) pari_err_TYPE("mfbracket",f);
    1899          28 :   if (!checkmf_i(g)) pari_err_TYPE("mfbracket",g);
    1900          28 :   if (m < 0) pari_err_TYPE("mfbracket [m<0]",stoi(m));
    1901          28 :   K = gaddgs(gadd(mf_get_gk(f), mf_get_gk(g)), 2*m);
    1902          28 :   if (gsigne(K) < 0) pari_err_IMPL("mfbracket for this form");
    1903          28 :   N = lcmii(mf_get_gN(f), mf_get_gN(g));
    1904          28 :   CHIf = mf_get_CHI(f);
    1905          28 :   CHIg = mf_get_CHI(g);
    1906          28 :   CHI = mfcharmul(CHIf, CHIg);
    1907          28 :   CHI = mfchiadjust(CHI, K, itou(N));
    1908          28 :   T = chicompat(CHI, CHIf, CHIg);
    1909          28 :   NK = mkgNK(N, K, CHI, mfsamefield(T, mf_get_field(f), mf_get_field(g)));
    1910          56 :   return gerepilecopy(av, T? tag4(t_MF_BRACKET, NK, f, g, utoi(m), T)
    1911          28 :                            : tag3(t_MF_BRACKET, NK, f, g, utoi(m)));
    1912             : }
    1913             : 
    1914             : /* remove 0 entries in L */
    1915             : static int
    1916        1260 : mflinear_strip(GEN *pF, GEN *pL)
    1917             : {
    1918        1260 :   pari_sp av = avma;
    1919        1260 :   GEN F = *pF, L = *pL;
    1920        1260 :   long i, j, l = lg(L);
    1921        1260 :   GEN F2 = cgetg(l, t_VEC), L2 = cgetg(l, t_VEC);
    1922        7693 :   for (i = j = 1; i < l; i++)
    1923             :   {
    1924        6433 :     if (gequal0(gel(L,i))) continue;
    1925        3633 :     gel(F2,j) = gel(F,i);
    1926        3633 :     gel(L2,j) = gel(L,i); j++;
    1927             :   }
    1928        1260 :   if (j == l) set_avma(av);
    1929             :   else
    1930             :   {
    1931         371 :     setlg(F2,j); *pF = F2;
    1932         371 :     setlg(L2,j); *pL = L2;
    1933             :   }
    1934        1260 :   return (j > 1);
    1935             : }
    1936             : static GEN
    1937        6076 : taglinear_i(long t, GEN NK, GEN F, GEN L)
    1938             : {
    1939             :   GEN dL;
    1940        6076 :   L = Q_remove_denom(L, &dL); if (!dL) dL = gen_1;
    1941        6076 :   return tag3(t, NK, F, L, dL);
    1942             : }
    1943             : static GEN
    1944        2464 : taglinear(GEN NK, GEN F, GEN L)
    1945             : {
    1946        2464 :   long t = ok_bhn_linear(F)? t_MF_LINEAR_BHN: t_MF_LINEAR;
    1947        2464 :    return taglinear_i(t, NK, F, L);
    1948             : }
    1949             : /* assume F has parameters NK = [N,K,CHI] */
    1950             : static GEN
    1951         294 : mflinear_i(GEN NK, GEN F, GEN L)
    1952             : {
    1953         294 :   if (!mflinear_strip(&F,&L)) return mftrivial();
    1954         294 :   return taglinear(NK, F,L);
    1955             : }
    1956             : static GEN
    1957         511 : mflinear_bhn(GEN mf, GEN L)
    1958             : {
    1959             :   long i, l;
    1960         511 :   GEN P, NK, F = MF_get_S(mf);
    1961         511 :   if (!mflinear_strip(&F,&L)) return mftrivial();
    1962         504 :   l = lg(L); P = pol_x(1);
    1963        2653 :   for (i = 1; i < l; i++)
    1964             :   {
    1965        2149 :     GEN c = gel(L,i);
    1966        2149 :     if (typ(c) == t_POLMOD && varn(gel(c,1)) == 1)
    1967         518 :       P = mfsamefield(NULL, P, gel(c,1));
    1968             :   }
    1969         504 :   NK = mkgNK(MF_get_gN(mf), MF_get_gk(mf), MF_get_CHI(mf), P);
    1970         504 :   return taglinear_i(t_MF_LINEAR_BHN,  NK, F,L);
    1971             : }
    1972             : 
    1973             : /* F vector of forms with same weight and character but varying level, return
    1974             :  * global [N,k,chi,P] */
    1975             : static GEN
    1976        3087 : vecmfNK(GEN F)
    1977             : {
    1978        3087 :   long i, l = lg(F);
    1979             :   GEN N, f;
    1980        3087 :   if (l == 1) return mkNK(1, 0, mfchartrivial());
    1981        3087 :   f = gel(F,1); N = mf_get_gN(f);
    1982       44499 :   for (i = 2; i < l; i++) N = lcmii(N, mf_get_gN(gel(F,i)));
    1983        3087 :   return mkgNK(N, mf_get_gk(f), mf_get_CHI(f), mf_get_field(f));
    1984             : }
    1985             : /* do not use mflinear: mflineardivtomat rely on F being constant across the
    1986             :  * basis where mflinear strips the ones matched by 0 coeffs. Assume k and CHI
    1987             :  * constant, N is allowed to vary. */
    1988             : static GEN
    1989        1176 : vecmflinear(GEN F, GEN C)
    1990             : {
    1991        1176 :   long i, t, l = lg(C);
    1992        1176 :   GEN NK, v = cgetg(l, t_VEC);
    1993        1176 :   if (l == 1) return v;
    1994        1176 :   t = ok_bhn_linear(F)? t_MF_LINEAR_BHN: t_MF_LINEAR;
    1995        1176 :   NK = vecmfNK(F);
    1996        4284 :   for (i = 1; i < l; i++) gel(v,i) = taglinear_i(t, NK, F, gel(C,i));
    1997        1176 :   return v;
    1998             : }
    1999             : /* vecmflinear(F,C), then divide everything by E, which has valuation 0 */
    2000             : static GEN
    2001         406 : vecmflineardiv0(GEN F, GEN C, GEN E)
    2002             : {
    2003         406 :   GEN v = vecmflinear(F, C);
    2004         406 :   long i, l = lg(v);
    2005         406 :   if (l == 1) return v;
    2006         406 :   gel(v,1) = mfdiv_val(gel(v,1), E, 0);
    2007        1568 :   for (i = 2; i < l; i++)
    2008             :   { /* v[i] /= E */
    2009        1162 :     GEN f = shallowcopy(gel(v,1));
    2010        1162 :     gel(f,2) = gel(v,i);
    2011        1162 :     gel(v,i) = f;
    2012             :   }
    2013         406 :   return v;
    2014             : }
    2015             : 
    2016             : /* Non empty linear combination of linear combinations of same
    2017             :  * F_j=\sum_i \mu_{i,j}G_i so R = \sum_i (\sum_j(\la_j\mu_{i,j})) G_i */
    2018             : static GEN
    2019        1911 : mflinear_linear(GEN F, GEN L, int strip)
    2020             : {
    2021        1911 :   long l = lg(F), j;
    2022        1911 :   GEN vF, M = cgetg(l, t_MAT);
    2023        1911 :   L = shallowcopy(L);
    2024       17976 :   for (j = 1; j < l; j++)
    2025             :   {
    2026       16065 :     GEN f = gel(F,j), c = gel(f,3), d = gel(f,4);
    2027       16065 :     if (typ(c) == t_VEC) c = shallowtrans(c);
    2028       16065 :     if (!isint1(d)) gel(L,j) = gdiv(gel(L,j),d);
    2029       16065 :     gel(M,j) = c;
    2030             :   }
    2031        1911 :   vF = gmael(F,1,2); L = RgM_RgC_mul(M,L);
    2032        1911 :   if (strip && !mflinear_strip(&vF,&L)) return mftrivial();
    2033        1911 :   return taglinear(vecmfNK(vF), vF, L);
    2034             : }
    2035             : /* F nonempty vector of forms of the form mfdiv(mflinear(B,v), E) where E
    2036             :  * does not vanish at oo, or mflinear(B,v). Apply mflinear(F, L) */
    2037             : static GEN
    2038        1911 : mflineardiv_linear(GEN F, GEN L, int strip)
    2039             : {
    2040        1911 :   long l = lg(F), j;
    2041             :   GEN v, E, f;
    2042        1911 :   if (lg(L) != l) pari_err_DIM("mflineardiv_linear");
    2043        1911 :   f = gel(F,1); /* l > 1 */
    2044        1911 :   if (mf_get_type(f) != t_MF_DIV) return mflinear_linear(F,L,strip);
    2045        1645 :   E = gel(f,3);
    2046        1645 :   v = cgetg(l, t_VEC);
    2047       16807 :   for (j = 1; j < l; j++) { GEN f = gel(F,j); gel(v,j) = gel(f,2); }
    2048        1645 :   return mfdiv_val(mflinear_linear(v,L,strip), E, 0);
    2049             : }
    2050             : static GEN
    2051         441 : vecmflineardiv_linear(GEN F, GEN M)
    2052             : {
    2053         441 :   long i, l = lg(M);
    2054         441 :   GEN v = cgetg(l, t_VEC);
    2055        1806 :   for (i = 1; i < l; i++) gel(v,i) = mflineardiv_linear(F, gel(M,i), 0);
    2056         441 :   return v;
    2057             : }
    2058             : 
    2059             : static GEN
    2060         630 : tobasis(GEN mf, GEN F, GEN L)
    2061             : {
    2062         630 :   if (checkmf_i(L) && mf) return mftobasis(mf, L, 0);
    2063         623 :   if (typ(F) != t_VEC) pari_err_TYPE("mflinear",F);
    2064         623 :   if (!is_vec_t(typ(L))) pari_err_TYPE("mflinear",L);
    2065         623 :   if (lg(L) != lg(F)) pari_err_DIM("mflinear");
    2066         623 :   return L;
    2067             : }
    2068             : GEN
    2069         672 : mflinear(GEN F, GEN L)
    2070             : {
    2071         672 :   pari_sp av = avma;
    2072         672 :   GEN G, NK, P, mf = checkMF_i(F), N = NULL, K = NULL, CHI = NULL;
    2073             :   long i, l;
    2074         672 :   if (mf)
    2075             :   {
    2076         525 :     GEN gk = MF_get_gk(mf);
    2077         525 :     F = MF_get_basis(F);
    2078         525 :     if (typ(gk) != t_INT)
    2079          42 :       return gerepilecopy(av, mflineardiv_linear(F, L, 1));
    2080         483 :     if (itou(gk) > 1 && space_is_cusp(MF_get_space(mf)))
    2081             :     {
    2082         266 :       L = tobasis(mf, F, L);
    2083         266 :       return gerepilecopy(av, mflinear_bhn(mf, L));
    2084             :     }
    2085             :   }
    2086         364 :   L = tobasis(mf, F, L);
    2087         364 :   if (!mflinear_strip(&F,&L)) return mftrivial();
    2088             : 
    2089         357 :   l = lg(F);
    2090         357 :   if (l == 2 && gequal1(gel(L,1))) return gerepilecopy(av, gel(F,1));
    2091         273 :   P = pol_x(1);
    2092         868 :   for (i = 1; i < l; i++)
    2093             :   {
    2094         602 :     GEN f = gel(F,i), c = gel(L,i), Ni, Ki;
    2095         602 :     if (!checkmf_i(f)) pari_err_TYPE("mflinear", f);
    2096         602 :     Ni = mf_get_gN(f); N = N? lcmii(N, Ni): Ni;
    2097         602 :     Ki = mf_get_gk(f);
    2098         602 :     if (!K) K = Ki;
    2099         329 :     else if (!gequal(K, Ki))
    2100           7 :       pari_err_TYPE("mflinear [different weights]", mkvec2(K,Ki));
    2101         595 :     P = mfsamefield(NULL, P, mf_get_field(f));
    2102         595 :     if (typ(c) == t_POLMOD && varn(gel(c,1)) == 1)
    2103         126 :       P = mfsamefield(NULL, P, gel(c,1));
    2104             :   }
    2105         266 :   G = znstar0(N,1);
    2106         847 :   for (i = 1; i < l; i++)
    2107             :   {
    2108         588 :     GEN CHI2 = mf_get_CHI(gel(F,i));
    2109         588 :     CHI2 = induce(G, CHI2);
    2110         588 :     if (!CHI) CHI = CHI2;
    2111         322 :     else if (!gequal(CHI, CHI2))
    2112           7 :       pari_err_TYPE("mflinear [different characters]", mkvec2(CHI,CHI2));
    2113             :   }
    2114         259 :   NK = mkgNK(N, K, CHI, P);
    2115         259 :   return gerepilecopy(av, taglinear(NK,F,L));
    2116             : }
    2117             : 
    2118             : GEN
    2119          42 : mfshift(GEN F, long sh)
    2120             : {
    2121          42 :   pari_sp av = avma;
    2122          42 :   if (!checkmf_i(F)) pari_err_TYPE("mfshift",F);
    2123          42 :   return gerepilecopy(av, tag2(t_MF_SHIFT, mf_get_NK(F), F, stoi(sh)));
    2124             : }
    2125             : static long
    2126          49 : mfval(GEN F)
    2127             : {
    2128          49 :   pari_sp av = avma;
    2129          49 :   long i = 0, n, sb;
    2130             :   GEN gk, gN;
    2131          49 :   if (!checkmf_i(F)) pari_err_TYPE("mfval", F);
    2132          49 :   gN = mf_get_gN(F);
    2133          49 :   gk = mf_get_gk(F);
    2134          49 :   sb = mfsturmNgk(itou(gN), gk);
    2135          70 :   for (n = 1; n <= sb;)
    2136             :   {
    2137             :     GEN v;
    2138          63 :     if (n > 0.5*sb) n = sb+1;
    2139          63 :     v = mfcoefs_i(F, n, 1);
    2140         119 :     for (; i <= n; i++)
    2141          98 :       if (!gequal0(gel(v, i+1))) return gc_long(av,i);
    2142          21 :     n <<= 1;
    2143             :   }
    2144           7 :   return gc_long(av,-1);
    2145             : }
    2146             : 
    2147             : GEN
    2148        2079 : mfdiv_val(GEN f, GEN g, long vg)
    2149             : {
    2150             :   GEN T, N, K, NK, CHI, CHIf, CHIg;
    2151        2079 :   if (vg) { f = mfshift(f,vg); g = mfshift(g,vg); }
    2152        2079 :   N = lcmii(mf_get_gN(f), mf_get_gN(g));
    2153        2079 :   K = gsub(mf_get_gk(f), mf_get_gk(g));
    2154        2079 :   CHIf = mf_get_CHI(f);
    2155        2079 :   CHIg = mf_get_CHI(g);
    2156        2079 :   CHI = mfchiadjust(mfchardiv(CHIf, CHIg), K, itos(N));
    2157        2079 :   T = chicompat(CHI, CHIf, CHIg);
    2158        2072 :   NK = mkgNK(N, K, CHI, mfsamefield(T, mf_get_field(f), mf_get_field(g)));
    2159        2072 :   return T? tag3(t_MF_DIV, NK, f, g, T): tag2(t_MF_DIV, NK, f, g);
    2160             : }
    2161             : GEN
    2162          49 : mfdiv(GEN F, GEN G)
    2163             : {
    2164          49 :   pari_sp av = avma;
    2165          49 :   long v = mfval(G);
    2166          49 :   if (!checkmf_i(F)) pari_err_TYPE("mfdiv", F);
    2167          42 :   if (v < 0 || (v && !gequal0(mfcoefs(F, v-1, 1))))
    2168          14 :     pari_err_DOMAIN("mfdiv", "ord(G)", ">", strtoGENstr("ord(F)"),
    2169             :                     mkvec2(F, G));
    2170          28 :   return gerepilecopy(av, mfdiv_val(F, G, v));
    2171             : }
    2172             : GEN
    2173         154 : mfderiv(GEN F, long m)
    2174             : {
    2175         154 :   pari_sp av = avma;
    2176             :   GEN NK, gk;
    2177         154 :   if (!checkmf_i(F)) pari_err_TYPE("mfderiv",F);
    2178         154 :   gk = gaddgs(mf_get_gk(F), 2*m);
    2179         154 :   NK = mkgNK(mf_get_gN(F), gk, mf_get_CHI(F), mf_get_field(F));
    2180         154 :   return gerepilecopy(av, tag2(t_MF_DERIV, NK, F, stoi(m)));
    2181             : }
    2182             : GEN
    2183          21 : mfderivE2(GEN F, long m)
    2184             : {
    2185          21 :   pari_sp av = avma;
    2186             :   GEN NK, gk;
    2187          21 :   if (!checkmf_i(F)) pari_err_TYPE("mfderivE2",F);
    2188          21 :   if (m < 0) pari_err_DOMAIN("mfderivE2","m","<",gen_0,stoi(m));
    2189          21 :   gk = gaddgs(mf_get_gk(F), 2*m);
    2190          21 :   NK = mkgNK(mf_get_gN(F), gk, mf_get_CHI(F), mf_get_field(F));
    2191          21 :   return gerepilecopy(av, tag2(t_MF_DERIVE2, NK, F, stoi(m)));
    2192             : }
    2193             : 
    2194             : GEN
    2195          14 : mftwist(GEN F, GEN D)
    2196             : {
    2197          14 :   pari_sp av = avma;
    2198             :   GEN NK, CHI, NT, Da;
    2199             :   long q;
    2200          14 :   if (!checkmf_i(F)) pari_err_TYPE("mftwist", F);
    2201          14 :   if (typ(D) != t_INT) pari_err_TYPE("mftwist", D);
    2202          14 :   Da = mpabs_shallow(D);
    2203          14 :   CHI = mf_get_CHI(F); q = mfcharconductor(CHI);
    2204          14 :   NT = glcm(glcm(mf_get_gN(F), mulsi(q, Da)), sqri(Da));
    2205          14 :   NK = mkgNK(NT, mf_get_gk(F), CHI, mf_get_field(F));
    2206          14 :   return gerepilecopy(av, tag2(t_MF_TWIST, NK, F, D));
    2207             : }
    2208             : 
    2209             : /***************************************************************/
    2210             : /*                 Generic cache handling                      */
    2211             : /***************************************************************/
    2212             : enum { cache_FACT, cache_DIV, cache_H, cache_D, cache_DIH };
    2213             : typedef struct {
    2214             :   const char *name;
    2215             :   GEN cache;
    2216             :   ulong minself, maxself;
    2217             :   void (*init)(long);
    2218             :   ulong miss, maxmiss;
    2219             :   long compressed;
    2220             : } cache;
    2221             : 
    2222             : static void constfact(long lim);
    2223             : static void constdiv(long lim);
    2224             : static void consttabh(long lim);
    2225             : static void consttabdihedral(long lim);
    2226             : static void constcoredisc(long lim);
    2227             : static THREAD cache caches[] = {
    2228             : { "Factors",  NULL,  50000,    50000, &constfact, 0, 0, 0 },
    2229             : { "Divisors", NULL,  50000,    50000, &constdiv, 0, 0, 0 },
    2230             : { "H",        NULL, 100000, 10000000, &consttabh, 0, 0, 1 },
    2231             : { "CorediscF",NULL, 100000, 10000000, &constcoredisc, 0, 0, 0 },
    2232             : { "Dihedral", NULL,   1000,     3000, &consttabdihedral, 0, 0, 0 },
    2233             : };
    2234             : 
    2235             : static void
    2236         703 : cache_reset(long id) { caches[id].miss = caches[id].maxmiss = 0; }
    2237             : static void
    2238        8445 : cache_delete(long id) { guncloneNULL(caches[id].cache); }
    2239             : static void
    2240         717 : cache_set(long id, GEN S)
    2241             : {
    2242         717 :   GEN old = caches[id].cache;
    2243         717 :   caches[id].cache = gclone(S);
    2244         717 :   guncloneNULL(old);
    2245         717 : }
    2246             : 
    2247             : /* handle a cache miss: store stats, possibly reset table; return value
    2248             :  * if (now) cached; return NULL on failure. HACK: some caches contain an
    2249             :  * ulong where the 0 value is impossible, and return it (typecast to GEN) */
    2250             : static GEN
    2251   364863004 : cache_get(long id, ulong D)
    2252             : {
    2253   364863004 :   cache *S = &caches[id];
    2254   364863004 :   const ulong d = S->compressed? D>>1: D;
    2255             :   ulong max, l;
    2256             : 
    2257   364863004 :   if (!S->cache)
    2258             :   {
    2259         467 :     max = maxuu(minuu(D, S->maxself), S->minself);
    2260         467 :     S->init(max);
    2261         467 :     l = lg(S->cache);
    2262             :   }
    2263             :   else
    2264             :   {
    2265   364862537 :     l = lg(S->cache);
    2266   364862537 :     if (l <= d)
    2267             :     {
    2268        1182 :       if (D > S->maxmiss) S->maxmiss = D;
    2269        1182 :       if (DEBUGLEVEL >= 3)
    2270           0 :         err_printf("miss in cache %s: %lu, max = %lu\n",
    2271             :                    S->name, D, S->maxmiss);
    2272        1182 :       if (S->miss++ >= 5 && D < S->maxself)
    2273             :       {
    2274          92 :         max = minuu(S->maxself, (long)(S->maxmiss * 1.2));
    2275          92 :         if (max <= S->maxself)
    2276             :         {
    2277          92 :           if (DEBUGLEVEL >= 3)
    2278           0 :             err_printf("resetting cache %s to %lu\n", S->name, max);
    2279          92 :           S->init(max); l = lg(S->cache);
    2280             :         }
    2281             :       }
    2282             :     }
    2283             :   }
    2284   364863004 :   return (l <= d)? NULL: gel(S->cache, d);
    2285             : }
    2286             : static GEN
    2287          70 : cache_report(long id)
    2288             : {
    2289          70 :   cache *S = &caches[id];
    2290          70 :   GEN v = zerocol(5);
    2291          70 :   gel(v,1) = strtoGENstr(S->name);
    2292          70 :   if (S->cache)
    2293             :   {
    2294          35 :     gel(v,2) = utoi(lg(S->cache)-1);
    2295          35 :     gel(v,3) = utoi(S->miss);
    2296          35 :     gel(v,4) = utoi(S->maxmiss);
    2297          35 :     gel(v,5) = utoi(gsizebyte(S->cache));
    2298             :   }
    2299          70 :   return v;
    2300             : }
    2301             : GEN
    2302          14 : getcache(void)
    2303             : {
    2304          14 :   pari_sp av = avma;
    2305          14 :   GEN M = cgetg(6, t_MAT);
    2306          14 :   gel(M,1) = cache_report(cache_FACT);
    2307          14 :   gel(M,2) = cache_report(cache_DIV);
    2308          14 :   gel(M,3) = cache_report(cache_H);
    2309          14 :   gel(M,4) = cache_report(cache_D);
    2310          14 :   gel(M,5) = cache_report(cache_DIH);
    2311          14 :   return gerepilecopy(av, shallowtrans(M));
    2312             : }
    2313             : 
    2314             : void
    2315        1689 : pari_close_mf(void)
    2316             : {
    2317        1689 :   cache_delete(cache_FACT);
    2318        1689 :   cache_delete(cache_DIV);
    2319        1689 :   cache_delete(cache_H);
    2320        1689 :   cache_delete(cache_D);
    2321        1689 :   cache_delete(cache_DIH);
    2322        1689 : }
    2323             : 
    2324             : /*************************************************************************/
    2325             : /* a odd, update local cache (recycle memory) */
    2326             : static GEN
    2327        4184 : update_factor_cache(long a, long lim, long *pb)
    2328             : {
    2329        4184 :   const long step = 16000; /* even; don't increase this: RAM cache thrashing */
    2330        4184 :   if (a + 2*step > lim)
    2331         338 :     *pb = lim; /* fuse last 2 chunks */
    2332             :   else
    2333        3846 :     *pb = a + step;
    2334        4184 :   return vecfactoroddu_i(a, *pb);
    2335             : }
    2336             : /* assume lim < MAX_LONG/8 */
    2337             : static void
    2338          96 : constcoredisc(long lim)
    2339             : {
    2340          96 :   pari_sp av2, av = avma;
    2341          96 :   GEN D = caches[cache_D].cache, CACHE = NULL;
    2342          96 :   long cachea, cacheb, N, LIM = !D ? 4 : lg(D)-1;
    2343          96 :   if (lim <= 0) lim = 5;
    2344          96 :   if (lim <= LIM) return;
    2345          96 :   cache_reset(cache_D);
    2346          96 :   D = zero_zv(lim);
    2347          79 :   av2 = avma;
    2348          79 :   cachea = cacheb = 0;
    2349     9929422 :   for (N = 1; N <= lim; N+=2)
    2350             :   { /* N odd */
    2351             :     long i, d, d2;
    2352             :     GEN F;
    2353     9929326 :     if (N > cacheb)
    2354             :     {
    2355        1206 :       set_avma(av2); cachea = N;
    2356        1206 :       CACHE = update_factor_cache(N, lim, &cacheb);
    2357             :     }
    2358     9929326 :     F = gel(CACHE, ((N-cachea)>>1)+1); /* factoru(N) */
    2359     9929326 :     D[N] = d = corediscs_fact(F); /* = 3 mod 4 or 4 mod 16 */
    2360     9929796 :     d2 = odd(d)? d<<3: d<<1;
    2361     9933105 :     for (i = 1;;)
    2362             :     {
    2363    13245286 :       if ((N << i) > lim) break;
    2364     6625154 :       D[N<<i] = d2; i++;
    2365     6625154 :       if ((N << i) > lim) break;
    2366     3312181 :       D[N<<i] = d; i++;
    2367             :     }
    2368             :   }
    2369          96 :   cache_set(cache_D, D);
    2370          96 :   set_avma(av);
    2371             : }
    2372             : 
    2373             : static void
    2374         207 : constfact(long lim)
    2375             : {
    2376             :   pari_sp av;
    2377         207 :   GEN VFACT = caches[cache_FACT].cache;
    2378         207 :   long LIM = VFACT? lg(VFACT)-1: 4;
    2379         207 :   if (lim <= 0) lim = 5;
    2380         207 :   if (lim <= LIM) return;
    2381         186 :   cache_reset(cache_FACT); av = avma;
    2382         186 :   cache_set(cache_FACT, vecfactoru_i(1,lim)); set_avma(av);
    2383             : }
    2384             : static void
    2385         179 : constdiv(long lim)
    2386             : {
    2387             :   pari_sp av;
    2388         179 :   GEN VFACT, VDIV = caches[cache_DIV].cache;
    2389         179 :   long N, LIM = VDIV? lg(VDIV)-1: 4;
    2390         179 :   if (lim <= 0) lim = 5;
    2391         179 :   if (lim <= LIM) return;
    2392         179 :   constfact(lim);
    2393         179 :   VFACT = caches[cache_FACT].cache;
    2394         179 :   cache_reset(cache_DIV); av = avma;
    2395         179 :   VDIV  = cgetg(lim+1, t_VEC);
    2396     8098321 :   for (N = 1; N <= lim; N++) gel(VDIV,N) = divisorsu_fact(gel(VFACT,N));
    2397         179 :   cache_set(cache_DIV, VDIV); set_avma(av);
    2398             : }
    2399             : 
    2400             : /* n > 1, D = divisors(n); sets L = 2*lambda(n), S = sigma(n) */
    2401             : static void
    2402    23814755 : lamsig(GEN D, long *pL, long *pS)
    2403             : {
    2404    23814755 :   pari_sp av = avma;
    2405    23814755 :   long i, l = lg(D), L = 1, S = D[l-1]+1;
    2406    89920722 :   for (i = 2; i < l; i++) /* skip d = 1 */
    2407             :   {
    2408    92977519 :     long d = D[i], nd = D[l-i]; /* nd = n/d */
    2409    92977519 :     if (d < nd) { L += d; S += d + nd; }
    2410             :     else
    2411             :     {
    2412    26871552 :       L <<= 1; if (d == nd) { L += d; S += d; }
    2413    26871552 :       break;
    2414             :     }
    2415             :   }
    2416    23814755 :   set_avma(av); *pL = L; *pS = S;
    2417    27418337 : }
    2418             : /* table of 6 * Hurwitz class numbers D <= lim */
    2419             : static void
    2420         242 : consttabh(long lim)
    2421             : {
    2422         242 :   pari_sp av = avma, av2;
    2423         242 :   GEN VHDH0, VDIV, CACHE = NULL;
    2424         242 :   GEN VHDH = caches[cache_H].cache;
    2425         242 :   long r, N, cachea, cacheb, lim0 = VHDH? lg(VHDH)-1: 2, LIM = lim0 << 1;
    2426             : 
    2427         242 :   if (lim <= 0) lim = 5;
    2428         242 :   if (lim <= LIM) return;
    2429         242 :   cache_reset(cache_H);
    2430         242 :   r = lim&3L; if (r) lim += 4-r;
    2431         242 :   cache_get(cache_DIV, lim);
    2432         242 :   VDIV = caches[cache_DIV].cache;
    2433         242 :   VHDH0 = cgetg(lim/2 + 1, t_VECSMALL);
    2434         242 :   VHDH0[1] = 2;
    2435         242 :   VHDH0[2] = 3;
    2436     6122722 :   for (N = 3; N <= lim0; N++) VHDH0[N] = VHDH[N];
    2437         242 :   av2 = avma;
    2438         242 :   cachea = cacheb = 0;
    2439    13971133 :   for (N = LIM + 3; N <= lim; N += 4)
    2440             :   {
    2441    14041580 :     long s = 0, limt = usqrt(N>>2), flsq = 0, ind, t, L, S;
    2442             :     GEN DN, DN2;
    2443    14034565 :     if (N + 2 >= lg(VDIV))
    2444             :     { /* use local cache */
    2445             :       GEN F;
    2446    11889125 :       if (N + 2 > cacheb)
    2447             :       {
    2448        2978 :         set_avma(av2); cachea = N;
    2449        2978 :         CACHE = update_factor_cache(N, lim+2, &cacheb);
    2450             :       }
    2451    11889125 :       F = gel(CACHE, ((N-cachea)>>1)+1); /* factoru(N) */
    2452    11889125 :       DN = divisorsu_fact(F);
    2453    11361153 :       F = gel(CACHE, ((N-cachea)>>1)+2); /* factoru(N+2) */
    2454    11361153 :       DN2 = divisorsu_fact(F);
    2455             :     }
    2456             :     else
    2457             :     { /* use global cache */
    2458     2145440 :       DN = gel(VDIV,N);
    2459     2145440 :       DN2 = gel(VDIV,N+2);
    2460             :     }
    2461    13349193 :     ind = N >> 1;
    2462  1547092831 :     for (t = 1; t <= limt; t++)
    2463             :     {
    2464  1533743638 :       ind -= (t<<2)-2; /* N/2 - 2t^2 */
    2465  1533743638 :       if (ind) s += VHDH0[ind]; else flsq = 1;
    2466             :     }
    2467    13349193 :     lamsig(DN, &L,&S);
    2468    13600654 :     VHDH0[N >> 1] = 2*S - 3*L - 2*s + flsq;
    2469    13600654 :     s = 0; flsq = 0; limt = (usqrt(N+2) - 1) >> 1;
    2470    12682853 :     ind = (N+1) >> 1;
    2471  1550652597 :     for (t = 1; t <= limt; t++)
    2472             :     {
    2473  1537969744 :       ind -= t<<2; /* (N+1)/2 - 2t(t+1) */
    2474  1537969744 :       if (ind) s += VHDH0[ind]; else flsq = 1;
    2475             :     }
    2476    12682853 :     lamsig(DN2, &L,&S);
    2477    13970891 :     VHDH0[(N+1) >> 1] = S - 3*(L >> 1) - s - flsq;
    2478             :   }
    2479         127 :   cache_set(cache_H, VHDH0); set_avma(av);
    2480             : }
    2481             : 
    2482             : /*************************************************************************/
    2483             : /* Core functions using factorizations, divisors of class numbers caches */
    2484             : /* TODO: myfactoru and factorization cache should be exported */
    2485             : static GEN
    2486    23327496 : myfactoru(long N)
    2487             : {
    2488    23327496 :   GEN z = cache_get(cache_FACT, N);
    2489    23327496 :   return z? gcopy(z): factoru(N);
    2490             : }
    2491             : static GEN
    2492    48881309 : mydivisorsu(long N)
    2493             : {
    2494    48881309 :   GEN z = cache_get(cache_DIV, N);
    2495    48881309 :   return z? leafcopy(z): divisorsu(N);
    2496             : }
    2497             : /* write -n = Df^2, D < 0 fundamental discriminant. Return D, set f. */
    2498             : static long
    2499   150118366 : mycoredisc2neg(ulong n, long *pf)
    2500             : {
    2501   150118366 :   ulong m, D = (ulong)cache_get(cache_D, n);
    2502   150118366 :   if (D) { *pf = usqrt(n/D); return -(long)D; }
    2503         260 :   m = mycore(n, pf);
    2504         260 :   if ((m&3) != 3) { m <<= 2; *pf >>= 1; }
    2505         260 :   return (long)-m;
    2506             : }
    2507             : /* write n = Df^2, D > 0 fundamental discriminant. Return D, set f. */
    2508             : static long
    2509          14 : mycoredisc2pos(ulong n, long *pf)
    2510             : {
    2511          14 :   ulong m = mycore(n, pf);
    2512          14 :   if ((m&3) != 1) { m <<= 2; *pf >>= 1; }
    2513          14 :   return (long)m;
    2514             : }
    2515             : 
    2516             : /* 1+p+...+p^e, e >= 1 */
    2517             : static ulong
    2518          57 : usumpow(ulong p, long e)
    2519             : {
    2520          57 :   ulong q = 1+p;
    2521             :   long i;
    2522         106 :   for (i = 1; i < e; i++) q = p*q + 1;
    2523          57 :   return q;
    2524             : }
    2525             : /* Hurwitz(D0 F^2)/ Hurwitz(D0)
    2526             :  * = \sum_{f|F}  f \prod_{p|f} (1-kro(D0/p)/p)
    2527             :  * = \prod_{p^e || F} (1 + (p^e-1) / (p-1) * (p-kro(D0/p))) */
    2528             : static long
    2529         341 : get_sh(long F, long D0)
    2530             : {
    2531         341 :   GEN fa = myfactoru(F), P = gel(fa,1), E = gel(fa,2);
    2532         341 :   long i, l = lg(P), t = 1;
    2533         911 :   for (i = 1; i < l; i++)
    2534             :   {
    2535         570 :     long p = P[i], e = E[i], s = kross(D0,p);
    2536         570 :     if (e == 1) { t *= 1 + p - s; continue; }
    2537         176 :     if (s == 1) { t *= upowuu(p,e); continue; }
    2538          57 :     t *= 1 + usumpow(p,e-1)*(p-s);
    2539             :   }
    2540         341 :   return t;
    2541             : }
    2542             : /* d > 0, d = 0,3 (mod 4). Return 6*hclassno(d); -d must be fundamental
    2543             :  * Faster than quadclassunit up to 5*10^5 or so */
    2544             : static ulong
    2545          40 : hclassno6u_count(ulong d)
    2546             : {
    2547          40 :   ulong a, b, b2, h = 0;
    2548          40 :   int f = 0;
    2549             : 
    2550          40 :   if (d > 500000)
    2551           7 :     return 6 * itou(gel(quadclassunit0(utoineg(d), 0, NULL, 0), 1));
    2552             : 
    2553             :   /* this part would work with -d non fundamental */
    2554          33 :   b = d&1; b2 = (1+d)>>2;
    2555          33 :   if (!b)
    2556             :   {
    2557         331 :     for (a=1; a*a<b2; a++)
    2558         330 :       if (b2%a == 0) h++;
    2559           1 :     f = (a*a==b2); b=2; b2=(4+d)>>2;
    2560             :   }
    2561        6646 :   while (b2*3 < d)
    2562             :   {
    2563        6613 :     if (b2%b == 0) h++;
    2564     1096547 :     for (a=b+1; a*a < b2; a++)
    2565     1089934 :       if (b2%a == 0) h += 2;
    2566        6613 :     if (a*a == b2) h++;
    2567        6613 :     b += 2; b2 = (b*b+d)>>2;
    2568             :   }
    2569          33 :   if (b2*3 == d) return 6*h+2;
    2570          33 :   if (f) return 6*h+3;
    2571          33 :   return 6*h;
    2572             : }
    2573             : /* D > 0; 6 * hclassno(D), using D = D0*F^2 */
    2574             : static long
    2575         381 : hclassno6u_2(ulong D, long D0, long F)
    2576             : {
    2577             :   long h;
    2578         381 :   if (F == 1) h = hclassno6u_count(D);
    2579             :   else
    2580             :   { /* second chance */
    2581         341 :     h = (ulong)cache_get(cache_H, -D0);
    2582         341 :     if (!h) h = hclassno6u_count(-D0);
    2583         341 :     h *= get_sh(F,D0);
    2584             :   }
    2585         381 :   return h;
    2586             : }
    2587             : /* D > 0; 6 * hclassno(D) (6*Hurwitz). Beware, cached value for D (=0,3 mod 4)
    2588             :  * is stored at D>>1 */
    2589             : ulong
    2590      158870 : hclassno6u(ulong D)
    2591             : {
    2592      158870 :   ulong z = (ulong)cache_get(cache_H, D);
    2593             :   long D0, F;
    2594      158870 :   if (z) return z;
    2595         374 :   D0 = mycoredisc2neg(D, &F);
    2596         374 :   return hclassno6u_2(D,D0,F);
    2597             : }
    2598             : /* same as hclassno6u without creating caches */
    2599             : ulong
    2600      125698 : hclassno6u_from_cache(ulong D)
    2601             : {
    2602      125698 :   cache *S = &caches[cache_H];
    2603             :   long D0, F;
    2604      125698 :   if (S->cache)
    2605             :   {
    2606      110600 :     const ulong d = D>>1; /* compressed */
    2607      110600 :     if ((ulong)lg(S->cache) > d) return S->cache[d];
    2608             :   }
    2609      125253 :   S = &caches[cache_D];
    2610      125253 :   if (!S->cache || (ulong)lg(S->cache) <= D) return 0;
    2611           7 :   D0 = mycoredisc2neg(D, &F);
    2612           7 :   return hclassno6u_2(D,D0,F);
    2613             : }
    2614             : /* same, where the decomposition D = D0*F^2 is already known */
    2615             : static ulong
    2616   135970680 : hclassno6u_i(ulong D, long D0, long F)
    2617             : {
    2618   135970680 :   ulong z = (ulong)cache_get(cache_H, D);
    2619   135970680 :   if (z) return z;
    2620           0 :   return hclassno6u_2(D,D0,F);
    2621             : }
    2622             : 
    2623             : #if 0
    2624             : /* D > 0, return h(-D) [ordinary class number].
    2625             :  * Assume consttabh(D or more) was previously called */
    2626             : static long
    2627             : hfromH(long D)
    2628             : {
    2629             :   pari_sp ltop = avma;
    2630             :   GEN m, d, fa = myfactoru(D), P = gel(fa,1), E = gel(fa,2);
    2631             :   GEN VH = caches[cache_H].cache;
    2632             :   long i, nd, S, l = lg(P);
    2633             : 
    2634             :   /* n = d[i] loops through squarefree divisors of f, where f^2 = largest square
    2635             :    * divisor of N = |D|; m[i] = moebius(n) */
    2636             :   nd = 1 << (l-1);
    2637             :   d = cgetg(nd+1, t_VECSMALL);
    2638             :   m = cgetg(nd+1, t_VECSMALL);
    2639             :   d[1] = 1; S = VH[D >> 1]; /* 6 hclassno(-D) */
    2640             :   m[1] = 1; nd = 1;
    2641             :   i = 1;
    2642             :   if (P[1] == 2 && E[1] <= 3) /* need D/n^2 to be a discriminant */
    2643             :   { if (odd(E[1]) || (E[1] == 2 && (D & 15) == 4)) i = 2; }
    2644             :   for (; i<l; i++)
    2645             :   {
    2646             :     long j, p = P[i];
    2647             :     if (E[i] == 1) continue;
    2648             :     for (j=1; j<=nd; j++)
    2649             :     {
    2650             :       long n, s, hn;
    2651             :       d[nd+j] = n = d[j] * p;
    2652             :       m[nd+j] = s = - m[j]; /* moebius(n) */
    2653             :       hn = VH[(D/(n*n)) >> 1]; /* 6 hclassno(-D/n^2) */
    2654             :       if (s > 0) S += hn; else S -= hn;
    2655             :     }
    2656             :     nd <<= 1;
    2657             :   }
    2658             :   return gc_long(ltop, S/6);
    2659             : }
    2660             : #endif
    2661             : /* D < -4 fundamental, h(D), ordinary class number */
    2662             : static long
    2663     6393499 : myh(long D)
    2664             : {
    2665     6393499 :   ulong z = (ulong)cache_get(cache_H, -D);
    2666     6393499 :   if (z) return z/6; /* should be hfromH(-D) if D nonfundamental */
    2667           0 :   return itou(quadclassno(stoi(D)));
    2668             : }
    2669             : 
    2670             : /*************************************************************************/
    2671             : /*                          TRACE FORMULAS                               */
    2672             : /* CHIP primitive, initialize for t_POLMOD output */
    2673             : static GEN
    2674       30527 : mfcharinit(GEN CHIP)
    2675             : {
    2676       30527 :   long n, o, l, vt, N = mfcharmodulus(CHIP);
    2677             :   GEN c, v, V, G, Pn;
    2678       30527 :   if (N == 1) return mkvec2(mkvec(gen_1), pol_x(0));
    2679        5348 :   G = gel(CHIP,1);
    2680        5348 :   v = ncharvecexpo(G, znconrey_normalized(G, gel(CHIP,2)));
    2681        5348 :   l = lg(v); V = cgetg(l, t_VEC);
    2682        5348 :   o = mfcharorder(CHIP);
    2683        5348 :   Pn = mfcharpol(CHIP); vt = varn(Pn);
    2684        5348 :   if (o <= 2)
    2685             :   {
    2686       58135 :     for (n = 1; n < l; n++)
    2687             :     {
    2688       53739 :       if (v[n] < 0) c = gen_0; else c = v[n]? gen_m1: gen_1;
    2689       53739 :       gel(V,n) = c;
    2690             :     }
    2691             :   }
    2692             :   else
    2693             :   {
    2694       16835 :     for (n = 1; n < l; n++)
    2695             :     {
    2696       15883 :       if (v[n] < 0) c = gen_0;
    2697             :       else
    2698             :       {
    2699        8890 :         c = Qab_zeta(v[n], o, vt);
    2700        8890 :         if (typ(c) == t_POL && lg(c) >= lg(Pn)) c = RgX_rem(c, Pn);
    2701             :       }
    2702       15883 :       gel(V,n) = c;
    2703             :     }
    2704             :   }
    2705        5348 :   return mkvec2(V, Pn);
    2706             : }
    2707             : static GEN
    2708      363790 : vchip_lift(GEN VCHI, long x, GEN C)
    2709             : {
    2710      363790 :   GEN V = gel(VCHI,1);
    2711      363790 :   long F = lg(V)-1;
    2712      363790 :   if (F == 1) return C;
    2713       18368 :   x %= F;
    2714       18368 :   if (!x) return C;
    2715       18368 :   if (x <= 0) x += F;
    2716       18368 :   return gmul(C, gel(V, x));
    2717             : }
    2718             : static long
    2719   224765515 : vchip_FC(GEN VCHI) { return lg(gel(VCHI,1))-1; }
    2720             : static GEN
    2721     5475918 : vchip_mod(GEN VCHI, GEN S)
    2722     5475918 : { return (typ(S) == t_POL)? RgX_rem(S, gel(VCHI,2)): S; }
    2723             : static GEN
    2724     1769142 : vchip_polmod(GEN VCHI, GEN S)
    2725     1769142 : { return (typ(S) == t_POL)? mkpolmod(S, gel(VCHI,2)): S; }
    2726             : 
    2727             : /* ceil(m/d) */
    2728             : static long
    2729      170086 : ceildiv(long m, long d)
    2730             : {
    2731             :   long q;
    2732      170086 :   if (!m) return 0;
    2733       45290 :   q = m/d; return m%d? q+1: q;
    2734             : }
    2735             : 
    2736             : /* contribution of scalar matrices in dimension formula */
    2737             : static GEN
    2738      332584 : A1(long N, long k)
    2739      332584 : { return sstoQ(mypsiu(N)*(k-1), 12); }
    2740             : static long
    2741        7364 : ceilA1(long N, long k)
    2742        7364 : { return ceildiv(mypsiu(N) * (k-1), 12); }
    2743             : 
    2744             : /* sturm bound, slightly larger than dimension */
    2745             : long
    2746       21084 : mfsturmNk(long N, long k) { return (mypsiu(N) * k) / 12; }
    2747             : long
    2748        2450 : mfsturmNgk(long N, GEN k)
    2749             : {
    2750        2450 :   long n,d; Qtoss(k,&n,&d);
    2751        2450 :   return 1 + (mypsiu(N)*n)/(d == 1? 12: 24);
    2752             : }
    2753             : static long
    2754          49 : mfsturmmf(GEN F) { return mfsturmNgk(mf_get_N(F), mf_get_gk(F)); }
    2755             : 
    2756             : /* List of all solutions of x^2 + x + 1 = 0 modulo N, x modulo N */
    2757             : static GEN
    2758         539 : sqrtm3modN(long N)
    2759             : {
    2760             :   pari_sp av;
    2761             :   GEN fa, P, E, B, mB, A, Q, T, R, v, gen_m3;
    2762         539 :   long l, i, n, ct, fl3 = 0, Ninit;
    2763         539 :   if (!odd(N) || (N%9) == 0) return cgetg(1,t_VECSMALL);
    2764         511 :   Ninit = N;
    2765         511 :   if ((N%3) == 0) { N /= 3; fl3 = 1; }
    2766         511 :   fa = myfactoru(N); P = gel(fa, 1); E = gel(fa, 2);
    2767         511 :   l = lg(P);
    2768         707 :   for (i = 1; i < l; i++)
    2769         518 :     if ((P[i]%3) == 2) return cgetg(1,t_VECSMALL);
    2770         189 :   A = cgetg(l, t_VECSMALL);
    2771         189 :   B = cgetg(l, t_VECSMALL);
    2772         189 :   mB= cgetg(l, t_VECSMALL);
    2773         189 :   Q = cgetg(l, t_VECSMALL); gen_m3 = utoineg(3);
    2774         385 :   for (i = 1; i < l; i++)
    2775             :   {
    2776         196 :     long p = P[i], e = E[i];
    2777         196 :     Q[i] = upowuu(p,e);
    2778         196 :     B[i] = itou( Zp_sqrt(gen_m3, utoipos(p), e) );
    2779         196 :     mB[i]= Q[i] - B[i];
    2780             :   }
    2781         189 :   ct = 1 << (l-1);
    2782         189 :   T = ZV_producttree(Q);
    2783         189 :   R = ZV_chinesetree(Q,T);
    2784         189 :   v = cgetg(ct+1, t_VECSMALL);
    2785         189 :   av = avma;
    2786         581 :   for (n = 1; n <= ct; n++)
    2787             :   {
    2788         392 :     long m = n-1, r;
    2789         812 :     for (i = 1; i < l; i++)
    2790             :     {
    2791         420 :       A[i] = (m&1L)? mB[i]: B[i];
    2792         420 :       m >>= 1;
    2793             :     }
    2794         392 :     r = itou( ZV_chinese_tree(A, Q, T, R) );
    2795         462 :     if (fl3) while (r%3) r += N;
    2796         392 :     set_avma(av); v[n] = odd(r) ? (r-1) >> 1 : (r+Ninit-1) >> 1;
    2797             :   }
    2798         189 :   return v;
    2799             : }
    2800             : 
    2801             : /* number of elliptic points of order 3 in X0(N) */
    2802             : static long
    2803        9744 : nu3(long N)
    2804             : {
    2805             :   long i, l;
    2806             :   GEN P;
    2807        9744 :   if (!odd(N) || (N%9) == 0) return 0;
    2808        8680 :   if ((N%3) == 0) N /= 3;
    2809        8680 :   P = gel(myfactoru(N), 1); l = lg(P);
    2810       12789 :   for (i = 1; i < l; i++) if ((P[i]%3) == 2) return 0;
    2811        3927 :   return 1L<<(l-1);
    2812             : }
    2813             : /* number of elliptic points of order 2 in X0(N) */
    2814             : static long
    2815       16856 : nu2(long N)
    2816             : {
    2817             :   long i, l;
    2818             :   GEN P;
    2819       16856 :   if ((N&3L) == 0) return 0;
    2820       16856 :   if (!odd(N)) N >>= 1;
    2821       16856 :   P = gel(myfactoru(N), 1); l = lg(P);
    2822       21133 :   for (i = 1; i < l; i++) if ((P[i]&3L) == 3) return 0;
    2823        3836 :   return 1L<<(l-1);
    2824             : }
    2825             : 
    2826             : /* contribution of elliptic matrices of order 3 in dimension formula
    2827             :  * Only depends on CHIP the primitive char attached to CHI */
    2828             : static GEN
    2829       42385 : A21(long N, long k, GEN CHI)
    2830             : {
    2831             :   GEN res, G, chi, o;
    2832             :   long a21, i, limx, S;
    2833       42385 :   if ((N&1L) == 0) return gen_0;
    2834       20293 :   a21 = k%3 - 1;
    2835       20293 :   if (!a21) return gen_0;
    2836       19649 :   if (N <= 3) return sstoQ(a21, 3);
    2837       10283 :   if (!CHI) return sstoQ(nu3(N) * a21, 3);
    2838         539 :   res = sqrtm3modN(N); limx = (N - 1) >> 1;
    2839         539 :   G = gel(CHI,1); chi = gel(CHI,2);
    2840         539 :   o = gmfcharorder(CHI);
    2841         931 :   for (S = 0, i = 1; i < lg(res); i++)
    2842             :   { /* (x,N) = 1; S += chi(x) + chi(x^2) */
    2843         392 :     long x = res[i];
    2844         392 :     if (x <= limx)
    2845             :     { /* CHI(x)=e(c/o), 3rd-root of 1 */
    2846         196 :       GEN c = znchareval(G, chi, utoi(x), o);
    2847         196 :       if (!signe(c)) S += 2; else S--;
    2848             :     }
    2849             :   }
    2850         539 :   return sstoQ(a21 * S, 3);
    2851             : }
    2852             : 
    2853             : /* List of all square roots of -1 modulo N */
    2854             : static GEN
    2855         595 : sqrtm1modN(long N)
    2856             : {
    2857             :   pari_sp av;
    2858             :   GEN fa, P, E, B, mB, A, Q, T, R, v;
    2859         595 :   long l, i, n, ct, fleven = 0;
    2860         595 :   if ((N&3L) == 0) return cgetg(1,t_VECSMALL);
    2861         595 :   if ((N&1L) == 0) { N >>= 1; fleven = 1; }
    2862         595 :   fa = myfactoru(N); P = gel(fa,1); E = gel(fa,2);
    2863         595 :   l = lg(P);
    2864         945 :   for (i = 1; i < l; i++)
    2865         665 :     if ((P[i]&3L) == 3) return cgetg(1,t_VECSMALL);
    2866         280 :   A = cgetg(l, t_VECSMALL);
    2867         280 :   B = cgetg(l, t_VECSMALL);
    2868         280 :   mB= cgetg(l, t_VECSMALL);
    2869         280 :   Q = cgetg(l, t_VECSMALL);
    2870         574 :   for (i = 1; i < l; i++)
    2871             :   {
    2872         294 :     long p = P[i], e = E[i];
    2873         294 :     Q[i] = upowuu(p,e);
    2874         294 :     B[i] = itou( Zp_sqrt(gen_m1, utoipos(p), e) );
    2875         294 :     mB[i]= Q[i] - B[i];
    2876             :   }
    2877         280 :   ct = 1 << (l-1);
    2878         280 :   T = ZV_producttree(Q);
    2879         280 :   R = ZV_chinesetree(Q,T);
    2880         280 :   v = cgetg(ct+1, t_VECSMALL);
    2881         280 :   av = avma;
    2882         868 :   for (n = 1; n <= ct; n++)
    2883             :   {
    2884         588 :     long m = n-1, r;
    2885        1232 :     for (i = 1; i < l; i++)
    2886             :     {
    2887         644 :       A[i] = (m&1L)? mB[i]: B[i];
    2888         644 :       m >>= 1;
    2889             :     }
    2890         588 :     r = itou( ZV_chinese_tree(A, Q, T, R) );
    2891         588 :     if (fleven && !odd(r)) r += N;
    2892         588 :     set_avma(av); v[n] = r;
    2893             :   }
    2894         280 :   return v;
    2895             : }
    2896             : 
    2897             : /* contribution of elliptic matrices of order 4 in dimension formula.
    2898             :  * Only depends on CHIP the primitive char attached to CHI */
    2899             : static GEN
    2900       42385 : A22(long N, long k, GEN CHI)
    2901             : {
    2902             :   GEN G, chi, o, res;
    2903             :   long S, a22, i, limx, o2;
    2904       42385 :   if ((N&3L) == 0) return gen_0;
    2905       29085 :   a22 = (k & 3L) - 1; /* (k % 4) - 1 */
    2906       29085 :   if (!a22) return gen_0;
    2907       29085 :   if (N <= 2) return sstoQ(a22, 4);
    2908       17661 :   if (!CHI) return sstoQ(nu2(N)*a22, 4);
    2909         805 :   if (mfcharparity(CHI) == -1) return gen_0;
    2910         595 :   res = sqrtm1modN(N); limx = (N - 1) >> 1;
    2911         595 :   G = gel(CHI,1); chi = gel(CHI,2);
    2912         595 :   o = gmfcharorder(CHI);
    2913         595 :   o2 = itou(o)>>1;
    2914        1183 :   for (S = 0, i = 1; i < lg(res); i++)
    2915             :   { /* (x,N) = 1, S += real(chi(x)) */
    2916         588 :     long x = res[i];
    2917         588 :     if (x <= limx)
    2918             :     { /* CHI(x)=e(c/o), 4th-root of 1 */
    2919         294 :       long c = itou( znchareval(G, chi, utoi(x), o) );
    2920         294 :       if (!c) S++; else if (c == o2) S--;
    2921             :     }
    2922             :   }
    2923         595 :   return sstoQ(a22 * S, 2);
    2924             : }
    2925             : 
    2926             : /* sumdiv(N,d,eulerphi(gcd(d,N/d))) */
    2927             : static long
    2928       37737 : nuinf(long N)
    2929             : {
    2930       37737 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
    2931       37737 :   long i, t = 1, l = lg(P);
    2932       80178 :   for (i=1; i<l; i++)
    2933             :   {
    2934       42441 :     long p = P[i], e = E[i];
    2935       42441 :     if (odd(e))
    2936       33936 :       t *= upowuu(p,e>>1) << 1;
    2937             :     else
    2938        8505 :       t *= upowuu(p,(e>>1)-1) * (p+1);
    2939             :   }
    2940       37737 :   return t;
    2941             : }
    2942             : 
    2943             : /* contribution of hyperbolic matrices in dimension formula */
    2944             : static GEN
    2945       42833 : A3(long N, long FC)
    2946             : {
    2947             :   long i, S, NF, l;
    2948             :   GEN D;
    2949       42833 :   if (FC == 1) return sstoQ(nuinf(N),2);
    2950        5096 :   D = mydivisorsu(N); l = lg(D);
    2951        5096 :   S = 0; NF = N/FC;
    2952       40579 :   for (i = 1; i < l; i++)
    2953             :   {
    2954       35483 :     long g = ugcd(D[i], D[l-i]);
    2955       35483 :     if (NF%g == 0) S += myeulerphiu(g);
    2956             :   }
    2957        5096 :   return sstoQ(S, 2);
    2958             : }
    2959             : 
    2960             : /* special contribution in weight 2 in dimension formula */
    2961             : static long
    2962       41993 : A4(long k, long FC)
    2963       41993 : { return (k==2 && FC==1)? 1: 0; }
    2964             : /* gcd(x,N) */
    2965             : static long
    2966   232804327 : myugcd(GEN GCD, ulong x)
    2967             : {
    2968   232804327 :   ulong N = lg(GCD)-1;
    2969   232804327 :   if (x >= N) x %= N;
    2970   232804327 :   return GCD[x+1];
    2971             : }
    2972             : /* 1_{gcd(x,N) = 1} * chi(x), return NULL if 0 */
    2973             : static GEN
    2974   330711167 : mychicgcd(GEN GCD, GEN VCHI, long x)
    2975             : {
    2976   330711167 :   long N = lg(GCD)-1;
    2977   330711167 :   if (N == 1) return gen_1;
    2978   262349834 :   x = umodsu(x, N);
    2979   262349834 :   if (GCD[x+1] != 1) return NULL;
    2980   218046960 :   x %= vchip_FC(VCHI); if (!x) return gen_1;
    2981     4468065 :   return gel(gel(VCHI,1), x);
    2982             : }
    2983             : 
    2984             : /* contribution of scalar matrices to trace formula */
    2985             : static GEN
    2986     5578242 : TA1(long N, long k, GEN VCHI, GEN GCD, long n)
    2987             : {
    2988             :   GEN S;
    2989             :   ulong m;
    2990     5578242 :   if (!uissquareall(n, &m)) return gen_0;
    2991      355621 :   if (m == 1) return A1(N,k); /* common */
    2992      319697 :   S = mychicgcd(GCD, VCHI, m);
    2993      319697 :   return S? gmul(gmul(powuu(m, k-2), A1(N,k)), S): gen_0;
    2994             : }
    2995             : 
    2996             : /* All square roots modulo 4N, x modulo 2N, precomputed to accelerate TA2 */
    2997             : static GEN
    2998      118538 : mksqr(long N)
    2999             : {
    3000      118538 :   pari_sp av = avma;
    3001      118538 :   long x, N2 = N << 1, N4 = N << 2;
    3002      118538 :   GEN v = const_vec(N2, cgetg(1, t_VECSMALL));
    3003      118538 :   gel(v, N2) = mkvecsmall(0); /* x = 0 */
    3004     3302726 :   for (x = 1; x <= N; x++)
    3005             :   {
    3006     3184188 :     long r = (((x*x - 1)%N4) >> 1) + 1;
    3007     3184188 :     gel(v,r) = vecsmall_append(gel(v,r), x);
    3008             :   }
    3009      118538 :   return gerepilecopy(av, v);
    3010             : }
    3011             : 
    3012             : static GEN
    3013      118538 : mkgcd(long N)
    3014             : {
    3015             :   GEN GCD, d;
    3016             :   long i, N2;
    3017      118538 :   if (N == 1) return mkvecsmall(N);
    3018       97825 :   GCD = cgetg(N + 1, t_VECSMALL);
    3019       97825 :   d = GCD+1; /* GCD[i+1] = d[i] = gcd(i,N) = gcd(N-i,N), i = 0..N-1 */
    3020       97825 :   d[0] = N; d[1] = d[N-1] = 1; N2 = N>>1;
    3021     1562736 :   for (i = 2; i <= N2; i++) d[i] = d[N-i] = ugcd(N, i);
    3022       97825 :   return GCD;
    3023             : }
    3024             : 
    3025             : /* Table of \sum_{x^2-tx+n=0 mod Ng}chi(x) for all g dividing gcd(N,F),
    3026             :  * F^2 largest such that (t^2-4n)/F^2=0 or 1 mod 4; t >= 0 */
    3027             : static GEN
    3028    10312768 : mutglistall(long t, long N, long NF, GEN VCHI, long n, GEN MUP, GEN li, GEN GCD)
    3029             : {
    3030    10312768 :   long i, lx = lg(li);
    3031    10312768 :   GEN DNF = mydivisorsu(NF), v = zerovec(NF);
    3032    10312768 :   long j, g, lDNF = lg(DNF);
    3033    28588613 :   for (i = 1; i < lx; i++)
    3034             :   {
    3035    18275845 :     long x = (li[i] + t) >> 1, y, lD;
    3036    18275845 :     GEN D, c = mychicgcd(GCD, VCHI, x);
    3037    18275845 :     if (li[i] && li[i] != N)
    3038             :     {
    3039    11857775 :       GEN c2 = mychicgcd(GCD, VCHI, t - x);
    3040    11857775 :       if (c2) c = c? gadd(c, c2): c2;
    3041             :     }
    3042    18275845 :     if (!c) continue;
    3043    13676572 :     y = (x*(x - t) + n) / N; /* exact division */
    3044    13676572 :     D = mydivisorsu(ugcd(labs(y), NF)); lD = lg(D);
    3045    38083628 :     for (j=1; j < lD; j++) { g = D[j]; gel(v,g) = gadd(gel(v,g), c); }
    3046             :   }
    3047             :   /* j = 1 corresponds to g = 1, and MUP[1] = 1 */
    3048    24950945 :   for (j=2; j < lDNF; j++) { g = DNF[j]; gel(v,g) = gmulsg(MUP[g], gel(v,g)); }
    3049    10312768 :   return v;
    3050             : }
    3051             : 
    3052             : /* special case (N,F) = 1: easier */
    3053             : static GEN
    3054   139805203 : mutg1(long t, long N, GEN VCHI, GEN li, GEN GCD)
    3055             : { /* (N,F) = 1 */
    3056   139805203 :   GEN S = NULL;
    3057   139805203 :   long i, lx = lg(li);
    3058   291524707 :   for (i = 1; i < lx; i++)
    3059             :   {
    3060   151719504 :     long x = (li[i] + t) >> 1;
    3061   151719504 :     GEN c = mychicgcd(GCD, VCHI, x);
    3062   151719504 :     if (c) S = S? gadd(S, c): c;
    3063   151719504 :     if (li[i] && li[i] != N)
    3064             :     {
    3065    81585840 :       c = mychicgcd(GCD, VCHI, t - x);
    3066    81585840 :       if (c) S = S? gadd(S, c): c;
    3067             :     }
    3068   151719504 :     if (S && !signe(S)) S = NULL; /* strive hard to add gen_0 */
    3069             :   }
    3070   139805203 :   return S; /* single value */
    3071             : }
    3072             : 
    3073             : /* Gegenbauer pol; n > 2, P = \sum_{0<=j<=n/2} (-1)^j (n-j)!/j!(n-2*j)! X^j */
    3074             : GEN
    3075      359371 : mfrhopol(long n)
    3076             : {
    3077             : #ifdef LONG_IS_64BIT
    3078      308076 :   const long M = 2642249;
    3079             : #else
    3080       51295 :   const long M = 1629;
    3081             : #endif
    3082      359371 :   long j, d = n >> 1; /* >= 1 */
    3083      359371 :   GEN P = cgetg(d + 3, t_POL);
    3084             : 
    3085      359371 :   if (n > M) pari_err_IMPL("mfrhopol for large weight"); /* avoid overflow */
    3086      359371 :   P[1] = evalvarn(0)|evalsigne(1);
    3087      359371 :   gel(P,2) = gen_1;
    3088      359371 :   gel(P,3) = utoineg(n-1); /* j = 1 */
    3089      359371 :   if (d > 1) gel(P,4) = utoipos(((n-3)*(n-2)) >> 1); /* j = 2 */
    3090      359371 :   if (d > 2) gel(P,5) = utoineg(((n-5)*(n-4)*(n-3)) / 6); /* j = 3 */
    3091     1508396 :   for (j = 4; j <= d; j++)
    3092     1149025 :     gel(P,j+2) = divis(mulis(gel(P,j+1), (n-2*j+1)*(n-2*j+2)), (n-j+1)*(-j));
    3093      359371 :   return P;
    3094             : }
    3095             : 
    3096             : /* polrecip(Q)(t2), assume Q(0) = 1 */
    3097             : GEN
    3098     3225029 : mfrhopol_u_eval(GEN Q, ulong t2)
    3099             : {
    3100     3225029 :   GEN T = addiu(gel(Q,3), t2);
    3101     3225024 :   long l = lg(Q), j;
    3102    37536118 :   for (j = 4; j < l; j++) T = addii(gel(Q,j), mului(t2, T));
    3103     3225028 :   return T;
    3104             : }
    3105             : GEN
    3106       56619 : mfrhopol_eval(GEN Q, GEN t2)
    3107             : {
    3108             :   long l, j;
    3109             :   GEN T;
    3110       56619 :   if (lgefint(t2) == 3) return mfrhopol_u_eval(Q, t2[2]);
    3111           0 :   l = lg(Q); T = addii(gel(Q,3), t2);
    3112           0 :   for (j = 4; j < l; j++) T = addii(gel(Q,j), mulii(t2, T));
    3113           0 :   return T;
    3114             : }
    3115             : /* return sh * sqrt(n)^nu * G_nu(t/(2*sqrt(n))) for t != 0
    3116             :  * else (sh/2) * sqrt(n)^nu * G_nu(0) [ implies nu is even ]
    3117             :  * G_nu(z) = \sum_{0<=j<=nu/2} (-1)^j (nu-j)!/j!(nu-2*j)! * (2z)^(nu-2*j)) */
    3118             : static GEN
    3119   143103831 : mfrhopowsimp(GEN Q, GEN sh, long nu, long t, long t2, long n)
    3120             : {
    3121             :   GEN T;
    3122   143103831 :   switch (nu)
    3123             :   {
    3124   137248951 :     case 0: return t? sh: gmul2n(sh,-1);
    3125     1125075 :     case 1: return gmulsg(t, sh);
    3126     1515647 :     case 2: return t? gmulsg(t2 - n, sh): gmul(gmul2n(stoi(-n), -1), sh);
    3127         427 :     case 3: return gmul(mulss(t, t2 - 2*n), sh);
    3128     3213731 :     default:
    3129     3213731 :       if (!t) return gmul(gmul2n(gel(Q, lg(Q) - 1), -1), sh);
    3130     3168408 :       T = mfrhopol_u_eval(Q, t2); if (odd(nu)) T = mulsi(t, T);
    3131     3168408 :       return gmul(T, sh);
    3132             :   }
    3133             : }
    3134             : 
    3135             : /* contribution of elliptic matrices to trace formula */
    3136             : static GEN
    3137     5578242 : TA2(long N, long k, GEN VCHI, long n, GEN SQRTS, GEN MUP, GEN GCD)
    3138             : {
    3139     5578242 :   const long n4 = n << 2, N4 = N << 2, nu = k - 2;
    3140     5578242 :   const long st = (!odd(N) && odd(n)) ? 2 : 1;
    3141             :   long limt, t;
    3142             :   GEN S, Q;
    3143             : 
    3144     5578242 :   limt = usqrt(n4);
    3145     5578242 :   if (limt*limt == n4) limt--;
    3146     5578242 :   Q = nu > 3 ? ZX_z_unscale(mfrhopol(nu), n) : NULL;
    3147     5578242 :   S = gen_0;
    3148   257906982 :   for (t = odd(k)? st: 0; t <= limt; t += st) /* t^2 < 4n */
    3149             :   {
    3150   252328740 :     pari_sp av = avma;
    3151   252328740 :     long t2 = t*t, D = n4 - t2, F, D0, NF;
    3152             :     GEN sh, li;
    3153             : 
    3154   252328740 :     li = gel(SQRTS, (umodsu(-D - 1, N4) >> 1) + 1);
    3155   259342880 :     if (lg(li) == 1) continue;
    3156   150117971 :     D0 = mycoredisc2neg(D, &F);
    3157   150117971 :     NF = myugcd(GCD, F);
    3158   150117971 :     if (NF == 1)
    3159             :     { /* (N,F) = 1 => single value in mutglistall */
    3160   139805203 :       GEN mut = mutg1(t, N, VCHI, li, GCD);
    3161   139805203 :       if (!mut) { set_avma(av); continue; }
    3162   135970680 :       sh = gmul(sstoQ(hclassno6u_i(D,D0,F),6), mut);
    3163             :     }
    3164             :     else
    3165             :     {
    3166    10312768 :       GEN v = mutglistall(t, N, NF, VCHI, n, MUP, li, GCD);
    3167    10312768 :       GEN DF = mydivisorsu(F);
    3168    10312768 :       long i, lDF = lg(DF);
    3169    10312768 :       sh = gen_0;
    3170    41135830 :       for (i = 1; i < lDF; i++)
    3171             :       {
    3172    30823062 :         long Ff, f = DF[i], g = myugcd(GCD, f);
    3173    30823062 :         GEN mut = gel(v, g);
    3174    30823062 :         if (gequal0(mut)) continue;
    3175    18504556 :         Ff = DF[lDF-i]; /* F/f */
    3176    18504556 :         if (Ff == 1) sh = gadd(sh, mut);
    3177             :         else
    3178             :         {
    3179    13313127 :           GEN P = gel(myfactoru(Ff), 1);
    3180    13313127 :           long j, lP = lg(P);
    3181    28734592 :           for (j = 1; j < lP; j++) { long p = P[j]; Ff -= kross(D0, p)*Ff/p; }
    3182    13313127 :           sh = gadd(sh, gmulsg(Ff, mut));
    3183             :         }
    3184             :       }
    3185    10312768 :       if (gequal0(sh)) { set_avma(av); continue; }
    3186     7133151 :       if (D0 == -3) sh = gdivgs(sh, 3);
    3187     6744766 :       else if (D0 == -4) sh = gdivgs(sh, 2);
    3188     6393499 :       else sh = gmulgs(sh, myh(D0));
    3189             :     }
    3190   143103831 :     S = gerepileupto(av, gadd(S, mfrhopowsimp(Q,sh,nu,t,t2,n)));
    3191             :   }
    3192     5578242 :   return S;
    3193             : }
    3194             : 
    3195             : /* compute global auxiliary data for TA3 */
    3196             : static GEN
    3197      118538 : mkbez(long N, long FC)
    3198             : {
    3199      118538 :   long ct, i, NF = N/FC;
    3200      118538 :   GEN w, D = mydivisorsu(N);
    3201      118538 :   long l = lg(D);
    3202             : 
    3203      118538 :   w = cgetg(l, t_VEC);
    3204      345324 :   for (i = ct = 1; i < l; i++)
    3205             :   {
    3206      324611 :     long u, v, h, c = D[i], Nc = D[l-i];
    3207      324611 :     if (c > Nc) break;
    3208      226786 :     h = cbezout(c, Nc, &u, &v);
    3209      226786 :     if (h == 1) /* shortcut */
    3210      163037 :       gel(w, ct++) = mkvecsmall4(1,u*c,1,i);
    3211       63749 :     else if (!(NF%h))
    3212       53991 :       gel(w, ct++) = mkvecsmall4(h,u*(c/h),myeulerphiu(h),i);
    3213             :   }
    3214      118538 :   setlg(w,ct); stackdummy((pari_sp)(w+ct),(pari_sp)(w+l));
    3215      118538 :   return w;
    3216             : }
    3217             : 
    3218             : /* contribution of hyperbolic matrices to trace formula, d * nd = n,
    3219             :  * DN = divisorsu(N) */
    3220             : static GEN
    3221    28727243 : auxsum(GEN VCHI, GEN GCD, long d, long nd, GEN DN, GEN BEZ)
    3222             : {
    3223    28727243 :   GEN S = gen_0;
    3224    28727243 :   long ct, g = nd - d, lDN = lg(DN), lBEZ = lg(BEZ);
    3225    71432319 :   for (ct = 1; ct < lBEZ; ct++)
    3226             :   {
    3227    42705076 :     GEN y, B = gel(BEZ, ct);
    3228    42705076 :     long ic, c, Nc, uch, h = B[1];
    3229    42705076 :     if (g%h) continue;
    3230    42039061 :     uch = B[2];
    3231    42039061 :     ic  = B[4];
    3232    42039061 :     c = DN[ic];
    3233    42039061 :     Nc= DN[lDN - ic]; /* Nc = N/c */
    3234    42039061 :     if (ugcd(Nc, nd) == 1)
    3235    36012264 :       y = mychicgcd(GCD, VCHI, d + uch*g); /* 0 if (c,d) > 1 */
    3236             :     else
    3237     6026797 :       y = NULL;
    3238    42039061 :     if (c != Nc && ugcd(Nc, d) == 1)
    3239             :     {
    3240    30940242 :       GEN y2 = mychicgcd(GCD, VCHI, nd - uch*g); /* 0 if (c,nd) > 1 */
    3241    30940242 :       if (y2) y = y? gadd(y, y2): y2;
    3242             :     }
    3243    42039061 :     if (y) S = gadd(S, gmulsg(B[3], y));
    3244             :   }
    3245    28727243 :   return S;
    3246             : }
    3247             : 
    3248             : static GEN
    3249     5578242 : TA3(long N, long k, GEN VCHI, GEN GCD, GEN Dn, GEN BEZ)
    3250             : {
    3251     5578242 :   GEN S = gen_0, DN = mydivisorsu(N);
    3252     5578242 :   long i, l = lg(Dn);
    3253    34305485 :   for (i = 1; i < l; i++)
    3254             :   {
    3255    34269561 :     long d = Dn[i], nd = Dn[l-i]; /* = n/d */
    3256             :     GEN t, u;
    3257    34269561 :     if (d > nd) break;
    3258    28727243 :     t = auxsum(VCHI, GCD, d, nd, DN, BEZ);
    3259    28727243 :     if (isintzero(t)) continue;
    3260    27724038 :     u = powuu(d,k-1); if (d == nd) u = gmul2n(u,-1);
    3261    27724038 :     S = gadd(S, gmul(u,t));
    3262             :   }
    3263     5578242 :   return S;
    3264             : }
    3265             : 
    3266             : /* special contribution in weight 2 in trace formula */
    3267             : static long
    3268     5578242 : TA4(long k, GEN VCHIP, GEN Dn, GEN GCD)
    3269             : {
    3270             :   long i, l, S;
    3271     5578242 :   if (k != 2 || vchip_FC(VCHIP) != 1) return 0;
    3272     4896269 :   l = lg(Dn); S = 0;
    3273    56759563 :   for (i = 1; i < l; i++)
    3274             :   {
    3275    51863294 :     long d = Dn[i]; /* gcd(N,n/d) == 1? */
    3276    51863294 :     if (myugcd(GCD, Dn[l-i]) == 1) S += d;
    3277             :   }
    3278     4896269 :   return S;
    3279             : }
    3280             : 
    3281             : /* precomputation of products occurring im mutg, again to accelerate TA2 */
    3282             : static GEN
    3283      118538 : mkmup(long N)
    3284             : {
    3285      118538 :   GEN fa = myfactoru(N), P = gel(fa,1), D = divisorsu_fact(fa);
    3286      118538 :   long i, lP = lg(P), lD = lg(D);
    3287      118538 :   GEN MUP = zero_zv(N);
    3288      118538 :   MUP[1] = 1;
    3289      418957 :   for (i = 2; i < lD; i++)
    3290             :   {
    3291      300419 :     long j, g = D[i], Ng = D[lD-i]; /*  N/g */
    3292      823690 :     for (j = 1; j < lP; j++) { long p = P[j]; if (Ng%p) g += g/p; }
    3293      300419 :     MUP[D[i]] = g;
    3294             :   }
    3295      118538 :   return MUP;
    3296             : }
    3297             : 
    3298             : /* quadratic nonresidues mod p; p odd prime, p^2 fits in a long */
    3299             : static GEN
    3300        2653 : non_residues(long p)
    3301             : {
    3302        2653 :   long i, j, p2 = p >> 1;
    3303        2653 :   GEN v = cgetg(p2+1, t_VECSMALL), w = const_vecsmall(p-1, 1);
    3304        4410 :   for (i = 2; i <= p2; i++) w[(i*i) % p] = 0; /* no need to check 1 */
    3305        8820 :   for (i = 2, j = 1; i < p; i++) if (w[i]) v[j++] = i;
    3306        2653 :   return v;
    3307             : }
    3308             : 
    3309             : /* CHIP primitive. Return t_VECSMALL v of length q such that
    3310             :  * Tr^new_{N,CHIP}(n) = 0 whenever v[(n%q) + 1] is nonzero */
    3311             : static GEN
    3312       30625 : mfnewzerodata(long N, GEN CHIP)
    3313             : {
    3314       30625 :   GEN V, M, L, faN = myfactoru(N), PN = gel(faN,1), EN = gel(faN,2);
    3315       30625 :   GEN G = gel(CHIP,1), chi = gel(CHIP,2);
    3316       30625 :   GEN fa = znstar_get_faN(G), P = ZV_to_zv(gel(fa,1)), E = gel(fa,2);
    3317       30625 :   long i, mod, j = 1, l = lg(PN);
    3318             : 
    3319       30625 :   M = cgetg(l, t_VECSMALL); M[1] = 0;
    3320       30625 :   V = cgetg(l, t_VEC);
    3321             :   /* Tr^new(n) = 0 if (n mod M[i]) in V[i]  */
    3322       30625 :   if ((N & 3) == 0)
    3323             :   {
    3324       12355 :     long e = EN[1];
    3325       12355 :     long c = (lg(P) > 1 && P[1] == 2)? E[1]: 0; /* c = v_2(FC) */
    3326             :     /* e >= 2 */
    3327       12355 :     if (c == e-1) return NULL; /* Tr^new = 0 */
    3328       12250 :     if (c == e)
    3329             :     {
    3330        3584 :       if (e == 2)
    3331             :       { /* sc: -4 */
    3332        1764 :         gel(V,1) = mkvecsmall(3);
    3333        1764 :         M[1] = 4;
    3334             :       }
    3335        1820 :       else if (e == 3)
    3336             :       { /* sc: -8 (CHI_2(-1)=-1<=>chi[1]=1) and 8 (CHI_2(-1)=1 <=> chi[1]=0) */
    3337        1820 :         long t = signe(gel(chi,1))? 7: 3;
    3338        1820 :         gel(V,1) = mkvecsmall2(5, t);
    3339        1820 :         M[1] = 8;
    3340             :       }
    3341             :     }
    3342        8666 :     else if (e == 5 && c == 3)
    3343         154 :     { /* sc: -8 (CHI_2(-1)=-1<=>chi[1]=1) and 8 (CHI_2(-1)=1 <=> chi[1]=0) */
    3344         154 :       long t = signe(gel(chi,1))? 7: 3;
    3345         154 :       gel(V,1) = mkvecsmalln(6, 2L,4L,5L,6L,8L,t);
    3346         154 :       M[1] = 8;
    3347             :     }
    3348        8512 :     else if ((e == 4 && c == 2) || (e == 5 && c <= 2) || (e == 6 && c <= 2)
    3349        6937 :          || (e >= 7 && c == e - 3))
    3350             :     { /* sc: 4 */
    3351        1575 :       gel(V,1) = mkvecsmall3(0,2,3);
    3352        1575 :       M[1] = 4;
    3353             :     }
    3354        6937 :     else if ((e <= 4 && c == 0) || (e >= 5 && c == e - 2))
    3355             :     { /* sc: 2 */
    3356        6580 :       gel(V,1) = mkvecsmall(0);
    3357        6580 :       M[1] = 2;
    3358             :     }
    3359         357 :     else if ((e == 6 && c == 3) || (e >= 7 && c <= e - 4))
    3360             :     { /* sc: -2 */
    3361         357 :       gel(V,1) = mkvecsmalln(7, 0L,2L,3L,4L,5L,6L,7L);
    3362         357 :       M[1] = 8;
    3363             :     }
    3364             :   }
    3365       30520 :   j = M[1]? 2: 1;
    3366       65317 :   for (i = odd(N)? 1: 2; i < l; i++) /* skip p=2, done above */
    3367             :   {
    3368       34797 :     long p = PN[i], e = EN[i];
    3369       34797 :     long z = zv_search(P, p), c = z? E[z]: 0; /* c = v_p(FC) */
    3370       34797 :     if ((e <= 2 && c == 1 && itos(gel(chi,z)) == (p>>1)) /* ord(CHI_p)=2 */
    3371       32606 :         || (e >= 3 && c <= e - 2))
    3372        2653 :     { /* sc: -p */
    3373        2653 :       GEN v = non_residues(p);
    3374        2653 :       if (e != 1) v = vecsmall_prepend(v, 0);
    3375        2653 :       gel(V,j) = v;
    3376        2653 :       M[j] = p; j++;
    3377             :     }
    3378       32144 :     else if (e >= 2 && c < e)
    3379             :     { /* sc: p */
    3380        2142 :       gel(V,j) = mkvecsmall(0);
    3381        2142 :       M[j] = p; j++;
    3382             :     }
    3383             :   }
    3384       30520 :   if (j == 1) return cgetg(1, t_VECSMALL);
    3385       14287 :   setlg(V,j); setlg(M,j); mod = zv_prod(M);
    3386       14287 :   L = zero_zv(mod);
    3387       31332 :   for (i = 1; i < j; i++)
    3388             :   {
    3389       17045 :     GEN v = gel(V,i);
    3390       17045 :     long s, m = M[i], lv = lg(v);
    3391       44842 :     for (s = 1; s < lv; s++)
    3392             :     {
    3393       27797 :       long a = v[s] + 1;
    3394       54383 :       do { L[a] = 1; a += m; } while (a <= mod);
    3395             :     }
    3396             :   }
    3397       14287 :   return L;
    3398             : }
    3399             : /* v=mfnewzerodata(N,CHI); returns TRUE if newtrace(n) must be zero,
    3400             :  * (but newtrace(n) may still be zero if we return FALSE) */
    3401             : static long
    3402     2377607 : mfnewchkzero(GEN v, long n) { long q = lg(v)-1; return q && v[(n%q) + 1]; }
    3403             : 
    3404             : /* if (!VCHIP): from mftraceform_cusp;
    3405             :  * else from initnewtrace and CHI is known to be primitive */
    3406             : static GEN
    3407      118538 : inittrace(long N, GEN CHI, GEN VCHIP)
    3408             : {
    3409             :   long FC;
    3410      118538 :   if (VCHIP)
    3411      118531 :     FC = mfcharmodulus(CHI);
    3412             :   else
    3413           7 :     VCHIP = mfcharinit(mfchartoprimitive(CHI, &FC));
    3414      118538 :   return mkvecn(5, mksqr(N), mkmup(N), mkgcd(N), VCHIP, mkbez(N, FC));
    3415             : }
    3416             : 
    3417             : /* p > 2 prime; return a sorted t_VECSMALL of primes s.t Tr^new(p) = 0 for all
    3418             :  * weights > 2 */
    3419             : static GEN
    3420       30520 : inittrconj(long N, long FC)
    3421             : {
    3422             :   GEN fa, P, E, v;
    3423             :   long i, k, l;
    3424             : 
    3425       30520 :   if (FC != 1) return cgetg(1,t_VECSMALL);
    3426             : 
    3427       25172 :   fa = myfactoru(N >> vals(N));
    3428       25172 :   P = gel(fa,1); l = lg(P);
    3429       25172 :   E = gel(fa,2);
    3430       25172 :   v = cgetg(l, t_VECSMALL);
    3431       55027 :   for (i = k = 1; i < l; i++)
    3432             :   {
    3433       29855 :     long j, p = P[i]; /* > 2 */
    3434       72422 :     for (j = 1; j < l; j++)
    3435       42567 :       if (j != i && E[j] == 1 && kross(-p, P[j]) == 1) v[k++] = p;
    3436             :   }
    3437       25172 :   setlg(v,k); return v;
    3438             : }
    3439             : 
    3440             : /* assume CHIP primitive, f(CHIP) | N; NZ = mfnewzerodata(N,CHIP) */
    3441             : static GEN
    3442       30520 : initnewtrace_i(long N, GEN CHIP, GEN NZ)
    3443             : {
    3444       30520 :   GEN T = const_vec(N, cgetg(1,t_VEC)), D, VCHIP;
    3445       30520 :   long FC = mfcharmodulus(CHIP), N1, N2, i, l;
    3446             : 
    3447       30520 :   if (!NZ) NZ = mkvecsmall(1); /*Tr^new = 0; initialize data nevertheless*/
    3448       30520 :   VCHIP = mfcharinit(CHIP);
    3449       30520 :   N1 = N/FC; newd_params(N1, &N2);
    3450       30520 :   D = mydivisorsu(N1/N2); l = lg(D);
    3451       30520 :   N2 *= FC;
    3452      149051 :   for (i = 1; i < l; i++)
    3453             :   {
    3454      118531 :     long M = D[i]*N2;
    3455      118531 :     gel(T,M) = inittrace(M, CHIP, VCHIP);
    3456             :   }
    3457       30520 :   gel(T,N) = shallowconcat(gel(T,N), mkvec2(NZ, inittrconj(N,FC)));
    3458       30520 :   return T;
    3459             : }
    3460             : /* don't initialize if Tr^new = 0, return NULL */
    3461             : static GEN
    3462       30625 : initnewtrace(long N, GEN CHI)
    3463             : {
    3464       30625 :   GEN CHIP = mfchartoprimitive(CHI, NULL), NZ = mfnewzerodata(N,CHIP);
    3465       30625 :   return NZ? initnewtrace_i(N, CHIP, NZ): NULL;
    3466             : }
    3467             : 
    3468             : /* (-1)^k */
    3469             : static long
    3470        7931 : m1pk(long k) { return odd(k)? -1 : 1; }
    3471             : static long
    3472        7581 : badchar(long N, long k, GEN CHI)
    3473        7581 : { return mfcharparity(CHI) != m1pk(k) || (CHI && N % mfcharconductor(CHI)); }
    3474             : 
    3475             : 
    3476             : static long
    3477       42056 : mfcuspdim_i(long N, long k, GEN CHI, GEN vSP)
    3478             : {
    3479       42056 :   pari_sp av = avma;
    3480             :   long FC;
    3481             :   GEN s;
    3482       42056 :   if (k <= 0) return 0;
    3483       42056 :   if (k == 1) return CHI? mf1cuspdim(N, CHI, vSP): 0;
    3484       41811 :   FC = CHI? mfcharconductor(CHI): 1;
    3485       41811 :   if (FC == 1) CHI = NULL;
    3486       41811 :   s = gsub(A1(N, k), gadd(A21(N, k, CHI), A22(N, k, CHI)));
    3487       41811 :   s = gadd(s, gsubsg(A4(k, FC), A3(N, FC)));
    3488       41811 :   return gc_long(av, itos(s));
    3489             : }
    3490             : /* dimension of space of cusp forms S_k(\G_0(N),CHI)
    3491             :  * Only depends on CHIP the primitive char attached to CHI */
    3492             : long
    3493        3269 : mfcuspdim(long N, long k, GEN CHI) { return mfcuspdim_i(N, k, CHI, NULL); }
    3494             : 
    3495             : /* dimension of whole space M_k(\G_0(N),CHI)
    3496             :  * Only depends on CHIP the primitive char attached to CHI; assumes !badchar */
    3497             : long
    3498         791 : mffulldim(long N, long k, GEN CHI)
    3499             : {
    3500         791 :   pari_sp av = avma;
    3501         791 :   long FC = CHI? mfcharconductor(CHI): 1;
    3502             :   GEN s;
    3503         791 :   if (k <= 0) return (k == 0 && FC == 1)? 1: 0;
    3504         791 :   if (k == 1) return gc_long(av, itos(A3(N, FC)) + mf1cuspdim(N, CHI, NULL));
    3505         574 :   if (FC == 1) CHI = NULL;
    3506         574 :   s = gsub(A1(N, k), gadd(A21(N, k, CHI), A22(N, k, CHI)));
    3507         574 :   s = gadd(s, A3(N, FC));
    3508         574 :   return gc_long(av, itos(s));
    3509             : }
    3510             : 
    3511             : /* Dimension of the space of Eisenstein series */
    3512             : long
    3513         231 : mfeisensteindim(long N, long k, GEN CHI)
    3514             : {
    3515         231 :   pari_sp av = avma;
    3516         231 :   long s, FC = CHI? mfcharconductor(CHI): 1;
    3517         231 :   if (k <= 0) return (k == 0 && FC == 1)? 1: 0;
    3518         231 :   s = itos(gmul2n(A3(N, FC), 1));
    3519         231 :   if (k > 1) s -= A4(k, FC); else s >>= 1;
    3520         231 :   return gc_long(av,s);
    3521             : }
    3522             : 
    3523             : enum { _SQRTS = 1, _MUP, _GCD, _VCHIP, _BEZ, _NEWLZ, _TRCONJ };
    3524             : /* Trace of T(n) on space of cuspforms; only depends on CHIP the primitive char
    3525             :  * attached to CHI */
    3526             : static GEN
    3527     5578242 : mfcusptrace_i(long N, long k, long n, GEN Dn, GEN S)
    3528             : {
    3529     5578242 :   pari_sp av = avma;
    3530             :   GEN a, b, VCHIP, GCD;
    3531             :   long t;
    3532     5578242 :   if (!n) return gen_0;
    3533     5578242 :   VCHIP = gel(S,_VCHIP);
    3534     5578242 :   GCD = gel(S,_GCD);
    3535     5578242 :   t = TA4(k, VCHIP, Dn, GCD);
    3536     5578242 :   a = TA1(N, k, VCHIP, GCD, n); if (t) a = gaddgs(a,t);
    3537     5578242 :   b = TA2(N, k, VCHIP, n, gel(S,_SQRTS), gel(S,_MUP), GCD);
    3538     5578242 :   b = gadd(b, TA3(N, k, VCHIP, GCD, Dn, gel(S,_BEZ)));
    3539     5578242 :   b = gsub(a,b);
    3540     5578242 :   if (typ(b) != t_POL) return gerepileupto(av, b);
    3541       38675 :   return gerepilecopy(av, vchip_polmod(VCHIP, b));
    3542             : }
    3543             : 
    3544             : static GEN
    3545     6620248 : mfcusptracecache(long N, long k, long n, GEN Dn, GEN S, cachenew_t *cache)
    3546             : {
    3547     6620248 :   GEN C = NULL, T = gel(cache->vfull,N);
    3548     6620248 :   long lcache = lg(T);
    3549     6620248 :   if (n < lcache) C = gel(T, n);
    3550     6620248 :   if (C) cache->cuspHIT++; else C = mfcusptrace_i(N, k, n, Dn, S);
    3551     6620248 :   cache->cuspTOTAL++;
    3552     6620248 :   if (n < lcache) gel(T,n) = C;
    3553     6620248 :   return C;
    3554             : }
    3555             : 
    3556             : /* return the divisors of n, known to be among the elements of D */
    3557             : static GEN
    3558      280007 : div_restrict(GEN D, ulong n)
    3559             : {
    3560             :   long i, j, l;
    3561      280007 :   GEN v, VDIV = caches[cache_DIV].cache;
    3562      280007 :   if (lg(VDIV) > n) return gel(VDIV,n);
    3563           0 :   l = lg(D);
    3564           0 :   v = cgetg(l, t_VECSMALL);
    3565           0 :   for (i = j = 1; i < l; i++)
    3566             :   {
    3567           0 :     ulong d = D[i];
    3568           0 :     if (n % d == 0) v[j++] = d;
    3569             :   }
    3570           0 :   setlg(v,j); return v;
    3571             : }
    3572             : 
    3573             : /* for some prime divisors of N, Tr^new(p) = 0 */
    3574             : static int
    3575      198762 : trconj(GEN T, long N, long n)
    3576      198762 : { return (lg(T) > 1 && N % n == 0 && zv_search(T, n)); }
    3577             : 
    3578             : /* n > 0; trace formula on new space */
    3579             : static GEN
    3580     2377607 : mfnewtrace_i(long N, long k, long n, cachenew_t *cache)
    3581             : {
    3582     2377607 :   GEN VCHIP, s, Dn, DN1, SN, S = cache->DATA;
    3583             :   long FC, N1, N2, N1N2, g, i, j, lDN1;
    3584             : 
    3585     2377607 :   if (!S) return gen_0;
    3586     2377607 :   SN = gel(S,N);
    3587     2377607 :   if (mfnewchkzero(gel(SN,_NEWLZ), n)) return gen_0;
    3588     1730495 :   if (k > 2 && trconj(gel(SN,_TRCONJ), N, n)) return gen_0;
    3589     1730467 :   VCHIP = gel(SN, _VCHIP); FC = vchip_FC(VCHIP);
    3590     1730467 :   N1 = N/FC; newt_params(N1, n, FC, &g, &N2);
    3591     1730467 :   N1N2 = N1/N2;
    3592     1730467 :   DN1 = mydivisorsu(N1N2); lDN1 = lg(DN1);
    3593     1730467 :   N2 *= FC;
    3594     1730467 :   Dn = mydivisorsu(n); /* this one is probably out of cache */
    3595     1730467 :   s = gmulsg(mubeta2(N1N2,n), mfcusptracecache(N2, k, n, Dn, gel(S,N2), cache));
    3596     6340241 :   for (i = 2; i < lDN1; i++)
    3597             :   { /* skip M1 = 1, done above */
    3598     4609774 :     long M1 = DN1[i], N1M1 = DN1[lDN1-i];
    3599     4609774 :     GEN Dg = mydivisorsu(ugcd(M1, g));
    3600     4609774 :     M1 *= N2;
    3601     4609774 :     s = gadd(s, gmulsg(mubeta2(N1M1,n),
    3602     4609774 :                        mfcusptracecache(M1, k, n, Dn, gel(S,M1), cache)));
    3603     4889781 :     for (j = 2; j < lg(Dg); j++) /* skip d = 1, done above */
    3604             :     {
    3605      280007 :       long d = Dg[j], ndd = n/(d*d), M = M1/d;
    3606      280007 :       GEN z = mulsi(mubeta2(N1M1,ndd), powuu(d,k-1)), C = vchip_lift(VCHIP,d,z);
    3607      280007 :       GEN Dndd = div_restrict(Dn, ndd);
    3608      280007 :       s = gadd(s, gmul(C, mfcusptracecache(M, k, ndd, Dndd, gel(S,M), cache)));
    3609             :     }
    3610     4609774 :     s = vchip_mod(VCHIP, s);
    3611             :   }
    3612     1730467 :   return vchip_polmod(VCHIP, s);
    3613             : }
    3614             : 
    3615             : static GEN
    3616       12201 : get_DIH(long N)
    3617             : {
    3618       12201 :   GEN x = cache_get(cache_DIH, N);
    3619       12201 :   return x? gcopy(x): mfdihedral(N);
    3620             : }
    3621             : static GEN
    3622        2359 : get_vDIH(long N, GEN D)
    3623             : {
    3624        2359 :   GEN x = const_vec(N, NULL);
    3625             :   long i, l;
    3626        2359 :   if (!D) D = mydivisorsu(N);
    3627        2359 :   l = lg(D);
    3628       14350 :   for (i = 1; i < l; i++) { long d = D[i]; gel(x, d) = get_DIH(d); }
    3629        2359 :   return x;
    3630             : }
    3631             : 
    3632             : /* divisors of N which are multiple of F */
    3633             : static GEN
    3634         322 : divisorsNF(long N, long F)
    3635             : {
    3636         322 :   GEN D = mydivisorsu(N / F);
    3637         322 :   long l = lg(D), i;
    3638         833 :   for (i = 1; i < l; i++) D[i] = N / D[i];
    3639         322 :   return D;
    3640             : }
    3641             : /* mfcuspdim(N,k,CHI) - mfnewdim(N,k,CHI); CHIP primitive (for efficiency) */
    3642             : static long
    3643        8106 : mfolddim_i(long N, long k, GEN CHIP, GEN vSP)
    3644             : {
    3645        8106 :   long S, i, l, F = mfcharmodulus(CHIP), N1 = N / F, N2;
    3646             :   GEN D;
    3647        8106 :   newd_params(N1, &N2); /* will ensure mubeta != 0 */
    3648        8106 :   D = mydivisorsu(N1/N2); l = lg(D); S = 0;
    3649        8106 :   if (k == 1 && !vSP) vSP = get_vDIH(N, divisorsNF(N, F));
    3650       31486 :   for (i = 2; i < l; i++)
    3651             :   {
    3652       23380 :     long d = mfcuspdim_i(N / D[i], k, CHIP, vSP);
    3653       23380 :     if (d) S -= mubeta(D[i]) * d;
    3654             :   }
    3655        8106 :   return S;
    3656             : }
    3657             : long
    3658         224 : mfolddim(long N, long k, GEN CHI)
    3659             : {
    3660         224 :   pari_sp av = avma;
    3661         224 :   GEN CHIP = mfchartoprimitive(CHI, NULL);
    3662         224 :   return gc_long(av, mfolddim_i(N, k, CHIP, NULL));
    3663             : }
    3664             : /* Only depends on CHIP the primitive char attached to CHI; assumes !badchar */
    3665             : long
    3666       15407 : mfnewdim(long N, long k, GEN CHI)
    3667             : {
    3668             :   pari_sp av;
    3669             :   long S, F;
    3670       15407 :   GEN vSP, CHIP = mfchartoprimitive(CHI, &F);
    3671       15407 :   vSP = (k == 1)? get_vDIH(N, divisorsNF(N, F)): NULL;
    3672       15407 :   S = mfcuspdim_i(N, k, CHIP, vSP); if (!S) return 0;
    3673        7623 :   av = avma; return gc_long(av, S - mfolddim_i(N, k, CHIP, vSP));
    3674             : }
    3675             : 
    3676             : /* trace form, given as closure */
    3677             : static GEN
    3678         938 : mftraceform_new(long N, long k, GEN CHI)
    3679             : {
    3680             :   GEN T;
    3681         938 :   if (k == 1) return initwt1newtrace(mfinit_Nkchi(N, 1, CHI, mf_CUSP, 0));
    3682         917 :   T = initnewtrace(N,CHI); if (!T) return mftrivial();
    3683         917 :   return tag(t_MF_NEWTRACE, mkNK(N,k,CHI), T);
    3684             : }
    3685             : static GEN
    3686          14 : mftraceform_cusp(long N, long k, GEN CHI)
    3687             : {
    3688          14 :   if (k == 1) return initwt1trace(mfinit_Nkchi(N, 1, CHI, mf_CUSP, 0));
    3689           7 :   return tag(t_MF_TRACE, mkNK(N,k,CHI), inittrace(N,CHI,NULL));
    3690             : }
    3691             : static GEN
    3692          98 : mftraceform_i(GEN NK, long space)
    3693             : {
    3694             :   GEN CHI;
    3695             :   long N, k;
    3696          98 :   checkNK(NK, &N, &k, &CHI, 0);
    3697          98 :   if (!mfdim_Nkchi(N, k, CHI, space)) return mftrivial();
    3698          77 :   switch(space)
    3699             :   {
    3700          56 :     case mf_NEW: return mftraceform_new(N, k, CHI);
    3701          14 :     case mf_CUSP:return mftraceform_cusp(N, k, CHI);
    3702             :   }
    3703           7 :   pari_err_DOMAIN("mftraceform", "space", "=", utoi(space), NK);
    3704             :   return NULL;/*LCOV_EXCL_LINE*/
    3705             : }
    3706             : GEN
    3707          98 : mftraceform(GEN NK, long space)
    3708          98 : { pari_sp av = avma; return gerepilecopy(av, mftraceform_i(NK,space)); }
    3709             : 
    3710             : static GEN
    3711       16730 : hecke_data(long N, long n)
    3712       16730 : { return mkvecsmall3(n, u_ppo(n, N), N); }
    3713             : /* 1/2-integral weight */
    3714             : static GEN
    3715          84 : heckef2_data(long N, long n)
    3716             : {
    3717             :   ulong f, fN, fN2;
    3718          84 :   if (!uissquareall(n, &f)) return NULL;
    3719          77 :   fN = u_ppo(f, N); fN2 = fN*fN;
    3720          77 :   return mkvec2(myfactoru(fN), mkvecsmall4(n, N, fN2, n/fN2));
    3721             : }
    3722             : /* N = mf_get_N(F) or a multiple */
    3723             : static GEN
    3724       23625 : mfhecke_i(long n, long N, GEN F)
    3725             : {
    3726       23625 :   if (n == 1) return F;
    3727       16401 :   return tag2(t_MF_HECKE, mf_get_NK(F), hecke_data(N,n), F);
    3728             : }
    3729             : 
    3730             : GEN
    3731         105 : mfhecke(GEN mf, GEN F, long n)
    3732             : {
    3733         105 :   pari_sp av = avma;
    3734             :   GEN NK, CHI, gk, DATA;
    3735             :   long N, nk, dk;
    3736         105 :   mf = checkMF(mf);
    3737         105 :   if (!checkmf_i(F)) pari_err_TYPE("mfhecke",F);
    3738         105 :   if (n <= 0) pari_err_TYPE("mfhecke [n <= 0]", stoi(n));
    3739         105 :   if (n == 1) return gcopy(F);
    3740         105 :   gk = mf_get_gk(F);
    3741         105 :   Qtoss(gk,&nk,&dk);
    3742         105 :   CHI = mf_get_CHI(F);
    3743         105 :   N = MF_get_N(mf);
    3744         105 :   if (dk == 2)
    3745             :   {
    3746          77 :     DATA = heckef2_data(N,n);
    3747          77 :     if (!DATA) return mftrivial();
    3748             :   }
    3749             :   else
    3750          28 :     DATA = hecke_data(N,n);
    3751          98 :   NK = mkgNK(lcmii(stoi(N), mf_get_gN(F)), gk, CHI, mf_get_field(F));
    3752          98 :   return gerepilecopy(av, tag2(t_MF_HECKE, NK, DATA, F));
    3753             : }
    3754             : 
    3755             : /* form F given by closure, compute B(d)(F) as closure (q -> q^d) */
    3756             : static GEN
    3757       33705 : mfbd_i(GEN F, long d)
    3758             : {
    3759             :   GEN D, NK, gk, CHI;
    3760       33705 :   if (d == 1) return F;
    3761       12635 :   if (d <= 0) pari_err_TYPE("mfbd [d <= 0]", stoi(d));
    3762       12635 :   if (mf_get_type(F) != t_MF_BD) D = utoi(d);
    3763           7 :   else { D = mului(d, gel(F,3)); F = gel(F,2); }
    3764       12635 :   gk = mf_get_gk(F); CHI = mf_get_CHI(F);
    3765       12635 :   if (typ(gk) != t_INT) CHI = mfcharmul(CHI, get_mfchar(utoi(d << 2)));
    3766       12635 :   NK = mkgNK(muliu(mf_get_gN(F), d), gk, CHI, mf_get_field(F));
    3767       12635 :   return tag2(t_MF_BD, NK, F, D);
    3768             : }
    3769             : GEN
    3770          42 : mfbd(GEN F, long d)
    3771             : {
    3772          42 :   pari_sp av = avma;
    3773          42 :   if (!checkmf_i(F)) pari_err_TYPE("mfbd",F);
    3774          42 :   return gerepilecopy(av, mfbd_i(F, d));
    3775             : }
    3776             : 
    3777             : /* A[i+1] = a(t*i^2) */
    3778             : static GEN
    3779         105 : RgV_shimura(GEN A, long n, long t, long N, long r, GEN CHI)
    3780             : {
    3781         105 :   GEN R, a0, Pn = mfcharpol(CHI);
    3782         105 :   long m, st, ord = mfcharorder(CHI), vt = varn(Pn), Nt = t == 1? N: ulcm(N,t);
    3783             : 
    3784         105 :   R = cgetg(n + 2, t_VEC);
    3785         105 :   st = odd(r)? -t: t;
    3786         105 :   a0 = gel(A, 1);
    3787         105 :   if (!gequal0(a0))
    3788             :   {
    3789          14 :     long o = mfcharorder(CHI);
    3790          14 :     if (st != 1 && odd(o)) o <<= 1;
    3791          14 :     a0 = gmul(a0, charLFwtk(Nt, r, CHI, o, st));
    3792             :   }
    3793         105 :   gel(R, 1) = a0;
    3794         637 :   for (m = 1; m <= n; m++)
    3795             :   {
    3796         532 :     GEN Dm = mydivisorsu(u_ppo(m, Nt)), S = gel(A, m*m + 1);
    3797         532 :     long i, l = lg(Dm);
    3798         805 :     for (i = 2; i < l; i++)
    3799             :     { /* (e,Nt) = 1; skip i = 1: e = 1, done above */
    3800         273 :       long e = Dm[i], me = m / e, a = mfcharevalord(CHI, e, ord);
    3801         273 :       GEN c, C = powuu(e, r - 1);
    3802         273 :       if (kross(st, e) == -1) C = negi(C);
    3803         273 :       c = Qab_Czeta(a, ord, C, vt);
    3804         273 :       S = gadd(S, gmul(c, gel(A, me*me + 1)));
    3805             :     }
    3806         532 :     gel(R, m+1) = S;
    3807             :   }
    3808         105 :   return degpol(Pn) > 1? gmodulo(R, Pn): R;
    3809             : }
    3810             : 
    3811             : static long
    3812          28 : mfisinkohnen(GEN mf, GEN F)
    3813             : {
    3814          28 :   GEN v, gk = MF_get_gk(mf), CHI = MF_get_CHI(mf);
    3815          28 :   long i, eps, N4 = MF_get_N(mf) >> 2, sb = mfsturmNgk(N4 << 4, gk) + 1;
    3816          28 :   eps = N4 % mfcharconductor(CHI)? -1 : 1;
    3817          28 :   if (odd(MF_get_r(mf))) eps = -eps;
    3818          28 :   v = mfcoefs(F, sb, 1);
    3819         686 :   for (i = 2;     i <= sb; i+=4) if (!gequal0(gel(v,i+1))) return 0;
    3820         245 :   for (i = 2+eps; i <= sb; i+=4) if (!gequal0(gel(v,i+1))) return 0;
    3821          14 :   return 1;
    3822             : }
    3823             : 
    3824             : static long
    3825          42 : mfshimura_space_cusp(GEN mf)
    3826             : {
    3827             :   long N4;
    3828          42 :   if (MF_get_r(mf) == 1 && (N4 = MF_get_N(mf) >> 2) >= 4)
    3829             :   {
    3830          21 :     GEN E = gel(myfactoru(N4), 2);
    3831          21 :     long ma = vecsmall_max(E);
    3832          21 :     if (ma > 2 || (ma == 2 && !mfcharistrivial(MF_get_CHI(mf)))) return 0;
    3833             :   }
    3834          28 :   return 1;
    3835             : }
    3836             : 
    3837             : /* D is either a discriminant (not necessarily fundamental) with
    3838             :    sign(D)=(-1)^{k-1/2}*eps, or a positive squarefree integer t, which is then
    3839             :    transformed into a fundamental discriminant of the correct sign. */
    3840             : GEN
    3841          49 : mfshimura(GEN mf, GEN F, long t)
    3842             : {
    3843          49 :   pari_sp av = avma;
    3844             :   GEN G, res, mf2, CHI;
    3845          49 :   long sb, M, r, N, space = mf_FULL;
    3846             : 
    3847          49 :   if (!checkmf_i(F)) pari_err_TYPE("mfshimura",F);
    3848          49 :   mf = checkMF(mf);
    3849          49 :   r = MF_get_r(mf);
    3850          49 :   if (r <= 0) pari_err_DOMAIN("mfshimura", "weight", "<=", ghalf, mf_get_gk(F));
    3851          49 :   if (t <= 0 || !uissquarefree(t)) pari_err_TYPE("mfshimura [t]", stoi(t));
    3852          42 :   N = MF_get_N(mf); M = N >> 1;
    3853          42 :   if (mfiscuspidal(mf,F))
    3854             :   {
    3855          28 :     if (mfshimura_space_cusp(mf)) space = mf_CUSP;
    3856          28 :     if (mfisinkohnen(mf,F)) M = N >> 2;
    3857             :   }
    3858          42 :   CHI = MF_get_CHI(mf);
    3859          42 :   mf2 = mfinit_Nkchi(M, r << 1, mfcharpow(CHI, gen_2), space, 0);
    3860          42 :   sb = mfsturm(mf2);
    3861          42 :   G = RgV_shimura(mfcoefs_i(F, sb*sb, t), sb, t, N, r, CHI);
    3862          42 :   res = mftobasis_i(mf2, G);
    3863             :   /* not mflinear(mf2,): we want lowest possible level */
    3864          42 :   G = mflinear(MF_get_basis(mf2), res);
    3865          42 :   return gerepilecopy(av, mkvec3(mf2, G, res));
    3866             : }
    3867             : 
    3868             : /* W ZabM (ZM if n = 1), a t_INT or NULL, b t_INT, ZXQ mod P or NULL.
    3869             :  * Write a/b = A/d with d t_INT and A Zab return [W,d,A,P] */
    3870             : static GEN
    3871        7448 : mkMinv(GEN W, GEN a, GEN b, GEN P)
    3872             : {
    3873        7448 :   GEN A = (b && typ(b) == t_POL)? Q_remove_denom(QXQ_inv(b,P), &b): NULL;
    3874        7448 :   if (a && b)
    3875             :   {
    3876        1204 :     a = Qdivii(a,b);
    3877        1204 :     if (typ(a) == t_INT) b = gen_1; else { b = gel(a,2); a = gel(a,1); }
    3878        1204 :     if (is_pm1(a)) a = NULL;
    3879             :   }
    3880        7448 :   if (a) A = A? ZX_Z_mul(A,a): a; else if (!A) A = gen_1;
    3881        7448 :   if (!b) b = gen_1;
    3882        7448 :   if (!P) P = gen_0;
    3883        7448 :   return mkvec4(W,b,A,P);
    3884             : }
    3885             : /* M square invertible QabM, return [M',d], M*M' = d*Id */
    3886             : static GEN
    3887         532 : QabM_Minv(GEN M, GEN P, long n)
    3888             : {
    3889             :   GEN dW, W, dM;
    3890         532 :   M = Q_remove_denom(M, &dM);
    3891         532 :   W = P? ZabM_inv(liftpol_shallow(M), P, n, &dW): ZM_inv(M, &dW);
    3892         532 :   return mkMinv(W, dM, dW, P);
    3893             : }
    3894             : /* Simplified form of mfclean, after a QabM_indexrank: M a ZabM with full
    3895             :  * column rank and z = indexrank(M) is known */
    3896             : static GEN
    3897         833 : mfclean2(GEN M, GEN z, GEN P, long n)
    3898             : {
    3899         833 :   GEN d, Minv, y = gel(z,1), W = rowpermute(M, y);
    3900         833 :   W = P? ZabM_inv(liftpol_shallow(W), P, n, &d): ZM_inv(W, &d);
    3901         833 :   M = rowslice(M, 1, y[lg(y)-1]);
    3902         833 :   Minv = mkMinv(W, NULL, d, P);
    3903         833 :   return mkvec3(y, Minv, M);
    3904             : }
    3905             : /* M QabM, lg(M)>1 and [y,z] its rank profile. Let Minv be the inverse of the
    3906             :  * invertible square matrix in mkMinv format. Return [y,Minv, M[..y[#y],]]
    3907             :  * P cyclotomic polynomial of order n > 2 or NULL */
    3908             : static GEN
    3909        4844 : mfclean(GEN M, GEN P, long n, int ratlift)
    3910             : {
    3911        4844 :   GEN W, v, y, z, d, Minv, dM, MdM = Q_remove_denom(M, &dM);
    3912        4844 :   if (n <= 2)
    3913        3773 :     W = ZM_pseudoinv(MdM, &v, &d);
    3914             :   else
    3915        1071 :     W = ZabM_pseudoinv_i(liftpol_shallow(MdM), P, n, &v, &d, ratlift);
    3916        4844 :   y = gel(v,1);
    3917        4844 :   z = gel(v,2);
    3918        4844 :   if (lg(z) != lg(MdM)) M = vecpermute(M,z);
    3919        4844 :   M = rowslice(M, 1, y[lg(y)-1]);
    3920        4844 :   Minv = mkMinv(W, dM, d, P);
    3921        4844 :   return mkvec3(y, Minv, M);
    3922             : }
    3923             : /* call mfclean using only CHI */
    3924             : static GEN
    3925        3906 : mfcleanCHI(GEN M, GEN CHI, int ratlift)
    3926             : {
    3927        3906 :   long n = mfcharorder(CHI);
    3928        3906 :   GEN P = (n <= 2)? NULL: mfcharpol(CHI);
    3929        3906 :   return mfclean(M, P, n, ratlift);
    3930             : }
    3931             : 
    3932             : /* DATA component of a t_MF_NEWTRACE. Was it stripped to save memory ? */
    3933             : static int
    3934       31535 : newtrace_stripped(GEN DATA)
    3935       31535 : { return DATA && (lg(DATA) == 5 && typ(gel(DATA,3)) == t_INT); }
    3936             : /* f a t_MF_NEWTRACE */
    3937             : static GEN
    3938       31535 : newtrace_DATA(long N, GEN f)
    3939             : {
    3940       31535 :   GEN DATA = gel(f,2);
    3941       31535 :   return newtrace_stripped(DATA)? initnewtrace(N, DATA): DATA;
    3942             : }
    3943             : /* reset cachenew for new level incorporating new DATA, tf a t_MF_NEWTRACE
    3944             :  * (+ possibly initialize 'full' for new allowed levels) */
    3945             : static void
    3946       31535 : reset_cachenew(cachenew_t *cache, long N, GEN tf)
    3947             : {
    3948             :   long i, n, l;
    3949       31535 :   GEN v, DATA = newtrace_DATA(N,tf);
    3950       31535 :   cache->DATA = DATA;
    3951       31535 :   if (!DATA) return;
    3952       31430 :   n = cache->n;
    3953       31430 :   v = cache->vfull; l = N+1; /* = lg(DATA) */
    3954     2087687 :   for (i = 1; i < l; i++)
    3955     2056257 :     if (typ(gel(v,i)) == t_INT && lg(gel(DATA,i)) != 1)
    3956       48608 :       gel(v,i) = const_vec(n, NULL);
    3957       31430 :   cache->VCHIP = gel(gel(DATA,N),_VCHIP);
    3958             : }
    3959             : /* initialize a cache of newtrace / cusptrace up to index n and level | N;
    3960             :  * DATA may be NULL (<=> Tr^new = 0). tf a t_MF_NEWTRACE */
    3961             : static void
    3962       12068 : init_cachenew(cachenew_t *cache, long n, long N, GEN tf)
    3963             : {
    3964       12068 :   long i, l = N+1; /* = lg(tf.DATA) when DATA != NULL */
    3965             :   GEN v;
    3966       12068 :   cache->n = n;
    3967       12068 :   cache->vnew = v = cgetg(l, t_VEC);
    3968      891849 :   for (i = 1; i < l; i++) gel(v,i) = (N % i)? gen_0: const_vec(n, NULL);
    3969       12068 :   cache->newHIT = cache->newTOTAL = cache->cuspHIT = cache->cuspTOTAL = 0;
    3970       12068 :   cache->vfull = v = zerovec(N);
    3971       12068 :   reset_cachenew(cache, N, tf);
    3972       12068 : }
    3973             : static void
    3974       16485 : dbg_cachenew(cachenew_t *C)
    3975             : {
    3976       16485 :   if (DEBUGLEVEL >= 2 && C)
    3977           0 :     err_printf("newtrace cache hits: new = %ld/%ld, cusp = %ld/%ld\n",
    3978             :                     C->newHIT, C->newTOTAL, C->cuspHIT, C->cuspTOTAL);
    3979       16485 : }
    3980             : 
    3981             : /* newtrace_{N,k}(d*i), i = n0, ..., n */
    3982             : static GEN
    3983      170086 : colnewtrace(long n0, long n, long d, long N, long k, cachenew_t *cache)
    3984             : {
    3985      170086 :   GEN v = cgetg(n-n0+2, t_COL);
    3986             :   long i;
    3987     4206301 :   for (i = n0; i <= n; i++) gel(v, i-n0+1) = mfnewtracecache(N, k, i*d, cache);
    3988      170086 :   return v;
    3989             : }
    3990             : /* T_n(l*m0, l*(m0+1), ..., l*m) F, F = t_MF_NEWTRACE [N,k],DATA, cache
    3991             :  * contains DATA != NULL as well as cached values of F */
    3992             : static GEN
    3993       84742 : heckenewtrace(long m0, long m, long l, long N, long NBIG, long k, long n, cachenew_t *cache)
    3994             : {
    3995       84742 :   long lD, a, k1, nl = n*l;
    3996       84742 :   GEN D, V, v = colnewtrace(m0, m, nl, N, k, cache); /* d=1 */
    3997             :   GEN VCHIP;
    3998       84742 :   if (n == 1) return v;
    3999       58156 :   VCHIP = cache->VCHIP;
    4000       58156 :   D = mydivisorsu(u_ppo(n, NBIG)); lD = lg(D);
    4001       58156 :   k1 = k - 1;
    4002      141939 :   for (a = 2; a < lD; a++)
    4003             :   { /* d > 1, (d,NBIG) = 1 */
    4004       83783 :     long i, j, d = D[a], c = ugcd(l, d), dl = d/c, m0d = ceildiv(m0, dl);
    4005       83783 :     GEN C = vchip_lift(VCHIP, d, powuu(d, k1));
    4006             :     /* m0=0: i = 1 => skip F(0) = 0 */
    4007       83783 :     if (!m0) { i = 1; j = dl; } else { i = 0; j = m0d*dl; }
    4008       83783 :     V = colnewtrace(m0d, m/dl, nl/(d*c), N, k, cache);
    4009             :     /* C = chi(d) d^(k-1) */
    4010      949927 :     for (; j <= m; i++, j += dl)
    4011      866144 :       gel(v,j-m0+1) = gadd(gel(v,j-m0+1), vchip_mod(VCHIP, gmul(C,gel(V,i+1))));
    4012             :   }
    4013       58156 :   return v;
    4014             : }
    4015             : 
    4016             : /* Given v = an[i], return an[d*i], i=0..n */
    4017             : static GEN
    4018        1806 : anextract(GEN v, long n, long d)
    4019             : {
    4020        1806 :   long i, id, l = n + 2;
    4021        1806 :   GEN w = cgetg(l, t_VEC);
    4022        1806 :   if (d == 1)
    4023        4165 :     for (i = 1; i < l; i++) gel(w, i) = gel(v, i);
    4024             :   else
    4025       10598 :     for (i = id = 1; i < l; i++, id += d) gel(w, i) = gel(v, id);
    4026        1806 :   return w;
    4027             : }
    4028             : /* T_n(F)(0, l, ..., l*m) */
    4029             : static GEN
    4030        2051 : hecke_i(long m, long l, GEN V, GEN F, GEN DATA)
    4031             : {
    4032             :   long k, n, nNBIG, NBIG, lD, M, a, t, nl;
    4033             :   GEN D, v, CHI;
    4034        2051 :   if (typ(DATA) == t_VEC)
    4035             :   { /* 1/2-integral k */
    4036          98 :     if (!V) { GEN S = gel(DATA,2); V = mfcoefs_i(F, m*l*S[3], S[4]); }
    4037          98 :     return RgV_heckef2(m, l, V, F, DATA);
    4038             :   }
    4039        1953 :   k = mf_get_k(F);
    4040        1953 :   n = DATA[1]; nl = n*l;
    4041        1953 :   nNBIG = DATA[2];
    4042        1953 :   NBIG = DATA[3];
    4043        1953 :   if (nNBIG == 1) return V? V: mfcoefs_i(F,m,nl);
    4044        1225 :   if (!V && mf_get_type(F) == t_MF_NEWTRACE)
    4045             :   { /* inline F to allow cache, T_n at level NBIG acting on Tr^new(N,k,CHI) */
    4046             :     cachenew_t cache;
    4047         322 :     long N = mf_get_N(F);
    4048         322 :     init_cachenew(&cache, m*nl, N, F);
    4049         322 :     v = heckenewtrace(0, m, l, N, NBIG, k, n, &cache);
    4050         322 :     dbg_cachenew(&cache);
    4051         322 :     settyp(v, t_VEC); return v;
    4052             :   }
    4053         903 :   CHI = mf_get_CHI(F);
    4054         903 :   D = mydivisorsu(nNBIG); lD = lg(D);
    4055         903 :   M = m + 1;
    4056         903 :   t = nNBIG * ugcd(nNBIG, l);
    4057         903 :   if (!V) V = mfcoefs_i(F, m * t, nl / t); /* usually nl = t */
    4058         903 :   v = anextract(V, m, t); /* mfcoefs(F, m, nl); d = 1 */
    4059        1806 :   for (a = 2; a < lD; a++)
    4060             :   { /* d > 1, (d, NBIG) = 1 */
    4061         903 :     long d = D[a], c = ugcd(l, d), dl = d/c, i, idl;
    4062         903 :     GEN C = gmul(mfchareval(CHI, d), powuu(d, k-1));
    4063         903 :     GEN w = anextract(V, m/dl, t/(d*c)); /* mfcoefs(F, m/dl, nl/(d*c)) */
    4064        4165 :     for (i = idl = 1; idl <= M; i++, idl += dl)
    4065        3262 :       gel(v,idl) = gadd(gel(v,idl), gmul(C, gel(w,i)));
    4066             :   }
    4067         903 :   return v;
    4068             : }
    4069             : 
    4070             : static GEN
    4071       11935 : mkmf(GEN x1, GEN x2, GEN x3, GEN x4, GEN x5)
    4072             : {
    4073       11935 :   GEN MF = obj_init(5, MF_SPLITN);
    4074       11935 :   gel(MF,1) = x1;
    4075       11935 :   gel(MF,2) = x2;
    4076       11935 :   gel(MF,3) = x3;
    4077       11935 :   gel(MF,4) = x4;
    4078       11935 :   gel(MF,5) = x5; return MF;
    4079             : }
    4080             : 
    4081             : /* return an integer b such that p | b => T_p^k Tr^new = 0, for all k > 0 */
    4082             : static long
    4083        7364 : get_badj(long N, long FC)
    4084             : {
    4085        7364 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
    4086        7364 :   long i, b = 1, l = lg(P);
    4087       19607 :   for (i = 1; i < l; i++)
    4088       12243 :     if (E[i] > 1 && u_lval(FC, P[i]) < E[i]) b *= P[i];
    4089        7364 :   return b;
    4090             : }
    4091             : /* in place, assume perm strictly increasing */
    4092             : static void
    4093        1302 : vecpermute_inplace(GEN v, GEN perm)
    4094             : {
    4095        1302 :   long i, l = lg(perm);
    4096       10976 :   for (i = 1; i < l; i++) gel(v,i) = gel(v,perm[i]);
    4097        1302 : }
    4098             : 
    4099             : /* Find basis of newspace using closures; assume k >= 2 and !badchar.
    4100             :  * Return NULL if space is empty, else
    4101             :  * [mf1, list of closures T(j)traceform, list of corresponding j, matrix] */
    4102             : static GEN
    4103       15162 : mfnewinit(long N, long k, GEN CHI, cachenew_t *cache, long init)
    4104             : {
    4105             :   GEN S, vj, M, CHIP, mf1, listj, P, tf;
    4106             :   long j, ct, ctlj, dim, jin, SB, sb, two, ord, FC, badj;
    4107             : 
    4108       15162 :   dim = mfnewdim(N, k, CHI);
    4109       15162 :   if (!dim && !init) return NULL;
    4110        7364 :   sb = mfsturmNk(N, k);
    4111        7364 :   CHIP = mfchartoprimitive(CHI, &FC);
    4112             :   /* remove newtrace data from S to save space in output: negligible slowdown */
    4113        7364 :   tf = tag(t_MF_NEWTRACE, mkNK(N,k,CHIP), CHIP);
    4114        7364 :   badj = get_badj(N, FC);
    4115             :   /* try sbsmall first: Sturm bound not sharp for new space */
    4116        7364 :   SB = ceilA1(N, k);
    4117        7364 :   listj = cgetg(2*sb + 3, t_VECSMALL);
    4118      355593 :   for (j = ctlj = 1; ctlj < 2*sb + 3; j++)
    4119      348229 :     if (ugcd(j, badj) == 1) listj[ctlj++] = j;
    4120        7364 :   if (init)
    4121             :   {
    4122        4018 :     init_cachenew(cache, (SB+1)*listj[dim+1], N, tf);
    4123        4018 :     if (init == -1 || !dim) return NULL; /* old space or dim = 0 */
    4124             :   }
    4125             :   else
    4126        3346 :     reset_cachenew(cache, N, tf);
    4127             :   /* cache.DATA is not NULL */
    4128        6909 :   ord = mfcharorder(CHIP);
    4129        6909 :   P = ord <= 2? NULL: mfcharpol(CHIP);
    4130        6909 :   vj = cgetg(dim+1, t_VECSMALL);
    4131        6909 :   M = cgetg(dim+1, t_MAT);
    4132        6916 :   for (two = 1, ct = 0, jin = 1; two <= 2; two++)
    4133             :   {
    4134        6916 :     long a, jlim = jin + sb;
    4135       20958 :     for (a = jin; a <= jlim; a++)
    4136             :     {
    4137             :       GEN z, vecz;
    4138       20951 :       ct++; vj[ct] = listj[a];
    4139       20951 :       gel(M, ct) = heckenewtrace(0, SB, 1, N, N, k, vj[ct], cache);
    4140       20951 :       if (ct < dim) continue;
    4141             : 
    4142        7560 :       z = QabM_indexrank(M, P, ord);
    4143        7560 :       vecz = gel(z, 2); ct = lg(vecz) - 1;
    4144        7560 :       if (ct == dim) { M = mkvec3(z, gen_0, M); break; } /*maximal rank, done*/
    4145         651 :       vecpermute_inplace(M, vecz);
    4146         651 :       vecpermute_inplace(vj, vecz);
    4147             :     }
    4148        6916 :     if (a <= jlim) break;
    4149             :     /* sbsmall was not sufficient, use Sturm bound: must extend M */
    4150          70 :     for (j = 1; j <= ct; j++)
    4151             :     {
    4152          63 :       GEN t = heckenewtrace(SB + 1, sb, 1, N, N, k, vj[j], cache);
    4153          63 :       gel(M,j) = shallowconcat(gel(M, j), t);
    4154             :     }
    4155           7 :     jin = jlim + 1; SB = sb;
    4156             :   }
    4157        6909 :   S = cgetg(dim + 1, t_VEC);
    4158       27167 :   for (j = 1; j <= dim; j++) gel(S, j) = mfhecke_i(vj[j], N, tf);
    4159        6909 :   dbg_cachenew(cache);
    4160        6909 :   mf1 = mkvec4(utoipos(N), utoipos(k), CHI, utoi(mf_NEW));
    4161        6909 :   return mkmf(mf1, cgetg(1,t_VEC), S, vj, M);
    4162             : }
    4163             : /* k > 1 integral, mf space is mf_CUSP or mf_FULL */
    4164             : static GEN
    4165          42 : mfinittonew(GEN mf)
    4166             : {
    4167          42 :   GEN CHI = MF_get_CHI(mf), S = MF_get_S(mf), vMjd = MFcusp_get_vMjd(mf);
    4168          42 :   GEN M = MF_get_M(mf), vj, mf1;
    4169          42 :   long i, j, l, l0 = lg(S), N0 = MF_get_N(mf);
    4170         203 :   for (i = l0-1; i > 0; i--)
    4171             :   {
    4172         189 :     long N = gel(vMjd,i)[1];
    4173         189 :     if (N != N0) break;
    4174             :   }
    4175          42 :   if (i == l0-1) return NULL;
    4176          35 :   S = vecslice(S, i+1, l0-1); /* forms of conductor N0 */
    4177          35 :   l = lg(S); vj = cgetg(l, t_VECSMALL);
    4178         196 :   for (j = 1; j < l; j++) vj[j] = gel(vMjd,j+i)[2];
    4179          35 :   M = vecslice(M, lg(M)-lg(S)+1, lg(M)-1); /* their coefficients */
    4180          35 :   M = mfcleanCHI(M, CHI, 0);
    4181          35 :   mf1 = mkvec4(utoipos(N0), MF_get_gk(mf), CHI, utoi(mf_NEW));
    4182          35 :   return mkmf(mf1, cgetg(1,t_VEC), S, vj, M);
    4183             : }
    4184             : 
    4185             : /* Bd(f)[m0..m], v = f[ceil(m0/d)..floor(m/d)], m0d = ceil(m0/d) */
    4186             : static GEN
    4187       78939 : RgC_Bd_expand(long m0, long m, GEN v, long d, long m0d)
    4188             : {
    4189             :   long i, j;
    4190             :   GEN w;
    4191       78939 :   if (d == 1) return v;
    4192       22442 :   w = zerocol(m-m0+1);
    4193       22442 :   if (!m0) { i = 1; j = d; } else { i = 0; j = m0d*d; }
    4194      429198 :   for (; j <= m; i++, j += d) gel(w,j-m0+1) = gel(v,i+1);
    4195       22442 :   return w;
    4196             : }
    4197             : /* S a nonempty vector of t_MF_BD(t_MF_HECKE(t_MF_NEWTRACE)); M the matrix
    4198             :  * of their coefficients r*0, r*1, ..., r*m0 (~ mfvectomat) or NULL (empty),
    4199             :  * extend it to coeffs up to m > m0. The forms B_d(T_j(tf_N))in S should be
    4200             :  * sorted by level N, then j, then increasing d. No reordering here. */
    4201             : static GEN
    4202        8351 : bhnmat_extend(GEN M, long m, long r, GEN S, cachenew_t *cache)
    4203             : {
    4204        8351 :   long i, mr, m0, m0r, Nold = 0, jold = 0, l = lg(S);
    4205        8351 :   GEN MAT = cgetg(l, t_MAT), v = NULL;
    4206        8351 :   if (M) { m0 = nbrows(M); m0r = m0 * r; } else m0 = m0r = 0;
    4207        8351 :   mr = m*r;
    4208       87290 :   for (i = 1; i < l; i++)
    4209             :   {
    4210             :     long d, j, md, N;
    4211       78939 :     GEN c, f = bhn_parse(gel(S,i), &d,&j); /* t_MF_NEWTRACE */
    4212       78939 :     N = mf_get_N(f);
    4213       78939 :     md = ceildiv(m0r,d);
    4214       78939 :     if (N != Nold) { reset_cachenew(cache, N, f); Nold = N; jold = 0; }
    4215       78939 :     if (!cache->DATA) { gel(MAT,i) = zerocol(m+1); continue; }
    4216       78939 :     if (j != jold || md)
    4217       63406 :     { v = heckenewtrace(md, mr/d, 1, N, N, mf_get_k(f), j,cache); jold=j; }
    4218       78939 :     c = RgC_Bd_expand(m0r, mr, v, d, md);
    4219       78939 :     if (r > 1) c = c_deflate(m-m0, r, c);
    4220       78939 :     if (M) c = shallowconcat(gel(M,i), c);
    4221       78939 :     gel(MAT,i) = c;
    4222             :   }
    4223        8351 :   return MAT;
    4224             : }
    4225             : 
    4226             : /* k > 1 */
    4227             : static GEN
    4228        3087 : mfinitcusp(long N, long k, GEN CHI, cachenew_t *cache, long space)
    4229             : {
    4230             :   long L, l, lDN1, FC, N1, d1, i, init;
    4231        3087 :   GEN vS, vMjd, DN1, vmf, CHIP = mfchartoprimitive(CHI, &FC);
    4232             : 
    4233        3087 :   d1 = (space == mf_OLD)? mfolddim_i(N, k, CHIP, NULL): mfcuspdim(N, k, CHIP);
    4234        3087 :   if (!d1) return NULL;
    4235        2814 :   N1 = N/FC; DN1 = mydivisorsu(N1); lDN1 = lg(DN1);
    4236        2814 :   init = (space == mf_OLD)? -1: 1;
    4237        2814 :   vmf = cgetg(lDN1, t_VEC);
    4238       16772 :   for (i = lDN1 - 1, l = 1; i; i--)
    4239             :   { /* by decreasing level to allow cache */
    4240       13958 :     GEN mf = mfnewinit(FC*DN1[i], k, CHIP, cache, init);
    4241       13958 :     if (mf) gel(vmf, l++) = mf;
    4242       13958 :     init = 0;
    4243             :   }
    4244        2814 :   setlg(vmf,l); vmf = vecreverse(vmf); /* reorder by increasing level */
    4245             : 
    4246        2814 :   L = mfsturmNk(N, k)+1;
    4247        2814 :   vS = vectrunc_init(L);
    4248        2814 :   vMjd = vectrunc_init(L);
    4249        8890 :   for (i = 1; i < l; i++)
    4250             :   {
    4251        6076 :     GEN DNM, mf = gel(vmf,i), S = MF_get_S(mf), vj = MFnew_get_vj(mf);
    4252        6076 :     long a, lDNM, lS = lg(S), M = MF_get_N(mf);
    4253        6076 :     DNM = mydivisorsu(N / M); lDNM = lg(DNM);
    4254       24262 :     for (a = 1; a < lS; a++)
    4255             :     {
    4256       18186 :       GEN tf = gel(S,a);
    4257       18186 :       long b, j = vj[a];
    4258       45108 :       for (b = 1; b < lDNM; b++)
    4259             :       {
    4260       26922 :         long d = DNM[b];
    4261       26922 :         vectrunc_append(vS, mfbd_i(tf, d));
    4262       26922 :         vectrunc_append(vMjd, mkvecsmall3(M, j, d));
    4263             :       }
    4264             :     }
    4265             :   }
    4266        2814 :   return mkmf(NULL, cgetg(1, t_VEC), vS, vMjd, NULL);
    4267             : }
    4268             : 
    4269             : long
    4270        4200 : mfsturm_mf(GEN mf)
    4271             : {
    4272        4200 :   GEN Mindex = MF_get_Mindex(mf);
    4273        4200 :   long n = lg(Mindex)-1;
    4274        4200 :   return n? Mindex[n]-1: 0;
    4275             : }
    4276             : 
    4277             : long
    4278         595 : mfsturm(GEN T)
    4279             : {
    4280             :   long N, nk, dk;
    4281         595 :   GEN CHI, mf = checkMF_i(T);
    4282         595 :   if (mf) return mfsturm_mf(mf);
    4283           7 :   checkNK2(T, &N, &nk, &dk, &CHI, 0);
    4284           7 :   return dk == 1 ? mfsturmNk(N, nk) : mfsturmNk(N, (nk + 1) >> 1);
    4285             : }
    4286             : long
    4287           7 : mfisequal(GEN F, GEN G, long lim)
    4288             : {
    4289           7 :   pari_sp av = avma;
    4290             :   long b;
    4291           7 :   if (!checkmf_i(F)) pari_err_TYPE("mfisequal",F);
    4292           7 :   if (!checkmf_i(G)) pari_err_TYPE("mfisequal",G);
    4293           7 :   b = lim? lim: maxss(mfsturmmf(F), mfsturmmf(G));
    4294           7 :   return gc_long(av, gequal(mfcoefs_i(F, b, 1), mfcoefs_i(G, b, 1)));
    4295             : }
    4296             : 
    4297             : GEN
    4298          35 : mffields(GEN mf)
    4299             : {
    4300          35 :   if (checkmf_i(mf)) return gcopy(mf_get_field(mf));
    4301          35 :   mf = checkMF(mf); return gcopy(MF_get_fields(mf));
    4302             : }
    4303             : 
    4304             : GEN
    4305         322 : mfeigenbasis(GEN mf)
    4306             : {
    4307         322 :   pari_sp ltop = avma;
    4308             :   GEN F, S, v, vP;
    4309             :   long i, l, k, dS;
    4310             : 
    4311         322 :   mf = checkMF(mf);
    4312         322 :   k = MF_get_k(mf);
    4313         322 :   S = MF_get_S(mf); dS = lg(S)-1;
    4314         322 :   if (!dS) return cgetg(1, t_VEC);
    4315         315 :   F = MF_get_newforms(mf);
    4316         315 :   vP = MF_get_fields(mf);
    4317         315 :   if (k == 1)
    4318             :   {
    4319         196 :     if (MF_get_space(mf) == mf_FULL)
    4320             :     {
    4321          14 :       long dE = lg(MF_get_E(mf)) - 1;
    4322          14 :       if (dE) F = rowslice(F, dE+1, dE+dS);
    4323             :     }
    4324         196 :     v = vecmflineardiv_linear(S, F);
    4325         196 :     l = lg(v);
    4326             :   }
    4327             :   else
    4328             :   {
    4329         119 :     GEN (*L)(GEN, GEN) = (MF_get_space(mf) == mf_FULL)? mflinear: mflinear_bhn;
    4330         119 :     l = lg(F); v = cgetg(l, t_VEC);
    4331         413 :     for (i = 1; i < l; i++) gel(v,i) = L(mf, gel(F,i));
    4332             :   }
    4333         819 :   for (i = 1; i < l; i++) mf_setfield(gel(v,i), gel(vP,i));
    4334         315 :   return gerepilecopy(ltop, v);
    4335             : }
    4336             : 
    4337             : /* Minv = [M, d, A], v a t_COL; A a Zab, d a t_INT; return (A/d) * M*v */
    4338             : static GEN
    4339        6573 : Minv_RgC_mul(GEN Minv, GEN v)
    4340             : {
    4341        6573 :   GEN M = gel(Minv,1), d = gel(Minv,2), A = gel(Minv,3);
    4342        6573 :   v = RgM_RgC_mul(M, v);
    4343        6573 :   if (!equali1(A))
    4344             :   {
    4345        1736 :     if (typ(A) == t_POL && degpol(A) > 0) A = mkpolmod(A, gel(Minv,4));
    4346        1736 :     v = RgC_Rg_mul(v, A);
    4347             :   }
    4348        6573 :   if (!equali1(d)) v = RgC_Rg_div(v, d);
    4349        6573 :   return v;
    4350             : }
    4351             : static GEN
    4352        1225 : Minv_RgM_mul(GEN Minv, GEN B)
    4353             : {
    4354        1225 :   long j, l = lg(B);
    4355        1225 :   GEN M = cgetg(l, t_MAT);
    4356        5425 :   for (j = 1; j < l; j++) gel(M,j) = Minv_RgC_mul(Minv, gel(B,j));
    4357        1225 :   return M;
    4358             : }
    4359             : /* B * Minv; allow B = NULL for Id */
    4360             : static GEN
    4361        2366 : RgM_Minv_mul(GEN B, GEN Minv)
    4362             : {
    4363        2366 :   GEN M = gel(Minv,1), d = gel(Minv,2), A = gel(Minv,3);
    4364        2366 :   if (B) M = RgM_mul(B, M);
    4365        2366 :   if (!equali1(A))
    4366             :   {
    4367         966 :     if (typ(A) == t_POL) A = mkpolmod(A, gel(Minv,4));
    4368         966 :     M = RgM_Rg_mul(M, A);
    4369             :   }
    4370        2366 :   if (!equali1(d)) M = RgM_Rg_div(M,d);
    4371        2366 :   return M;
    4372             : }
    4373             : 
    4374             : /* perm vector of strictly increasing indices, v a vector or arbitrary length;
    4375             :  * the last r entries of perm fall beyond v.
    4376             :  * Return v o perm[1..(-r)], discarding the last r entries of v */
    4377             : static GEN
    4378        1134 : vecpermute_partial(GEN v, GEN perm, long *r)
    4379             : {
    4380        1134 :   long i, n = lg(v)-1, l = lg(perm);
    4381             :   GEN w;
    4382        1134 :   if (perm[l-1] <= n) { *r = 0; return vecpermute(v,perm); }
    4383          63 :   for (i = 1; i < l; i++)
    4384          63 :     if (perm[i] > n) break;
    4385          21 :   *r = l - i; l = i;
    4386          21 :   w = cgetg(l, typ(v));
    4387          63 :   for (i = 1; i < l; i++) gel(w,i) = gel(v,perm[i]);
    4388          21 :   return w;
    4389             : }
    4390             : 
    4391             : /* given form F, find coeffs of F on mfbasis(mf). If power series, not
    4392             :  * guaranteed correct if precision less than Sturm bound */
    4393             : static GEN
    4394        1197 : mftobasis_i(GEN mf, GEN F)
    4395             : {
    4396             :   GEN v, Mindex, Minv;
    4397        1197 :   if (!MF_get_dim(mf)) return cgetg(1, t_COL);
    4398        1197 :   Mindex = MF_get_Mindex(mf);
    4399        1197 :   Minv = MF_get_Minv(mf);
    4400        1197 :   if (checkmf_i(F))
    4401             :   {
    4402         259 :     long n = Mindex[lg(Mindex)-1];
    4403         259 :     v = vecpermute(mfcoefs_i(F, n, 1), Mindex);
    4404         259 :     return Minv_RgC_mul(Minv, v);
    4405             :   }
    4406             :   else
    4407             :   {
    4408         938 :     GEN A = gel(Minv,1), d = gel(Minv,2);
    4409             :     long r;
    4410         938 :     v = F;
    4411         938 :     switch(typ(F))
    4412             :     {
    4413           0 :       case t_SER: v = sertocol(v);
    4414         938 :       case t_VEC: case t_COL: break;
    4415           0 :       default: pari_err_TYPE("mftobasis", F);
    4416             :     }
    4417         938 :     if (lg(v) == 1) pari_err_TYPE("mftobasis",v);
    4418         938 :     v = vecpermute_partial(v, Mindex, &r);
    4419         938 :     if (!r) return Minv_RgC_mul(Minv, v); /* single solution */
    4420             :     /* affine space of dimension r */
    4421          21 :     v = RgM_RgC_mul(vecslice(A, 1, lg(v)-1), v);
    4422          21 :     if (!equali1(d)) v = RgC_Rg_div(v,d);
    4423          21 :     return mkvec2(v, vecslice(A, lg(A)-r, lg(A)-1));
    4424             :   }
    4425             : }
    4426             : 
    4427             : static GEN
    4428         546 : const_mat(long n, GEN x)
    4429             : {
    4430         546 :   long j, l = n+1;
    4431         546 :   GEN A = cgetg(l,t_MAT);
    4432        3990 :   for (j = 1; j < l; j++) gel(A,j) = const_col(n, x);
    4433         546 :   return A;
    4434             : }
    4435             : 
    4436             : /* L is the mftobasis of a form on CUSP space. We allow mf_FULL or mf_CUSP */
    4437             : static GEN
    4438         273 : mftonew_i(GEN mf, GEN L, long *plevel)
    4439             : {
    4440             :   GEN S, listMjd, CHI, res, Aclos, Acoef, D, perm;
    4441         273 :   long N1, LC, lD, i, l, t, level, N = MF_get_N(mf);
    4442             : 
    4443         273 :   if (MF_get_k(mf) == 1) pari_err_IMPL("mftonew in weight 1");
    4444         273 :   listMjd = MFcusp_get_vMjd(mf);
    4445         273 :   CHI = MF_get_CHI(mf); LC = mfcharconductor(CHI);
    4446         273 :   S = MF_get_S(mf);
    4447             : 
    4448         273 :   N1 = N/LC;
    4449         273 :   D = mydivisorsu(N1); lD = lg(D);
    4450         273 :   perm = cgetg(N1+1, t_VECSMALL);
    4451        1995 :   for (i = 1; i < lD; i++) perm[D[i]] = i;
    4452         273 :   Aclos = const_mat(lD-1, cgetg(1,t_VEC));
    4453         273 :   Acoef = const_mat(lD-1, cgetg(1,t_VEC));
    4454         273 :   l = lg(listMjd);
    4455        2863 :   for (i = 1; i < l; i++)
    4456             :   {
    4457             :     long M, d;
    4458             :     GEN v;
    4459        2590 :     if (gequal0(gel(L,i))) continue;
    4460         266 :     v = gel(listMjd, i);
    4461         266 :     M = perm[ v[1]/LC ];
    4462         266 :     d = perm[ v[3] ];
    4463         266 :     gcoeff(Aclos,M,d) = vec_append(gcoeff(Aclos,M,d), gel(S,i));
    4464         266 :     gcoeff(Acoef,M,d) = shallowconcat(gcoeff(Acoef,M,d), gel(L,i));
    4465             :   }
    4466         273 :   res = cgetg(l, t_VEC); level = 1;
    4467        1995 :   for (i = t = 1; i < lD; i++)
    4468             :   {
    4469        1722 :     long j, M = D[i]*LC;
    4470        1722 :     GEN gM = utoipos(M);
    4471       15120 :     for (j = 1; j < lD; j++)
    4472             :     {
    4473       13398 :       GEN f = gcoeff(Aclos,i,j), C, NK;
    4474             :       long d;
    4475       13398 :       if (lg(f) == 1) continue;
    4476         238 :       NK = mf_get_NK(gel(f,1));
    4477         238 :       d = D[j];
    4478         238 :       C = gcoeff(Acoef,i,j);
    4479         238 :       level = ulcm(level, M*d);
    4480         238 :       gel(res,t++) = mkvec3(gM, utoipos(d), mflinear_i(NK,f,C));
    4481             :     }
    4482             :   }
    4483         273 :   if (plevel) *plevel = level;
    4484         273 :   setlg(res, t); return res;
    4485             : }
    4486             : GEN
    4487          35 : mftonew(GEN mf, GEN F)
    4488             : {
    4489          35 :   pari_sp av = avma;
    4490             :   GEN ES;
    4491             :   long s;
    4492          35 :   mf = checkMF(mf);
    4493          35 :   s = MF_get_space(mf);
    4494          35 :   if (s != mf_FULL && s != mf_CUSP)
    4495           7 :     pari_err_TYPE("mftonew [not a full or cuspidal space]", mf);
    4496          28 :   ES = mftobasisES(mf,F);
    4497          21 :   if (!gequal0(gel(ES,1)))
    4498           0 :     pari_err_TYPE("mftonew [not a cuspidal form]", F);
    4499          21 :   F = gel(ES,2);
    4500          21 :   return gerepilecopy(av, mftonew_i(mf,F, NULL));
    4501             : }
    4502             : 
    4503             : static GEN mfeisenstein_i(long k, GEN CHI1, GEN CHI2);
    4504             : 
    4505             : /* mfinit(F * Theta) */
    4506             : static GEN
    4507          84 : mf2init(GEN mf)
    4508             : {
    4509          84 :   GEN CHI = MF_get_CHI(mf), gk = gadd(MF_get_gk(mf), ghalf);
    4510          84 :   long N = MF_get_N(mf);
    4511          84 :   return mfinit_Nkchi(N, itou(gk), mfchiadjust(CHI, gk, N), mf_FULL, 0);
    4512             : }
    4513             : 
    4514             : static long
    4515         602 : mfvec_first_cusp(GEN v)
    4516             : {
    4517         602 :   long i, l = lg(v);
    4518        1428 :   for (i = 1; i < l; i++)
    4519             :   {
    4520        1337 :     GEN F = gel(v,i);
    4521        1337 :     long t = mf_get_type(F);
    4522        1337 :     if (t == t_MF_BD) { F = gel(F,2); t = mf_get_type(F); }
    4523        1337 :     if (t == t_MF_HECKE) { F = gel(F,3); t = mf_get_type(F); }
    4524        1337 :     if (t == t_MF_NEWTRACE) break;
    4525             :   }
    4526         602 :   return i;
    4527             : }
    4528             : /* vF a vector of mf F of type DIV(LINEAR(BAS,L), f) in (lcm) level N,
    4529             :  * F[2]=LINEAR(BAS,L), F[2][2]=BAS=fixed basis (Eisenstein or bhn type),
    4530             :  * F[2][3]=L, F[3]=f; mfvectomat(vF, n) */
    4531             : static GEN
    4532         609 : mflineardivtomat(long N, GEN vF, long n)
    4533             : {
    4534         609 :   GEN F, M, f, fc, ME, dB, B, a0, V = NULL;
    4535         609 :   long lM, lF = lg(vF), j;
    4536             : 
    4537         609 :   if (lF == 1) return cgetg(1,t_MAT);
    4538         602 :   F = gel(vF,1);
    4539         602 :   if (lg(F) == 5)
    4540             :   { /* chicompat */
    4541         273 :     V = gmael(F,4,4);
    4542         273 :     if (typ(V) == t_INT) V = NULL;
    4543             :   }
    4544         602 :   M = gmael(F,2,2); /* BAS */
    4545         602 :   lM = lg(M);
    4546         602 :   j = mfvec_first_cusp(M);
    4547         602 :   if (j == 1) ME = NULL;
    4548             :   else
    4549             :   { /* BAS starts by Eisenstein */
    4550         140 :     ME = mfvectomat(vecslice(M,1,j-1), n, 1);
    4551         140 :     M = vecslice(M, j,lM-1);
    4552             :   }
    4553         602 :   M = bhnmat_extend_nocache(NULL, N, n, 1, M);
    4554         602 :   if (ME) M = shallowconcat(ME,M);
    4555             :   /* M = mfcoefs of BAS */
    4556         602 :   B = cgetg(lF, t_MAT);
    4557         602 :   dB= cgetg(lF, t_VEC);
    4558        2863 :   for (j = 1; j < lF; j++)
    4559             :   {
    4560        2261 :     GEN g = gel(vF, j); /* t_MF_DIV */
    4561        2261 :     gel(B,j) = RgM_RgC_mul(M, gmael(g,2,3));
    4562        2261 :     gel(dB,j)= gmael(g,2,4);
    4563             :   }
    4564         602 :   f = mfcoefsser(gel(F,3),n);
    4565         602 :   a0 = polcoef_i(f, 0, -1);
    4566         602 :   if (gequal0(a0) || gequal1(a0))
    4567         301 :     a0 = NULL;
    4568             :   else
    4569         301 :     f = gdiv(ser_unscale(f, a0), a0);
    4570         602 :   fc = ginv(f);
    4571        2863 :   for (j = 1; j < lF; j++)
    4572             :   {
    4573        2261 :     pari_sp av = avma;
    4574        2261 :     GEN LISer = RgV_to_ser_full(gel(B,j)), f;
    4575        2261 :     if (a0) LISer = gdiv(ser_unscale(LISer, a0), a0);
    4576        2261 :     f = gmul(LISer, fc);
    4577        2261 :     if (a0) f = ser_unscale(f, ginv(a0));
    4578        2261 :     f = sertocol(f); setlg(f, n+2);
    4579        2261 :     if (!gequal1(gel(dB,j))) f = RgC_Rg_div(f, gel(dB,j));
    4580        2261 :     gel(B,j) = gerepileupto(av,f);
    4581             :   }
    4582         602 :   if (V) B = gmodulo(QabM_tracerel(V, 0, B), gel(V,1));
    4583         602 :   return B;
    4584             : }
    4585             : 
    4586             : static GEN
    4587         308 : mfheckemat_mfcoefs(GEN mf, GEN B, GEN DATA)
    4588             : {
    4589         308 :   GEN Mindex = MF_get_Mindex(mf), Minv = MF_get_Minv(mf);
    4590         308 :   long j, l = lg(B), sb = mfsturm_mf(mf);
    4591         308 :   GEN b = MF_get_basis(mf), Q = cgetg(l, t_VEC);
    4592        1365 :   for (j = 1; j < l; j++)
    4593             :   {
    4594        1057 :     GEN v = hecke_i(sb, 1, gel(B,j), gel(b,j), DATA); /* Tn b[j] */
    4595        1057 :     settyp(v,t_COL); gel(Q,j) = vecpermute(v, Mindex);
    4596             :   }
    4597         308 :   return Minv_RgM_mul(Minv,Q);
    4598             : }
    4599             : /* T_p^2, p prime, 1/2-integral weight; B = mfcoefs(mf,sb*p^2,1) or (mf,sb,p^2)
    4600             :  * if p|N */
    4601             : static GEN
    4602           7 : mfheckemat_mfcoefs_p2(GEN mf, long p, GEN B)
    4603             : {
    4604           7 :   pari_sp av = avma;
    4605           7 :   GEN DATA = heckef2_data(MF_get_N(mf), p*p);
    4606           7 :   return gerepileupto(av, mfheckemat_mfcoefs(mf, B, DATA));
    4607             : }
    4608             : /* convert Mindex from row-index to mfcoef indexation: a(n) is stored in
    4609             :  * mfcoefs()[n+1], so subtract 1 from all indices */
    4610             : static GEN
    4611          49 : Mindex_as_coef(GEN mf)
    4612             : {
    4613          49 :   GEN v, Mindex = MF_get_Mindex(mf);
    4614          49 :   long i, l = lg(Mindex);
    4615          49 :   v = cgetg(l, t_VECSMALL);
    4616         210 :   for (i = 1; i < l; i++) v[i] = Mindex[i]-1;
    4617          49 :   return v;
    4618             : }
    4619             : /* T_p, p prime; B = mfcoefs(mf,sb*p,1) or (mf,sb,p) if p|N; integral weight */
    4620             : static GEN
    4621          35 : mfheckemat_mfcoefs_p(GEN mf, long p, GEN B)
    4622             : {
    4623          35 :   pari_sp av = avma;
    4624          35 :   GEN vm, Q, C, Minv = MF_get_Minv(mf);
    4625          35 :   long lm, k, i, j, l = lg(B), N = MF_get_N(mf);
    4626             : 
    4627          35 :   if (N % p == 0) return Minv_RgM_mul(Minv, rowpermute(B, MF_get_Mindex(mf)));
    4628          21 :   k = MF_get_k(mf);
    4629          21 :   C = gmul(mfchareval(MF_get_CHI(mf), p), powuu(p, k-1));
    4630          21 :   vm = Mindex_as_coef(mf); lm = lg(vm);
    4631          21 :   Q = cgetg(l, t_MAT);
    4632         147 :   for (j = 1; j < l; j++) gel(Q,j) = cgetg(lm, t_COL);
    4633         147 :   for (i = 1; i < lm; i++)
    4634             :   {
    4635         126 :     long m = vm[i], mp = m*p;
    4636         126 :     GEN Cm = (m % p) == 0? C : NULL;
    4637        1260 :     for (j = 1; j < l; j++)
    4638             :     {
    4639        1134 :       GEN S = gel(B,j), s = gel(S, mp + 1);
    4640        1134 :       if (Cm) s = gadd(s, gmul(C, gel(S, m/p + 1)));
    4641        1134 :       gcoeff(Q, i, j) = s;
    4642             :     }
    4643             :   }
    4644          21 :   return gerepileupto(av, Minv_RgM_mul(Minv,Q));
    4645             : }
    4646             : /* Matrix of T(p), p prime, dim(mf) > 0 and integral weight */
    4647             : static GEN
    4648         301 : mfheckemat_p(GEN mf, long p)
    4649             : {
    4650         301 :   pari_sp av = avma;
    4651         301 :   long N = MF_get_N(mf), sb = mfsturm_mf(mf);
    4652         301 :   GEN B = (N % p)? mfcoefs_mf(mf, sb * p, 1): mfcoefs_mf(mf, sb, p);
    4653         301 :   return gerepileupto(av, mfheckemat_mfcoefs(mf, B, hecke_data(N,p)));
    4654             : }
    4655             : 
    4656             : /* mf_NEW != (0), weight > 1, p prime. Use
    4657             :  * T(p) T(j) = T(j*p) + p^{k-1} \chi(p) 1_{p | j, p \nmid N} T(j/p) */
    4658             : static GEN
    4659         882 : mfnewmathecke_p(GEN mf, long p)
    4660             : {
    4661         882 :   pari_sp av = avma;
    4662         882 :   GEN tf, vj = MFnew_get_vj(mf), CHI = MF_get_CHI(mf);
    4663         882 :   GEN Mindex = MF_get_Mindex(mf), Minv = MF_get_Minv(mf);
    4664         882 :   long N = MF_get_N(mf), k = MF_get_k(mf);
    4665         882 :   long i, j, lvj = lg(vj), lim = vj[lvj-1] * p;
    4666         882 :   GEN M, perm, V, need = zero_zv(lim);
    4667         882 :   GEN C = (N % p)? gmul(mfchareval(CHI,p), powuu(p,k-1)): NULL;
    4668         882 :   tf = mftraceform_new(N, k, CHI);
    4669        3801 :   for (i = 1; i < lvj; i++)
    4670             :   {
    4671        2919 :     j = vj[i]; need[j*p] = 1;
    4672        2919 :     if (N % p && j % p == 0) need[j/p] = 1;
    4673             :   }
    4674         882 :   perm = zero_zv(lim);
    4675         882 :   V = cgetg(lim+1, t_VEC);
    4676       12243 :   for (i = j = 1; i <= lim; i++)
    4677       11361 :     if (need[i]) { gel(V,j) = mfhecke_i(i, N, tf); perm[i] = j; j++; }
    4678         882 :   setlg(V, j);
    4679         882 :   V = bhnmat_extend_nocache(NULL, N, mfsturm_mf(mf), 1, V);
    4680         882 :   V = rowpermute(V, Mindex); /* V[perm[i]] = coeffs(T_i newtrace) */
    4681         882 :   M = cgetg(lvj, t_MAT);
    4682        3801 :   for (i = 1; i < lvj; i++)
    4683             :   {
    4684             :     GEN t;
    4685        2919 :     j = vj[i]; t = gel(V, perm[j*p]);
    4686        2919 :     if (C && j % p == 0) t = RgC_add(t, RgC_Rg_mul(gel(V, perm[j/p]),C));
    4687        2919 :     gel(M,i) = t;
    4688             :   }
    4689         882 :   return gerepileupto(av, Minv_RgM_mul(Minv, M));
    4690             : }
    4691             : 
    4692             : GEN
    4693          77 : mfheckemat(GEN mf, GEN vn)
    4694             : {
    4695          77 :   pari_sp av = avma;
    4696          77 :   long lv, lvP, i, N, dim, nk, dk, p, sb, flint = (typ(vn)==t_INT);
    4697             :   GEN CHI, res, vT, FA, B, vP;
    4698             : 
    4699          77 :   mf = checkMF(mf);
    4700          77 :   if (typ(vn) != t_VECSMALL) vn = gtovecsmall(vn);
    4701          77 :   N = MF_get_N(mf); CHI = MF_get_CHI(mf); Qtoss(MF_get_gk(mf), &nk, &dk);
    4702          77 :   dim = MF_get_dim(mf);
    4703          77 :   lv = lg(vn);
    4704          77 :   res = cgetg(lv, t_VEC);
    4705          77 :   FA = cgetg(lv, t_VEC);
    4706          77 :   vP = cgetg(lv, t_VEC);
    4707          77 :   vT = const_vec(vecsmall_max(vn), NULL);
    4708         182 :   for (i = 1; i < lv; i++)
    4709             :   {
    4710         105 :     ulong n = (ulong)labs(vn[i]);
    4711             :     GEN fa;
    4712         105 :     if (!n) pari_err_TYPE("mfheckemat", vn);
    4713         105 :     if (dk == 1 || uissquareall(n, &n)) fa = myfactoru(n);
    4714           0 :     else { n = 0; fa = myfactoru(1); } /* dummy: T_{vn[i]} = 0 */
    4715         105 :     vn[i] = n;
    4716         105 :     gel(FA,i) = fa;
    4717         105 :     gel(vP,i) = gel(fa,1);
    4718             :   }
    4719          77 :   vP = shallowconcat1(vP); vecsmall_sort(vP);
    4720          77 :   vP = vecsmall_uniq_sorted(vP); /* all primes occurring in vn */
    4721          77 :   lvP = lg(vP); if (lvP == 1) goto END;
    4722          56 :   p = vP[lvP-1];
    4723          56 :   sb = mfsturm_mf(mf);
    4724          56 :   if (dk == 1 && nk != 1 && MF_get_space(mf) == mf_NEW)
    4725          21 :     B = NULL; /* special purpose mfnewmathecke_p is faster */
    4726          35 :   else if (lvP == 2 && N % p == 0)
    4727          21 :     B = mfcoefs_mf(mf, sb, dk==2? p*p: p); /* single prime | N, can optimize */
    4728             :   else
    4729          14 :     B = mfcoefs_mf(mf, sb * (dk==2? p*p: p), 1); /* general initialization */
    4730         126 :   for (i = 1; i < lvP; i++)
    4731             :   {
    4732          70 :     long j, l, q, e = 1;
    4733             :     GEN C, Tp, u1, u0;
    4734          70 :     p = vP[i];
    4735         189 :     for (j = 1; j < lv; j++) e = maxss(e, z_lval(vn[j], p));
    4736          70 :     if (!B)
    4737          28 :       Tp = mfnewmathecke_p(mf, p);
    4738          42 :     else if (dk == 2)
    4739           7 :       Tp = mfheckemat_mfcoefs_p2(mf,p, (lvP==2||N%p)? B: matdeflate(sb,p*p,B));
    4740             :     else
    4741          35 :       Tp = mfheckemat_mfcoefs_p(mf, p, (lvP==2||N%p)? B: matdeflate(sb,p,B));
    4742          70 :     gel(vT, p) = Tp;
    4743          70 :     if (e == 1) continue;
    4744          14 :     u0 = gen_1;
    4745          14 :     if (dk == 2)
    4746             :     {
    4747           0 :       C = N % p? gmul(mfchareval(CHI,p*p), powuu(p, nk-2)): NULL;
    4748           0 :       if (e == 2) u0 = sstoQ(p+1,p); /* special case T_{p^4} */
    4749             :     }
    4750             :     else
    4751          14 :       C = N % p? gmul(mfchareval(CHI,p),   powuu(p, nk-1)): NULL;
    4752          28 :     for (u1=Tp, q=p, l=2; l <= e; l++)
    4753             :     { /* u0 = T_{p^{l-2}}, u1 = T_{p^{l-1}} for l > 2 */
    4754          14 :       GEN v = gmul(Tp, u1);
    4755          14 :       if (C) v = gsub(v, gmul(C, u0));
    4756             :       /* q = p^l, vT[q] = T_q for k integer else T_{q^2} */
    4757          14 :       q *= p; u0 = u1; gel(vT, q) = u1 = v;
    4758             :     }
    4759             :   }
    4760          56 : END:
    4761             :   /* vT[p^e] = T_{p^e} for all p^e occurring below */
    4762         182 :   for (i = 1; i < lv; i++)
    4763             :   {
    4764         105 :     long n = vn[i], j, lP;
    4765             :     GEN fa, P, E, M;
    4766         105 :     if (n == 0) { gel(res,i) = zeromat(dim,dim); continue; }
    4767         105 :     if (n == 1) { gel(res,i) = matid(dim); continue; }
    4768          77 :     fa = gel(FA,i);
    4769          77 :     P = gel(fa,1); lP = lg(P);
    4770          77 :     E = gel(fa,2); M = gel(vT, upowuu(P[1], E[1]));
    4771          84 :     for (j = 2; j < lP; j++) M = RgM_mul(M, gel(vT, upowuu(P[j], E[j])));
    4772          77 :     gel(res,i) = M;
    4773             :   }
    4774          77 :   if (flint) res = gel(res,1);
    4775          77 :   return gerepilecopy(av, res);
    4776             : }
    4777             : 
    4778             : /* f = \sum_i v[i] T_listj[i] (Trace Form) attached to v; replace by f/a_1(f) */
    4779             : static GEN
    4780        1449 : mf_normalize(GEN mf, GEN v)
    4781             : {
    4782        1449 :   GEN c, dc = NULL, M = MF_get_M(mf), Mindex = MF_get_Mindex(mf);
    4783        1449 :   v = Q_primpart(v);
    4784        1449 :   c = RgMrow_RgC_mul(M, v, 2); /* a_1(f) */
    4785        1449 :   if (gequal1(c)) return v;
    4786         875 :   if (typ(c) == t_POL) c = gmodulo(c, mfcharpol(MF_get_CHI(mf)));
    4787         875 :   if (typ(c) == t_POLMOD && varn(gel(c,1)) == 1 && degpol(gel(c,1)) >= 40
    4788           7 :                          && Mindex[1] == 2
    4789           7 :                          && mfcharorder(MF_get_CHI(mf)) <= 2)
    4790           7 :   { /* normalize using expansion at infinity (small coefficients) */
    4791           7 :     GEN w, P = gel(c,1), a1 = gel(c,2);
    4792           7 :     long i, l = lg(Mindex);
    4793           7 :     w = cgetg(l, t_COL);
    4794           7 :     gel(w,1) = gen_1;
    4795         280 :     for (i = 2; i < l; i++)
    4796             :     {
    4797         273 :       c = liftpol_shallow(RgMrow_RgC_mul(M, v, Mindex[i]));
    4798         273 :       gel(w,i) = QXQ_div(c, a1, P);
    4799             :     }
    4800             :     /* w = expansion at oo of normalized form */
    4801           7 :     v = Minv_RgC_mul(MF_get_Minv(mf), Q_remove_denom(w, &dc));
    4802           7 :     v = gmodulo(v, P); /* back to mfbasis coefficients */
    4803             :   }
    4804             :   else
    4805             :   {
    4806         868 :     c = ginv(c);
    4807         868 :     if (typ(c) == t_POLMOD) c = Q_remove_denom(c, &dc);
    4808         868 :     v = RgC_Rg_mul(v, c);
    4809             :   }
    4810         875 :   if (dc) v = RgC_Rg_div(v, dc);
    4811         875 :   return v;
    4812             : }
    4813             : static void
    4814         413 : pol_red(GEN NF, GEN *pP, GEN *pa, long flag)
    4815             : {
    4816         413 :   GEN dP, a, P = *pP;
    4817         413 :   long d = degpol(P);
    4818             : 
    4819         413 :   *pa = a = pol_x(varn(P));
    4820         413 :   if (d * (NF ? nf_get_degree(NF): 1) > 30) return;
    4821             : 
    4822         406 :   dP = RgX_disc(P);
    4823         406 :   if (typ(dP) != t_INT)
    4824          91 :   { dP = gnorm(dP); if (typ(dP) != t_INT) pari_err_BUG("mfnewsplit"); }
    4825         406 :   if (d == 2 || expi(dP) < 62)
    4826             :   {
    4827         371 :     if (expi(dP) < 31)
    4828         371 :       P = NF? rnfpolredabs(NF, P,flag): polredabs0(P,flag);
    4829             :     else
    4830           0 :       P = NF? rnfpolredbest(NF,P,flag): polredbest(P,flag);
    4831         371 :     if (flag)
    4832             :     {
    4833         343 :       a = gel(P,2); if (typ(a) == t_POLMOD) a = gel(a,2);
    4834         343 :       P = gel(P,1);
    4835             :     }
    4836             :   }
    4837         406 :   *pP = P;
    4838         406 :   *pa = a;
    4839             : }
    4840             : 
    4841             : /* Diagonalize and normalize. See mfsplit for meaning of flag. */
    4842             : static GEN
    4843        1043 : mfspclean(GEN mf, GEN mf0, GEN NF, long ord, GEN simplesp, long flag)
    4844             : {
    4845        1043 :   const long vz = 1;
    4846        1043 :   long i, l = lg(simplesp), dim = MF_get_dim(mf);
    4847        1043 :   GEN res = cgetg(l, t_MAT), pols = cgetg(l, t_VEC);
    4848        1043 :   GEN zeros = (mf == mf0)? NULL: zerocol(dim - MF_get_dim(mf0));
    4849        2520 :   for (i = 1; i < l; i++)
    4850             :   {
    4851        1477 :     GEN ATP = gel(simplesp, i), A = gel(ATP,1), P = gel(ATP,3);
    4852        1477 :     long d = degpol(P);
    4853        1477 :     GEN a, v = (flag && d > flag)? NULL: gel(A,1);
    4854        1477 :     if (d == 1) P = pol_x(vz);
    4855             :     else
    4856             :     {
    4857         413 :       pol_red(NF, &P, &a, !!v);
    4858         413 :       if (v)
    4859             :       { /* Mod(a,P) root of charpoly(T), K*gpowers(a) = eigenvector of T */
    4860         385 :         GEN K, den, M = cgetg(d+1, t_MAT), T = gel(ATP,2);
    4861             :         long j;
    4862         385 :         T = shallowtrans(T);
    4863         385 :         gel(M,1) = vec_ei(d,1); /* basis of cyclic vectors */
    4864        1274 :         for (j = 2; j <= d; j++) gel(M,j) = RgM_RgC_mul(T, gel(M,j-1));
    4865         385 :         M = Q_primpart(M);
    4866         126 :         K = NF? ZabM_inv(liftpol_shallow(M), nf_get_pol(NF), ord, &den)
    4867         385 :               : ZM_inv(M,&den);
    4868         385 :         K = shallowtrans(K);
    4869         385 :         v = gequalX(a)? pol_x_powers(d, vz): RgXQ_powers(a, d-1, P);
    4870         385 :         v = gmodulo(RgM_RgC_mul(A, RgM_RgC_mul(K,v)), P);
    4871             :       }
    4872             :     }
    4873        1477 :     if (v)
    4874             :     {
    4875        1449 :       v = mf_normalize(mf0, v); if (zeros) v = shallowconcat(zeros,v);
    4876        1449 :       gel(res,i) = v; if (flag) setlg(res,i+1);
    4877             :     }
    4878             :     else
    4879          28 :       gel(res,i) = zerocol(dim);
    4880        1477 :     gel(pols,i) = P;
    4881             :   }
    4882        1043 :   return mkvec2(res, pols);
    4883             : }
    4884             : 
    4885             : /* return v = v_{X-r}(P), and set Z = P / (X-r)^v */
    4886             : static long
    4887          70 : RgX_valrem_root(GEN P, GEN r, GEN *Z)
    4888             : {
    4889             :   long v;
    4890         140 :   for (v = 0; degpol(P); v++)
    4891             :   {
    4892         140 :     GEN t, Q = RgX_div_by_X_x(P, r, &t);
    4893         140 :     if (!gequal0(t)) break;
    4894          70 :     P = Q;
    4895             :   }
    4896          70 :   *Z = P; return v;
    4897             : }
    4898             : static GEN
    4899        1260 : mynffactor(GEN NF, GEN P, long dimlim)
    4900             : {
    4901             :   long i, l, v;
    4902             :   GEN R, E;
    4903        1260 :   if (dimlim != 1)
    4904             :   {
    4905         700 :     R = NF? nffactor(NF, P): QX_factor(P);
    4906         700 :     if (!dimlim) return R;
    4907          21 :     E = gel(R,2);
    4908          21 :     R = gel(R,1); l = lg(R);
    4909          98 :     for (i = 1; i < l; i++)
    4910          91 :       if (degpol(gel(R,i)) > dimlim) break;
    4911          21 :     if (i == 1) return NULL;
    4912          21 :     setlg(E,i);
    4913          21 :     setlg(R,i); return mkmat2(R, E);
    4914             :   }
    4915             :   /* dimlim = 1 */
    4916         560 :   R = nfroots(NF, P); l = lg(R);
    4917         560 :   if (l == 1) return NULL;
    4918         497 :   v = varn(P);
    4919         497 :   settyp(R, t_COL);
    4920         497 :   if (degpol(P) == l-1)
    4921         441 :     E = const_col(l-1, gen_1);
    4922             :   else
    4923             :   {
    4924          56 :     E = cgetg(l, t_COL);
    4925         126 :     for (i = 1; i < l; i++) gel(E,i) = utoi(RgX_valrem_root(P, gel(R,i), &P));
    4926             :   }
    4927         497 :   R = deg1_from_roots(R, v);
    4928         497 :   return mkmat2(R, E);
    4929             : }
    4930             : 
    4931             : /* Let K be a number field attached to NF (Q if NF = NULL). A K-vector
    4932             :  * space of dimension d > 0 is given by a t_MAT A (n x d, full column rank)
    4933             :  * giving a K-basis, X a section (d x n: left pseudo-inverse of A). Return a
    4934             :  * pair (T, fa), where T is an element of the Hecke algebra (a sum of Tp taken
    4935             :  * from vector vTp) acting on A (a d x d t_MAT) and fa is the factorization of
    4936             :  * its characteristic polynomial, limited to factors of degree <= dimlim if
    4937             :  * dimlim != 0 (return NULL if there are no factors of degree <= dimlim) */
    4938             : static GEN
    4939        1211 : findbestsplit(GEN NF, GEN vTp, GEN A, GEN X, long dimlim, long vz)
    4940             : {
    4941        1211 :   GEN T = NULL, Tkeep = NULL, fakeep = NULL;
    4942        1211 :   long lmax = 0, i, lT = lg(vTp);
    4943        1421 :   for (i = 1; i < lT; i++)
    4944             :   {
    4945        1421 :     GEN D, P, E, fa, TpA = gel(vTp,i);
    4946             :     long l;
    4947        2408 :     if (typ(TpA) == t_INT) break;
    4948        1260 :     if (lg(TpA) > lg(A)) TpA = RgM_mul(X, RgM_mul(TpA, A)); /* Tp | A */
    4949        1260 :     T = T ? RgM_add(T, TpA) : TpA;
    4950        1260 :     if (!NF) { P = QM_charpoly_ZX(T); setvarn(P, vz); }
    4951             :     else
    4952             :     {
    4953         259 :       P = charpoly(Q_remove_denom(T, &D), vz);
    4954         259 :       if (D) P = gdiv(RgX_unscale(P, D), powiu(D, degpol(P)));
    4955             :     }
    4956        1260 :     fa = mynffactor(NF, P, dimlim);
    4957        1260 :     if (!fa) return NULL;
    4958        1197 :     E = gel(fa, 2);
    4959             :     /* characteristic polynomial is separable ? */
    4960        1197 :     if (isint1(vecmax(E))) { Tkeep = T; fakeep = fa; break; }
    4961         210 :     l = lg(E);
    4962             :     /* characteristic polynomial has more factors than before ? */
    4963         210 :     if (l > lmax) { lmax = l; Tkeep = T; fakeep = fa; }
    4964             :   }
    4965        1148 :   return mkvec2(Tkeep, fakeep);
    4966             : }
    4967             : 
    4968             : static GEN
    4969         210 : nfcontent(GEN nf, GEN v)
    4970             : {
    4971         210 :   long i, l = lg(v);
    4972         210 :   GEN c = gel(v,1);
    4973        1134 :   for (i = 2; i < l; i++) c = idealadd(nf, c, gel(v,i));
    4974         210 :   if (typ(c) == t_MAT && gequal1(gcoeff(c,1,1))) c = gen_1;
    4975         210 :   return c;
    4976             : }
    4977             : static GEN
    4978         329 : nf_primpart(GEN nf, GEN B)
    4979             : {
    4980         329 :   switch(typ(B))
    4981             :   {
    4982         210 :     case t_COL:
    4983             :     {
    4984         210 :       GEN A = matalgtobasis(nf, B), c = nfcontent(nf, A);
    4985         210 :       if (typ(c) == t_INT) return B;
    4986          21 :       c = idealred_elt(nf,c);
    4987          21 :       A = Q_primpart( nfC_nf_mul(nf, A, Q_primpart(nfinv(nf,c))) );
    4988          21 :       A = liftpol_shallow( matbasistoalg(nf, A) );
    4989          21 :       if (gexpo(A) > gexpo(B)) A = B;
    4990          21 :       return A;
    4991             :     }
    4992         119 :     case t_MAT:
    4993             :     {
    4994             :       long i, l;
    4995         119 :       GEN A = cgetg_copy(B, &l);
    4996         329 :       for (i = 1; i < l; i++) gel(A,i) = nf_primpart(nf, gel(B,i));
    4997         119 :       return A;
    4998             :     }
    4999           0 :     default:
    5000           0 :       pari_err_TYPE("nf_primpart", B);
    5001             :       return NULL; /*LCOV_EXCL_LINE*/
    5002             :   }
    5003             : }
    5004             : 
    5005             : /* rotate entries of v to accomodate new entry 'x' (push out oldest entry) */
    5006             : static void
    5007        1155 : vecpush(GEN v, GEN x)
    5008             : {
    5009             :   long i;
    5010        5775 :   for (i = lg(v)-1; i > 1; i--) gel(v,i) = gel(v,i-1);
    5011        1155 :   gel(v,1) = x;
    5012        1155 : }
    5013             : 
    5014             : /* sort t_VEC of vector spaces by increasing dimension */
    5015             : static GEN
    5016        1043 : sort_by_dim(GEN v)
    5017             : {
    5018        1043 :   long i, l = lg(v);
    5019        1043 :   GEN D = cgetg(l, t_VECSMALL);
    5020        2520 :   for (i = 1; i < l; i++) D[i] = lg(gmael(v,i,2));
    5021        1043 :   return vecpermute(v , vecsmall_indexsort(D));
    5022             : }
    5023             : static GEN
    5024        1043 : split_starting_space(GEN mf)
    5025             : {
    5026        1043 :   long d = MF_get_dim(mf), d2;
    5027        1043 :   GEN id = matid(d);
    5028        1043 :   switch(MF_get_space(mf))
    5029             :   {
    5030        1036 :     case mf_NEW:
    5031        1036 :     case mf_CUSP: return mkvec2(id, id);
    5032             :   }
    5033           7 :   d2 = lg(MF_get_S(mf))-1;
    5034           7 :   return mkvec2(vecslice(id, d-d2+1,d),
    5035             :                 shallowconcat(zeromat(d2,d-d2),matid(d2)));
    5036             : }
    5037             : /* If dimlim > 0, keep only the dimension <= dimlim eigenspaces.
    5038             :  * See mfsplit for the meaning of flag. */
    5039             : static GEN
    5040        1442 : split_ii(GEN mf, long dimlim, long flag, GEN vSP, long *pnewd)
    5041             : {
    5042             :   forprime_t iter;
    5043        1442 :   GEN CHI = MF_get_CHI(mf), empty = cgetg(1, t_VEC), mf0 = mf;
    5044             :   GEN NF, POLCYC, todosp, Tpbigvec, simplesp;
    5045        1442 :   long N = MF_get_N(mf), k = MF_get_k(mf);
    5046        1442 :   long ord, FC, NEWT, dimsimple = 0, newd = -1;
    5047        1442 :   const long NBH = 5, vz = 1;
    5048             :   ulong p;
    5049             : 
    5050        1442 :   switch(MF_get_space(mf))
    5051             :   {
    5052        1169 :     case mf_NEW: break;
    5053         266 :     case mf_CUSP:
    5054             :     case mf_FULL:
    5055             :     {
    5056             :       GEN CHIP;
    5057         266 :       if (k > 1) { mf0 = mfinittonew(mf); break; }
    5058         245 :       CHIP = mfchartoprimitive(CHI, NULL);
    5059         245 :       newd = lg(MF_get_S(mf))-1 - mfolddim_i(N, k, CHIP, vSP);
    5060         245 :       break;
    5061             :     }
    5062           7 :     default: pari_err_TYPE("mfsplit [space does not contain newspace]", mf);
    5063             :       return NULL; /*LCOV_EXCL_LINE*/
    5064             :   }
    5065        1435 :   if (newd < 0) newd = mf0? MF_get_dim(mf0): 0;
    5066        1435 :   *pnewd = newd;
    5067        1435 :   if (!newd) return mkvec2(cgetg(1, t_MAT), empty);
    5068             : 
    5069        1043 :   NEWT = (k > 1 && MF_get_space(mf0) == mf_NEW);
    5070        1043 :   todosp = mkvec( split_starting_space(mf0) );
    5071        1043 :   simplesp = empty;
    5072        1043 :   FC = mfcharconductor(CHI);
    5073        1043 :   ord = mfcharorder(CHI);
    5074        1043 :   if (ord <= 2) NF = POLCYC = NULL;
    5075             :   else
    5076             :   {
    5077         196 :     POLCYC = mfcharpol(CHI);
    5078         196 :     NF = nfinit(POLCYC,DEFAULTPREC);
    5079             :   }
    5080        1043 :   Tpbigvec = zerovec(NBH);
    5081        1043 :   u_forprime_init(&iter, 2, ULONG_MAX);
    5082        1435 :   while (dimsimple < newd && (p = u_forprime_next(&iter)))
    5083             :   {
    5084             :     GEN nextsp;
    5085             :     long ind;
    5086        1435 :     if (N % (p*p) == 0 && N/p % FC == 0) continue; /* T_p = 0 in this case */
    5087        1155 :     vecpush(Tpbigvec, NEWT? mfnewmathecke_p(mf0,p): mfheckemat_p(mf0,p));
    5088        1155 :     nextsp = empty;
    5089        1456 :     for (ind = 1; ind < lg(todosp); ind++)
    5090             :     {
    5091        1211 :       GEN tmp = gel(todosp, ind), fa, P, E, D, Tp, DTp;
    5092        1211 :       GEN A = gel(tmp, 1);
    5093        1211 :       GEN X = gel(tmp, 2);
    5094             :       long lP, i;
    5095        1211 :       tmp = findbestsplit(NF, Tpbigvec, A, X, dimlim, vz);
    5096        1260 :       if (!tmp) continue; /* nothing there */
    5097        1148 :       Tp = gel(tmp, 1);
    5098        1148 :       fa = gel(tmp, 2);
    5099        1148 :       P = gel(fa, 1);
    5100        1148 :       E = gel(fa, 2); lP = lg(P);
    5101             :       /* lP > 1 */
    5102        1148 :       if (DEBUGLEVEL) err_printf("Exponents = %Ps\n", E);
    5103        1148 :       if (lP == 2)
    5104             :       {
    5105         770 :         GEN P1 = gel(P,1);
    5106         770 :         long e1 = itos(gel(E,1)), d1 = degpol(P1);
    5107         770 :         if (e1 * d1 == lg(Tp)-1)
    5108             :         {
    5109         721 :           if (e1 > 1) nextsp = vec_append(nextsp, mkvec2(A,X));
    5110             :           else
    5111             :           { /* simple module */
    5112         693 :             simplesp = vec_append(simplesp, mkvec3(A,Tp,P1));
    5113         931 :             if ((dimsimple += d1) == newd) goto END;
    5114             :           }
    5115          49 :           continue;
    5116             :         }
    5117             :       }
    5118             :       /* Found splitting */
    5119         427 :       DTp = Q_remove_denom(Tp, &D);
    5120        1148 :       for (i = 1; i < lP; i++)
    5121             :       {
    5122         959 :         GEN Ai, Xi, dXi, AAi, v, y, Pi = gel(P,i);
    5123         959 :         Ai = RgX_RgM_eval(D? RgX_rescale(Pi,D): Pi, DTp);
    5124         959 :         Ai = QabM_ker(Ai, POLCYC, ord);
    5125         959 :         if (NF) Ai = nf_primpart(NF, Ai);
    5126             : 
    5127         959 :         AAi = RgM_mul(A, Ai);
    5128             :         /* gives section, works on nonsquare matrices */
    5129         959 :         Xi = QabM_pseudoinv(Ai, POLCYC, ord, &v, &dXi);
    5130         959 :         Xi = RgM_Rg_div(Xi, dXi);
    5131         959 :         y = gel(v,1);
    5132         959 :         if (isint1(gel(E,i)))
    5133             :         {
    5134         784 :           GEN Tpi = RgM_mul(Xi, RgM_mul(rowpermute(Tp,y), Ai));
    5135         784 :           simplesp = vec_append(simplesp, mkvec3(AAi, Tpi, Pi));
    5136         784 :           if ((dimsimple += degpol(Pi)) == newd) goto END;
    5137             :         }
    5138             :         else
    5139             :         {
    5140         175 :           Xi = RgM_mul(Xi, rowpermute(X,y));
    5141         175 :           nextsp = vec_append(nextsp, mkvec2(AAi, Xi));
    5142             :         }
    5143             :       }
    5144             :     }
    5145         245 :     todosp = nextsp; if (lg(todosp) == 1) break;
    5146             :   }
    5147           0 : END:
    5148        1043 :   if (DEBUGLEVEL) err_printf("end split, need to clean\n");
    5149        1043 :   return mfspclean(mf, mf0, NF, ord, sort_by_dim(simplesp), flag);
    5150             : }
    5151             : static GEN
    5152          28 : dim_filter(GEN v, long dim)
    5153             : {
    5154          28 :   GEN P = gel(v,2);
    5155          28 :   long j, l = lg(P);
    5156         140 :   for (j = 1; j < l; j++)
    5157         126 :     if (degpol(gel(P,j)) > dim)
    5158             :     {
    5159          14 :       v = mkvec2(vecslice(gel(v,1),1,j-1), vecslice(P,1,j-1));
    5160          14 :       break;
    5161             :     }
    5162          28 :   return v;
    5163             : }
    5164             : static long
    5165         273 : dim_sum(GEN v)
    5166             : {
    5167         273 :   GEN P = gel(v,2);
    5168         273 :   long j, l = lg(P), d = 0;
    5169         679 :   for (j = 1; j < l; j++) d += degpol(gel(P,j));
    5170         273 :   return d;
    5171             : }
    5172             : static GEN
    5173        1134 : split_i(GEN mf, long dimlim, long flag)
    5174        1134 : { long junk; return split_ii(mf, dimlim, flag, NULL, &junk); }
    5175             : /* mf is either already split or output by mfinit. Splitting is done only for
    5176             :  * newspace except in weight 1. If flag = 0 (default) split completely.
    5177             :  * If flag = d > 0, only give the Galois polynomials in degree > d
    5178             :  * Flag is ignored if dimlim = 1. */
    5179             : GEN
    5180          98 : mfsplit(GEN mf0, long dimlim, long flag)
    5181             : {
    5182          98 :   pari_sp av = avma;
    5183          98 :   GEN v, mf = checkMF_i(mf0);
    5184          98 :   if (!mf) pari_err_TYPE("mfsplit", mf0);
    5185          98 :   if ((v = obj_check(mf, MF_SPLIT)))
    5186          28 :   { if (dimlim) v = dim_filter(v, dimlim); }
    5187          70 :   else if (dimlim && (v = obj_check(mf, MF_SPLITN)))
    5188          21 :   { v = (itos(gel(v,1)) >= dimlim)? dim_filter(gel(v,2), dimlim): NULL; }
    5189          98 :   if (!v)
    5190             :   {
    5191             :     long newd;
    5192          70 :     v = split_ii(mf, dimlim, flag, NULL, &newd);
    5193          70 :     if (lg(v) == 1) obj_insert(mf, MF_SPLITN, mkvec2(utoi(dimlim), v));
    5194          70 :     else if (!flag)
    5195             :     {
    5196          49 :       if (dim_sum(v) == newd) obj_insert(mf, MF_SPLIT,v);
    5197          21 :       else obj_insert(mf, MF_SPLITN, mkvec2(utoi(dimlim), v));
    5198             :     }
    5199             :   }
    5200          98 :   return gerepilecopy(av, v);
    5201             : }
    5202             : static GEN
    5203         224 : split(GEN mf) { return split_i(mf,0,0); }
    5204             : GEN
    5205         756 : MF_get_newforms(GEN mf) { return gel(obj_checkbuild(mf,MF_SPLIT,&split),1); }
    5206             : GEN
    5207         567 : MF_get_fields(GEN mf) { return gel(obj_checkbuild(mf,MF_SPLIT,&split),2); }
    5208             : 
    5209             : /*************************************************************************/
    5210             : /*                     Modular forms of Weight 1                         */
    5211             : /*************************************************************************/
    5212             : /* S_1(G_0(N)), small N. Return 1 if definitely empty; return 0 if maybe
    5213             :  * nonempty  */
    5214             : static int
    5215       16604 : wt1empty(long N)
    5216             : {
    5217       16604 :   if (N <= 100) switch (N)
    5218             :   { /* nonempty [32/100] */
    5219        5446 :     case 23: case 31: case 39: case 44: case 46:
    5220             :     case 47: case 52: case 55: case 56: case 57:
    5221             :     case 59: case 62: case 63: case 68: case 69:
    5222             :     case 71: case 72: case 76: case 77: case 78:
    5223             :     case 79: case 80: case 83: case 84: case 87:
    5224             :     case 88: case 92: case 93: case 94: case 95:
    5225        5446 :     case 99: case 100: return 0;
    5226        3549 :     default: return 1;
    5227             :   }
    5228        7609 :   if (N <= 600) switch(N)
    5229             :   { /* empty [111/500] */
    5230         336 :     case 101: case 102: case 105: case 106: case 109:
    5231             :     case 113: case 121: case 122: case 123: case 125:
    5232             :     case 130: case 134: case 137: case 146: case 149:
    5233             :     case 150: case 153: case 157: case 162: case 163:
    5234             :     case 169: case 170: case 173: case 178: case 181:
    5235             :     case 182: case 185: case 187: case 193: case 194:
    5236             :     case 197: case 202: case 205: case 210: case 218:
    5237             :     case 221: case 226: case 233: case 241: case 242:
    5238             :     case 245: case 246: case 250: case 257: case 265:
    5239             :     case 267: case 269: case 274: case 277: case 281:
    5240             :     case 289: case 293: case 298: case 305: case 306:
    5241             :     case 313: case 314: case 317: case 326: case 337:
    5242             :     case 338: case 346: case 349: case 353: case 361:
    5243             :     case 362: case 365: case 369: case 370: case 373:
    5244             :     case 374: case 377: case 386: case 389: case 394:
    5245             :     case 397: case 401: case 409: case 410: case 421:
    5246             :     case 425: case 427: case 433: case 442: case 449:
    5247             :     case 457: case 461: case 466: case 481: case 482:
    5248             :     case 485: case 490: case 493: case 509: case 514:
    5249             :     case 521: case 530: case 533: case 534: case 538:
    5250             :     case 541: case 545: case 554: case 557: case 562:
    5251             :     case 565: case 569: case 577: case 578: case 586:
    5252         336 :     case 593: return 1;
    5253        6972 :     default: return 0;
    5254             :   }
    5255         301 :   return 0;
    5256             : }
    5257             : 
    5258             : static GEN
    5259          28 : initwt1trace(GEN mf)
    5260             : {
    5261          28 :   GEN S = MF_get_S(mf), v, H;
    5262             :   long l, i;
    5263          28 :   if (lg(S) == 1) return mftrivial();
    5264          28 :   H = mfheckemat(mf, Mindex_as_coef(mf));
    5265          28 :   l = lg(H); v = cgetg(l, t_VEC);
    5266          63 :   for (i = 1; i < l; i++) gel(v,i) = gtrace(gel(H,i));
    5267          28 :   v = Minv_RgC_mul(MF_get_Minv(mf), v);
    5268          28 :   return mflineardiv_linear(S, v, 1);
    5269             : }
    5270             : static GEN
    5271          21 : initwt1newtrace(GEN mf)
    5272             : {
    5273          21 :   GEN v, D, S, Mindex, CHI = MF_get_CHI(mf);
    5274          21 :   long FC, lD, i, sb, N1, N2, lM, N = MF_get_N(mf);
    5275          21 :   CHI = mfchartoprimitive(CHI, &FC);
    5276          21 :   if (N % FC || mfcharparity(CHI) == 1) return mftrivial();
    5277          21 :   D = mydivisorsu(N/FC); lD = lg(D);
    5278          21 :   S = MF_get_S(mf);
    5279          21 :   if (lg(S) == 1) return mftrivial();
    5280          21 :   N2 = newd_params2(N);
    5281          21 :   N1 = N / N2;
    5282          21 :   Mindex = MF_get_Mindex(mf);
    5283          21 :   lM = lg(Mindex);
    5284          21 :   sb = Mindex[lM-1];
    5285          21 :   v = zerovec(sb+1);
    5286          42 :   for (i = 1; i < lD; i++)
    5287             :   {
    5288          21 :     long M = FC*D[i], j;
    5289          21 :     GEN tf = initwt1trace(M == N? mf: mfinit_Nkchi(M, 1, CHI, mf_CUSP, 0));
    5290             :     GEN listd, w;
    5291          21 :     if (mf_get_type(tf) == t_MF_CONST) continue;
    5292          21 :     w = mfcoefs_i(tf, sb, 1);
    5293          21 :     if (M == N) { v = gadd(v, w); continue; }
    5294           0 :     listd = mydivisorsu(u_ppo(ugcd(N/M, N1), FC));
    5295           0 :     for (j = 1; j < lg(listd); j++)
    5296             :     {
    5297           0 :       long d = listd[j], d2 = d*d; /* coprime to FC */
    5298           0 :       GEN dk = mfchareval(CHI, d);
    5299           0 :       long NMd = N/(M*d), m;
    5300           0 :       for (m = 1; m <= sb/d2; m++)
    5301             :       {
    5302           0 :         long be = mubeta2(NMd, m);
    5303           0 :         if (be)
    5304             :         {
    5305           0 :           GEN c = gmul(dk, gmulsg(be, gel(w, m+1)));
    5306           0 :           long n = m*d2;
    5307           0 :           gel(v, n+1) = gadd(gel(v, n+1), c);
    5308             :         }
    5309             :       }
    5310             :     }
    5311             :   }
    5312          21 :   if (gequal0(gel(v,2))) return mftrivial();
    5313          21 :   v = vecpermute(v,Mindex);
    5314          21 :   v = Minv_RgC_mul(MF_get_Minv(mf), v);
    5315          21 :   return mflineardiv_linear(S, v, 1);
    5316             : }
    5317             : 
    5318             : /* i*p + 1, i*p < lim corresponding to a_p(f_j), a_{2p}(f_j)...  */
    5319             : static GEN
    5320        1820 : pindices(long p, long lim)
    5321             : {
    5322        1820 :   GEN v = cgetg(lim, t_VECSMALL);
    5323             :   long i, ip;
    5324       20811 :   for (i = 1, ip = p + 1; ip < lim; i++, ip += p) v[i] = ip;
    5325        1820 :   setlg(v, i); return v;
    5326             : }
    5327             : 
    5328             : /* assume !wt1empty(N), in particular N>25 */
    5329             : /* Returns [[lim,p], mf (weight 2), p*lim x dim matrix] */
    5330             : static GEN
    5331        1820 : mf1_pre(long N)
    5332             : {
    5333             :   pari_timer tt;
    5334             :   GEN mf, v, L, I, M, Minv, den;
    5335             :   long B, lim, LIM, p;
    5336             : 
    5337        1820 :   if (DEBUGLEVEL) timer_start(&tt);
    5338        1820 :   mf = mfinit_Nkchi(N, 2, mfchartrivial(), mf_CUSP, 0);
    5339        1820 :   if (DEBUGLEVEL)
    5340           0 :     timer_printf(&tt, "mf1basis [pre]: S_2(%ld), dim = %ld",
    5341             :                  N, MF_get_dim(mf));
    5342        1820 :   M = MF_get_M(mf); Minv = MF_get_Minv(mf); den = gel(Minv,2);
    5343        1820 :   B = mfsturm_mf(mf);
    5344        1820 :   if (uisprime(N))
    5345             :   {
    5346         392 :     lim = 2 * MF_get_dim(mf); /* ensure mfstabiter's first kernel ~ square */
    5347         392 :     p = 2;
    5348             :   }
    5349             :   else
    5350             :   {
    5351             :     forprime_t S;
    5352        1428 :     u_forprime_init(&S, 2, N);
    5353        2562 :     while ((p = u_forprime_next(&S)))
    5354        2562 :       if (N % p) break;
    5355        1428 :     lim = B + 1;
    5356             :   }
    5357        1820 :   LIM = (N & (N - 1))? 2 * lim: 3 * lim; /* N power of 2 ? */
    5358        1820 :   L = mkvecsmall4(lim, LIM, mfsturmNk(N,1), p);
    5359        1820 :   M = bhnmat_extend_nocache(M, N, LIM-1, 1, MF_get_S(mf));
    5360        1820 :   if (DEBUGLEVEL) timer_printf(&tt, "mf1basis [pre]: bnfmat_extend");
    5361        1820 :   v = pindices(p, LIM);
    5362        1820 :   if (!LIM) return mkvec4(L, mf, M, v);
    5363        1820 :   I = RgM_Rg_div(ZM_mul(rowslice(M, B+2, LIM), gel(Minv,1)), den);
    5364        1820 :   I = Q_remove_denom(I, &den);
    5365        1820 :   if (DEBUGLEVEL) timer_printf(&tt, "mf1basis [prec]: Iden");
    5366        1820 :   return mkvec5(L, mf, M, v, mkvec2(I, den));
    5367             : }
    5368             : 
    5369             : /* lg(A) > 1, E a t_POL */
    5370             : static GEN
    5371         644 : mfmatsermul(GEN A, GEN E)
    5372             : {
    5373         644 :   long j, l = lg(A), r = nbrows(A);
    5374         644 :   GEN M = cgetg(l, t_MAT);
    5375         644 :   E = RgXn_red_shallow(E, r+1);
    5376        5677 :   for (j = 1; j < l; j++)
    5377             :   {
    5378        5033 :     GEN c = RgV_to_RgX(gel(A,j), 0);
    5379        5033 :     gel(M, j) = RgX_to_RgC(RgXn_mul(c, E, r+1), r);
    5380             :   }
    5381         644 :   return M;
    5382             : }
    5383             : /* lg(Ap) > 1, Ep an Flxn */
    5384             : static GEN
    5385        1113 : mfmatsermul_Fl(GEN Ap, GEN Ep, ulong p)
    5386             : {
    5387        1113 :   long j, l = lg(Ap), r = nbrows(Ap);
    5388        1113 :   GEN M = cgetg(l, t_MAT);
    5389       41209 :   for (j = 1; j < l; j++)
    5390             :   {
    5391       40096 :     GEN c = Flv_to_Flx(gel(Ap,j), 0);
    5392       40096 :     gel(M,j) = Flx_to_Flv(Flxn_mul(c, Ep, r+1, p), r);
    5393             :   }
    5394        1113 :   return M;
    5395             : }
    5396             : 
    5397             : /* CHI mod F | N, return mfchar of modulus N.
    5398             :  * FIXME: wasteful, G should be precomputed  */
    5399             : static GEN
    5400       12964 : mfcharinduce(GEN CHI, long N)
    5401             : {
    5402             :   GEN G, chi;
    5403       12964 :   if (mfcharmodulus(CHI) == N) return CHI;
    5404        1414 :   G = znstar0(utoipos(N), 1);
    5405        1414 :   chi = zncharinduce(gel(CHI,1), gel(CHI,2), G);
    5406        1414 :   CHI = leafcopy(CHI);
    5407        1414 :   gel(CHI,1) = G;
    5408        1414 :   gel(CHI,2) = chi; return CHI;
    5409             : }
    5410             : 
    5411             : static GEN
    5412        3983 : gmfcharno(GEN CHI)
    5413             : {
    5414        3983 :   GEN G = gel(CHI,1), chi = gel(CHI,2);
    5415        3983 :   return mkintmod(znconreyexp(G, chi), znstar_get_N(G));
    5416             : }
    5417             : static long
    5418       13538 : mfcharno(GEN CHI)
    5419             : {
    5420       13538 :   GEN n = znconreyexp(gel(CHI,1), gel(CHI,2));
    5421       13538 :   return itou(n);
    5422             : }
    5423             : 
    5424             : /* return k such that minimal mfcharacter in Galois orbit of CHI is CHI^k */
    5425             : static long
    5426       12054 : mfconreyminimize(GEN CHI)
    5427             : {
    5428       12054 :   GEN G = gel(CHI,1), cyc, chi;
    5429       12054 :   cyc = ZV_to_zv(znstar_get_cyc(G));
    5430       12054 :   chi = ZV_to_zv(znconreychar(G, gel(CHI,2)));
    5431       12054 :   return zv_cyc_minimize(cyc, chi, coprimes_zv(mfcharorder(CHI)));
    5432             : }
    5433             : 
    5434             : /* find scalar c such that first nonzero entry of c*v is 1; return c*v */
    5435             : static GEN
    5436        1953 : RgV_normalize(GEN v, GEN *pc)
    5437             : {
    5438        1953 :   long i, l = lg(v);
    5439        4739 :   for (i = 1; i < l; i++)
    5440             :   {
    5441        4739 :     GEN c = gel(v,i);
    5442        4739 :     if (!gequal0(c))
    5443             :     {
    5444        1953 :       if (gequal1(c)) break;
    5445         651 :       *pc = ginv(c); return RgV_Rg_mul(v, *pc);
    5446             :     }
    5447             :   }
    5448        1302 :   *pc = gen_1; return v;
    5449             : }
    5450             : /* pS != NULL; dim > 0 */
    5451             : static GEN
    5452         770 : mftreatdihedral(long N, GEN DIH, GEN POLCYC, long ordchi, GEN *pS)
    5453             : {
    5454         770 :   long l = lg(DIH), lim = mfsturmNk(N, 1), i;
    5455         770 :   GEN Minv, C = cgetg(l, t_VEC), M = cgetg(l, t_MAT);
    5456        2310 :   for (i = 1; i < l; i++)
    5457             :   {
    5458        1540 :     GEN c, v = mfcoefs_i(gel(DIH,i), lim, 1);
    5459        1540 :     gel(M,i) = RgV_normalize(v, &c);
    5460        1540 :     gel(C,i) = Rg_col_ei(c, l-1, i);
    5461             :   }
    5462         770 :   Minv = gel(mfclean(M,POLCYC,ordchi,0),2);
    5463         770 :   M = RgM_Minv_mul(M, Minv);
    5464         770 :   C = RgM_Minv_mul(C, Minv);
    5465         770 :   *pS = vecmflinear(DIH, C); return M;
    5466             : }
    5467             : 
    5468             : /* same mode a maximal ideal above q */
    5469             : static GEN
    5470        2408 : Tpmod(GEN Ap, GEN A, ulong chip, long p, ulong q)
    5471             : {
    5472        2408 :   GEN B = leafcopy(Ap);
    5473        2408 :   long i, ip, l = lg(B);
    5474       86345 :   for (i = 1, ip = p; ip < l; i++, ip += p)
    5475       83937 :     B[ip] = Fl_add(B[ip], Fl_mul(A[i], chip, q), q);
    5476        2408 :   return B;
    5477             : }
    5478             : /* Tp(f_1), ..., Tp(f_d) mod q */
    5479             : static GEN
    5480         301 : matTpmod(GEN Ap, GEN A, ulong chip, long p, ulong q)
    5481             : {
    5482             :   long i, l;
    5483         301 :   GEN B = cgetg_copy(A, &l);
    5484        2709 :   for (i = 1; i < l; i++) gel(B,i) = Tpmod(gel(Ap,i), gel(A,i), chip, p, q);
    5485         301 :   return B;
    5486             : }
    5487             : 
    5488             : /* Ap[i] = a_{p*i}(F), A[i] = a_i(F), i = 1..lim
    5489             :  * Tp(f)[n] = a_{p*n}(f) + chi(p) a_{n/p}(f) * 1_{p | n} */
    5490             : static GEN
    5491         469 : Tp(GEN Ap, GEN A, GEN chip, long p)
    5492             : {
    5493         469 :   GEN B = leafcopy(Ap);
    5494         469 :   long i, ip, l = lg(B);
    5495       12915 :   for (i = 1, ip = p; ip < l; i++, ip += p)
    5496       12446 :     gel(B,ip) = gadd(gel(B,ip), gmul(gel(A,i), chip));
    5497         469 :   return B;
    5498             : }
    5499             : /* Tp(f_1), ..., Tp(f_d) mod q */
    5500             : static GEN
    5501          56 : matTp(GEN Ap, GEN A, GEN chip, long p)
    5502             : {
    5503             :   long i, l;
    5504          56 :   GEN B = cgetg_copy(A, &l);
    5505         525 :   for (i = 1; i < l; i++) gel(B,i) = Tp(gel(Ap,i), gel(A,i), chip, p);
    5506          56 :   return B;
    5507             : }
    5508             : 
    5509             : static GEN
    5510         378 : _RgXQM_mul(GEN x, GEN y, GEN T)
    5511         378 : { return T? RgXQM_mul(x, y, T): RgM_mul(x, y); }
    5512             : /* largest T-stable Q(CHI)-subspace of Q(CHI)-vector space spanned by columns
    5513             :  * of A */
    5514             : static GEN
    5515          28 : mfstabiter(GEN *pC, GEN A0, GEN chip, GEN TMP, GEN P, long ordchi)
    5516             : {
    5517          28 :   GEN A, Ap, vp = gel(TMP,4), C = NULL;
    5518          28 :   long i, lA, lim1 = gel(TMP,1)[3], p = gel(TMP,1)[4];
    5519             :   pari_timer tt;
    5520             : 
    5521          28 :   Ap = rowpermute(A0, vp);
    5522          28 :   A = rowslice(A0, 2, nbrows(Ap)+1); /* remove a0 */
    5523             :   for(;;)
    5524          28 :   {
    5525          56 :     GEN R = shallowconcat(matTp(Ap, A, chip, p), A);
    5526          56 :     GEN B = QabM_ker(R, P, ordchi);
    5527          56 :     long lB = lg(B);
    5528          56 :     if (DEBUGLEVEL)
    5529           0 :       timer_printf(&tt, "mf1basis: Hecke intersection (dim %ld)", lB-1);
    5530          56 :     if (lB == 1) return NULL;
    5531          56 :     lA = lg(A); if (lB == lA) break;
    5532          28 :     B = rowslice(B, 1, lA-1);
    5533          28 :     Ap = _RgXQM_mul(Ap, B, P);
    5534          28 :     A = _RgXQM_mul(A, B, P);
    5535          28 :     C = C? _RgXQM_mul(C, B, P): B;
    5536             :   }
    5537          28 :   if (nbrows(A) < lim1)
    5538             :   {
    5539          14 :     A0 = rowslice(A0, 2, lim1);
    5540          14 :     A = C? _RgXQM_mul(A0, C, P): A0;
    5541             :   }
    5542             :   else /* all needed coefs computed */
    5543          14 :     A = rowslice(A, 1, lim1-1);
    5544          28 :   if (*pC) C = C? _RgXQM_mul(*pC, C, P): *pC;
    5545             :   /* put back a0 */
    5546         119 :   for (i = 1; i < lA; i++) gel(A,i) = vec_prepend(gel(A,i), gen_0);
    5547          28 :   *pC = C; return A;
    5548             : }
    5549             : 
    5550             : static long
    5551         252 : mfstabitermod(GEN A, GEN vp, ulong chip, long p, ulong q)
    5552             : {
    5553         252 :   GEN Ap, C = NULL;
    5554         252 :   Ap = rowpermute(A, vp);
    5555         252 :   A = rowslice(A, 2, nbrows(Ap)+1);
    5556             :   while (1)
    5557          49 :   {
    5558         301 :     GEN Rp = shallowconcat(matTpmod(Ap, A, chip, p, q), A);
    5559         301 :     GEN B = Flm_ker(Rp, q);
    5560         301 :     long lA = lg(A), lB = lg(B);
    5561         301 :     if (lB == 1) return 0;
    5562         266 :     if (lB == lA) return lA-1;
    5563          49 :     B = rowslice(B, 1, lA-1);
    5564          49 :     Ap = Flm_mul(Ap, B, q);
    5565          49 :     A = Flm_mul(A, B, q);
    5566          49 :     C = C? Flm_mul(C, B, q): B;
    5567             :   }
    5568             : }
    5569             : 
    5570             : static GEN
    5571         581 : mfcharinv_i(GEN CHI)
    5572             : {
    5573         581 :   GEN G = gel(CHI,1);
    5574         581 :   CHI = leafcopy(CHI); gel(CHI,2) =  zncharconj(G, gel(CHI,2)); return CHI;
    5575             : }
    5576             : 
    5577             : /* upper bound dim S_1(Gamma_0(N),chi) performing the linear algebra mod p */
    5578             : static long
    5579         581 : mf1dimmod(GEN E1, GEN E, GEN chip, long ordchi, long dih, GEN TMP)
    5580             : {
    5581         581 :   GEN E1i, A, vp, mf, C = NULL;
    5582         581 :   ulong q, r = QabM_init(ordchi, &q);
    5583             :   long lim, LIM, p;
    5584             : 
    5585         581 :   LIM = gel(TMP,1)[2]; lim = gel(TMP,1)[1];
    5586         581 :   mf= gel(TMP,2);
    5587         581 :   A = gel(TMP,3);
    5588         581 :   A = QabM_to_Flm(A, r, q);
    5589         581 :   E1 = QabX_to_Flx(E1, r, q);
    5590         581 :   E1i = Flxn_inv(E1, nbrows(A), q);
    5591         581 :   if (E)
    5592             :   {
    5593         560 :     GEN Iden = gel(TMP,5), I = gel(Iden,1), den = gel(Iden,2);
    5594         560 :     GEN Mindex = MF_get_Mindex(mf), F = rowslice(A, 1, LIM);
    5595         560 :     GEN E1ip = Flxn_red(E1i, LIM);
    5596         560 :     ulong d = den? umodiu(den, q): 1;
    5597         560 :     long i, nE = lg(E) - 1;
    5598             :     pari_sp av;
    5599             : 
    5600         560 :     I = ZM_to_Flm(I, q);
    5601         560 :     if (d != 1) I = Flm_Fl_mul(I, Fl_inv(d, q), q);
    5602         560 :     av = avma;
    5603        1092 :     for (i = 1; i <= nE; i++)
    5604             :     {
    5605         861 :       GEN e = Flxn_mul(E1ip, QabX_to_Flx(gel(E,i), r, q), LIM, q);
    5606         861 :       GEN B = mfmatsermul_Fl(F, e, q), z;
    5607         861 :       GEN B2 = Flm_mul(I, rowpermute(B, Mindex), q);
    5608         861 :       B = rowslice(B, lim+1,LIM);
    5609         861 :       z = Flm_ker(Flm_sub(B2, B, q), q);
    5610         861 :       if (lg(z)-1 == dih) return dih;
    5611         532 :       C = C? Flm_mul(C, z, q): z;
    5612         532 :       F = Flm_mul(F, z, q);
    5613         532 :       gerepileall(av, 2, &F,&C);
    5614             :     }
    5615         231 :     A = F;
    5616             :   }
    5617             :   /* use Schaeffer */
    5618         252 :   p = gel(TMP,1)[4]; vp = gel(TMP,4);
    5619         252 :   A = mfmatsermul_Fl(A, E1i, q);
    5620         252 :   return mfstabitermod(A, vp, Qab_to_Fl(chip, r, q), p, q);
    5621             : }
    5622             : 
    5623             : static GEN
    5624         224 : mf1intermat(GEN A, GEN Mindex, GEN e, GEN Iden, long lim, GEN POLCYC)
    5625             : {
    5626         224 :   long j, l = lg(A), LIM = nbrows(A);
    5627         224 :   GEN I = gel(Iden,1), den = gel(Iden,2), B = cgetg(l, t_MAT);
    5628             : 
    5629        5257 :   for (j = 1; j < l; j++)
    5630             :   {
    5631        5033 :     pari_sp av = avma;
    5632        5033 :     GEN c = RgV_to_RgX(gel(A,j), 0), c1, c2;
    5633        5033 :     c = RgX_to_RgC(RgXn_mul(c, e, LIM), LIM);
    5634        5033 :     if (POLCYC) c = liftpol_shallow(c);
    5635        5033 :     c1 = vecslice(c, lim+1, LIM);
    5636        5033 :     if (den) c1 = RgC_Rg_mul(c1, den);
    5637        5033 :     c2 = RgM_RgC_mul(I, vecpermute(c, Mindex));
    5638        5033 :     gel(B, j) = gerepileupto(av, RgC_sub(c2, c1));
    5639             :   }
    5640         224 :   return B;
    5641             : }
    5642             : /* Compute the full S_1(\G_0(N),\chi); return NULL if space is empty; else
    5643             :  * if pS is NULL, return stoi(dim), where dim is the dimension; else *pS is
    5644             :  * set to a vector of forms making up a basis, and return the matrix of their
    5645             :  * Fourier expansions. pdih gives the dimension of the subspace generated by
    5646             :  * dihedral forms; TMP is from mf1_pre or NULL. */
    5647             : static GEN
    5648       11256 : mf1basis(long N, GEN CHI, GEN TMP, GEN vSP, GEN *pS, long *pdih)
    5649             : {
    5650       11256 :   GEN E = NULL, EB, E1, E1i, dE1i, mf, A, C, POLCYC, DIH, Minv, chip;
    5651       11256 :   long nE = 0, p, LIM, lim, lim1, i, lA, dimp, ordchi, dih;
    5652             :   pari_timer tt;
    5653             :   pari_sp av;
    5654             : 
    5655       11256 :   if (pdih) *pdih = 0;
    5656       11256 :   if (pS) *pS = NULL;
    5657       11256 :   if (wt1empty(N) || mfcharparity(CHI) != -1) return NULL;
    5658       10962 :   ordchi = mfcharorder(CHI);
    5659       10962 :   if (uisprime(N) && ordchi > 4) return NULL;
    5660       10934 :   if (pS)
    5661             :   {
    5662        3843 :     DIH = mfdihedralcusp(N, CHI, vSP);
    5663        3843 :     dih = lg(DIH) - 1;
    5664             :   }
    5665             :   else
    5666             :   {
    5667        7091 :     DIH = NULL;
    5668        7091 :     dih = mfdihedralcuspdim(N, CHI, vSP);
    5669             :   }
    5670       10934 :   POLCYC = (ordchi <= 2)? NULL: mfcharpol(CHI);
    5671       10934 :   if (pdih) *pdih = dih;
    5672       10934 :   if (N <= 600) switch(N)
    5673             :   {
    5674             :     long m;
    5675         126 :     case 219: case 273: case 283: case 331: case 333: case 344: case 416:
    5676             :     case 438: case 468: case 491: case 504: case 546: case 553: case 563:
    5677             :     case 566: case 581: case 592:
    5678         126 :       break; /* one chi with both exotic and dihedral forms */
    5679        9485 :     default: /* only dihedral forms */
    5680        9485 :       if (!dih) return NULL;
    5681             :       /* fall through */
    5682             :     case 124: case 133: case 148: case 171: case 201: case 209: case 224:
    5683             :     case 229: case 248: case 261: case 266: case 288: case 296: case 301:
    5684             :     case 309: case 325: case 342: case 371: case 372: case 380: case 399:
    5685             :     case 402: case 403: case 404: case 408: case 418: case 432: case 444:
    5686             :     case 448: case 451: case 453: case 458: case 496: case 497: case 513:
    5687             :     case 522: case 527: case 532: case 576: case 579:
    5688             :       /* no chi with both exotic and dihedral; one chi with exotic forms */
    5689        3234 :       if (dih)
    5690             :       {
    5691        2324 :         if (!pS) return utoipos(dih);
    5692         728 :         return mftreatdihedral(N, DIH, POLCYC, ordchi, pS) ;
    5693             :       }
    5694         910 :       m = mfcharno(mfcharinduce(CHI,N));
    5695         910 :       if (N == 124 && (m != 67 && m != 87)) return NULL;
    5696         784 :       if (N == 133 && (m != 83 && m !=125)) return NULL;
    5697         490 :       if (N == 148 && (m !=105 && m !=117)) return NULL;
    5698         364 :       if (N == 171 && (m != 94 && m !=151)) return NULL;
    5699         364 :       if (N == 201 && (m != 29 && m !=104)) return NULL;
    5700         364 :       if (N == 209 && (m != 87 && m !=197)) return NULL;
    5701         364 :       if (N == 224 && (m != 95 && m !=191)) return NULL;
    5702         364 :       if (N == 229 && (m !=107 && m !=122)) return NULL;
    5703         364 :       if (N == 248 && (m != 87 && m !=191)) return NULL;
    5704         273 :       if (N == 261 && (m != 46 && m !=244)) return NULL;
    5705         273 :       if (N == 266 && (m != 83 && m !=125)) return NULL;
    5706         273 :       if (N == 288 && (m != 31 && m !=223)) return NULL;
    5707         273 :       if (N == 296 && (m !=105 && m !=265)) return NULL;
    5708             :   }
    5709         581 :   if (DEBUGLEVEL)
    5710           0 :     err_printf("mf1basis: start character %Ps, conductor = %ld, order = %ld\n",
    5711             :                gmfcharno(CHI), mfcharconductor(CHI), ordchi);
    5712         581 :   if (!TMP) TMP = mf1_pre(N);
    5713         581 :   lim = gel(TMP,1)[1]; LIM = gel(TMP,1)[2]; lim1 = gel(TMP,1)[3];
    5714         581 :   p = gel(TMP,1)[4];
    5715         581 :   mf  = gel(TMP,2);
    5716         581 :   A   = gel(TMP,3);
    5717         581 :   EB = mfeisensteinbasis(N, 1, mfcharinv_i(CHI));
    5718         581 :   nE = lg(EB) - 1;
    5719         581 :   E1 = RgV_to_RgX(mftocol(gel(EB,1), LIM-1, 1), 0); /* + O(x^LIM) */
    5720         581 :   if (--nE)
    5721         560 :     E = RgM_to_RgXV(mfvectomat(vecslice(EB, 2, nE+1), LIM-1, 1), 0);
    5722         581 :   chip = mfchareval(CHI, p); /* != 0 */
    5723         581 :   if (DEBUGLEVEL) timer_start(&tt);
    5724         581 :   av = avma; dimp = mf1dimmod(E1, E, chip, ordchi, dih, TMP);
    5725         581 :   set_avma(av);
    5726         581 :   if (DEBUGLEVEL) timer_printf(&tt, "mf1basis: dim mod p is %ld", dimp);
    5727         581 :   if (!dimp) return NULL;
    5728         266 :   if (!pS) return utoi(dimp);
    5729         210 :   if (dimp == dih) return mftreatdihedral(N, DIH, POLCYC, ordchi, pS);
    5730         168 :   E1i = RgXn_inv(E1, LIM); /* E[1] does not vanish at oo */
    5731         168 :   if (POLCYC) E1i = liftpol_shallow(E1i);
    5732         168 :   E1i = Q_remove_denom(E1i, &dE1i);
    5733         168 :   if (DEBUGLEVEL)
    5734             :   {
    5735           0 :     GEN a0 = gel(E1,2);
    5736           0 :     if (typ(a0) == t_POLMOD) a0 = gnorm(a0);
    5737           0 :     a0 = Q_abs_shallow(a0);
    5738           0 :     timer_printf(&tt, "mf1basis: invert E; norm(a0(E)) = %Ps", a0);
    5739             :   }
    5740         168 :   C = NULL;
    5741         168 :   if (nE)
    5742             :   { /* mf attached to S2(N), fi = mfbasis(mf)
    5743             :      * M = coefs(f1,...,fd) up to LIM
    5744             :      * F = coefs(F1,...,FD) = M * C, for some matrix C over Q(chi),
    5745             :      * initially 1, eventually giving \cap_E S2 / E; D <= d.
    5746             :      * B = coefs(E/E1 F1, .., E/E1 FD); we want X in Q(CHI)^d and
    5747             :      * Y in Q(CHI)^D such that
    5748             :      *   B * X = M * Y, i.e. Minv * rowpermute(B, Mindex * X) = Y
    5749             :      *(B  - I * rowpermute(B, Mindex)) * X = 0.
    5750             :      * where I = M * Minv. Rows of (B - I * ...) are 0 up to lim so
    5751             :      * are not included */
    5752         154 :     GEN Mindex = MF_get_Mindex(mf), Iden  = gel(TMP,5);
    5753             :     pari_timer TT;
    5754         154 :     pari_sp av = avma;
    5755         154 :     if (DEBUGLEVEL) timer_start(&TT);
    5756         238 :     for (i = 1; i <= nE; i++)
    5757             :     {
    5758         224 :       pari_sp av2 = avma;
    5759             :       GEN e, z, B;
    5760             : 
    5761         224 :       e = Q_primpart(RgXn_mul(E1i, gel(E,i), LIM));
    5762         224 :       if (DEBUGLEVEL) timer_printf(&TT, "mf1basis: E[%ld] / E[1]", i+1);
    5763             :       /* the first time A is over Z and it is more efficient to lift than
    5764             :          * to let RgXn_mul use Kronecker's trick */
    5765         224 :       if (POLCYC && i == 1) e = liftpol_shallow(e);
    5766         224 :       B = mf1intermat(A, Mindex, e, Iden, lim, i == 1? NULL: POLCYC);
    5767         224 :       if (DEBUGLEVEL) timer_printf(&TT, "mf1basis: ... intermat");
    5768         224 :       z = gerepileupto(av2, QabM_ker(B, POLCYC, ordchi));
    5769         224 :       if (DEBUGLEVEL)
    5770           0 :         timer_printf(&TT, "mf1basis: ... kernel (dim %ld)",lg(z)-1);
    5771         224 :       if (lg(z) == 1) return NULL;
    5772         224 :       if (lg(z) == lg(A)) { set_avma(av2); continue; } /* no progress */
    5773         224 :       C = C? _RgXQM_mul(C, z, POLCYC): z;
    5774         224 :       A = _RgXQM_mul(A, z, POLCYC);
    5775         224 :       if (DEBUGLEVEL) timer_printf(&TT, "mf1basis: ... updates");
    5776         224 :       if (lg(z)-1 == dimp) break;
    5777          84 :       if (gc_needed(av, 1))
    5778             :       {
    5779           0 :         if (DEBUGMEM > 1) pari_warn(warnmem,"mf1basis i = %ld", i);
    5780           0 :         gerepileall(av, 2, &A, &C);
    5781             :       }
    5782             :     }
    5783         154 :     if (DEBUGLEVEL) timer_printf(&tt, "mf1basis: intersection [total]");
    5784             :   }
    5785         168 :   lA = lg(A);
    5786         168 :   if (lA-1 == dimp)
    5787             :   {
    5788         140 :     A = mfmatsermul(rowslice(A, 1, lim1), E1i);
    5789         140 :     if (POLCYC) A = RgXQM_red(A, POLCYC);
    5790         140 :     if (DEBUGLEVEL) timer_printf(&tt, "mf1basis: matsermul [1]");
    5791             :   }
    5792             :   else
    5793             :   {
    5794          28 :     A = mfmatsermul(A, E1i);
    5795          28 :     if (POLCYC) A = RgXQM_red(A, POLCYC);
    5796          28 :     if (DEBUGLEVEL) timer_printf(&tt, "mf1basis: matsermul [2]");
    5797          28 :     A = mfstabiter(&C, A, chip, TMP, POLCYC, ordchi);
    5798          28 :     if (DEBUGLEVEL) timer_printf(&tt, "mf1basis: Hecke stability");
    5799          28 :     if (!A) return NULL;
    5800             :   }
    5801         168 :   if (dE1i) C = RgM_Rg_mul(C, dE1i);
    5802         168 :   if (POLCYC)
    5803             :   {
    5804         147 :     A = QXQM_to_mod_shallow(A, POLCYC);
    5805         147 :     C = QXQM_to_mod_shallow(C, POLCYC);
    5806             :   }
    5807         168 :   lA = lg(A);
    5808         581 :   for (i = 1; i < lA; i++)
    5809             :   {
    5810         413 :     GEN c, v = gel(A,i);
    5811         413 :     gel(A,i) = RgV_normalize(v, &c);
    5812         413 :     gel(C,i) = RgC_Rg_mul(gel(C,i), c);
    5813             :   }
    5814         168 :   Minv = gel(mfclean(A, POLCYC, ordchi, 0), 2);
    5815         168 :   A = RgM_Minv_mul(A, Minv);
    5816         168 :   C = RgM_Minv_mul(C, Minv);
    5817         168 :   *pS = vecmflineardiv0(MF_get_S(mf), C, gel(EB,1));
    5818         168 :   return A;
    5819             : }
    5820             : 
    5821             : static void
    5822         392 : MF_set_space(GEN mf, long x) { gmael(mf,1,4) = utoi(x); }
    5823             : static GEN
    5824         238 : mf1_cusptonew(GEN mf, GEN vSP)
    5825             : {
    5826         238 :   const long vy = 1;
    5827             :   long i, lP, dSnew, ct;
    5828         238 :   GEN vP, F, S, Snew, vF, v = split_ii(mf, 0, 0, vSP, &i);
    5829             : 
    5830         238 :   F = gel(v,1);
    5831         238 :   vP= gel(v,2); lP = lg(vP);
    5832         238 :   if (lP == 1) { obj_insert(mf, MF_SPLIT, v); return NULL; }
    5833         224 :   MF_set_space(mf, mf_NEW);
    5834         224 :   S = MF_get_S(mf);
    5835         224 :   dSnew = dim_sum(v);
    5836         224 :   Snew = cgetg(dSnew + 1, t_VEC); ct = 0;
    5837         224 :   vF = cgetg(lP, t_MAT);
    5838         518 :   for (i = 1; i < lP; i++)
    5839             :   {
    5840         294 :     GEN V, P = gel(vP,i), f = liftpol_shallow(gel(F,i));
    5841         294 :     long j, d = degpol(P);
    5842         294 :     gel(vF,i) = V = zerocol(dSnew);
    5843         294 :     if (d == 1)
    5844             :     {
    5845         140 :       gel(Snew, ct+1) = mflineardiv_linear(S, f, 0);
    5846         140 :       gel(V, ct+1) = gen_1;
    5847             :     }
    5848             :     else
    5849             :     {
    5850         154 :       f = RgXV_to_RgM(f,d);
    5851         469 :       for (j = 1; j <= d; j++)
    5852             :       {
    5853         315 :         gel(Snew, ct+j) = mflineardiv_linear(S, row(f,j), 0);
    5854         315 :         gel(V, ct+j) = mkpolmod(pol_xn(j-1,vy), P);
    5855             :       }
    5856             :     }
    5857         294 :     ct += d;
    5858             :   }
    5859         224 :   obj_insert(mf, MF_SPLIT, mkvec2(vF, vP));
    5860         224 :   gel(mf,3) = Snew; return mf;
    5861             : }
    5862             : static GEN
    5863        3955 : mf1init(long N, GEN CHI, GEN TMP, GEN vSP, long space, long flraw)
    5864             : {
    5865        3955 :   GEN mf, mf1, S, M = mf1basis(N, CHI, TMP, vSP, &S, NULL);
    5866        3955 :   if (!M) return NULL;
    5867         938 :   mf1 = mkvec4(stoi(N), gen_1, CHI, utoi(mf_CUSP));
    5868         938 :   mf = mkmf(mf1, cgetg(1,t_VEC), S, gen_0, NULL);
    5869         938 :   if (space == mf_NEW)
    5870             :   {
    5871         238 :     gel(mf,5) = mfcleanCHI(M,CHI, 0);
    5872         238 :     mf = mf1_cusptonew(mf, vSP); if (!mf) return NULL;
    5873         224 :     if (!flraw) M = mfcoefs_mf(mf, mfsturmNk(N,1)+1, 1);
    5874             :   }
    5875         924 :   gel(mf,5) = flraw? zerovec(3): mfcleanCHI(M, CHI, 0);
    5876         924 :   return mf;
    5877             : }
    5878             : 
    5879             : static GEN
    5880         994 : mfEMPTY(GEN mf1)
    5881             : {
    5882         994 :   GEN Minv = mkMinv(cgetg(1,t_MAT), NULL,NULL,NULL);
    5883         994 :   GEN M = mkvec3(cgetg(1,t_VECSMALL), Minv, cgetg(1,t_MAT));
    5884         994 :   return mkmf(mf1, cgetg(1,t_VEC), cgetg(1,t_VEC), cgetg(1,t_VEC), M);
    5885             : }
    5886             : static GEN
    5887         616 : mfEMPTYall(long N, GEN gk, GEN vCHI, long space)
    5888             : {
    5889             :   long i, l;
    5890             :   GEN v, gN, gs;
    5891         616 :   if (!vCHI) return cgetg(1, t_VEC);
    5892          14 :   gN = utoipos(N); gs = utoi(space);
    5893          14 :   l = lg(vCHI); v = cgetg(l, t_VEC);
    5894          42 :   for (i = 1; i < l; i++) gel(v,i) = mfEMPTY(mkvec4(gN,gk,gel(vCHI,i),gs));
    5895          14 :   return v;
    5896             : }
    5897             : 
    5898             : static GEN
    5899        3983 : fmt_dim(GEN CHI, long d, long dih)
    5900        3983 : { return mkvec4(gmfcharorder(CHI), gmfcharno(CHI), utoi(d), stoi(dih)); }
    5901             : /* merge two vector of fmt_dim's for the same vector of characters. If CHI
    5902             :  * is not NULL, remove dim-0 spaces and add character from CHI */
    5903             : static GEN
    5904           7 : merge_dims(GEN V, GEN W, GEN CHI)
    5905             : {
    5906           7 :   long i, j, id, l = lg(V);
    5907           7 :   GEN A = cgetg(l, t_VEC);
    5908           7 :   if (l == 1) return A;
    5909           7 :   id = CHI? 1: 3;
    5910          21 :   for (i = j = 1; i < l; i++)
    5911             :   {
    5912          14 :     GEN v = gel(V,i), w = gel(W,i);
    5913          14 :     long dv = itou(gel(v,id)), dvh = itou(gel(v,id+1)), d;
    5914          14 :     long dw = itou(gel(w,id)), dwh = itou(gel(w,id+1));
    5915          14 :     d = dv + dw;
    5916          14 :     if (d || CHI)
    5917          14 :       gel(A,j++) = CHI? fmt_dim(gel(CHI,i),d, dvh+dwh)
    5918          14 :                       : mkvec2s(d,dvh+dwh);
    5919             :   }
    5920           7 :   setlg(A, j); return A;
    5921             : }
    5922             : static GEN
    5923        3010 : mfdim0all(GEN w)
    5924             : {
    5925        3038 :   if (w) retconst_vec(lg(w)-1, zerovec(2));
    5926        3003 :   return cgetg(1,t_VEC);
    5927             : }
    5928             : static long
    5929        7301 : mf1cuspdim_i(long N, GEN CHI, GEN TMP, GEN vSP, long *dih)
    5930             : {
    5931        7301 :   pari_sp av = avma;
    5932        7301 :   GEN b = mf1basis(N, CHI, TMP, vSP, NULL, dih);
    5933        7301 :   return gc_long(av, b? itou(b): 0);
    5934             : }
    5935             : 
    5936             : static long
    5937         462 : mf1cuspdim(long N, GEN CHI, GEN vSP)
    5938             : {
    5939         462 :   if (!vSP) vSP = get_vDIH(N, divisorsNF(N, mfcharconductor(CHI)));
    5940         462 :   return mf1cuspdim_i(N, CHI, NULL, vSP, NULL);
    5941             : }
    5942             : static GEN
    5943        4144 : mf1cuspdimall(long N, GEN vCHI)
    5944             : {
    5945             :   GEN z, TMP, w, vSP;
    5946             :   long i, j, l;
    5947        4144 :   if (wt1empty(N)) return mfdim0all(vCHI);
    5948        1141 :   w = mf1chars(N,vCHI);
    5949        1141 :   l = lg(w); if (l == 1) return cgetg(1,t_VEC);
    5950        1141 :   z = cgetg(l, t_VEC);
    5951        1141 :   TMP = mf1_pre(N); vSP = get_vDIH(N, NULL);
    5952        7861 :   for (i = j = 1; i < l; i++)
    5953             :   {
    5954        6720 :     GEN CHI = gel(w,i);
    5955        6720 :     long dih, d = mf1cuspdim_i(N, CHI, TMP, vSP, &dih);
    5956        6720 :     if (vCHI)
    5957          42 :       gel(z,j++) = mkvec2s(d, dih);
    5958        6678 :     else if (d)
    5959        1428 :       gel(z,j++) = fmt_dim(CHI, d, dih);
    5960             :   }
    5961        1141 :   setlg(z,j); return z;
    5962             : }
    5963             : 
    5964             : /* dimension of S_1(Gamma_1(N)) */
    5965             : static long
    5966        4123 : mf1cuspdimsum(long N)
    5967             : {
    5968        4123 :   pari_sp av = avma;
    5969        4123 :   GEN v = mf1cuspdimall(N, NULL);
    5970        4123 :   long i, ct = 0, l = lg(v);
    5971        5544 :   for (i = 1; i < l; i++)
    5972             :   {
    5973        1421 :     GEN w = gel(v,i); /* [ord(CHI),*,dim,*] */
    5974        1421 :     ct += itou(gel(w,3))*myeulerphiu(itou(gel(w,1)));
    5975             :   }
    5976        4123 :   return gc_long(av,ct);
    5977             : }
    5978             : 
    5979             : static GEN
    5980          56 : mf1newdimall(long N, GEN vCHI)
    5981             : {
    5982             :   GEN z, w, vTMP, vSP, fa, P, E;
    5983             :   long i, c, l, lw, P1;
    5984          56 :   if (wt1empty(N)) return mfdim0all(vCHI);
    5985          56 :   w = mf1chars(N,vCHI);
    5986          56 :   lw = lg(w); if (lw == 1) return cgetg(1,t_VEC);
    5987          56 :   vTMP = const_vec(N, NULL);
    5988          56 :   vSP = get_vDIH(N, NULL);
    5989          56 :   gel(vTMP,N) = mf1_pre(N);
    5990             :   /* if p || N and p \nmid F(CHI), S_1^new(G0(N),chi) = 0 */
    5991          56 :   fa = znstar_get_faN(gmael(w,1,1));
    5992          56 :   P = gel(fa,1); l = lg(P);
    5993          56 :   E = gel(fa,2);
    5994         154 :   for (i = P1 = 1; i < l; i++)
    5995          98 :     if (E[i] == 1) P1 *= itou(gel(P,i));
    5996             :   /* P1 = \prod_{v_p(N) = 1} p */
    5997          56 :   z = cgetg(lw, t_VEC);
    5998         182 :   for (i = c = 1; i < lw; i++)
    5999             :   {
    6000             :     long S, j, l, F, dihnew;
    6001         126 :     GEN D, CHI = gel(w,i), CHIP = mfchartoprimitive(CHI,&F);
    6002             : 
    6003         126 :     S = F % P1? 0: mf1cuspdim_i(N, CHI, gel(vTMP,N), vSP, &dihnew);
    6004         126 :     if (!S)
    6005             :     {
    6006          56 :       if (vCHI) gel(z, c++) = zerovec(2);
    6007          56 :       continue;
    6008             :     }
    6009          70 :     D = mydivisorsu(N/F); l = lg(D);
    6010          77 :     for (j = l-2; j > 0; j--) /* skip last M = N */
    6011             :     {
    6012           7 :       long M = D[j]*F, m, s, dih;
    6013           7 :       GEN TMP = gel(vTMP,M);
    6014           7 :       if (wt1empty(M) || !(m = mubeta(D[l-j]))) continue; /*m = mubeta(N/M)*/
    6015           7 :       if (!TMP) gel(vTMP,M) = TMP = mf1_pre(M);
    6016           7 :       s = mf1cuspdim_i(M, CHIP, TMP, vSP, &dih);
    6017           7 :       if (s) { S += m * s; dihnew += m * dih; }
    6018             :     }
    6019          70 :     if (vCHI)
    6020          63 :       gel(z,c++) = mkvec2s(S, dihnew);
    6021           7 :     else if (S)
    6022           7 :       gel(z, c++) = fmt_dim(CHI, S, dihnew);
    6023             :   }
    6024          56 :   setlg(z,c); return z;
    6025             : }
    6026             : 
    6027             : static GEN
    6028          28 : mf1olddimall(long N, GEN vCHI)
    6029             : {
    6030             :   long i, j, l;
    6031             :   GEN z, w;
    6032          28 :   if (wt1empty(N)) return mfdim0all(vCHI);
    6033          28 :   w = mf1chars(N,vCHI);
    6034          28 :   l = lg(w); z = cgetg(l, t_VEC);
    6035          84 :   for (i = j = 1; i < l; i++)
    6036             :   {
    6037          56 :     GEN CHI = gel(w,i);
    6038          56 :     long d = mfolddim(N, 1, CHI);
    6039          56 :     if (vCHI)
    6040          28 :       gel(z,j++) = mkvec2s(d,d?-1:0);
    6041          28 :     else if (d)
    6042           7 :       gel(z, j++) = fmt_dim(CHI, d, -1);
    6043             :   }
    6044          28 :   setlg(z,j); return z;
    6045             : }
    6046             : 
    6047             : static long
    6048         469 : mf1olddimsum(long N)
    6049             : {
    6050             :   GEN D;
    6051         469 :   long N2, i, l, S = 0;
    6052         469 :   newd_params(N, &N2); /* will ensure mubeta != 0 */
    6053         469 :   D = mydivisorsu(N/N2); l = lg(D);
    6054        2485 :   for (i = 2; i < l; i++)
    6055             :   {
    6056        2016 :     long M = D[l-i]*N2, d = mf1cuspdimsum(M);
    6057        2016 :     if (d) S -= mubeta(D[i]) * d;
    6058             :   }
    6059         469 :   return S;
    6060             : }
    6061             : static long
    6062        1050 : mf1newdimsum(long N)
    6063             : {
    6064        1050 :   long S = mf1cuspdimsum(N);
    6065        1050 :   return S? S - mf1olddimsum(N): 0;
    6066             : }
    6067             : 
    6068             : /* return the automorphism of a degree-2 nf */
    6069             : static GEN
    6070        5754 : nf2_get_conj(GEN nf)
    6071             : {
    6072        5754 :   GEN pol = nf_get_pol(nf);
    6073        5754 :   return deg1pol_shallow(gen_m1, negi(gel(pol,3)), varn(pol));
    6074             : }
    6075             : 
    6076             : static long
    6077         210 : mfisdihedral(GEN vF, GEN DIH)
    6078             : {
    6079         210 :   GEN vG = gel(DIH,1), M = gel(DIH,2), v, G, bnr, w, gen, D, f, nf, con;
    6080             :   GEN f0, f0b, xin;
    6081             :   long i, l, e, j, L, n;
    6082         210 :   if (lg(M) == 1) return 0;
    6083          28 :   v = RgM_RgC_invimage(M, vF);
    6084          28 :   if (!v) return 0;
    6085          28 :   l = lg(v);
    6086          28 :   for (i = 1; i < l; i++)
    6087          28 :     if (!gequal0(gel(v,i))) break;
    6088          28 :   if (i == l) return 0;
    6089          28 :   G = gel(vG,i);
    6090          28 :   bnr = gel(G,2); D = cyc_get_expo(bnr_get_cyc(bnr));
    6091          28 :   w = gel(G,3);
    6092          28 :   f = bnr_get_mod(bnr);
    6093          28 :   nf = bnr_get_nf(bnr);
    6094          28 :   con = nf2_get_conj(nf);
    6095          28 :   f0 = gel(f,1); f0b = galoisapply(nf, con, f0);
    6096          28 :   xin = zv_to_ZV(gel(w,2)); /* xi(bnr.gen[i]) = e(xin[i] / D) */
    6097          28 :   if (!gequal(f0,f0b))
    6098             :   { /* finite part of conductor not ambiguous */
    6099          14 :     GEN a = idealmul(nf, f0, idealdivexact(nf, f0b, idealadd(nf, f0, f0b)));
    6100          14 :     GEN bnr0 = bnr, S;
    6101          14 :     bnr = Buchray(bnr_get_bnf(bnr), mkvec2(a, gel(f,2)), nf_INIT | nf_GEN);
    6102          14 :     S = bnrsurjection(bnr, bnr0);
    6103          14 :     xin = RgV_RgM_mul(xin, gel(S,1));
    6104             :     /* still xi(gen[i]) = e(xin[i] / D), for the new generators */
    6105             :   }
    6106          28 :   gen = bnr_get_gen(bnr); L = lg(gen);
    6107          42 :   for (j = 1, e = itou(D); j < L; j++)
    6108             :   {
    6109          35 :     GEN Ng = idealnorm(nf, gel(gen,j));
    6110          35 :     GEN a = shifti(gel(xin,j), 1); /* xi(g_j^2) = e(a/D) */
    6111          35 :     GEN b = FpV_dotproduct(xin, isprincipalray(bnr,Ng), D);
    6112          35 :     GEN m = Fp_sub(a, b, D); /* xi(g_j/\bar{g_j}) = e(m/D) */
    6113          35 :     e = ugcd(e, itou(m)); if (e == 1) break;
    6114             :   }
    6115          28 :   n = itou(D) / e;
    6116          28 :   return n == 1? 4: 2*n;
    6117             : }
    6118             : 
    6119             : static ulong
    6120         119 : myradicalu(ulong n) { return zv_prod(gel(myfactoru(n),1)); }
    6121             : 
    6122             : /* list of fundamental discriminants unramified outside N, with sign s
    6123             :  * [s = 0 => no sign condition] */
    6124             : static GEN
    6125         119 : mfunram(long N, long s)
    6126             : {
    6127         119 :   long cN = myradicalu(N >> vals(N)), p = 1, m = 1, l, c, i;
    6128         119 :   GEN D = mydivisorsu(cN), res;
    6129         119 :   l = lg(D);
    6130         119 :   if (s == 1) m = 0; else if (s == -1) p = 0;
    6131         119 :   res = cgetg(6*l - 5, t_VECSMALL);
    6132         119 :   c = 1;
    6133         119 :   if (!odd(N))
    6134             :   { /* d = 1 */
    6135          56 :     if (p) res[c++] = 8;
    6136          56 :     if (m) { res[c++] =-8; res[c++] =-4; }
    6137             :   }
    6138         364 :   for (i = 2; i < l; i++)
    6139             :   { /* skip d = 1, done above */
    6140         245 :     long d = D[i], d4 = d & 3L; /* d odd, squarefree, d4 = 1 or 3 */
    6141         245 :     if (d4 == 1) { if (p) res[c++] = d; }
    6142         182 :     else         { if (m) res[c++] =-d; }
    6143         245 :     if (!odd(N))
    6144             :     {
    6145          56 :       if (p) { res[c++] = 8*d; if (d4 == 3) res[c++] = 4*d; }
    6146          56 :       if (m) { res[c++] =-8*d; if (d4 == 1) res[c++] =-4*d; }
    6147             :     }
    6148             :   }
    6149         119 :   setlg(res, c); return res;
    6150             : }
    6151             : 
    6152             : /* Return 1 if F is definitely not S4 type; return 0 on failure. */
    6153             : static long
    6154         105 : mfisnotS4(long N, GEN w)
    6155             : {
    6156         105 :   GEN D = mfunram(N, 0);
    6157         105 :   long i, lD = lg(D), lw = lg(w);
    6158         616 :   for (i = 1; i < lD; i++)
    6159             :   {
    6160         511 :     long p, d = D[i], ok = 0;
    6161        1442 :     for (p = 2; p < lw; p++)
    6162        1442 :       if (w[p] && kross(d,p) == -1) { ok = 1; break; }
    6163         511 :     if (!ok) return 0;
    6164             :   }
    6165         105 :   return 1;
    6166             : }
    6167             : 
    6168             : /* Return 1 if Q(sqrt(5)) \not\subset Q(F), i.e. F is definitely not A5 type;
    6169             :  * return 0 on failure. */
    6170             : static long
    6171         105 : mfisnotA5(GEN F)
    6172             : {
    6173         105 :   GEN CHI = mf_get_CHI(F), P = mfcharpol(CHI), T, Q;
    6174             : 
    6175         105 :   if (mfcharorder(CHI) % 5 == 0) return 0;
    6176         105 :   T = mf_get_field(F); if (degpol(T) == 1) return 1;
    6177         105 :   if (degpol(P) > 1) T = rnfequation(P,T);
    6178         105 :   Q = gsubgs(pol_xn(2,varn(T)), 5);
    6179         105 :   return (typ(nfisincl(Q, T)) == t_INT);
    6180             : }
    6181             : 
    6182             : /* v[p+1]^2 / chi(p) - 2 = z + 1/z with z primitive root of unity of order n,
    6183             :  * return n */
    6184             : static long
    6185        6741 : mffindrootof1(GEN v, long p, GEN CHI)
    6186             : {
    6187        6741 :   GEN ap = gel(v,p+1), u0, u1, u1k, u2;
    6188        6741 :   long c = 1;
    6189        6741 :   if (gequal0(ap)) return 2;
    6190        5033 :   u0 = gen_2; u1k = u1 = gsubgs(gdiv(gsqr(ap), mfchareval(CHI, p)), 2);
    6191       14812 :   while (!gequalsg(2, liftpol_shallow(u1))) /* u1 = z^c + z^-c */
    6192             :   {
    6193        9779 :     u2 = gsub(gmul(u1k, u1), u0);
    6194        9779 :     u0 = u1; u1 = u2; c++;
    6195             :   }
    6196        5033 :   return c;
    6197             : }
    6198             : 
    6199             : /* we known that F is not dihedral */
    6200             : static long
    6201         182 : mfgaloistype_i(long N, GEN CHI, GEN F, GEN v)
    6202             : {
    6203             :   forprime_t iter;
    6204         182 :   long lim = lg(v)-2;
    6205         182 :   GEN w = zero_zv(lim);
    6206             :   pari_sp av;
    6207             :   ulong p;
    6208         182 :   u_forprime_init(&iter, 2, lim);
    6209         182 :   av = avma;
    6210        5292 :   while((p = u_forprime_next(&iter))) if (N%p) switch(mffindrootof1(v, p, CHI))
    6211             :   {
    6212        1400 :     case 1: case 2: continue;
    6213        3451 :     case 3: w[p] = 1; break;
    6214          70 :     case 4: return -24; /* S4 */
    6215           0 :     case 5: return -60; /* A5 */
    6216           7 :     default: pari_err_DOMAIN("mfgaloistype", "form", "not a",
    6217             :                              strtoGENstr("cuspidal eigenform"), F);
    6218           0 :     set_avma(av);
    6219             :   }
    6220         364 :   if (mfisnotS4(N,w) && mfisnotA5(F)) return -12; /* A4 */
    6221           0 :   return 0; /* FAILURE */
    6222             : }
    6223             : 
    6224             : static GEN
    6225         210 : mfgaloistype0(long N, GEN CHI, GEN F, GEN DIH, long lim)
    6226             : {
    6227         210 :   pari_sp av = avma;
    6228         210 :   GEN vF = mftocol(F, lim, 1);
    6229         210 :   long t = mfisdihedral(vF, DIH), bound;
    6230         210 :   if (t) { set_avma(av); return stoi(t); }
    6231         182 :   bound = maxss(200, 5*expu(N)*expu(N));
    6232             :   for(;;)
    6233             :   {
    6234         182 :     t = mfgaloistype_i(N, CHI, F, vF);
    6235         175 :     set_avma(av); if (t) return stoi(t);
    6236           0 :     if (lim > bound) return gen_0;
    6237           0 :     lim += lim >> 1;
    6238           0 :     vF = mfcoefs_i(F,lim,1);
    6239             :   }
    6240             : }
    6241             : 
    6242             : /* If f is NULL, give all the galoistypes, otherwise just for f */
    6243             : /* Return 0 to indicate failure; in this case the type is either -12 or -60,
    6244             :  * most likely -12. FIXME using the Galois representation. */
    6245             : GEN
    6246         217 : mfgaloistype(GEN NK, GEN f)
    6247             : {
    6248         217 :   pari_sp av = avma;
    6249         217 :   GEN CHI, T, F, DIH, SP, mf = checkMF_i(NK);
    6250             :   long N, k, lL, i, lim, SB;
    6251             : 
    6252         217 :   if (f && !checkmf_i(f)) pari_err_TYPE("mfgaloistype", f);
    6253         210 :   if (mf)
    6254             :   {
    6255         175 :     N = MF_get_N(mf);
    6256         175 :     k = MF_get_k(mf);
    6257         175 :     CHI = MF_get_CHI(mf);
    6258             :   }
    6259             :   else
    6260             :   {
    6261          35 :     checkNK(NK, &N, &k, &CHI, 0);
    6262          35 :     mf = f? NULL: mfinit_i(NK, mf_NEW);
    6263             :   }
    6264         210 :   if (k != 1) pari_err_DOMAIN("mfgaloistype", "k", "!=", gen_1, stoi(k));
    6265         210 :   SB = mf? mfsturm_mf(mf): mfsturmNk(N,1);
    6266         210 :   SP = get_DIH(N);
    6267         210 :   DIH = mfdihedralnew(N, CHI, SP);
    6268         210 :   lim = lg(DIH) == 1? 200: SB;
    6269         210 :   DIH = mkvec2(DIH, mfvectomat(DIH,SB,1));
    6270         210 :   if (f) return gerepileuptoint(av, mfgaloistype0(N,CHI, f, DIH, lim));
    6271         112 :   F = mfeigenbasis(mf); lL = lg(F);
    6272         112 :   T = cgetg(lL, t_VEC);
    6273         224 :   for (i=1; i < lL; i++) gel(T,i) = mfgaloistype0(N, CHI, gel(F,i), DIH, lim);
    6274         112 :   return gerepileupto(av, T);
    6275             : }
    6276             : 
    6277             : /******************************************************************/
    6278             : /*                   Find all dihedral forms.                     */
    6279             : /******************************************************************/
    6280             : /* lim >= 2 */
    6281             : static void
    6282          14 : consttabdihedral(long lim) { cache_set(cache_DIH, mfdihedralall(lim)); }
    6283             : 
    6284             : /* a ideal coprime to bnr modulus */
    6285             : static long
    6286       90559 : mfdiheval(GEN bnr, GEN w, GEN a)
    6287             : {
    6288       90559 :   GEN L, cycn = gel(w,1), chin = gel(w,2);
    6289       90559 :   long ordmax = cycn[1];
    6290       90559 :   L = ZV_to_Flv(isprincipalray(bnr,a), ordmax);
    6291       90559 :   return Flv_dotproduct(chin, L, ordmax);
    6292             : }
    6293             : 
    6294             : /* A(x^k) mod T = polcyclo(m), 0 <= k < m */
    6295             : static GEN
    6296       30247 : Galois(GEN A, long k, GEN T, long m)
    6297             : {
    6298             :   GEN B;
    6299             :   long i, ik, d;
    6300       30247 :   if (typ(A) != t_POL) return A;
    6301        7413 :   if (varn(A) != varn(T))
    6302             :   {
    6303          14 :     B = cgetg_copy(A, &d); B[1] = A[1];
    6304          35 :     for (i = 2; i < d; i++) gel(B, i) = Galois(gel(A, i), k, T, m);
    6305          14 :     return B;
    6306             :   }
    6307        7399 :   if ((d = degpol(A)) <= 0) return A;
    6308        7042 :   B = cgetg(m + 2, t_POL); B[1] = A[1]; gel(B,2) = gel(A,2);
    6309       61313 :   for (i = 1; i < m; i++) gel(B, i+2) = gen_0;
    6310       23877 :   for (i = 1, ik = k; i <= d; i++, ik = Fl_add(ik, k, m))
    6311       16835 :     gel(B, ik + 2) = gel(A, i+2);
    6312        7042 :   return QX_ZX_rem(normalizepol(B), T);
    6313             : }
    6314             : static GEN
    6315        1001 : vecGalois(GEN v, long k, GEN T, long m)
    6316             : {
    6317             :   long i, l;
    6318        1001 :   GEN w = cgetg_copy(v,&l);
    6319       31227 :   for (i = 1; i < l; i++) gel(w,i) = Galois(gel(v,i), k, T, m);
    6320        1001 :   return w;
    6321             : }
    6322             : 
    6323             : static GEN
    6324      176603 : fix_pol(GEN S, GEN Pn, int *trace)
    6325             : {
    6326      176603 :   if (typ(S) != t_POL) return S;
    6327       95669 :   S = RgX_rem(S, Pn);
    6328       95669 :   if (typ(S) == t_POL)
    6329             :   {
    6330       95669 :     switch(lg(S))
    6331             :     {
    6332       34622 :       case 2: return gen_0;
    6333       15015 :       case 3: return gel(S,2);
    6334             :     }
    6335       46032 :     *trace = 1;
    6336             :   }
    6337       46032 :   return S;
    6338             : }
    6339             : 
    6340             : static GEN
    6341       12537 : dihan(GEN bnr, GEN w, GEN k0j, long m, ulong lim)
    6342             : {
    6343       12537 :   GEN nf = bnr_get_nf(bnr), f = bid_get_ideal(bnr_get_bid(bnr));
    6344       12537 :   GEN v = zerovec(lim+1), cycn = gel(w,1), Tinit = gel(w,3);
    6345       12537 :   GEN Pn = gel(Tinit,lg(Tinit)==4? 2: 1);
    6346       12537 :   long j, ordmax = cycn[1];
    6347       12537 :   long D = itos(nf_get_disc(nf)), vt = varn(Pn);
    6348       12537 :   int trace = 0;
    6349             :   ulong p, n;
    6350             :   forprime_t T;
    6351             : 
    6352       12537 :   if (!lim) return v;
    6353       12327 :   gel(v,2) = gen_1;
    6354       12327 :   u_forprime_init(&T, 2, lim);
    6355             :   /* fill in prime powers first */
    6356       96635 :   while ((p = u_forprime_next(&T)))
    6357             :   {
    6358             :     GEN vP, vchiP, S;
    6359             :     long k, lP;
    6360             :     ulong q, qk;
    6361       84308 :     if (kross(D,p) >= 0) q = p;
    6362       35469 :     else if (!(q = umuluu_le(p,p,lim))) continue;
    6363             :     /* q = Norm P */
    6364       55699 :     vP = idealprimedec(nf, utoipos(p));
    6365       55699 :     lP = lg(vP);
    6366       55699 :     vchiP = cgetg(lP, t_VECSMALL);
    6367      150990 :     for (j = k = 1; j < lP; j++)
    6368             :     {
    6369       95291 :       GEN P = gel(vP,j);
    6370       95291 :       if (!idealval(nf, f, P)) vchiP[k++] = mfdiheval(bnr,w,P);
    6371             :     }
    6372       55699 :     if (k == 1) continue;
    6373       52745 :     setlg(vchiP, k); lP = k;
    6374       52745 :     if (lP == 2)
    6375             :     { /* one prime above p not dividing f */
    6376       14931 :       long s, s0 = vchiP[1];
    6377       24549 :       for (qk=q, s = s0;; s = Fl_add(s,s0,ordmax))
    6378             :       {
    6379       24549 :         S = Qab_zeta(s, ordmax, vt);
    6380       24549 :         gel(v, qk+1) = fix_pol(S, Pn, &trace);
    6381       24549 :         if (!(qk = umuluu_le(qk,q,lim))) break;
    6382             :       }
    6383             :     }
    6384             :     else /* two primes above p not dividing f */
    6385             :     {
    6386       37814 :       long s, s0 = vchiP[1], s1 = vchiP[2];
    6387       37814 :       for (qk=q, k = 1;; k++)
    6388       14462 :       { /* sum over a,b s.t. Norm( P1^a P2^b ) = q^k, i.e. a+b = k */
    6389             :         long a;
    6390       52276 :         GEN S = gen_0;
    6391      176883 :         for (a = 0; a <= k; a++)
    6392             :         {
    6393      124607 :           s = Fl_add(Fl_mul(a, s0, ordmax), Fl_mul(k-a, s1, ordmax), ordmax);
    6394      124607 :           S = gadd(S, Qab_zeta(s, ordmax, vt));
    6395             :         }
    6396       52276 :         gel(v, qk+1) = fix_pol(S, Pn, &trace);
    6397       52276 :         if (!(qk = umuluu_le(qk,q,lim))) break;
    6398             :       }
    6399             :     }
    6400             :   }
    6401             :   /* complete with nonprime powers */
    6402      237181 :   for (n = 2; n <= lim; n++)
    6403             :   {
    6404      224854 :     GEN S, fa = myfactoru(n), P = gel(fa, 1), E = gel(fa, 2);
    6405             :     long q;
    6406      224854 :     if (lg(P) == 2) continue;
    6407             :     /* not a prime power */
    6408       99778 :     q = upowuu(P[1],E[1]);
    6409       99778 :     S = gmul(gel(v, q + 1), gel(v, n/q + 1));
    6410       99778 :     gel(v, n+1) = fix_pol(S, Pn, &trace);
    6411             :   }
    6412       12327 :   if (trace)
    6413             :   {
    6414        6678 :     long k0 = k0j[1], jdeg = k0j[2];
    6415        6678 :     v = QabV_tracerel(Tinit, jdeg, v); /* Apply Galois Mod(k0, ordw) */
    6416        6678 :     if (k0 > 1) v = vecGalois(v, k0, gel(Tinit,1), m);
    6417             :   }
    6418       12327 :   return v;
    6419             : }
    6420             : 
    6421             : /* as cyc_normalize for t_VECSMALL cyc */
    6422             : static GEN
    6423       26810 : cyc_normalize_zv(GEN cyc)
    6424             : {
    6425       26810 :   long i, o = cyc[1], l = lg(cyc); /* > 1 */
    6426       26810 :   GEN D = cgetg(l, t_VECSMALL);
    6427       31185 :   D[1] = o; for (i = 2; i < l; i++) D[i] = o / cyc[i];
    6428       26810 :   return D;
    6429             : }
    6430             : /* as char_normalize for t_VECSMALLs */
    6431             : static GEN
    6432      118517 : char_normalize_zv(GEN chi, GEN ncyc)
    6433             : {
    6434      118517 :   long i, l = lg(chi);
    6435      118517 :   GEN c = cgetg(l, t_VECSMALL);
    6436      118517 :   if (l > 1) {
    6437      118517 :     c[1] = chi[1];
    6438      160454 :     for (i = 2; i < l; i++) c[i] = chi[i] * ncyc[i];
    6439             :   }
    6440      118517 :   return c;
    6441             : }
    6442             : 
    6443             : static GEN
    6444        9240 : dihan_bnf(long D)
    6445             : {
    6446        9240 :   GEN c = getrand(), bnf;
    6447        9240 :   setrand(gen_1);
    6448        9240 :   bnf = Buchall(quadpoly(stoi(D)), nf_FORCE, LOWDEFAULTPREC);
    6449        9240 :   setrand(c);
    6450        9240 :   return bnf;
    6451             : }
    6452             : static GEN
    6453       37639 : dihan_bnr(GEN bnf, GEN A)
    6454             : {
    6455       37639 :   GEN c = getrand(), bnr;
    6456       37639 :   setrand(gen_1);
    6457       37639 :   bnr = Buchray(bnf, A, nf_INIT|nf_GEN);
    6458       37639 :   setrand(c);
    6459       37639 :   return bnr;
    6460             : }
    6461             : /* Hecke xi * (D/.) = Dirichlet chi, return v in Q^r st chi(g_i) = e(v[i]).
    6462             :  * cycn = cyc_normalize_zv(bnr.cyc), chin = char_normalize_zv(chi,cyc) */
    6463             : static GEN
    6464       34489 : bnrchartwist2conrey(GEN chin, GEN cycn, GEN bnrconreyN, GEN kroconreyN)
    6465             : {
    6466       34489 :   long l = lg(bnrconreyN), c1 = cycn[1], i;
    6467       34489 :   GEN v = cgetg(l, t_COL);
    6468      125363 :   for (i = 1; i < l; i++)
    6469             :   {
    6470       90874 :     GEN d = sstoQ(zv_dotproduct(chin, gel(bnrconreyN,i)), c1);
    6471       90874 :     if (kroconreyN[i] < 0) d = gadd(d, ghalf);
    6472       90874 :     gel(v,i) = d;
    6473             :   }
    6474       34489 :   return v;
    6475             : }
    6476             : 
    6477             : /* chi(g_i) = e(v[i]) denormalize wrt Conrey generators orders */
    6478             : static GEN
    6479       34489 : conreydenormalize(GEN znN, GEN v)
    6480             : {
    6481       34489 :   GEN gcyc = znstar_get_conreycyc(znN), w;
    6482       34489 :   long l = lg(v), i;
    6483       34489 :   w = cgetg(l, t_COL);
    6484      125363 :   for (i = 1; i < l; i++)
    6485       90874 :     gel(w,i) = modii(gmul(gel(v,i), gel(gcyc,i)), gel(gcyc,i));
    6486       34489 :   return w;
    6487             : }
    6488             : 
    6489             : static long
    6490       84028 : Miyake(GEN vchi, GEN gb, GEN cycn)
    6491             : {
    6492       84028 :   long i, e = cycn[1], lb = lg(gb);
    6493       84028 :   GEN v = char_normalize_zv(vchi, cycn);
    6494      124992 :   for (i = 1; i < lb; i++)
    6495      100268 :     if ((zv_dotproduct(v, gel(gb,i)) -  v[i]) % e) return 1;
    6496       24724 :   return 0;
    6497             : }
    6498             : 
    6499             : /* list of Hecke characters not induced by a Dirichlet character up to Galois
    6500             :  * conjugation, whose conductor is bnr.cond; cycn = cyc_normalize(bnr.cyc)*/
    6501             : static GEN
    6502       26810 : mklvchi(GEN bnr, GEN cycn, GEN gb)
    6503             : {
    6504       26810 :   GEN cyc = bnr_get_cyc(bnr), cycsmall = ZV_to_zv(cyc);
    6505       26810 :   GEN vchi = cyc2elts(cycsmall);
    6506       26810 :   long ordmax = cycsmall[1], c, i, l;
    6507       26810 :   l = lg(vchi);
    6508      304024 :   for (i = c = 1; i < l; i++)
    6509             :   {
    6510      277214 :     GEN chi = gel(vchi,i);
    6511      277214 :     if (!gb || Miyake(chi, gb, cycn)) gel(vchi, c++) = Flv_to_ZV(chi);
    6512             :   }
    6513       26810 :   setlg(vchi, c); l = c;
    6514      279300 :   for (i = 1; i < l; i++)
    6515             :   {
    6516      252490 :     GEN chi = gel(vchi,i);
    6517             :     long n;
    6518      252490 :     if (!chi) continue;
    6519     1055754 :     for (n = 2; n < ordmax; n++)
    6520      966476 :       if (ugcd(n, ordmax) == 1)
    6521             :       {
    6522      397670 :         GEN tmp = vecmodii(gmulsg(n, chi), cyc);
    6523             :         long j;
    6524     7623539 :         for (j = i+1; j < l; j++)
    6525     7225869 :           if (gel(vchi,j) && gequal(gel(vchi,j), tmp)) gel(vchi,j) = NULL;
    6526             :       }
    6527             :   }
    6528      279300 :   for (i = c = 1; i < l; i++)
    6529             :   {
    6530      252490 :     GEN chi = gel(vchi,i);
    6531      252490 :     if (chi && bnrisconductor(bnr, chi)) gel(vchi, c++) = chi;
    6532             :   }
    6533       26810 :   setlg(vchi, c); return vchi;
    6534             : }
    6535             : 
    6536             : static GEN
    6537        7805 : get_gb(GEN bnr, GEN con)
    6538             : {
    6539        7805 :   GEN gb, g = bnr_get_gen(bnr), nf = bnr_get_nf(bnr);
    6540        7805 :   long i, l = lg(g);
    6541        7805 :   gb = cgetg(l, t_VEC);
    6542       18326 :   for (i = 1; i < l; i++)
    6543       10521 :     gel(gb,i) = ZV_to_zv(isprincipalray(bnr, galoisapply(nf, con, gel(g,i))));
    6544        7805 :   return gb;
    6545             : }
    6546             : static GEN
    6547       15862 : get_bnrconreyN(GEN bnr, GEN znN)
    6548             : {
    6549       15862 :   GEN z, g = znstar_get_conreygen(znN);
    6550       15862 :   long i, l = lg(g);
    6551       15862 :   z = cgetg(l, t_VEC);
    6552       57134 :   for (i = 1; i < l; i++) gel(z,i) = ZV_to_zv(isprincipalray(bnr,gel(g,i)));
    6553       15862 :   return z;
    6554             : }
    6555             : /* con = NULL if D > 0 or if D < 0 and id != idcon. */
    6556             : static GEN
    6557       33698 : mfdihedralcommon(GEN bnf, GEN id, GEN znN, GEN kroconreyN, long N, long D, GEN con)
    6558             : {
    6559       33698 :   GEN bnr = dihan_bnr(bnf, id), cyc = ZV_to_zv( bnr_get_cyc(bnr) );
    6560             :   GEN bnrconreyN, cycn, cycN, Lvchi, res, P, vT;
    6561             :   long j, ordmax, l, lc, deghecke, vt;
    6562             : 
    6563       33698 :   lc = lg(cyc); if (lc == 1) return NULL;
    6564       26810 :   cycn = cyc_normalize_zv(cyc);
    6565       26810 :   Lvchi = mklvchi(bnr, cycn, con? get_gb(bnr, con): NULL);
    6566       26810 :   l = lg(Lvchi);
    6567       26810 :   if (l == 1) return NULL;
    6568             : 
    6569       15862 :   bnrconreyN = get_bnrconreyN(bnr, znN);
    6570       15862 :   cycN = ZV_to_zv(znstar_get_cyc(znN));
    6571       15862 :   ordmax = cyc[1];
    6572       15862 :   vT = const_vec(odd(ordmax)? ordmax << 1: ordmax, NULL);
    6573       15862 :   vt = fetch_user_var("t");
    6574       15862 :   P = polcyclo(ordmax, vt);
    6575       15862 :   gel(vT,ordmax) = Qab_trace_init(ordmax, ordmax, P, P);
    6576       15862 :   deghecke = myeulerphiu(ordmax);
    6577       15862 :   res = cgetg(l, t_VEC);
    6578       50351 :   for (j = 1; j < l; j++)
    6579             :   {
    6580       34489 :     GEN T, v, vchi = ZV_to_zv(gel(Lvchi,j));
    6581       34489 :     GEN chi, chin = char_normalize_zv(vchi, cycn);
    6582             :     long o, vnum, k0, degrel;
    6583       34489 :     v = bnrchartwist2conrey(chin, cycn, bnrconreyN, kroconreyN);
    6584       34489 :     o = itou(Q_denom(v));
    6585       34489 :     T = gel(vT, o);
    6586       34489 :     if (!T) gel(vT,o) = T = Qab_trace_init(ordmax, o, P, polcyclo(o,vt));
    6587       34489 :     chi = conreydenormalize(znN, v);
    6588       34489 :     vnum = itou(znconreyexp(znN, chi));
    6589       34489 :     chi = ZV_to_zv(znconreychar(znN,chi));
    6590       34489 :     degrel = deghecke / degpol(gel(T,1));
    6591       34489 :     k0 = zv_cyc_minimize(cycN, chi, coprimes_zv(o));
    6592       34489 :     vnum = Fl_powu(vnum, k0, N);
    6593             :     /* encodes degrel forms: jdeg = 0..degrel-1 */
    6594       34489 :     gel(res,j) = mkvec3(mkvecsmalln(5, N, k0 % o, vnum, D, degrel),
    6595             :                         id, mkvec3(cycn,chin,T));
    6596             :   }
    6597       15862 :   return res;
    6598             : }
    6599             : 
    6600             : static long
    6601       49364 : is_cond(long D, long n)
    6602             : {
    6603       49364 :   if (D > 0) return n != 4 || (D&7L) == 1;
    6604       30114 :   return n != 2 && n != 3 && (n != 4 || (D&7L)!=1);
    6605             : }
    6606             : /* Append to v all dihedral weight 1 forms coming from D, if fundamental.
    6607             :  * level in [l1, l2] */
    6608             : static void
    6609       18718 : append_dihedral(GEN v, long D, long l1, long l2)
    6610             : {
    6611       18718 :   long Da = labs(D), no, i, numi, ct, min, max;
    6612             :   GEN bnf, con, vI, resall, arch1, arch2;
    6613             :   pari_sp av;
    6614             : 
    6615             :   /* min <= Nf <= max */
    6616       18718 :   max = l2 / Da;
    6617       18718 :   if (l1 == l2)
    6618             :   { /* assume Da | l2 */
    6619         140 :     min = max;
    6620         140 :     if (D > 0 && min < 3) return;
    6621             :   }
    6622             :   else /* assume l1 < l2 */
    6623       18578 :     min = (l1 + Da-1)/Da;
    6624       18718 :   if (!sisfundamental(D)) return;
    6625             : 
    6626        5726 :   av = avma;
    6627        5726 :   bnf = dihan_bnf(D);
    6628        5726 :   con = nf2_get_conj(bnf_get_nf(bnf));
    6629        5726 :   vI = ideallist(bnf, max);
    6630       55090 :   numi = 0; for (i = min; i <= max; i++) numi += lg(gel(vI, i)) - 1;
    6631        5726 :   if (D > 0)
    6632             :   {
    6633        1428 :     numi <<= 1;
    6634        1428 :     arch1 = mkvec2(gen_1,gen_0);
    6635        1428 :     arch2 = mkvec2(gen_0,gen_1);
    6636             :   }
    6637             :   else
    6638        4298 :     arch1 = arch2 = NULL;
    6639        5726 :   resall = cgetg(numi+1, t_VEC); ct = 1;
    6640       55090 :   for (no = min; no <= max; no++) if (is_cond(D, no))
    6641             :   {
    6642       44646 :     long N = Da*no, lc, lI;
    6643       44646 :     GEN I = gel(vI, no), znN = znstar0(utoipos(N), 1), conreyN, kroconreyN;
    6644             : 
    6645       44646 :     conreyN = znstar_get_conreygen(znN); lc = lg(conreyN);
    6646       44646 :     kroconreyN = cgetg(lc, t_VECSMALL);
    6647      166054 :     for (i = 1; i < lc; i++) kroconreyN[i] = krosi(D, gel(conreyN, i));
    6648       44646 :     lI = lg(I);
    6649       87822 :     for (i = 1; i < lI; i++)
    6650             :     {
    6651       43176 :       GEN id = gel(I, i), idcon, z;
    6652             :       long j;
    6653       43176 :       if (typ(id) == t_INT) continue;
    6654       28182 :       idcon = galoisapply(bnf, con, id);
    6655       51408 :       for (j = i; j < lI; j++)
    6656       51408 :         if (gequal(idcon, gel(I, j))) { gel(I, j) = gen_0; break; }
    6657       28182 :       if (D < 0)
    6658             :       {
    6659       17479 :         GEN conk = i == j ? con : NULL;
    6660       17479 :         z = mfdihedralcommon(bnf, id, znN, kroconreyN, N, D, conk);
    6661       17479 :         if (z) gel(resall, ct++) = z;
    6662             :       }
    6663             :       else
    6664             :       {
    6665             :         GEN ide;
    6666       10703 :         ide = mkvec2(id, arch1);
    6667       10703 :         z = mfdihedralcommon(bnf, ide, znN, kroconreyN, N, D, NULL);
    6668       10703 :         if (z) gel(resall, ct++) = z;
    6669       10703 :         if (gequal(idcon,id)) continue;
    6670        5516 :         ide = mkvec2(id, arch2);
    6671        5516 :         z = mfdihedralcommon(bnf, ide, znN, kroconreyN, N, D, NULL);
    6672        5516 :         if (z) gel(resall, ct++) = z;
    6673             :       }
    6674             :     }
    6675             :   }
    6676        5726 :   if (ct == 1) set_avma(av);
    6677             :   else
    6678             :   {
    6679        4816 :     setlg(resall, ct);
    6680        4816 :     vectrunc_append(v, gerepilecopy(av, shallowconcat1(resall)));
    6681             :   }
    6682             : }
    6683             : 
    6684             : static long
    6685       42042 : di_N(GEN a) { return gel(a,1)[1]; }
    6686             : static GEN
    6687          14 : mfdihedral(long N)
    6688             : {
    6689          14 :   GEN D = mydivisorsu(N), res = vectrunc_init(2*N);
    6690          14 :   long j, l = lg(D);
    6691         105 :   for (j = 2; j < l; j++)
    6692             :   { /* skip d = 1 */
    6693          91 :     long d = D[j];
    6694          91 :     if (d == 2) continue;
    6695          84 :     append_dihedral(res, -d, N,N);
    6696          84 :     if (d >= 5 && D[l-j] >= 3) append_dihedral(res, d, N,N); /* Nf >= 3 */
    6697             :   }
    6698          14 :   if (lg(res) > 1) res = shallowconcat1(res);
    6699          14 :   return res;
    6700             : }
    6701             : /* All primitive dihedral weight 1 forms of leven in [1, N], N > 1 */
    6702             : static GEN
    6703          14 : mfdihedralall(long N)
    6704             : {
    6705          14 :   GEN res = vectrunc_init(2*N), z;
    6706             :   long D, ct, i;
    6707             : 
    6708       13986 :   for (D = -3; D >= -N; D--) append_dihedral(res, D, 1,N);
    6709             :   /* Nf >= 3 (GTM 193, prop 3.3.18) */
    6710        4620 :   for (D = N / 3; D >= 5; D--) append_dihedral(res, D, 1,N);
    6711          14 :   ct = lg(res);
    6712          14 :   if (ct > 1)
    6713             :   { /* sort wrt N */
    6714          14 :     res = shallowconcat1(res);
    6715          14 :     res = vecpermute(res, indexvecsort(res, mkvecsmall(1)));
    6716          14 :     ct = lg(res);
    6717             :   }
    6718          14 :   z = const_vec(N, cgetg(1,t_VEC));
    6719        7658 :   for (i = 1; i < ct;)
    6720             :   { /* regroup result sharing the same N */
    6721        7644 :     long n = di_N(gel(res,i)), j = i+1, k;
    6722             :     GEN v;
    6723       34412 :     while (j < ct && di_N(gel(res,j)) == n) j++;
    6724        7644 :     gel(z, n) = v = cgetg(j-i+1, t_VEC);
    6725       42056 :     for (k = 1; i < j; k++,i++) gel(v,k) = gel(res,i);
    6726             :   }
    6727          14 :   return z;
    6728             : }
    6729             : 
    6730             : /* return [vF, index], where vecpermute(vF,index) generates dihedral forms
    6731             :  * for character CHI */
    6732             : static GEN
    6733       24885 : mfdihedralnew_i(long N, GEN CHI, GEN SP)
    6734             : {
    6735             :   GEN bnf, Tinit, Pm, vf, M, V, NK;
    6736             :   long Dold, d, ordw, i, SB, c, l, k0, k1, chino, chinoorig, lv;
    6737             : 
    6738       24885 :   lv = lg(SP); if (lv == 1) return NULL;
    6739       12054 :   CHI = mfcharinduce(CHI,N);
    6740       12054 :   ordw = mfcharorder(CHI);
    6741       12054 :   chinoorig = mfcharno(CHI);
    6742       12054 :   k0 = mfconreyminimize(CHI);
    6743       12054 :   chino = Fl_powu(chinoorig, k0, N);
    6744       12054 :   k1 = Fl_inv(k0 % ordw, ordw);
    6745       12054 :   V = cgetg(lv, t_VEC);
    6746       12054 :   d = 0;
    6747       38717 :   for (i = l = 1; i < lv; i++)
    6748             :   {
    6749       26663 :     GEN sp = gel(SP,i), T = gel(sp,1);
    6750       26663 :     if (T[3] != chino) continue;
    6751        3941 :     d += T[5];
    6752        3941 :     if (k1 != 1)
    6753             :     {
    6754          77 :       GEN t = leafcopy(T);
    6755          77 :       t[3] = chinoorig;
    6756          77 :       t[2] = (t[2]*k1) % ordw;
    6757          77 :       sp = mkvec4(t, gel(sp,2), gel(sp,3), gel(sp,4));
    6758             :     }
    6759        3941 :     gel(V, l++) = sp;
    6760             :   }
    6761       12054 :   setlg(V, l); /* dihedral forms of level N and character CHI */
    6762       12054 :   if (l == 1) return NULL;
    6763             : 
    6764        2492 :   SB = mfsturmNk(N,1) + 1;
    6765        2492 :   M = cgetg(d+1, t_MAT);
    6766        2492 :   vf = cgetg(d+1, t_VEC);
    6767        2492 :   NK = mkNK(N, 1, CHI);
    6768        2492 :   bnf = NULL; Dold = 0;
    6769        6433 :   for (i = c = 1; i < l; i++)
    6770             :   { /* T = [N, k0, conreyno, D, degrel] */
    6771        3941 :     GEN bnr, Vi = gel(V,i), T = gel(Vi,1), id = gel(Vi,2), w = gel(Vi,3);
    6772        3941 :     long jdeg, k0i = T[2], D = T[4], degrel = T[5];
    6773             : 
    6774        3941 :     if (D != Dold) { Dold = D; bnf = dihan_bnf(D); }
    6775        3941 :     bnr = dihan_bnr(bnf, id);
    6776       11599 :     for (jdeg = 0; jdeg < degrel; jdeg++,c++)
    6777             :     {
    6778        7658 :       GEN k0j = mkvecsmall2(k0i, jdeg), an = dihan(bnr, w, k0j, ordw, SB);
    6779        7658 :       settyp(an, t_COL); gel(M,c) = an;
    6780        7658 :       gel(vf,c) = tag3(t_MF_DIHEDRAL, NK, bnr, w, k0j);
    6781             :     }
    6782             :   }
    6783        2492 :   Tinit = gmael3(V,1,3,3); Pm = gel(Tinit,1);
    6784        2492 :   V = QabM_indexrank(M, degpol(Pm)==1? NULL: Pm, ordw);
    6785        2492 :   return mkvec2(vf,gel(V,2));
    6786             : }
    6787             : static long
    6788       16114 : mfdihedralnewdim(long N, GEN CHI, GEN SP)
    6789             : {
    6790       16114 :   pari_sp av = avma;
    6791       16114 :   GEN S = mfdihedralnew_i(N, CHI, SP);
    6792       16114 :   return gc_long(av, S? lg(gel(S,2))-1: 0);
    6793             : }
    6794             : static GEN
    6795        8771 : mfdihedralnew(long N, GEN CHI, GEN SP)
    6796             : {
    6797        8771 :   pari_sp av = avma;
    6798        8771 :   GEN S = mfdihedralnew_i(N, CHI, SP);
    6799        8771 :   if (!S) { set_avma(av); return cgetg(1, t_VEC); }
    6800         875 :   return vecpermute(gel(S,1), gel(S,2));
    6801             : }
    6802             : 
    6803             : static long
    6804        7091 : mfdihedralcuspdim(long N, GEN CHI, GEN vSP)
    6805             : {
    6806        7091 :   pari_sp av = avma;
    6807             :   GEN D, CHIP;
    6808             :   long F, i, lD, dim;
    6809             : 
    6810        7091 :   CHIP = mfchartoprimitive(CHI, &F);
    6811        7091 :   D = mydivisorsu(N/F); lD = lg(D);
    6812        7091 :   dim = mfdihedralnewdim(N, CHI, gel(vSP,N)); /* d = 1 */
    6813       16114 :   for (i = 2; i < lD; i++)
    6814             :   {
    6815        9023 :     long d = D[i], a = mfdihedralnewdim(N/d, CHIP, gel(vSP, N/d));
    6816        9023 :     if (a) dim += a * mynumdivu(d);
    6817             :   }
    6818        7091 :   return gc_long(av,dim);
    6819             : }
    6820             : 
    6821             : static GEN
    6822        7168 : mfbdall(GEN E, long N)
    6823             : {
    6824        7168 :   GEN v, D = mydivisorsu(N);
    6825        7168 :   long i, j, nD = lg(D) - 1, nE = lg(E) - 1;
    6826        7168 :   v = cgetg(nD*nE + 1, t_VEC);
    6827       10052 :   for (j = 1; j <= nE; j++)
    6828             :   {
    6829        2884 :     GEN Ej = gel(E, j);
    6830        8904 :     for (i = 0; i < nD; i++) gel(v, i*nE + j) = mfbd_i(Ej, D[i+1]);
    6831             :   }
    6832        7168 :   return v;
    6833             : }
    6834             : static GEN
    6835        3843 : mfdihedralcusp(long N, GEN CHI, GEN vSP)
    6836             : {
    6837        3843 :   pari_sp av = avma;
    6838             :   GEN D, CHIP, z;
    6839             :   long F, i, lD;
    6840             : 
    6841        3843 :   CHIP = mfchartoprimitive(CHI, &F);
    6842        3843 :   D = mydivisorsu(N/F); lD = lg(D);
    6843        3843 :   z = cgetg(lD, t_VEC);
    6844        3843 :   gel(z,1) = mfdihedralnew(N, CHI, gel(vSP,N));
    6845        8561 :   for (i = 2; i < lD; i++) /* skip 1 */
    6846             :   {
    6847        4718 :     GEN LF = mfdihedralnew(N / D[i], CHIP, gel(vSP, N / D[i]));
    6848        4718 :     gel(z,i) = mfbdall(LF, D[i]);
    6849             :   }
    6850        3843 :   return gerepilecopy(av, shallowconcat1(z));
    6851             : }
    6852             : 
    6853             : /* used to decide between ratlift and comatrix for ZM_inv; ratlift is better
    6854             :  * when N has many divisors */
    6855             : static int
    6856        2478 : abundant(ulong N) { return mynumdivu(N) >= 8; }
    6857             : 
    6858             : /* CHI an mfchar */
    6859             : static int
    6860         371 : cmp_ord(void *E, GEN a, GEN b)
    6861             : {
    6862         371 :   GEN chia = MF_get_CHI(a), chib = MF_get_CHI(b);
    6863         371 :   (void)E; return cmpii(gmfcharorder(chia), gmfcharorder(chib));
    6864             : }
    6865             : /* mfinit structure.
    6866             : -- mf[1] contains [N,k,CHI,space],
    6867             : -- mf[2] contains vector of closures of Eisenstein series, empty if not
    6868             :    full space.
    6869             : -- mf[3] contains vector of closures, so #mf[3] = dimension of cusp/new space.
    6870             : -- mf[4] contains the corresponding indices: either j for T(j)tf if newspace,
    6871             :    or [M,j,d] for B(d)T(j)tf_M if cuspspace or oldspace.
    6872             : -- mf[5] contains the matrix M of first coefficients of basis, never cleaned.
    6873             :  * NK is either [N,k] or [N,k,CHI].
    6874             :  * mfinit does not do the splitting, only the basis generation. */
    6875             : 
    6876             : /* Set flraw to 1 if do not need mf[5]: no mftobasis etc..., only the
    6877             :    expansions of the basis elements are needed. */
    6878             : 
    6879             : static GEN
    6880        4844 : mfinit_Nkchi(long N, long k, GEN CHI, long space, long flraw)
    6881             : {
    6882        4844 :   GEN M = NULL, mf = NULL, mf1 = mkvec4(utoi(N), stoi(k), CHI, utoi(space));
    6883        4844 :   long sb = mfsturmNk(N, k);
    6884             :   cachenew_t cache;
    6885        4844 :   if (k < 0 || badchar(N, k, CHI)) return mfEMPTY(mf1);
    6886        4809 :   if (k == 0) /*nothing*/;
    6887        4767 :   else if (k == 1)
    6888             :   {
    6889         364 :     switch (space)
    6890             :     {
    6891         336 :       case mf_NEW:
    6892             :       case mf_FULL:
    6893         336 :       case mf_CUSP: mf = mf1init(N, CHI, NULL, get_vDIH(N,NULL), space, flraw);
    6894         336 :                     break;
    6895          14 :       case mf_EISEN:break;
    6896           7 :       case mf_OLD: pari_err_IMPL("mfinit in weight 1 for old space");
    6897           7 :       default: pari_err_FLAG("mfinit");
    6898             :     }
    6899             :   }
    6900             :   else /* k >= 2 */
    6901             :   {
    6902        4403 :     long ord = mfcharorder(CHI);
    6903        4403 :     GEN z = NULL, P = (ord <= 2)? NULL: mfcharpol(CHI);
    6904        4403 :     switch(space)
    6905             :     {
    6906         105 :       case mf_EISEN:
    6907         105 :         break;
    6908        1204 :       case mf_NEW:
    6909        1204 :         mf = mfnewinit(N, k, CHI, &cache, 1);
    6910        1204 :         if (mf && !flraw) { M = MF_get_M(mf); z = MF_get_Mindex(mf); }
    6911        1204 :         break;
    6912        3087 :       case mf_OLD:
    6913             :       case mf_CUSP:
    6914             :       case mf_FULL:
    6915        3087 :         mf = mfinitcusp(N, k, CHI, &cache, space);
    6916        3087 :         if (mf && !flraw)
    6917             :         {
    6918        2184 :           GEN S = MF_get_S(mf);
    6919        2184 :           M = bhnmat_extend(M, sb+1, 1, S, &cache);
    6920        2184 :           if (space != mf_FULL) gel(mf,5) = mfcleanCHI(M, CHI, abundant(N));
    6921             :         }
    6922        3087 :         dbg_cachenew(&cache);
    6923        3087 :         break;
    6924           7 :       default: pari_err_FLAG("mfinit");
    6925             :     }
    6926        4396 :     if (z) gel(mf,5) = mfclean2(M, z, P, ord);
    6927             :   }
    6928        4788 :   if (!mf) mf = mfEMPTY(mf1);
    6929             :   else
    6930             :   {
    6931        3857 :     gel(mf,1) = mf1;
    6932        3857 :     if (flraw) gel(mf,5) = zerovec(3);
    6933             :   }
    6934        4788 :   if (!space_is_cusp(space))
    6935             :   {
    6936         763 :     GEN E = mfeisensteinbasis(N, k, CHI);
    6937         763 :     gel(mf,2) = E;
    6938         763 :     if (!flraw)
    6939             :     {
    6940         483 :       if (M)
    6941         189 :         M = shallowconcat(mfvectomat(E, sb+1, 1), M);
    6942             :       else
    6943         294 :         M = mfcoefs_mf(mf, sb+1, 1);
    6944         483 :       gel(mf,5) = mfcleanCHI(M, CHI, abundant(N));
    6945             :     }
    6946             :   }
    6947        4788 :   return mf;
    6948             : }
    6949             : 
    6950             : /* mfinit for k = nk/dk */
    6951             : static GEN
    6952        2604 : mfinit_Nndkchi(long N, long nk, long dk, GEN CHI, long space, long flraw)
    6953         245 : { return (dk == 2)? mf2init_Nkchi(N, nk >> 1, CHI, space, flraw)
    6954        2849 :                   : mfinit_Nkchi(N, nk, CHI, space, flraw); }
    6955             : static GEN
    6956        3283 : mfinit_i(GEN NK, long space)
    6957             : {
    6958             :   GEN CHI, mf;
    6959             :   long N, k, dk, joker;
    6960        3283 :   if (checkmf_i(NK))
    6961             :   {
    6962         147 :     N = mf_get_N(NK);
    6963         147 :     Qtoss(mf_get_gk(NK), &k, &dk);
    6964         147 :     CHI = mf_get_CHI(NK);
    6965             :   }
    6966        3136 :   else if ((mf = checkMF_i(NK)))
    6967             :   {
    6968          21 :     long s = MF_get_space(mf);
    6969          21 :     if (s == space) return mf;
    6970          21 :     Qtoss(MF_get_gk(mf), &k, &dk);
    6971          21 :     if (dk == 1 && k > 1 && space == mf_NEW && (s == mf_CUSP || s == mf_FULL))
    6972          21 :       return mfinittonew(mf);
    6973           0 :     N = MF_get_N(mf);
    6974           0 :     CHI = MF_get_CHI(mf);
    6975             :   }
    6976             :   else
    6977        3115 :     checkNK2(NK, &N, &k, &dk, &CHI, 1);
    6978        3241 :   joker = !CHI || typ(CHI) == t_COL;
    6979        3241 :   if (joker)
    6980             :   {
    6981        1162 :     GEN mf, vCHI = CHI;
    6982             :     long i, j, l;
    6983        1162 :     if (CHI && lg(CHI) == 1) return cgetg(1,t_VEC);
    6984        1155 :     if (k < 0) return mfEMPTYall(N, sstoQ(k,dk), CHI, space);
    6985        1141 :     if (k == 1 && dk == 1 && space != mf_EISEN)
    6986         504 :     {
    6987             :       GEN TMP, vSP, gN, gs;
    6988             :       pari_timer tt;
    6989        1106 :       if (space != mf_CUSP && space != mf_NEW)
    6990           0 :         pari_err_IMPL("mfinit([N,1,wildcard], space != cusp or new space)");
    6991        1106 :       if (wt1empty(N)) return mfEMPTYall(N, gen_1, CHI, space);
    6992         504 :       vCHI = mf1chars(N,vCHI);
    6993         504 :       l = lg(vCHI); mf = cgetg(l, t_VEC); if (l == 1) return mf;
    6994         504 :       TMP = mf1_pre(N); vSP = get_vDIH(N, NULL);
    6995         504 :       gN = utoipos(N); gs = utoi(space);
    6996         504 :       if (DEBUGLEVEL) timer_start(&tt);
    6997        4123 :       for (i = j = 1; i < l; i++)
    6998             :       {
    6999        3619 :         pari_sp av = avma;
    7000        3619 :         GEN c = gel(vCHI,i), z = mf1init(N, c, TMP, vSP, space, 0);
    7001        3619 :         if (z) z = gerepilecopy(av, z);
    7002             :         else
    7003             :         {
    7004        2905 :           set_avma(av);
    7005        2905 :           if (CHI) z = mfEMPTY(mkvec4(gN,gen_1,c,gs));
    7006             :         }
    7007        3619 :         if (z) gel(mf, j++) = z;
    7008        3619 :         if (DEBUGLEVEL)
    7009           0 :           timer_printf(&tt, "mf1basis: character %ld / %ld (order = %ld)",
    7010             :                        i, l-1, mfcharorder(c));
    7011             :       }
    7012             :     }
    7013             :     else
    7014             :     {
    7015          35 :       vCHI = mfchars(N,k,dk,vCHI);
    7016          35 :       l = lg(vCHI); mf = cgetg(l, t_VEC);
    7017         119 :       for (i = j = 1; i < l; i++)
    7018             :       {
    7019          84 :         pari_sp av = avma;
    7020          84 :         GEN v = mfinit_Nndkchi(N, k, dk, gel(vCHI,i), space, 0);
    7021          84 :         if (MF_get_dim(v) || CHI) gel(mf, j++) = v; else set_avma(av);
    7022             :       }
    7023             :     }
    7024         539 :     setlg(mf,j);
    7025         539 :     if (!CHI) gen_sort_inplace(mf, NULL, &cmp_ord, NULL);
    7026         539 :     return mf;
    7027             :   }
    7028        2079 :   return mfinit_Nndkchi(N, k, dk, CHI, space, 0);
    7029             : }
    7030             : GEN
    7031        2324 : mfinit(GEN NK, long space)
    7032             : {
    7033        2324 :   pari_sp av = avma;
    7034        2324 :   return gerepilecopy(av, mfinit_i(NK, space));
    7035             : }
    7036             : 
    7037             : /* UTILITY FUNCTIONS */
    7038             : static void
    7039         364 : cusp_canon(GEN cusp, long N, long *pA, long *pC)
    7040             : {
    7041         364 :   pari_sp av = avma;
    7042             :   long A, C, tc, cg;
    7043         364 :   if (N <= 0) pari_err_DOMAIN("mfcuspwidth","N","<=",gen_0,stoi(N));
    7044         357 :   if (!cusp || (tc = typ(cusp)) == t_INFINITY) { *pA = 1; *pC = N; return; }
    7045         350 :   if (tc != t_INT && tc != t_FRAC) pari_err_TYPE("checkcusp", cusp);
    7046         350 :   Qtoss(cusp, &A,&C);
    7047         350 :   if (N % C)
    7048             :   {
    7049             :     ulong uC;
    7050          14 :     long u = Fl_invgen((C-1)%N + 1, N, &uC);
    7051          14 :     A = Fl_mul(A, u, N);
    7052          14 :     C = (long)uC;
    7053             :   }
    7054         350 :   cg = ugcd(C, N/C);
    7055         420 :   while (ugcd(A, N) > 1) A += cg;
    7056         350 :   *pA = A % N; *pC = C; set_avma(av);
    7057             : }
    7058             : static long
    7059         903 : mfcuspcanon_width(long N, long C)
    7060         903 : { return (!C || C == N)? 1 : N / ugcd(N, Fl_sqr(umodsu(C,N),N)); }
    7061             : /* v = [a,c] a ZC, width of cusp (a:c) */
    7062             : static long
    7063        8750 : mfZC_width(long N, GEN v)
    7064             : {
    7065        8750 :   ulong C = umodiu(gel(v,2), N);
    7066        8750 :   return (C == 0)? 1: N / ugcd(N, Fl_sqr(C,N));
    7067             : }
    7068             : long
    7069         161 : mfcuspwidth(GEN gN, GEN cusp)
    7070             : {
    7071         161 :   long N = 0, A, C;
    7072             :   GEN mf;
    7073         161 :   if (typ(gN) == t_INT) N = itos(gN);
    7074          42 :   else if ((mf = checkMF_i(gN))) N = MF_get_N(mf);
    7075           0 :   else pari_err_TYPE("mfcuspwidth", gN);
    7076         161 :   cusp_canon(cusp, N, &A, &C);
    7077         154 :   return mfcuspcanon_width(N, C);
    7078             : }
    7079             : 
    7080             : /* Q a t_INT */
    7081             : static GEN
    7082          14 : findq(GEN al, GEN Q)
    7083             : {
    7084             :   long n;
    7085          14 :   if (typ(al) == t_FRAC && cmpii(gel(al,2), Q) <= 0)
    7086           0 :     return mkvec(mkvec2(gel(al,1), gel(al,2)));
    7087          14 :   n = 1 + (long)ceil(2.0781*gtodouble(glog(Q, LOWDEFAULTPREC)));
    7088          14 :   return contfracpnqn(gboundcf(al,n), n);
    7089             : }
    7090             : static GEN
    7091          91 : findqga(long N, GEN z)
    7092             : {
    7093          91 :   GEN Q, LDC, CK = NULL, DK = NULL, ma, x, y = imag_i(z);
    7094             :   long j, l;
    7095          91 :   if (gcmpgs(gmulsg(2*N, y), 1) >= 0) return NULL;
    7096          14 :   x = real_i(z);
    7097          14 :   Q = ground(ginv(gsqrt(gmulsg(N, y), LOWDEFAULTPREC)));
    7098          14 :   LDC = findq(gmulsg(-N,x), Q);
    7099          14 :   ma = gen_1; l = lg(LDC);
    7100          35 :   for (j = 1; j < l; j++)
    7101             :   {
    7102          21 :     GEN D, DC = gel(LDC,j), C1 = gel(DC,2);
    7103          21 :     if (cmpii(C1,Q) > 0) break;
    7104          21 :     D = gel(DC,1);
    7105          21 :     if (ugcdiu(D,N) == 1)
    7106             :     {
    7107           7 :       GEN C = mului(N, C1), den;
    7108           7 :       den = gadd(gsqr(gmul(C,y)), gsqr(gadd(D, gmul(C,x))));
    7109           7 :       if (gcmp(den, ma) < 0) { ma = den; CK = C; DK = D; }
    7110             :     }
    7111             :   }
    7112          14 :   return DK? mkvec2(CK, DK): NULL;
    7113             : }
    7114             : 
    7115             : static long
    7116         154 : valNC2(GEN P, GEN E, long e)
    7117             : {
    7118         154 :   long i, d = 1, l = lg(P);
    7119         476 :   for (i = 1; i < l; i++)
    7120             :   {
    7121         322 :     long v = u_lval(e, P[i]) << 1;
    7122         322 :     if (v == E[i] + 1) v--;
    7123         322 :     d *= upowuu(P[i], v);
    7124             :   }
    7125         154 :   return d;
    7126             : }
    7127             : 
    7128             : static GEN
    7129          42 : findqganew(long N, GEN z)
    7130             : {
    7131          42 :   GEN MI, DI, x = real_i(z), y = imag_i(z), Ck = gen_0, Dk = gen_1, fa, P, E;
    7132             :   long i;
    7133          42 :   MI = ginv(utoi(N));
    7134          42 :   DI = mydivisorsu(mysqrtu(N));
    7135          42 :   fa = myfactoru(N); P = gel(fa,1); E = gel(fa,2);
    7136         196 :   for (i = 1; i < lg(DI); i++)
    7137             :   {
    7138         154 :     long e = DI[i], g;
    7139             :     GEN U, C, D, m;
    7140         154 :     (void)cxredsl2(gmulsg(e, z), &U);
    7141         154 :     C = gcoeff(U,2,1); if (!signe(C)) continue;
    7142         154 :     D = gcoeff(U,2,2);
    7143         154 :     g = ugcdiu(D,e);
    7144         154 :     if (g > 1) { C = muliu(C,e/g); D = diviuexact(D,g); } else C = muliu(C,e);
    7145         154 :     m = gadd(gsqr(gadd(gmul(C, x), D)), gsqr(gmul(C, y)));
    7146         154 :     m = gdivgs(m, valNC2(P, E, e));
    7147         154 :     if (gcmp(m, MI) < 0) { MI = m; Ck = C; Dk = D; }
    7148             :   }
    7149          42 :   return signe(Ck)? mkvec2(Ck, Dk): NULL;
    7150             : }
    7151             : 
    7152             : /* Return z' and U = [a,b;c,d] \in SL_2(Z), z' = U*z,
    7153             :  * Im(z')/width(U.oo) > sqrt(3)/(2N). Set *pczd = c*z+d */
    7154             : static GEN
    7155         168 : cxredga0N(long N, GEN z, GEN *pU, GEN *pczd, long flag)
    7156             : {
    7157         168 :   GEN v = NULL, A, B, C, D;
    7158             :   long e;
    7159         168 :   if (N == 1) return cxredsl2_i(z, pU, pczd);
    7160         133 :   e = gexpo(gel(z,2));
    7161         133 :   if (e < 0) z = gprec_wensure(z, precision(z) + nbits2extraprec(-e));
    7162         133 :   v = flag? findqganew(N,z): findqga(N,z);
    7163         133 :   if (!v) { *pU = matid(2); *pczd = gen_1; return z; }
    7164          49 :   C = gel(v,1);
    7165          49 :   D = gel(v,2);
    7166          49 :   if (!is_pm1(bezout(C,D, &B,&A))) pari_err_BUG("cxredga0N [gcd > 1]");
    7167          49 :   B = negi(B);
    7168          49 :   *pU = mkmat2(mkcol2(A,C), mkcol2(B,D));
    7169          49 :   *pczd = gadd(gmul(C,z), D);
    7170          49 :   return gdiv(gadd(gmul(A,z), B), *pczd);
    7171             : }
    7172             : 
    7173             : static GEN
    7174         154 : lfunthetaall(GEN b, GEN vL, GEN t, long bitprec)
    7175             : {
    7176         154 :   long i, l = lg(vL);
    7177         154 :   GEN v = cgetg(l, t_VEC);
    7178         336 :   for (i = 1; i < l; i++)
    7179             :   {
    7180         182 :     GEN T, L = gel(vL,i), a0 = gel(L,1), ldata = gel(L,2);
    7181         182 :     GEN van = gel(ldata_get_an(ldata),2);
    7182         182 :     if (lg(van) == 1)
    7183             :     {
    7184           0 :       T = gmul(b, a0);
    7185           0 :       if (isexactzero(T)) { GEN z = real_0_bit(-bitprec); T = mkcomplex(z,z); }
    7186             :     }
    7187             :     else
    7188             :     {
    7189         182 :       T = gmul2n(lfuntheta(ldata, t, 0, bitprec), -1);
    7190         182 :       T = gmul(b, gadd(a0, T));
    7191             :     }
    7192         182 :     gel(v,i) = T;
    7193             :   }
    7194         154 :   return l == 2? gel(v,1): v;
    7195             : }
    7196             : 
    7197             : /* P in ZX, irreducible */
    7198             : static GEN
    7199         182 : ZX_roots(GEN P, long prec)
    7200             : {
    7201         182 :   long d = degpol(P);
    7202         182 :   if (d == 1) return mkvec(gen_0);
    7203         182 :   if (d == 2 && isint1(gel(P,2)) && isintzero(gel(P,3)) && isint1(gel(P,4)))
    7204           7 :     return mkvec2(powIs(3), gen_I()); /* order as polroots */
    7205         294 :   return (ZX_sturm_irred(P) == d)? ZX_realroots_irred(P, prec)
    7206         294 :                                  : QX_complex_roots(P, prec);
    7207             : }
    7208             : /* initializations for RgX_RgV_eval / RgC_embed */
    7209             : static GEN
    7210         217 : rootspowers(GEN v)
    7211             : {
    7212         217 :   long i, l = lg(v);
    7213         217 :   GEN w = cgetg(l, t_VEC);
    7214         868 :   for (i = 1; i < l; i++) gel(w,i) = gpowers(gel(v,i), l-2);
    7215         217 :   return w;
    7216             : }
    7217             : /* mf embeddings attached to Q(chi)/(T), chi attached to cyclotomic P */
    7218             : static GEN
    7219         889 : getembed(GEN P, GEN T, GEN zcyclo, long prec)
    7220             : {
    7221             :   long i, l;
    7222             :   GEN v;
    7223         889 :   if (degpol(P) == 1) P = NULL; /* mfcharpol for quadratic char */
    7224         889 :   if (degpol(T) == 1) T = NULL; /* dim 1 orbit */
    7225         889 :   if (T && P)
    7226          35 :   { /* K(y) / (T(y)), K = Q(t)/(P) cyclotomic */
    7227          35 :     GEN vr = RgX_is_ZX(T)? ZX_roots(T,prec): roots(RgX_embed1(T,zcyclo), prec);
    7228          35 :     v = rootspowers(vr); l = lg(v);
    7229         105 :     for (i = 1; i < l; i++) gel(v,i) = mkcol3(P,zcyclo,gel(v,i));
    7230             :   }
    7231         854 :   else if (T)
    7232             :   { /* Q(y) / (T(y)), T noncyclotomic */
    7233         182 :     GEN vr = ZX_roots(T, prec);
    7234         182 :     v = rootspowers(vr); l = lg(v);
    7235         763 :     for (i = 1; i < l; i++) gel(v,i) = mkcol2(T, gel(v,i));
    7236             :   }
    7237             :   else /* cyclotomic or rational */
    7238         672 :     v = mkvec(P? mkvec2(P, zcyclo): cgetg(1,t_VEC));
    7239         889 :   return v;
    7240             : }
    7241             : static GEN
    7242         742 : grootsof1_CHI(GEN CHI, long prec)
    7243         742 : { return grootsof1(mfcharorder(CHI), prec); }
    7244             : /* return the [Q(F):Q(chi)] embeddings of F */
    7245             : static GEN
    7246         581 : mfgetembed(GEN F, long prec)
    7247             : {
    7248         581 :   GEN T = mf_get_field(F), CHI = mf_get_CHI(F), P = mfcharpol(CHI);
    7249         581 :   return getembed(P, T, grootsof1_CHI(CHI, prec), prec);
    7250             : }
    7251             : static GEN
    7252           7 : mfchiembed(GEN mf, long prec)
    7253             : {
    7254           7 :   GEN CHI = MF_get_CHI(mf), P = mfcharpol(CHI);
    7255           7 :   return getembed(P, pol_x(0), grootsof1_CHI(CHI, prec), prec);
    7256             : }
    7257             : /* mfgetembed for the successive eigenforms in MF_get_newforms */
    7258             : static GEN
    7259         154 : mfeigenembed(GEN mf, long prec)
    7260             : {
    7261         154 :   GEN vP = MF_get_fields(mf), vF = MF_get_newforms(mf);
    7262         154 :   GEN zcyclo, vE, CHI = MF_get_CHI(mf), P = mfcharpol(CHI);
    7263         154 :   long i, l = lg(vP);
    7264         154 :   vF = Q_remove_denom(liftpol_shallow(vF), NULL);
    7265         154 :   prec += nbits2extraprec(gexpo(vF));
    7266         154 :   zcyclo = grootsof1_CHI(CHI, prec);
    7267         154 :   vE = cgetg(l, t_VEC);
    7268         455 :   for (i = 1; i < l; i++) gel(vE,i) = getembed(P, gel(vP,i), zcyclo, prec);
    7269         154 :   return vE;
    7270             : }
    7271             : 
    7272             : static int
    7273          28 : checkPv(GEN P, GEN v)
    7274          28 : { return typ(P) == t_POL && is_vec_t(typ(v)) && lg(v)-1 >= degpol(P); }
    7275             : static int
    7276          28 : checkemb_i(GEN E)
    7277             : {
    7278          28 :   long t = typ(E), l = lg(E);
    7279          28 :   if (t == t_VEC) return l == 1 || (l == 3 && checkPv(gel(E,1), gel(E,2)));
    7280          21 :   if (t != t_COL) return 0;
    7281          21 :   if (l == 3) return checkPv(gel(E,1), gel(E,2));
    7282          21 :   return l == 4 && is_vec_t(typ(gel(E,2))) && checkPv(gel(E,1), gel(E,3));
    7283             : }
    7284             : static GEN
    7285          28 : anyembed(GEN v, GEN E)
    7286             : {
    7287          28 :   switch(typ(v))
    7288             :   {
    7289          21 :     case t_VEC: case t_COL: return mfvecembed(E, v);
    7290           7 :     case t_MAT: return mfmatembed(E, v);
    7291             :   }
    7292           0 :   return mfembed(E, v);
    7293             : }
    7294             : GEN
    7295          49 : mfembed0(GEN E, GEN v, long prec)
    7296             : {
    7297          49 :   pari_sp av = avma;
    7298          49 :   GEN mf, vE = NULL;
    7299          49 :   if (checkmf_i(E)) vE = mfgetembed(E, prec);
    7300          35 :   else if ((mf = checkMF_i(E))) vE = mfchiembed(mf, prec);
    7301          49 :   if (vE)
    7302             :   {
    7303          21 :     long i, l = lg(vE);
    7304             :     GEN w;
    7305          21 :     if (!v) return gerepilecopy(av, l == 2? gel(vE,1): vE);
    7306           0 :     w = cgetg(l, t_VEC);
    7307           0 :     for (i = 1; i < l; i++) gel(w,i) = anyembed(v, gel(vE,i));
    7308           0 :     return gerepilecopy(av, l == 2? gel(w,1): w);
    7309             :   }
    7310          28 :   if (!checkemb_i(E) || !v) pari_err_TYPE("mfembed", E);
    7311          28 :   return gerepilecopy(av, anyembed(v,E));
    7312             : }
    7313             : 
    7314             : /* dummy lfun create for theta evaluation */
    7315             : static GEN
    7316         924 : mfthetaancreate(GEN van, GEN N, GEN k)
    7317             : {
    7318         924 :   GEN L = zerovec(6);
    7319         924 :   gel(L,1) = lfuntag(t_LFUN_GENERIC, van);
    7320         924 :   gel(L,3) = mkvec2(gen_0, gen_1);
    7321         924 :   gel(L,4) = k;
    7322         924 :   gel(L,5) = N; return L;
    7323             : }
    7324             : /* destroy van and prepare to evaluate theta(sigma(van)), for all sigma in
    7325             :  * embeddings vector vE */
    7326             : static GEN
    7327         329 : van_embedall(GEN van, GEN vE, GEN gN, GEN gk)
    7328             : {
    7329         329 :   GEN a0 = gel(van,1), vL;
    7330         329 :   long i, lE = lg(vE), l = lg(van);
    7331         329 :   van++; van[0] = evaltyp(t_VEC) | evallg(l-1); /* remove a0 */
    7332         329 :   vL = cgetg(lE, t_VEC);
    7333         889 :   for (i = 1; i < lE; i++)
    7334             :   {
    7335         560 :     GEN E = gel(vE,i), v = mfvecembed(E, van);
    7336         560 :     gel(vL,i) = mkvec2(mfembed(E,a0), mfthetaancreate(v, gN, gk));
    7337             :   }
    7338         329 :   return vL;
    7339             : }
    7340             : 
    7341             : static int
    7342        1036 : cusp_AC(GEN cusp, long *A, long *C)
    7343             : {
    7344        1036 :   switch(typ(cusp))
    7345             :   {
    7346         105 :     case t_INFINITY: *A = 1; *C = 0; break;
    7347         273 :     case t_INT:  *A = itos(cusp); *C = 1; break;
    7348         441 :     case t_FRAC: *A = itos(gel(cusp, 1)); *C = itos(gel(cusp, 2)); break;
    7349         217 :     case t_REAL: case t_COMPLEX:
    7350         217 :       *A = 0; *C = 0;
    7351         217 :       if (gsigne(imag_i(cusp)) <= 0)
    7352           7 :         pari_err_DOMAIN("mfeval","imag(tau)","<=",gen_0,cusp);
    7353         210 :       return 0;
    7354           0 :     default: pari_err_TYPE("cusp_AC", cusp);
    7355             :   }
    7356         819 :   return 1;
    7357             : }
    7358             : static GEN
    7359         518 : cusp2mat(long A, long C)
    7360             : { long B, D;
    7361         518 :   cbezout(A, C, &D, &B);
    7362         518 :   return mkmat22s(A, -B, C, D);
    7363             : }
    7364             : static GEN
    7365          21 : mkS(void) { return mkmat22s(0,-1,1,0); }
    7366             : 
    7367             : /* if t is a cusp, return F(t), else NULL */
    7368             : static GEN
    7369         350 : evalcusp(GEN mf, GEN F, GEN t, long prec)
    7370             : {
    7371             :   long A, C;
    7372             :   GEN R;
    7373         350 :   if (!cusp_AC(t, &A,&C)) return NULL;
    7374         189 :   if (C % mf_get_N(F) == 0) return gel(mfcoefs_i(F, 0, 1), 1);
    7375         175 :   R = mfgaexpansion(mf, F, cusp2mat(A,C), 0, prec);
    7376         175 :   return gequal0(gel(R,1))? gmael(R,3,1): gen_0;
    7377             : }
    7378             : /* Evaluate an mf closure numerically, i.e., in the usual sense, either for a
    7379             :  * single tau or a vector of tau; for each, return a vector of results
    7380             :  * corresponding to all complex embeddings of F. If flag is nonzero, allow
    7381             :  * replacing F by F | gamma to increase imag(gamma^(-1).tau) [ expensive if
    7382             :  * MF_EISENSPACE not present ] */
    7383             : static GEN
    7384         161 : mfeval_i(GEN mf, GEN F, GEN vtau, long flag, long bitprec)
    7385             : {
    7386             :   GEN L0, vL, vb, sqN, vczd, vTAU, vs, van, vE;
    7387         161 :   long N = MF_get_N(mf), N0, ta, lv, i, prec = nbits2prec(bitprec);
    7388         161 :   GEN gN = utoipos(N), gk = mf_get_gk(F), gk1 = gsubgs(gk,1), vgk;
    7389         161 :   long flscal = 0;
    7390             : 
    7391             :   /* gen_0 is ignored, second component assumes Ramanujan-Petersson in
    7392             :    * 1/2-integer weight */
    7393         161 :   vgk = mkvec2(gen_0, mfiscuspidal(mf,F)? gmul2n(gk1,-1): gk1);
    7394         161 :   ta = typ(vtau);
    7395         161 :   if (!is_vec_t(ta)) { flscal = 1; vtau = mkvec(vtau); ta = t_VEC; }
    7396         161 :   lv = lg(vtau);
    7397         161 :   sqN = sqrtr_abs(utor(N, prec));
    7398         161 :   vs = const_vec(lv-1, NULL);
    7399         161 :   vb = const_vec(lv-1, NULL);
    7400         161 :   vL = cgetg(lv, t_VEC);
    7401         161 :   vTAU = cgetg(lv, t_VEC);
    7402         161 :   vczd = cgetg(lv, t_VEC);
    7403         161 :   L0 = mfthetaancreate(NULL, gN, vgk); /* only for thetacost */
    7404         161 :   vE = mfgetembed(F, prec);
    7405         161 :   N0 = 0;
    7406         343 :   for (i = 1; i < lv; i++)
    7407             :   {
    7408         189 :     GEN z = gel(vtau,i), tau, U;
    7409             :     long w, n;
    7410             : 
    7411         189 :     gel(vs,i) = evalcusp(mf, F, z, prec);
    7412         182 :     if (gel(vs,i)) continue;
    7413         154 :     tau = cxredga0N(N, z, &U, &gel(vczd,i), flag);
    7414         154 :     if (!flag) w = 0; else { w = mfZC_width(N, gel(U,1)); tau = gdivgs(tau,w); }
    7415         154 :     gel(vTAU,i) = mulcxmI(gmul(tau, sqN));
    7416         154 :     n = lfunthetacost(L0, real_i(gel(vTAU,i)), 0, bitprec);
    7417         154 :     if (N0 < n) N0 = n;
    7418         154 :     if (flag)
    7419             :     {
    7420          42 :       GEN A, al, v = mfslashexpansion(mf, F, ZM_inv(U,NULL), n, 0, &A, prec);
    7421          42 :       gel(vL,i) = van_embedall(v, vE, gN, vgk);
    7422          42 :       al = gel(A,1);
    7423          42 :       if (!gequal0(al))
    7424           7 :         gel(vb,i) = gexp(gmul(gmul(gmulsg(w,al),PiI2(prec)), tau), prec);
    7425             :     }
    7426             :   }
    7427         154 :   if (!flag)
    7428             :   {
    7429         112 :     van = mfcoefs_i(F, N0, 1);
    7430         112 :     vL = const_vec(lv-1, van_embedall(van, vE, gN, vgk));
    7431             :   }
    7432         336 :   for (i = 1; i < lv; i++)
    7433             :   {
    7434             :     GEN T;
    7435         182 :     if (gel(vs,i)) continue;
    7436         154 :     T = gpow(gel(vczd,i), gneg(gk), prec);
    7437         154 :     if (flag && gel(vb,i)) T = gmul(T, gel(vb,i));
    7438         154 :     gel(vs,i) = lfunthetaall(T, gel(vL,i), gel(vTAU,i), bitprec);
    7439             :   }
    7440         154 :   return flscal? gel(vs,1): vs;
    7441             : }
    7442             : 
    7443             : static long
    7444        1141 : mfistrivial(GEN F)
    7445             : {
    7446        1141 :   switch(mf_get_type(F))
    7447             :   {
    7448           7 :     case t_MF_CONST: return lg(gel(F,2)) == 1;
    7449         259 :     case t_MF_LINEAR: case t_MF_LINEAR_BHN: return gequal0(gel(F,3));
    7450         875 :     default: return 0;
    7451             :   }
    7452             : }
    7453             : 
    7454             : static long
    7455         959 : mf_same_k(GEN mf, GEN f) { return gequal(MF_get_gk(mf), mf_get_gk(f)); }
    7456             : static long
    7457         917 : mf_same_CHI(GEN mf, GEN f)
    7458             : {
    7459         917 :   GEN F1, F2, chi1, chi2, CHI1 = MF_get_CHI(mf), CHI2 = mf_get_CHI(f);
    7460             :   /* are the primitive chars attached to CHI1 and CHI2 equal ? */
    7461         917 :   F1 = znconreyconductor(gel(CHI1,1), gel(CHI1,2), &chi1);
    7462         917 :   if (typ(F1) == t_VEC) F1 = gel(F1,1);
    7463         917 :   F2 = znconreyconductor(gel(CHI2,1), gel(CHI2,2), &chi2);
    7464         917 :   if (typ(F2) == t_VEC) F2 = gel(F2,1);
    7465         917 :   return equalii(F1,F2) && ZV_equal(chi1,chi2);
    7466             : }
    7467             : /* check k and CHI rigorously, but not coefficients nor N */
    7468             : static long
    7469         238 : mfisinspace_i(GEN mf, GEN F)
    7470             : {
    7471         238 :   return mfistrivial(F) || (mf_same_k(mf,F) && mf_same_CHI(mf,F));
    7472             : }
    7473             : static void
    7474           7 : err_space(GEN F)
    7475           7 : { pari_err_DOMAIN("mftobasis", "form", "does not belong to",
    7476           0 :                   strtoGENstr("space"), F); }
    7477             : 
    7478             : static long
    7479         147 : mfcheapeisen(GEN mf)
    7480             : {
    7481         147 :   long k, L, N = MF_get_N(mf);
    7482             :   GEN P;
    7483         147 :   if (N <= 70) return 1;
    7484          84 :   k = itos(gceil(MF_get_gk(mf)));
    7485          84 :   if (odd(k)) k--;
    7486          84 :   switch (k)
    7487             :   {
    7488           0 :     case 2:  L = 190; break;
    7489          14 :     case 4:  L = 162; break;
    7490          70 :     case 6:
    7491          70 :     case 8:  L = 88; break;
    7492           0 :     case 10: L = 78; break;
    7493           0 :     default: L = 66; break;
    7494             :   }
    7495          84 :   P = gel(myfactoru(N), 1);
    7496          84 :   return P[lg(P)-1] <= L;
    7497             : }
    7498             : 
    7499             : static GEN
    7500         182 : myimag_i(GEN tau)
    7501             : {
    7502         182 :   long tc = typ(tau);
    7503         182 :   if (tc == t_INFINITY || tc == t_INT || tc == t_FRAC)
    7504          28 :     return gen_1;
    7505         154 :   if (tc == t_VEC)
    7506             :   {
    7507             :     long ltau, i;
    7508           7 :     GEN z = cgetg_copy(tau, &ltau);
    7509          42 :     for (i=1; i<ltau; i++) gel(z,i) = myimag_i(gel(tau,i));
    7510           7 :     return z;
    7511             :   }
    7512         147 :   return imag_i(tau);
    7513             : }
    7514             : 
    7515             : static GEN
    7516         147 : mintau(GEN vtau)
    7517             : {
    7518         147 :   if (!is_vec_t(typ(vtau))) return myimag_i(vtau);
    7519           7 :   return (lg(vtau) == 1)? gen_1: vecmin(myimag_i(vtau));
    7520             : }
    7521             : 
    7522             : /* initialization for mfgaexpansion: what does not depend on cusp */
    7523             : static GEN
    7524         945 : mf_eisendec(GEN mf, GEN F, long prec)
    7525             : {
    7526         945 :   GEN B = liftpol_shallow(mfeisensteindec(mf, F)), v = variables_vecsmall(B);
    7527         945 :   GEN Mvecj = obj_check(mf, MF_EISENSPACE);
    7528         945 :   long l = lg(v), i, ord;
    7529         945 :   if (lg(Mvecj) < 5) Mvecj = gel(Mvecj,1);
    7530         945 :   ord = itou(gel(Mvecj,4));
    7531        1001 :   for (i = 1; i < l; i++)
    7532         714 :     if (v[i] != 1)
    7533             :     {
    7534             :       GEN d;
    7535             :       long e;
    7536         658 :       B = Q_remove_denom(B, &d);
    7537         658 :       e = gexpo(B);
    7538         658 :       if (e > 0) prec += nbits2prec(e);
    7539         658 :       B = gsubst(B, v[i], rootsof1u_cx(ord, prec));
    7540         658 :       if (d) B = gdiv(B, d);
    7541         658 :       break;
    7542             :     }
    7543         945 :   return B;
    7544             : }
    7545             : 
    7546             : GEN
    7547         161 : mfeval(GEN mf0, GEN F, GEN vtau, long bitprec)
    7548             : {
    7549         161 :   pari_sp av = avma;
    7550         161 :   long flnew = 1;
    7551         161 :   GEN mf = checkMF_i(mf0);
    7552         161 :   if (!mf) pari_err_TYPE("mfeval", mf0);
    7553         161 :   if (!checkmf_i(F)) pari_err_TYPE("mfeval", F);
    7554         161 :   if (!mfisinspace_i(mf, F)) err_space(F);
    7555         161 :   if (!obj_check(mf, MF_EISENSPACE)) flnew = mfcheapeisen(mf);
    7556         161 :   if (flnew && gcmpgs(gmulsg(2*MF_get_N(mf), mintau(vtau)), 1) >= 0) flnew = 0;
    7557         161 :   return gerepilecopy(av, mfeval_i(mf, F, vtau, flnew, bitprec));
    7558             : }
    7559             : 
    7560             : static long
    7561         189 : val(GEN v, long bit)
    7562             : {
    7563         189 :   long c, l = lg(v);
    7564         392 :   for (c = 1; c < l; c++)
    7565         378 :     if (gexpo(gel(v,c)) > -bit) return c-1;
    7566          14 :   return -1;
    7567             : }
    7568             : GEN
    7569         203 : mfcuspval(GEN mf, GEN F, GEN cusp, long bitprec)
    7570             : {
    7571         203 :   pari_sp av = avma;
    7572         203 :   long lvE, w, N, sb, n, A, C, prec = nbits2prec(bitprec);
    7573             :   GEN ga, gk, vE;
    7574         203 :   mf = checkMF(mf);
    7575         203 :   if (!checkmf_i(F)) pari_err_TYPE("mfcuspval",F);
    7576         203 :   N = MF_get_N(mf);
    7577         203 :   cusp_canon(cusp, N, &A, &C);
    7578         203 :   gk = mf_get_gk(F);
    7579         203 :   if (typ(gk) != t_INT)
    7580             :   {
    7581          42 :     GEN FT = mfmultheta(F), mf2 = obj_checkbuild(mf, MF_MF2INIT, &mf2init);
    7582          42 :     GEN r = mfcuspval(mf2, FT, cusp, bitprec);
    7583          42 :     if ((C & 3L) == 2)
    7584             :     {
    7585          14 :       GEN z = sstoQ(1,4);
    7586          14 :       r = gsub(r, typ(r) == t_VEC? const_vec(lg(r)-1, z): z);
    7587             :     }
    7588          42 :     return gerepileupto(av, r);
    7589             :   }
    7590         161 :   vE = mfgetembed(F, prec);
    7591         161 :   lvE = lg(vE);
    7592         161 :   w = mfcuspcanon_width(N, C);
    7593         161 :   sb = w * mfsturmNk(N, itos(gk));
    7594         161 :   ga = cusp2mat(A,C);
    7595         168 :   for (n = 8;; n = minss(sb, n << 1))
    7596           7 :   {
    7597         168 :     GEN R = mfgaexpansion(mf, F, ga, n, prec), res = liftpol_shallow(gel(R,3));
    7598         168 :     GEN v = cgetg(lvE-1, t_VECSMALL);
    7599         168 :     long j, ok = 1;
    7600         168 :     res = RgC_embedall(res, vE);
    7601         357 :     for (j = 1; j < lvE; j++)
    7602             :     {
    7603         189 :       v[j] = val(gel(res,j), bitprec/2);
    7604         189 :       if (v[j] < 0) ok = 0;
    7605             :     }
    7606         168 :     if (ok)
    7607             :     {
    7608         154 :       res = cgetg(lvE, t_VEC);
    7609         329 :       for (j = 1; j < lvE; j++) gel(res,j) = gadd(gel(R,1), sstoQ(v[j], w));
    7610         154 :       return gerepilecopy(av, lvE==2? gel(res,1): res);
    7611             :     }
    7612          14 :     if (n == sb) return lvE==2? mkoo(): const_vec(lvE-1, mkoo()); /* 0 */
    7613             :   }
    7614             : }
    7615             : 
    7616             : long
    7617         224 : mfiscuspidal(GEN mf, GEN F)
    7618             : {
    7619         224 :   pari_sp av = avma;
    7620             :   GEN mf2;
    7621         224 :   if (space_is_cusp(MF_get_space(mf))) return 1;
    7622          98 :   if (typ(mf_get_gk(F)) == t_INT)
    7623             :   {
    7624          56 :     GEN v = mftobasis(mf,F,0), vE = vecslice(v, 1, lg(MF_get_E(mf))-1);
    7625          56 :     return gc_long(av, gequal0(vE));
    7626             :   }
    7627          42 :   if (!gequal0(mfak_i(F, 0))) return 0;
    7628          21 :   mf2 = obj_checkbuild(mf, MF_MF2INIT, &mf2init);
    7629          21 :   return mfiscuspidal(mf2, mfmultheta(F));
    7630             : }
    7631             : 
    7632             : /* F = vector of newforms in mftobasis format */
    7633             : static GEN
    7634          98 : mffrickeeigen_i(GEN mf, GEN F, GEN vE, long prec)
    7635             : {
    7636          98 :   GEN M, Z, L0, gN = MF_get_gN(mf), gk = MF_get_gk(mf);
    7637          98 :   long N0, i, lM, bit = prec2nbits(prec), k = itou(gk);
    7638          98 :   long LIM = 5; /* Sturm bound is enough */
    7639             : 
    7640          98 :   L0 = mfthetaancreate(NULL, gN, gk); /* only for thetacost */
    7641          98 : START:
    7642          98 :   N0 = lfunthetacost(L0, gen_1, LIM, bit);
    7643          98 :   M = mfcoefs_mf(mf, N0, 1);
    7644          98 :   lM = lg(F);
    7645          98 :   Z = cgetg(lM, t_VEC);
    7646         273 :   for (i = 1; i < lM; i++)
    7647             :   { /* expansion of D * F[i] */
    7648         175 :     GEN D, z, van = RgM_RgC_mul(M, Q_remove_denom(gel(F,i), &D));
    7649         175 :     GEN L = van_embedall(van, gel(vE,i), gN, gk);
    7650         175 :     long l = lg(L), j, bit_add = D? expi(D): 0;
    7651         175 :     gel(Z,i) = z = cgetg(l, t_VEC);
    7652         553 :     for (j = 1; j < l; j++)
    7653             :     {
    7654             :       GEN v, C, C0;
    7655             :       long m, e;
    7656         511 :       for (m = 0; m <= LIM; m++)
    7657             :       {
    7658         511 :         v = lfuntheta(gmael(L,j,2), gen_1, m, bit);
    7659         511 :         if (gexpo(v) > bit_add - bit/2) break;
    7660             :       }
    7661         378 :       if (m > LIM) { LIM <<= 1; goto START; }
    7662         378 :       C = mulcxpowIs(gdiv(v,conj_i(v)), 2*m - k);
    7663         378 :       C0 = grndtoi(C, &e); if (e < 5-bit_accuracy(precision(C))) C = C0;
    7664         378 :       gel(z,j) = C;
    7665             :     }
    7666             :   }
    7667          98 :   return Z;
    7668             : }
    7669             : static GEN
    7670          77 : mffrickeeigen(GEN mf, GEN vE, long prec)
    7671             : {
    7672          77 :   GEN D = obj_check(mf, MF_FRICKE);
    7673          77 :   if (D) { long p = gprecision(D); if (!p || p >= prec) return D; }
    7674          70 :   D = mffrickeeigen_i(mf, MF_get_newforms(mf), vE, prec);
    7675          70 :   return obj_insert(mf, MF_FRICKE, D);
    7676             : }
    7677             : 
    7678             : /* integral weight, new space for primitive quadratic character CHIP;
    7679             :  * MF = vector of embedded eigenforms coefs on mfbasis, by orbit.
    7680             :  * Assume N > Q > 1 and (Q,f(CHIP)) = 1 */
    7681             : static GEN
    7682          56 : mfatkineigenquad(GEN mf, GEN CHIP, long Q, GEN MF, long bitprec)
    7683             : {
    7684             :   GEN L0, la2, S, F, vP, tau, wtau, Z, va, vb, den, coe, sqrtQ, sqrtN;
    7685          56 :   GEN M, gN, gk = MF_get_gk(mf);
    7686          56 :   long N0, x, yq, i, j, lF, dim, muQ, prec = nbits2prec(bitprec);
    7687          56 :   long N = MF_get_N(mf), k = itos(gk), NQ = N / Q;
    7688             : 
    7689             :   /* Q coprime to FC */
    7690          56 :   F = MF_get_newforms(mf);
    7691          56 :   vP = MF_get_fields(mf);
    7692          56 :   lF = lg(F);
    7693          56 :   Z = cgetg(lF, t_VEC);
    7694          56 :   S = MF_get_S(mf); dim = lg(S) - 1;
    7695          56 :   muQ = mymoebiusu(Q);
    7696          56 :   if (muQ)
    7697             :   {
    7698          42 :     GEN SQ = cgetg(dim+1,t_VEC), Qk = gpow(stoi(Q), sstoQ(k-2, 2), prec);
    7699          42 :     long i, bit2 = bitprec >> 1;
    7700         154 :     for (j = 1; j <= dim; j++) gel(SQ,j) = mfak_i(gel(S,j), Q);
    7701          84 :     for (i = 1; i < lF; i++)
    7702             :     {
    7703          42 :       GEN S = RgV_dotproduct(gel(F,i), SQ), T = gel(vP,i);
    7704             :       long e;
    7705          42 :       if (degpol(T) > 1 && typ(S) != t_POLMOD) S = gmodulo(S, T);
    7706          42 :       S = grndtoi(gdiv(conjvec(S, prec), Qk), &e);
    7707          42 :       if (e > -bit2) pari_err_PREC("mfatkineigenquad");
    7708          42 :       if (muQ == -1) S = gneg(S);
    7709          42 :       gel(Z,i) = S;
    7710             :     }
    7711          42 :     return Z;
    7712             :   }
    7713          14 :   la2 = mfchareval(CHIP, Q); /* 1 or -1 */
    7714          14 :   (void)cbezout(Q, NQ, &x, &yq);
    7715          14 :   sqrtQ = sqrtr_abs(utor(Q,prec));
    7716          14 :   tau = mkcomplex(gadd(sstoQ(-1, NQ), ginv(utoi(1000))),
    7717             :                   divru(sqrtQ, N));
    7718          14 :   den = gaddgs(gmulsg(NQ, tau), 1);
    7719          14 :   wtau = gdiv(gsub(gmulsg(x, tau), sstoQ(yq, Q)), den);
    7720          14 :   coe = gpowgs(gmul(sqrtQ, den), k);
    7721             : 
    7722          14 :   sqrtN = sqrtr_abs(utor(N,prec));
    7723          14 :   tau  = mulcxmI(gmul(tau,  sqrtN));
    7724          14 :   wtau = mulcxmI(gmul(wtau, sqrtN));
    7725          14 :   gN = utoipos(N);
    7726          14 :   L0 = mfthetaancreate(NULL, gN, gk); /* only for thetacost */
    7727          14 :   N0 = maxss(lfunthetacost(L0,real_i(tau), 0,bitprec),
    7728             :              lfunthetacost(L0,real_i(wtau),0,bitprec));
    7729          14 :   M = mfcoefs_mf(mf, N0, 1);
    7730          14 :   va = cgetg(dim+1, t_VEC);
    7731          14 :   vb = cgetg(dim+1, t_VEC);
    7732         105 :   for (j = 1; j <= dim; j++)
    7733             :   {
    7734          91 :     GEN L, v = vecslice(gel(M,j), 2, N0+1); /* remove a0 */
    7735          91 :     settyp(v, t_VEC); L = mfthetaancreate(v, gN, gk);
    7736          91 :     gel(va,j) = lfuntheta(L, tau,0,bitprec);
    7737          91 :     gel(vb,j) = lfuntheta(L,wtau,0,bitprec);
    7738             :   }
    7739          84 :   for (i = 1; i < lF; i++)
    7740             :   {
    7741          70 :     GEN z, FE = gel(MF,i);
    7742          70 :     long l = lg(FE);
    7743          70 :     z = cgetg(l, t_VEC);
    7744          70 :     for (j = 1; j < l; j++)
    7745             :     {
    7746          70 :       GEN f = gel(FE,j), a = RgV_dotproduct(va,f), b = RgV_dotproduct(vb,f);
    7747          70 :       GEN la = ground( gdiv(b, gmul(a,coe)) );
    7748          70 :       if (!gequal(gsqr(la), la2)) pari_err_PREC("mfatkineigenquad");
    7749          70 :       if (typ(la) == t_INT)
    7750             :       {
    7751          70 :         if (j != 1) pari_err_BUG("mfatkineigenquad");
    7752          70 :         z = const_vec(l-1, la); break;
    7753             :       }
    7754           0 :       gel(z,j) = la;
    7755             :     }
    7756          70 :     gel(Z,i) = z;
    7757             :   }
    7758          14 :   return Z;
    7759             : }
    7760             : 
    7761             : static GEN
    7762          84 : myusqrt(ulong a, long prec)
    7763             : {
    7764          84 :   if (a == 1UL) return gen_1;
    7765          70 :   if (uissquareall(a, &a)) return utoipos(a);
    7766          49 :   return sqrtr_abs(utor(a, prec));
    7767             : }
    7768             : /* Assume mf is a nontrivial new space, rational primitive character CHIP
    7769             :  * and (Q,FC) = 1 */
    7770             : static GEN
    7771         105 : mfatkinmatnewquad(GEN mf, GEN CHIP, long Q, long flag, long PREC)
    7772             : {
    7773         105 :   GEN cM, M, D, MF, den, vE, F = MF_get_newforms(mf);
    7774         105 :   long i, c, e, prec, bitprec, lF = lg(F), N = MF_get_N(mf), k = MF_get_k(mf);
    7775             : 
    7776         105 :   if (Q == 1) return mkvec4(gen_0, matid(MF_get_dim(mf)), gen_1, mf);
    7777         105 :   den = gel(MF_get_Minv(mf), 2);
    7778         105 :   bitprec = expi(den) + 64;
    7779         105 :   if (!flag) bitprec = maxss(bitprec, prec2nbits(PREC));
    7780             : 
    7781          35 : START:
    7782         105 :   prec = nbits2prec(bitprec);
    7783         105 :   vE = mfeigenembed(mf, prec);
    7784         105 :   M = cgetg(lF, t_VEC);
    7785         280 :   for (i = 1; i < lF; i++) gel(M,i) = RgC_embedall(gel(F,i), gel(vE,i));
    7786         105 :   if (Q != N)
    7787             :   {
    7788          56 :     D = mfatkineigenquad(mf, CHIP, Q, M, bitprec);
    7789          56 :     c = odd(k)? Q: 1;
    7790             :   }
    7791             :   else
    7792             :   {
    7793          49 :     D = mffrickeeigen(mf, vE, prec);
    7794          49 :     c = mfcharmodulus(CHIP); if (odd(k)) c = -Q/c;
    7795             :   }
    7796         105 :   D = shallowconcat1(D);
    7797         105 :   if (vec_isconst(D)) { MF = diagonal_shallow(D); flag = 0; }
    7798             :   else
    7799             :   {
    7800          63 :     M = shallowconcat1(M);
    7801          63 :     MF = RgM_mul(matmuldiagonal(M,D), ginv(M));
    7802             :   }
    7803         105 :   if (!flag) return mkvec4(gen_0, MF, gen_1, mf);
    7804             : 
    7805          21 :   if (c > 0)
    7806          21 :     cM = myusqrt(c, PREC);
    7807             :   else
    7808             :   {
    7809           0 :     MF = imag_i(MF); c = -c;
    7810           0 :     cM = mkcomplex(gen_0, myusqrt(c,PREC));
    7811             :   }
    7812          21 :   if (c != 1) MF = RgM_Rg_mul(MF, myusqrt(c,prec));
    7813          21 :   MF = grndtoi(RgM_Rg_mul(MF,den), &e);
    7814          21 :   if (e > -32) { bitprec <<= 1; goto START; }
    7815          21 :   MF = RgM_Rg_div(MF, den);
    7816          21 :   if (is_rational_t(typ(cM)) && !isint1(cM))
    7817           0 :   { MF = RgM_Rg_div(MF, cM); cM = gen_1; }
    7818          21 :   return mkvec4(gen_0, MF, cM, mf);
    7819             : }
    7820             : 
    7821             : /* let CHI mod N, Q || N, return \bar{CHI_Q} * CHI_{N/Q} */
    7822             : static GEN
    7823          91 : mfcharAL(GEN CHI, long Q)
    7824             : {
    7825          91 :   GEN G = gel(CHI,1), c = gel(CHI,2), cycc, d, P, E, F;
    7826          91 :   long l = lg(c), N = mfcharmodulus(CHI), i;
    7827          91 :   if (N == Q) return mfcharconj(CHI);
    7828          42 :   if (N == 1) return CHI;
    7829          42 :   CHI = leafcopy(CHI);
    7830          42 :   gel(CHI,2) = d = leafcopy(c);
    7831          42 :   F = znstar_get_faN(G);
    7832          42 :   P = gel(F,1);
    7833          42 :   E = gel(F,2);
    7834          42 :   cycc = znstar_get_conreycyc(G);
    7835          42 :   if (!odd(Q) && equaliu(gel(P,1), 2) && E[1] >= 3)
    7836          14 :     gel(d,2) = Fp_neg(gel(d,2), gel(cycc,2));
    7837          56 :   else for (i = 1; i < l; i++)
    7838          28 :     if (!umodui(Q, gel(P,i))) gel(d,i) = Fp_neg(gel(d,i), gel(cycc,i));
    7839          42 :   return CHI;
    7840             : }
    7841             : static long
    7842         217 : atkin_get_NQ(long N, long Q, const char *f)
    7843             : {
    7844         217 :   long NQ = N / Q;
    7845         217 :   if (N % Q) pari_err_DOMAIN(f,"N % Q","!=",gen_0,utoi(Q));
    7846         217 :   if (ugcd(NQ, Q) > 1) pari_err_DOMAIN(f,"gcd(Q,N/Q)","!=",gen_1,utoi(Q));
    7847         217 :   return NQ;
    7848             : }
    7849             : 
    7850             : /* transform mf to new_NEW if possible */
    7851             : static GEN
    7852        1274 : MF_set_new(GEN mf)
    7853             : {
    7854        1274 :   GEN vMjd, vj, gk = MF_get_gk(mf);
    7855             :   long l, j;
    7856        1274 :   if (MF_get_space(mf) != mf_CUSP
    7857        1274 :       || typ(gk) != t_INT || itou(gk) == 1) return mf;
    7858         175 :   vMjd = MFcusp_get_vMjd(mf); l = lg(vMjd);
    7859         175 :   if (l > 1 && gel(vMjd,1)[1] != MF_get_N(mf)) return mf; /* oldspace != 0 */
    7860         168 :   mf = shallowcopy(mf);
    7861         168 :   gel(mf,1) = shallowcopy(gel(mf,1));
    7862         168 :   MF_set_space(mf, mf_NEW);
    7863         168 :   vj = cgetg(l, t_VECSMALL);
    7864         917 :   for (j = 1; j < l; j++) vj[j] = gel(vMjd, j)[2];
    7865         168 :   gel(mf,4) = vj; return mf;
    7866             : }
    7867             : 
    7868             : /* if flag = 1, rationalize, else don't */
    7869             : static GEN
    7870         196 : mfatkininit_i(GEN mf, long Q, long flag, long prec)
    7871             : {
    7872             :   GEN M, B, C, CHI, CHIAL, G, chi, P, z, g, mfB, s, Mindex, Minv;
    7873         196 :   long j, l, lim, ord, FC, NQ, cQ, nk, dk, N = MF_get_N(mf);
    7874             : 
    7875         196 :   B = MF_get_basis(mf); l = lg(B);
    7876         196 :   M = cgetg(l, t_MAT); if (l == 1) return mkvec4(gen_0,M,gen_1,mf);
    7877         196 :   Qtoss(MF_get_gk(mf), &nk,&dk);
    7878         196 :   Q = labs(Q);
    7879         196 :   NQ = atkin_get_NQ(N, Q, "mfatkininit");
    7880         196 :   CHI = MF_get_CHI(mf);
    7881         196 :   CHI = mfchartoprimitive(CHI, &FC);
    7882         196 :   ord = mfcharorder(CHI);
    7883         196 :   mf = MF_set_new(mf);
    7884         196 :   if (MF_get_space(mf) == mf_NEW && ord <= 2 && NQ % FC == 0 && dk == 1)
    7885         105 :     return mfatkinmatnewquad(mf, CHI, Q, flag, prec);
    7886             :   /* now flag != 0 */
    7887          91 :   G   = gel(CHI,1);
    7888          91 :   chi = gel(CHI,2);
    7889          91 :   if (Q == N) { g = mkmat22s(0, -1, N, 0); cQ = NQ; } /* Fricke */
    7890             :   else
    7891             :   {
    7892          28 :     GEN F, gQP = utoi(ugcd(Q, FC));
    7893             :     long t, v;
    7894          28 :     chi = znchardecompose(G, chi, gQP);
    7895          28 :     F = znconreyconductor(G, chi, &chi);
    7896          28 :     G = znstar0(F,1);
    7897          28 :     (void)cbezout(Q, NQ, &t, &v);
    7898          28 :     g = mkmat22s(Q*t, 1, -N*v, Q);
    7899          28 :     cQ = -NQ*v;
    7900             :   }
    7901          91 :   C = s = gen_1;
    7902             :   /* N.B. G,chi are G_Q,chi_Q [primitive] at this point */
    7903          91 :   if (lg(chi) != 1) C = ginv( znchargauss(G, chi, gen_1, prec2nbits(prec)) );
    7904          91 :   if (dk == 1)
    7905          84 :   { if (odd(nk)) s = myusqrt(Q,prec); }
    7906             :   else
    7907             :   {
    7908           7 :     long r = nk >> 1; /* k-1/2 */
    7909           7 :     s = gpow(utoipos(Q), mkfracss(odd(r)? 1: 3, 4), prec);
    7910           7 :     if (odd(cQ))
    7911             :     {
    7912           7 :       long t = r + ((cQ-1) >> 1);
    7913           7 :       s = mkcomplex(s, odd(t)? gneg(s): s);
    7914             :     }
    7915             :   }
    7916          91 :   if (!isint1(s)) C = gmul(C, s);
    7917          91 :   CHIAL = mfcharAL(CHI, Q);
    7918          91 :   if (dk == 2)
    7919           7 :     CHIAL = mfcharmul(CHIAL, induce(gel(CHIAL,1), utoipos(odd(Q) ? Q<<2 : Q)));
    7920          91 :   CHIAL = mfchartoprimitive(CHIAL,NULL);
    7921          91 :   mfB = gequal(CHIAL,CHI)? mf: mfinit_Nndkchi(N,nk,dk,CHIAL,MF_get_space(mf),0);
    7922          91 :   Mindex = MF_get_Mindex(mfB);
    7923          91 :   Minv = MF_get_Minv(mfB);
    7924          91 :   P = z = NULL;
    7925          91 :   if (ord > 2) { P = mfcharpol(CHI); z = rootsof1u_cx(ord, prec); }
    7926          91 :   lim = maxss(mfsturm(mfB), mfsturm(mf)) + 1;
    7927         287 :   for (j = 1; j < l; j++)
    7928             :   {
    7929         196 :     GEN v = mfslashexpansion(mf, gel(B,j), g, lim, 0, NULL, prec+EXTRAPREC64);
    7930             :     long junk;
    7931         196 :     if (!isint1(C)) v = RgV_Rg_mul(v, C);
    7932         196 :     v = bestapprnf(v, P, z, prec);
    7933         196 :     v = vecpermute_partial(v, Mindex, &junk);
    7934         196 :     v = Minv_RgC_mul(Minv, v); /* cf mftobasis_i */
    7935         196 :     gel(M, j) = v;
    7936             :   }
    7937          91 :   if (is_rational_t(typ(C)) && !gequal1(C)) { M = gdiv(M, C); C = gen_1; }
    7938          91 :   if (mfB == mf) mfB = gen_0;
    7939          91 :   return mkvec4(mfB, M, C, mf);
    7940             : }
    7941             : GEN
    7942          77 : mfatkininit(GEN mf, long Q, long prec)
    7943             : {
    7944          77 :   pari_sp av = avma;
    7945          77 :   mf = checkMF(mf); return gerepilecopy(av, mfatkininit_i(mf, Q, 1, prec));
    7946             : }
    7947             : static void
    7948          56 : checkmfa(GEN z)
    7949             : {
    7950          56 :   if (typ(z) != t_VEC || lg(z) != 5 || typ(gel(z,2)) != t_MAT
    7951          56 :       || !checkMF_i(gel(z,4))
    7952          56 :       || (!isintzero(gel(z,1)) && !checkMF_i(gel(z,1))))
    7953           0 :     pari_err_TYPE("mfatkin [please apply mfatkininit()]",z);
    7954          56 : }
    7955             : 
    7956             : /* Apply atkin Q to closure F */
    7957             : GEN
    7958          56 : mfatkin(GEN mfa, GEN F)
    7959             : {
    7960          56 :   pari_sp av = avma;
    7961             :   GEN z, mfB, MQ, mf;
    7962          56 :   checkmfa(mfa);
    7963          56 :   mfB= gel(mfa,1);
    7964          56 :   MQ = gel(mfa,2);
    7965          56 :   mf = gel(mfa,4);
    7966          56 :   if (typ(mfB) == t_INT) mfB = mf;
    7967          56 :   z = RgM_RgC_mul(MQ, mftobasis_i(mf,F));
    7968          56 :   return gerepileupto(av, mflinear(mfB, z));
    7969             : }
    7970             : 
    7971             : GEN
    7972          49 : mfatkineigenvalues(GEN mf, long Q, long prec)
    7973             : {
    7974          49 :   pari_sp av = avma;
    7975             :   GEN vF, L, CHI, M, mfatk, C, MQ, vE, mfB;
    7976             :   long N, NQ, l, i;
    7977             : 
    7978          49 :   mf = checkMF(mf); N = MF_get_N(mf);
    7979          49 :   vF = MF_get_newforms(mf); l = lg(vF);
    7980             :   /* N.B. k is integral */
    7981          49 :   if (l == 1) { set_avma(av); return cgetg(1, t_VEC); }
    7982          49 :   L = cgetg(l, t_VEC);
    7983          49 :   if (Q == 1)
    7984             :   {
    7985           7 :     GEN vP = MF_get_fields(mf);
    7986          21 :     for (i = 1; i < l; i++) gel(L,i) = const_vec(degpol(gel(vP,i)), gen_1);
    7987           7 :     return L;
    7988             :   }
    7989          42 :   vE = mfeigenembed(mf,prec);
    7990          42 :   if (Q == N) return gerepileupto(av, mffrickeeigen(mf, vE, prec));
    7991          21 :   Q = labs(Q);
    7992          21 :   NQ = atkin_get_NQ(N, Q, "mfatkineigenvalues"); /* != 1 */
    7993          21 :   mfatk = mfatkininit(mf, Q, prec);
    7994          21 :   mfB= gel(mfatk,1); if (typ(mfB) != t_VEC) mfB = mf;
    7995          21 :   MQ = gel(mfatk,2);
    7996          21 :   C  = gel(mfatk,3);
    7997          21 :   M = row(mfcoefs_mf(mfB,1,1), 2); /* vec of a_1(b_i) for mfbasis functions */
    7998          56 :   for (i = 1; i < l; i++)
    7999             :   {
    8000          35 :     GEN c = RgV_dotproduct(RgM_RgC_mul(MQ,gel(vF,i)), M); /* C * eigen_i */
    8001          35 :     gel(L,i) = Rg_embedall_i(c, gel(vE,i));
    8002             :   }
    8003          21 :   if (!gequal1(C)) L = gdiv(L, C);
    8004          21 :   CHI = MF_get_CHI(mf);
    8005          21 :   if (mfcharorder(CHI) <= 2 && NQ % mfcharconductor(CHI) == 0) L = ground(L);
    8006          21 :   return gerepilecopy(av, L);
    8007             : }
    8008             : 
    8009             : /* expand B_d V, keeping same length */
    8010             : static GEN
    8011        5852 : bdexpand(GEN V, long d)
    8012             : {
    8013             :   GEN W;
    8014             :   long N, n;
    8015        5852 :   if (d == 1) return V;
    8016        2121 :   N = lg(V)-1; W = zerovec(N);
    8017       42749 :   for (n = 0; n <= (N-1)/d; n++) gel(W, n*d+1) = gel(V, n+1);
    8018        2121 :   return W;
    8019             : }
    8020             : /* expand B_d V, increasing length up to lim */
    8021             : static GEN
    8022         287 : bdexpandall(GEN V, long d, long lim)
    8023             : {
    8024             :   GEN W;
    8025             :   long N, n;
    8026         287 :   if (d == 1) return V;
    8027          35 :   N = lg(V)-1; W = zerovec(lim);
    8028         259 :   for (n = 0; n <= N-1 && n*d <= lim; n++) gel(W, n*d+1) = gel(V, n+1);
    8029          35 :   return W;
    8030             : }
    8031             : 
    8032             : static void
    8033        8813 : parse_vecj(GEN T, GEN *E1, GEN *E2)
    8034             : {
    8035        8813 :   if (lg(T)==3) { *E1 = gel(T,1); *E2 = gel(T,2); }
    8036        4732 :   else { *E1 = T; *E2 = NULL; }
    8037        8813 : }
    8038             : 
    8039             : /* g in M_2(Z) ? */
    8040             : static int
    8041        2744 : check_M2Z(GEN g)
    8042        2744 : {  return typ(g) == t_MAT && lg(g) == 3 && lgcols(g) == 3 && RgM_is_ZM(g); }
    8043             : /* g in SL_2(Z) ? */
    8044             : static int
    8045        1673 : check_SL2Z(GEN g) { return check_M2Z(g) && equali1(ZM_det(g)); }
    8046             : 
    8047             : static GEN
    8048        9023 : mfcharcxeval(GEN CHI, long n, long prec)
    8049             : {
    8050        9023 :   ulong ord, N = mfcharmodulus(CHI);
    8051             :   GEN ordg;
    8052        9023 :   if (N == 1) return gen_1;
    8053        3696 :   if (ugcd(N, labs(n)) > 1) return gen_0;
    8054        3696 :   ordg = gmfcharorder(CHI);
    8055        3696 :   ord = itou(ordg);
    8056        3696 :   return rootsof1q_cx(znchareval_i(CHI,n,ordg), ord, prec);
    8057             : }
    8058             : 
    8059             : static GEN
    8060        4837 : RgV_shift(GEN V, GEN gn)
    8061             : {
    8062             :   long i, n, l;
    8063             :   GEN W;
    8064        4837 :   if (typ(gn) != t_INT) pari_err_BUG("RgV_shift [n not integral]");
    8065        4837 :   n = itos(gn);
    8066        4837 :   if (n < 0) pari_err_BUG("RgV_shift [n negative]");
    8067        4837 :   if (!n) return V;
    8068         112 :   W = cgetg_copy(V, &l); if (n > l-1) n = l-1;
    8069         308 :   for (i=1; i <= n; i++) gel(W,i) = gen_0;
    8070        4900 :   for (    ; i < l; i++) gel(W,i) = gel(V, i-n);
    8071         112 :   return W;
    8072             : }
    8073             : static GEN
    8074        7483 : hash_eisengacx(hashtable *H, void *E, long w, GEN ga, long n, long prec)
    8075             : {
    8076        7483 :   ulong h = H->hash(E);
    8077        7483 :   hashentry *e = hash_search2(H, E, h);
    8078             :   GEN v;
    8079        7483 :   if (e) v = (GEN)e->val;
    8080             :   else
    8081             :   {
    8082        5012 :     v = mfeisensteingacx((GEN)E, w, ga, n, prec);
    8083        5012 :     hash_insert2(H, E, (void*)v, h);
    8084             :   }
    8085        7483 :   return v;
    8086             : }
    8087             : static GEN
    8088        4837 : vecj_expand(GEN B, hashtable *H, long w, GEN ga, long n, long prec)
    8089             : {
    8090             :   GEN E1, E2, v;
    8091        4837 :   parse_vecj(B, &E1, &E2);
    8092        4837 :   v = hash_eisengacx(H, (void*)E1, w, ga, n, prec);
    8093        4837 :   if (E2)
    8094             :   {
    8095        2590 :     GEN u = hash_eisengacx(H, (void*)E2, w, ga, n, prec);
    8096        2590 :     GEN a = gadd(gel(v,1), gel(u,1));
    8097        2590 :     GEN b = RgV_mul_RgXn(gel(v,2), gel(u,2));
    8098        2590 :     v = mkvec2(a,b);
    8099             :   }
    8100        4837 :   return v;
    8101             : }
    8102             : static GEN
    8103        1008 : shift_M(GEN M, GEN Valpha, long w)
    8104             : {
    8105        1008 :   long i, l = lg(Valpha);
    8106        1008 :   GEN almin = vecmin(Valpha);
    8107        5845 :   for (i = 1; i < l; i++)
    8108             :   {
    8109        4837 :     GEN alpha = gel(Valpha, i), gsh = gmulsg(w, gsub(alpha,almin));
    8110        4837 :     gel(M,i) = RgV_shift(gel(M,i), gsh);
    8111             :   }
    8112        1008 :   return almin;
    8113             : }
    8114             : static GEN mfeisensteinspaceinit(GEN NK);
    8115             : #if 0
    8116             : /* ga in M_2^+(Z)), n >= 0 */
    8117             : static GEN
    8118             : mfgaexpansion_init(GEN mf, GEN ga, long n, long prec)
    8119             : {
    8120             :   GEN M, Mvecj, vecj, almin, Valpha;
    8121             :   long i, w, l, N = MF_get_N(mf), c = itos(gcoeff(ga,2,1));
    8122             :   hashtable *H;
    8123             : 
    8124             :   if (c % N == 0)
    8125             :   { /* ga in G_0(N), trivial case; w = 1 */
    8126             :     GEN chid = mfcharcxeval(MF_get_CHI(mf), itos(gcoeff(ga,2,2)), prec);
    8127             :     return mkvec2(chid, utoi(n));
    8128             :   }
    8129             : 
    8130             :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    8131             :   if (lg(Mvecj) < 5) pari_err_IMPL("mfgaexpansion_init in this case");
    8132             :   w = mfcuspcanon_width(N, c);
    8133             :   vecj = gel(Mvecj, 3);
    8134             :   l = lg(vecj);
    8135             :   M = cgetg(l, t_VEC);
    8136             :   Valpha = cgetg(l, t_VEC);
    8137             :   H = hash_create(l, (ulong(*)(void*))&hash_GEN,
    8138             :                      (int(*)(void*,void*))&gidentical, 1);
    8139             :   for (i = 1; i < l; i++)
    8140             :   {
    8141             :     GEN v = vecj_expand(gel(vecj,i), H, w, ga, n, prec);
    8142             :     gel(Valpha,i) = gel(v,1);
    8143             :     gel(M,i) = gel(v,2);
    8144             :   }
    8145             :   almin = shift_M(M, Valpha, w);
    8146             :   return mkvec3(almin, utoi(w), M);
    8147             : }
    8148             : /* half-integer weight not supported; vF = [F,eisendec(F)].
    8149             :  * Minit = mfgaexpansion_init(mf, ga, n, prec) */
    8150             : static GEN
    8151             : mfgaexpansion_with_init(GEN Minit, GEN vF)
    8152             : {
    8153             :   GEN v;
    8154             :   if (lg(Minit) == 3)
    8155             :   { /* ga in G_0(N) */
    8156             :     GEN chid = gel(Minit,1), gn = gel(Minit,2);
    8157             :     v = mfcoefs_i(gel(vF,1), itou(gn), 1);
    8158             :     v = mkvec3(gen_0, gen_1, RgV_Rg_mul(v,chid));
    8159             :   }
    8160             :   else
    8161             :   {
    8162             :     GEN V = RgM_RgC_mul(gel(Minit,3), gel(vF,2));
    8163             :     v = mkvec3(gel(Minit,1), gel(Minit,2), V);
    8164             :   }
    8165             :   return v;
    8166             : }
    8167             : #endif
    8168             : 
    8169             : /* B = mfeisensteindec(F) already embedded, ga in M_2^+(Z)), n >= 0 */
    8170             : static GEN
    8171        1008 : mfgaexpansion_i(GEN mf, GEN B0, GEN ga, long n, long prec)
    8172             : {
    8173        1008 :   GEN M, Mvecj, vecj, almin, Valpha, B, E = NULL;
    8174        1008 :   long i, j, w, nw, l, N = MF_get_N(mf), bit = prec2nbits(prec) / 2;
    8175             :   hashtable *H;
    8176             : 
    8177        1008 :   Mvecj = obj_check(mf, MF_EISENSPACE);
    8178        1008 :   if (lg(Mvecj) < 5) { E = gel(Mvecj, 2); Mvecj = gel(Mvecj, 1); }
    8179        1008 :   vecj = gel(Mvecj, 3);
    8180        1008 :   l = lg(vecj);
    8181        1008 :   B = cgetg(l, t_COL);
    8182        1008 :   M = cgetg(l, t_VEC);
    8183        1008 :   Valpha = cgetg(l, t_VEC);
    8184        1008 :   w = mfZC_width(N, gel(ga,1));
    8185        1008 :   nw = E ? n + w : n;
    8186        1008 :   H = hash_create(l, (ulong(*)(void*))&hash_GEN,
    8187             :                      (int(*)(void*,void*))&gidentical, 1);
    8188        8743 :   for (i = j = 1; i < l; i++)
    8189             :   {
    8190             :     GEN v;
    8191        7735 :     if (gequal0(gel(B0,i))) continue;
    8192        4837 :     v = vecj_expand(gel(vecj,i), H, w, ga, nw, prec);
    8193        4837 :     gel(B,j) = gel(B0,i);
    8194        4837 :     gel(Valpha,j) = gel(v,1);
    8195        4837 :     gel(M,j) = gel(v,2); j++;
    8196             :   }
    8197        1008 :   setlg(Valpha, j);
    8198        1008 :   setlg(B, j);
    8199        1008 :   setlg(M, j); l = j;
    8200        1008 :   if (l == 1) return mkvec3(gen_0, utoi(w), zerovec(n+1));
    8201        1008 :   almin = shift_M(M, Valpha, w);
    8202        1008 :   B = RgM_RgC_mul(M, B); l = lg(B);
    8203      147602 :   for (i = 1; i < l; i++)
    8204      146594 :     if (gexpo(gel(B,i)) < -bit) gel(B,i) = gen_0;
    8205        1008 :   settyp(B, t_VEC);
    8206        1008 :   if (E)
    8207             :   {
    8208             :     GEN v, e;
    8209          56 :     long ell = 0, vB, ve;
    8210         126 :     for (i = 1; i < l; i++)
    8211         126 :       if (!gequal0(gel(B,i))) break;
    8212          56 :     vB = i-1;
    8213          56 :     v = hash_eisengacx(H, (void*)E, w, ga, n + vB, prec);
    8214          56 :     e = gel(v,2); l = lg(e);
    8215          56 :     for (i = 1; i < l; i++)
    8216          56 :       if (!gequal0(gel(e,i))) break;
    8217          56 :     ve = i-1;
    8218          56 :     almin = gsub(almin, gel(v,1));
    8219          56 :     if (gsigne(almin) < 0)
    8220             :     {
    8221           0 :       GEN gell = gceil(gmulsg(-w, almin));
    8222           0 :       ell = itos(gell);
    8223           0 :       almin = gadd(almin, gdivgs(gell, w));
    8224           0 :       if (nw < ell) pari_err_IMPL("alpha < 0 in mfgaexpansion");
    8225             :     }
    8226          56 :     if (ve) { ell += ve; e = vecslice(e, ve+1, l-1); }
    8227          56 :     B = vecslice(B, ell + 1, minss(n + ell + 1, lg(B)-1));
    8228          56 :     B = RgV_div_RgXn(B, e);
    8229             :   }
    8230        1008 :   return mkvec3(almin, utoi(w), B);
    8231             : }
    8232             : 
    8233             : /* Theta multiplier: assume 4 | C, (C,D)=1 */
    8234             : static GEN
    8235         301 : mfthetamultiplier(GEN C, GEN D)
    8236             : {
    8237         301 :   long s = kronecker(C, D);
    8238         301 :   if (Mod4(D) == 1) return s > 0 ? gen_1: gen_m1;
    8239          84 :   return s > 0? powIs(3): gen_I();
    8240             : }
    8241             : /* theta | [*,*;C,D] defined over Q(i) [else over Q] */
    8242             : static int
    8243          56 : mfthetaI(long C, long D) { return odd(C) || (D & 3) == 3; }
    8244             : /* (theta | M) [0..n], assume (C,D) = 1 */
    8245             : static GEN
    8246         301 : mfthetaexpansion(GEN M, long n)
    8247             : {
    8248         301 :   GEN w, s, al, sla, E, V = zerovec(n+1), C = gcoeff(M,2,1), D = gcoeff(M,2,2);
    8249         301 :   long lim, la, f, C4 = Mod4(C);
    8250         301 :   switch (C4)
    8251             :   {
    8252          70 :     case 0: al = gen_0; w = gen_1;
    8253          70 :       s = mfthetamultiplier(C,D);
    8254          70 :       lim = usqrt(n); gel(V, 1) = s;
    8255          70 :       s = gmul2n(s, 1);
    8256         756 :       for (f = 1; f <= lim; f++) gel(V, f*f + 1) = s;
    8257          70 :       break;
    8258         105 :     case 2: al = sstoQ(1,4); w = gen_1;
    8259         105 :       E = subii(C, shifti(D,1)); /* (E, D) = 1 */
    8260         105 :       s = gmul2n(mfthetamultiplier(E, D), 1);
    8261         105 :       if ((!signe(E) && equalim1(D)) || (signe(E) > 0 && signe(C) < 0))
    8262          14 :         s = gneg(s);
    8263         105 :       lim = (usqrt(n << 2) - 1) >> 1;
    8264         966 :       for (f = 0; f <= lim; f++) gel(V, f*(f+1) + 1) = s;
    8265         105 :       break;
    8266         126 :     default: al = gen_0; w = utoipos(4);
    8267         126 :       la = (-Mod4(D)*C4) & 3L;
    8268         126 :       E = negi(addii(D, mului(la, C)));
    8269         126 :       s = mfthetamultiplier(E, C); /* (E,C) = 1 */
    8270         126 :       if (signe(C) < 0 && signe(E) >= 0) s = gneg(s);
    8271         126 :       s = gsub(s, mulcxI(s));
    8272         126 :       sla = gmul(s, powIs(-la));
    8273         126 :       lim = usqrt(n); gel(V, 1) = gmul2n(s, -1);
    8274        1624 :       for (f = 1; f <= lim; f++) gel(V, f*f + 1) = odd(f) ? sla : s;
    8275         126 :       break;
    8276             :   }
    8277         301 :   return mkvec3(al, w, V);
    8278             : }
    8279             : 
    8280             : /* F 1/2 integral weight */
    8281             : static GEN
    8282         301 : mf2gaexpansion(GEN mf2, GEN F, GEN ga, long n, long prec)
    8283             : {
    8284         301 :   GEN FT = mfmultheta(F), mf = obj_checkbuild(mf2, MF_MF2INIT, &mf2init);
    8285         301 :   GEN res, V1, Tres, V2, al, V, gsh, C = gcoeff(ga,2,1);
    8286         301 :   long w2, N = MF_get_N(mf), w = mfcuspcanon_width(N, umodiu(C,N));
    8287         301 :   long ext = (Mod4(C) != 2)? 0: (w+3) >> 2;
    8288         301 :   long prec2 = prec + nbits2extraprec((long)M_PI/(2*M_LN2)*sqrt(n + ext));
    8289         301 :   res = mfgaexpansion(mf, FT, ga, n + ext, prec2);
    8290         301 :   Tres = mfthetaexpansion(ga, n + ext);
    8291         301 :   V1 = gel(res,3);
    8292         301 :   V2 = gel(Tres,3);
    8293         301 :   al = gsub(gel(res,1), gel(Tres,1));
    8294         301 :   w2 = itos(gel(Tres,2));
    8295         301 :   if (w != itos(gel(res,2)) || w % w2)
    8296           0 :     pari_err_BUG("mf2gaexpansion [incorrect w2 or w]");
    8297         301 :   if (w2 != w) V2 = bdexpand(V2, w/w2);
    8298         301 :   V = RgV_div_RgXn(V1, V2);
    8299         301 :   gsh = gfloor(gmulsg(w, al));
    8300         301 :   if (!gequal0(gsh))
    8301             :   {
    8302          35 :     al = gsub(al, gdivgs(gsh, w));
    8303          35 :     if (gsigne(gsh) > 0)
    8304             :     {
    8305           0 :       V = RgV_shift(V, gsh);
    8306           0 :       V = vecslice(V, 1, n + 1);
    8307             :     }
    8308             :     else
    8309             :     {
    8310          35 :       long sh = -itos(gsh), i;
    8311          35 :       if (sh > ext) pari_err_BUG("mf2gaexpansion [incorrect sh]");
    8312         154 :       for (i = 1; i <= sh; i++)
    8313         119 :         if (!gequal0(gel(V,i))) pari_err_BUG("mf2gaexpansion [sh too large]");
    8314          35 :       V = vecslice(V, sh+1, n + sh+1);
    8315             :     }
    8316             :   }
    8317         301 :   obj_free(mf); return mkvec3(al, stoi(w), gprec_wtrunc(V, prec));
    8318             : }
    8319             : 
    8320             : static GEN
    8321          70 : mfgaexpansionatkin(GEN mf, GEN F, GEN C, GEN D, long Q, long n, long prec)
    8322             : {
    8323          70 :   GEN mfa = mfatkininit_i(mf, Q, 0, prec), MQ = gel(mfa,2);
    8324          70 :   long i, FC, k = MF_get_k(mf);
    8325          70 :   GEN x, v, V, z, s, CHI = mfchartoprimitive(MF_get_CHI(mf), &FC);
    8326             : 
    8327             :   /* V = mfcoefs(F | w_Q, n), can't use mfatkin because MQ nonrational */
    8328          70 :   V = RgM_RgC_mul(mfcoefs_mf(mf,n,1), RgM_RgC_mul(MQ, mftobasis_i(mf,F)));
    8329          70 :   (void)bezout(utoipos(Q), C, &x, &v);
    8330          70 :   s = mfchareval(CHI, (umodiu(x, FC) * umodiu(D, FC)) % FC);
    8331          70 :   s = gdiv(s, gpow(utoipos(Q), sstoQ(k,2), prec));
    8332          70 :   V = RgV_Rg_mul(V, s);
    8333          70 :   z = rootsof1powinit(umodiu(D,Q)*umodiu(v,Q) % Q, Q, prec);
    8334        8253 :   for (i = 1; i <= n+1; i++) gel(V,i) = gmul(gel(V,i), rootsof1pow(z, i-1));
    8335          70 :   return mkvec3(gen_0, utoipos(Q), V);
    8336             : }
    8337             : 
    8338             : static long
    8339          70 : inveis_extraprec(long N, GEN ga, GEN Mvecj, long n)
    8340             : {
    8341          70 :   long e, w = mfZC_width(N, gel(ga,1));
    8342          70 :   GEN f, E = gel(Mvecj,2), v = mfeisensteingacx(E, w, ga, n, DEFAULTPREC);
    8343          70 :   v = gel(v,2);
    8344          70 :   f = RgV_to_RgX(v,0); n -= RgX_valrem(f, &f);
    8345          70 :   e = gexpo(RgXn_inv(f, n+1));
    8346          70 :   return (e > 0)? nbits2extraprec(e): 0;
    8347             : }
    8348             : /* allow F of the form [F, mf_eisendec(F)]~ */
    8349             : static GEN
    8350        1666 : mfgaexpansion(GEN mf, GEN F, GEN ga, long n, long prec)
    8351             : {
    8352        1666 :   GEN v, EF = NULL, res, Mvecj, c, d;
    8353             :   long precnew, N;
    8354             : 
    8355        1666 :   if (n < 0) pari_err_DOMAIN("mfgaexpansion", "n", "<", gen_0, stoi(n));
    8356        1666 :   if (typ(F) == t_COL && lg(F) == 3) { EF = gel(F,2); F = gel(F,1); }
    8357        1666 :   if (!checkmf_i(F)) pari_err_TYPE("mfgaexpansion", F);
    8358        1666 :   if (!check_SL2Z(ga)) pari_err_TYPE("mfgaexpansion",ga);
    8359        1666 :   if (typ(mf_get_gk(F)) != t_INT) return mf2gaexpansion(mf, F, ga, n, prec);
    8360        1365 :   c = gcoeff(ga,2,1);
    8361        1365 :   d = gcoeff(ga,2,2);
    8362        1365 :   N = MF_get_N(mf);
    8363        1365 :   if (!umodiu(c, mf_get_N(F)))
    8364             :   { /* trivial case: ga in Gamma_0(N) */
    8365         287 :     long w = mfcuspcanon_width(N, umodiu(c,N));
    8366         287 :     GEN CHI = mf_get_CHI(F);
    8367         287 :     GEN chid = mfcharcxeval(CHI, umodiu(d,mfcharmodulus(CHI)), prec);
    8368         287 :     v = mfcoefs_i(F, n/w, 1); if (!isint1(chid)) v = RgV_Rg_mul(v,chid);
    8369         287 :     return mkvec3(gen_0, stoi(w), bdexpandall(v,w,n+1));
    8370             :   }
    8371        1078 :   mf = MF_set_new(mf);
    8372        1078 :   if (MF_get_space(mf) == mf_NEW)
    8373             :   {
    8374         441 :     long cN = umodiu(c,N), g = ugcd(cN,N), Q = N/g;
    8375         441 :     GEN CHI = MF_get_CHI(mf);
    8376         441 :     if (ugcd(cN, Q)==1 && mfcharorder(CHI) <= 2
    8377         217 :                        && g % mfcharconductor(CHI) == 0
    8378         112 :                        && degpol(mf_get_field(F)) == 1)
    8379          70 :       return mfgaexpansionatkin(mf, F, c, d, Q, n, prec);
    8380             :   }
    8381        1008 :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    8382        1008 :   precnew = prec;
    8383        1008 :   if (lg(Mvecj) < 5) precnew += inveis_extraprec(N, ga, Mvecj, n);
    8384        1008 :   if (!EF) EF = mf_eisendec(mf, F, precnew);
    8385        1008 :   res = mfgaexpansion_i(mf, EF, ga, n, precnew);
    8386        1008 :   return precnew == prec ? res : gprec_wtrunc(res, prec);
    8387             : }
    8388             : 
    8389             : /* parity = -1 or +1 */
    8390             : static GEN
    8391         217 : findd(long N, long parity)
    8392             : {
    8393         217 :   GEN L, D = mydivisorsu(N);
    8394         217 :   long i, j, l = lg(D);
    8395         217 :   L = cgetg(l, t_VEC);
    8396        1218 :   for (i = j = 1; i < l; i++)
    8397             :   {
    8398        1001 :     long d = D[i];
    8399        1001 :     if (parity == -1) d = -d;
    8400        1001 :     if (sisfundamental(d)) gel(L,j++) = stoi(d);
    8401             :   }
    8402         217 :   setlg(L,j); return L;
    8403             : }
    8404             : /* does ND contain a divisor of N ? */
    8405             : static int
    8406         413 : seenD(long N, GEN ND)
    8407             : {
    8408         413 :   long j, l = lg(ND);
    8409         427 :   for (j = 1; j < l; j++)
    8410          14 :     if (N % ND[j] == 0) return 1;
    8411         413 :   return 0;
    8412             : }
    8413             : static GEN
    8414          56 : search_levels(GEN vN, const char *f)
    8415             : {
    8416          56 :   switch(typ(vN))
    8417             :   {
    8418          21 :     case t_INT: vN = mkvecsmall(itos(vN)); break;
    8419          35 :     case t_VEC: case t_COL: vN = ZV_to_zv(vN); break;
    8420           0 :     case t_VECSMALL: vN = leafcopy(vN); break;
    8421           0 :     default: pari_err_TYPE(f, vN);
    8422             :   }
    8423          56 :   vecsmall_sort(vN); return vN;
    8424             : }
    8425             : GEN
    8426          28 : mfsearch(GEN NK, GEN V, long space)
    8427             : {
    8428          28 :   pari_sp av = avma;
    8429             :   GEN F, gk, NbyD, vN;
    8430             :   long n, nk, dk, parity, nV, i, lvN;
    8431             : 
    8432          28 :   if (typ(NK) != t_VEC || lg(NK) != 3) pari_err_TYPE("mfsearch", NK);
    8433          28 :   gk = gel(NK,2);
    8434          28 :   if (typ(gmul2n(gk, 1)) != t_INT) pari_err_TYPE("mfsearch [k]", gk);
    8435          28 :   switch(typ(V))
    8436             :   {
    8437          28 :     case t_VEC: V = shallowtrans(V);
    8438          28 :     case t_COL: break;
    8439           0 :     default: pari_err_TYPE("mfsearch [V]", V);
    8440             :   }
    8441          28 :   vN = search_levels(gel(NK,1), "mfsearch [N]");
    8442          28 :   if (gequal0(V)) { set_avma(av); retmkvec(mftrivial()); }
    8443          14 :   lvN = lg(vN);
    8444             : 
    8445          14 :   Qtoss(gk, &nk,&dk);
    8446          14 :   parity = (dk == 1 && odd(nk)) ? -1 : 1;
    8447          14 :   nV = lg(V)-2;
    8448          14 :   F = cgetg(1, t_VEC);
    8449          14 :   NbyD = const_vec(vN[lvN-1], cgetg(1,t_VECSMALL));
    8450         231 :   for (n = 1; n < lvN; n++)
    8451             :   {
    8452         217 :     long N = vN[n];
    8453             :     GEN L;
    8454         217 :     if (N <= 0 || (dk == 2 && (N & 3))) continue;
    8455         217 :     L = findd(N, parity);
    8456         630 :     for (i = 1; i < lg(L); i++)
    8457             :     {
    8458         413 :       GEN mf, M, CO, gD = gel(L,i);
    8459         413 :       GEN *ND = (GEN*)NbyD + itou(gD); /* points to NbyD[|D|] */
    8460             : 
    8461         413 :       if (seenD(N, *ND)) continue;
    8462         413 :       mf = mfinit_Nndkchi(N, nk, dk, get_mfchar(gD), space, 1);
    8463         413 :       M = mfcoefs_mf(mf, nV, 1);
    8464         413 :       CO = inverseimage(M, V); if (lg(CO) == 1) continue;
    8465             : 
    8466          42 :       F = vec_append(F, mflinear(mf,CO));
    8467          42 :       *ND = vecsmall_append(*ND, N); /* add to NbyD[|D|] */
    8468             :     }
    8469             :   }
    8470          14 :   return gerepilecopy(av, F);
    8471             : }
    8472             : 
    8473             : static GEN
    8474         882 : search_from_split(GEN mf, GEN vap, GEN vlp)
    8475             : {
    8476         882 :   pari_sp av = avma;
    8477         882 :   long lvlp = lg(vlp), j, jv, l1;
    8478         882 :   GEN v, NK, S1, S, M = NULL;
    8479             : 
    8480         882 :   S1 = gel(split_i(mf, 1, 0), 1); /* rational newforms */
    8481         882 :   l1 = lg(S1);
    8482         882 :   if (l1 == 1) return gc_NULL(av);
    8483         448 :   v = cgetg(l1, t_VEC);
    8484         448 :   S = MF_get_S(mf);
    8485         448 :   NK = mf_get_NK(gel(S,1));
    8486         448 :   if (lvlp > 1) M = rowpermute(mfcoefs_mf(mf, vlp[lvlp-1], 1), vlp);
    8487         966 :   for (j = jv = 1; j < l1; j++)
    8488             :   {
    8489         518 :     GEN vF = gel(S1,j);
    8490             :     long t;
    8491         651 :     for (t = lvlp-1; t > 0; t--)
    8492             :     { /* lhs = vlp[j]-th coefficient of eigenform */
    8493         595 :       GEN rhs = gel(vap,t), lhs = RgMrow_RgC_mul(M, vF, t);
    8494         595 :       if (!gequal(lhs, rhs)) break;
    8495             :     }
    8496         518 :     if (!t) gel(v,jv++) = mflinear_i(NK,S,vF);
    8497             :   }
    8498         448 :   if (jv == 1) return gc_NULL(av);
    8499          56 :   setlg(v,jv); return v;
    8500             : }
    8501             : GEN
    8502          28 : mfeigensearch(GEN NK, GEN AP)
    8503             : {
    8504          28 :   pari_sp av = avma;
    8505          28 :   GEN k, vN, vap, vlp, vres = cgetg(1, t_VEC), D;
    8506             :   long n, lvN, i, l, even;
    8507             : 
    8508          28 :   if (!AP) l = 1;
    8509             :   else
    8510             :   {
    8511          28 :     l = lg(AP);
    8512          28 :     if (typ(AP) != t_VEC) pari_err_TYPE("mfeigensearch",AP);
    8513             :   }
    8514          28 :   vap = cgetg(l, t_VEC);
    8515          28 :   vlp = cgetg(l, t_VECSMALL);
    8516          28 :   if (l > 1)
    8517             :   {
    8518          28 :     GEN perm = indexvecsort(AP, mkvecsmall(1));
    8519          77 :     for (i = 1; i < l; i++)
    8520             :     {
    8521          49 :       GEN v = gel(AP,perm[i]), gp, ap;
    8522          49 :       if (typ(v) != t_VEC || lg(v) != 3) pari_err_TYPE("mfeigensearch", AP);
    8523          49 :       gp = gel(v,1);
    8524          49 :       ap = gel(v,2);
    8525          49 :       if (typ(gp) != t_INT || (typ(ap) != t_INT && typ(ap) != t_INTMOD))
    8526           0 :         pari_err_TYPE("mfeigensearch", AP);
    8527          49 :       gel(vap,i) = ap;
    8528          49 :       vlp[i] = itos(gp)+1; if (vlp[i] < 0) pari_err_TYPE("mfeigensearch", AP);
    8529             :     }
    8530             :   }
    8531          28 :   l = lg(NK);
    8532          28 :   if (typ(NK) != t_VEC || l != 3) pari_err_TYPE("mfeigensearch",NK);
    8533          28 :   k = gel(NK,2);
    8534          28 :   vN = search_levels(gel(NK,1), "mfeigensearch [N]");
    8535          28 :   lvN = lg(vN);
    8536          28 :   vecsmall_sort(vlp);
    8537          28 :   even = !mpodd(k);
    8538         966 :   for (n = 1; n < lvN; n++)
    8539             :   {
    8540         938 :     pari_sp av2 = avma;
    8541             :     GEN mf, L;
    8542         938 :     long N = vN[n];
    8543         938 :     if (even) D = gen_1;
    8544             :     else
    8545             :     {
    8546         112 :       long r = (N&3L);
    8547         112 :       if (r == 1 || r == 2) continue;
    8548          56 :       D = stoi( corediscs(-N, NULL) ); /* < 0 */
    8549             :     }
    8550         882 :     mf = mfinit_i(mkvec3(utoipos(N), k, D), mf_NEW);
    8551         882 :     L = search_from_split(mf, vap, vlp);
    8552         882 :     if (L) vres = shallowconcat(vres, L); else set_avma(av2);
    8553             :   }
    8554          28 :   return gerepilecopy(av, vres);
    8555             : }
    8556             : 
    8557             : /* tf_{N,k}(n) */
    8558             : static GEN
    8559     4036215 : mfnewtracecache(long N, long k, long n, cachenew_t *cache)
    8560             : {
    8561     4036215 :   GEN C = NULL, S;
    8562             :   long lcache;
    8563     4036215 :   if (!n) return gen_0;
    8564     3904118 :   S = gel(cache->vnew,N);
    8565     3904118 :   lcache = lg(S);
    8566     3904118 :   if (n < lcache) C = gel(S, n);
    8567     3904118 :   if (C) cache->newHIT++;
    8568     2377607 :   else C = mfnewtrace_i(N,k,n,cache);
    8569     3904118 :   cache->newTOTAL++;
    8570     3904118 :   if (n < lcache) gel(S,n) = C;
    8571     3904118 :   return C;
    8572             : }
    8573             : 
    8574             : static long
    8575        1393 : mfdim_Nkchi(long N, long k, GEN CHI, long space)
    8576             : {
    8577        1393 :   if (k < 0 || badchar(N,k,CHI)) return 0;
    8578        1092 :   if (k == 0)
    8579          35 :     return mfcharistrivial(CHI) && !space_is_cusp(space)? 1: 0;
    8580        1057 :   switch(space)
    8581             :   {
    8582         245 :     case mf_NEW: return mfnewdim(N,k,CHI);
    8583         196 :     case mf_CUSP:return mfcuspdim(N,k,CHI);
    8584         168 :     case mf_OLD: return mfolddim(N,k,CHI);
    8585         217 :     case mf_FULL:return mffulldim(N,k,CHI);
    8586         231 :     case mf_EISEN: return mfeisensteindim(N,k,CHI);
    8587           0 :     default: pari_err_FLAG("mfdim");
    8588             :   }
    8589             :   return 0;/*LCOV_EXCL_LINE*/
    8590             : }
    8591             : static long
    8592        2114 : mf1dimsum(long N, long space)
    8593             : {
    8594        2114 :   switch(space)
    8595             :   {
    8596        1050 :     case mf_NEW:  return mf1newdimsum(N);
    8597        1057 :     case mf_CUSP: return mf1cuspdimsum(N);
    8598           7 :     case mf_OLD:  return mf1olddimsum(N);
    8599             :   }
    8600           0 :   pari_err_FLAG("mfdim");
    8601             :   return 0; /*LCOV_EXCL_LINE*/
    8602             : }
    8603             : /* mfdim for k = nk/dk */
    8604             : static long
    8605       44744 : mfdim_Nndkchi(long N, long nk, long dk, GEN CHI, long space)
    8606       43463 : { return (dk == 2)? mf2dim_Nkchi(N, nk >> 1, CHI, space)
    8607       88186 :                   : mfdim_Nkchi(N, nk, CHI, space); }
    8608             : /* FIXME: use direct dim Gamma1(N) formula, don't compute individual spaces */
    8609             : static long
    8610         252 : mfkdimsum(long N, long k, long dk, long space)
    8611             : {
    8612         252 :   GEN w = mfchars(N, k, dk, NULL);
    8613         252 :   long i, j, D = 0, l = lg(w);
    8614        1239 :   for (i = j = 1; i < l; i++)
    8615             :   {
    8616         987 :     GEN CHI = gel(w,i);
    8617         987 :     long d = mfdim_Nndkchi(N,k,dk,CHI,space);
    8618         987 :     if (d) D += d * myeulerphiu(mfcharorder(CHI));
    8619             :   }
    8620         252 :   return D;
    8621             : }
    8622             : static GEN
    8623         105 : mf1dims(long N, GEN vCHI, long space)
    8624             : {
    8625         105 :   GEN D = NULL;
    8626         105 :   switch(space)
    8627             :   {
    8628          56 :     case mf_NEW: D = mf1newdimall(N, vCHI); break;
    8629          21 :     case mf_CUSP:D = mf1cuspdimall(N, vCHI); break;
    8630          28 :     case mf_OLD: D = mf1olddimall(N, vCHI); break;
    8631           0 :     default: pari_err_FLAG("mfdim");
    8632             :   }
    8633         105 :   return D;
    8634             : }
    8635             : static GEN
    8636        2961 : mfkdims(long N, long k, long dk, GEN vCHI, long space)
    8637             : {
    8638        2961 :   GEN D, w = mfchars(N, k, dk, vCHI);
    8639        2961 :   long i, j, l = lg(w);
    8640        2961 :   D = cgetg(l, t_VEC);
    8641       46592 :   for (i = j = 1; i < l; i++)
    8642             :   {
    8643       43631 :     GEN CHI = gel(w,i);
    8644       43631 :     long d = mfdim_Nndkchi(N,k,dk,CHI,space);
    8645       43631 :     if (vCHI)
    8646         574 :       gel(D, j++) = mkvec2s(d, 0);
    8647       43057 :     else if (d)
    8648        2520 :       gel(D, j++) = fmt_dim(CHI, d, 0);
    8649             :   }
    8650        2961 :   setlg(D,j); return D;
    8651             : }
    8652             : GEN
    8653        5719 : mfdim(GEN NK, long space)
    8654             : {
    8655        5719 :   pari_sp av = avma;
    8656             :   long N, k, dk, joker;
    8657             :   GEN CHI, mf;
    8658        5719 :   if ((mf = checkMF_i(NK))) return utoi(MF_get_dim(mf));
    8659        5586 :   checkNK2(NK, &N, &k, &dk, &CHI, 2);
    8660        5586 :   if (!CHI) joker = 1;
    8661             :   else
    8662        2611 :     switch(typ(CHI))
    8663             :     {
    8664        2373 :       case t_INT: joker = 2; break;
    8665         112 :       case t_COL: joker = 3; break;
    8666         126 :       default: joker = 0; break;
    8667             :     }
    8668        5586 :   if (joker)
    8669             :   {
    8670             :     long d;
    8671             :     GEN D;
    8672        5460 :     if (k < 0) switch(joker)
    8673             :     {
    8674           0 :       case 1: return cgetg(1,t_VEC);
    8675           7 :       case 2: return gen_0;
    8676           0 :       case 3: return mfdim0all(CHI);
    8677             :     }
    8678        5453 :     if (k == 0)
    8679             :     {
    8680          28 :       if (space_is_cusp(space)) switch(joker)
    8681             :       {
    8682           7 :         case 1: return cgetg(1,t_VEC);
    8683           0 :         case 2: return gen_0;
    8684           7 :         case 3: return mfdim0all(CHI);
    8685             :       }
    8686          14 :       switch(joker)
    8687             :       {
    8688             :         long i, l;
    8689           7 :         case 1: retmkvec(fmt_dim(mfchartrivial(),0,0));
    8690           0 :         case 2: return gen_1;
    8691           7 :         case 3: l = lg(CHI); D = cgetg(l,t_VEC);
    8692          35 :                 for (i = 1; i < l; i++)
    8693             :                 {
    8694          28 :                   long t = mfcharistrivial(gel(CHI,i));
    8695          28 :                   gel(D,i) = mkvec2(t? gen_1: gen_0, gen_0);
    8696             :                 }
    8697           7 :                 return D;
    8698             :       }
    8699             :     }
    8700        5425 :     if (dk == 1 && k == 1 && space != mf_EISEN)
    8701         105 :     {
    8702        2219 :       long fix = 0, space0 = space;
    8703        2219 :       if (space == mf_FULL) space = mf_CUSP; /* remove Eisenstein part */
    8704        2219 :       if (joker == 2)
    8705             :       {
    8706        2114 :         d = mf1dimsum(N, space);
    8707        2114 :         if (space0 == mf_FULL) d += mfkdimsum(N,k,dk,mf_EISEN);/*add it back*/
    8708        2114 :         set_avma(av); return utoi(d);
    8709             :       }
    8710             :       /* must initialize explicitly: trivial spaces for E_k/S_k differ */
    8711         105 :       if (space0 == mf_FULL)
    8712             :       {
    8713           7 :         if (!CHI) fix = 1; /* must remove 0 spaces */
    8714           7 :         CHI = mfchars(N, k, dk, CHI);
    8715             :       }
    8716         105 :       D = mf1dims(N, CHI, space);
    8717         105 :       if (space0 == mf_FULL)
    8718             :       {
    8719           7 :         GEN D2 = mfkdims(N, k, dk, CHI, mf_EISEN);
    8720           7 :         D = merge_dims(D, D2, fix? CHI: NULL);
    8721             :       }
    8722             :     }
    8723             :     else
    8724             :     {
    8725        3206 :       if (joker==2) { d = mfkdimsum(N,k,dk,space); set_avma(av); return utoi(d); }
    8726        2954 :       D = mfkdims(N, k, dk, CHI, space);
    8727             :     }
    8728        3059 :     if (!CHI) return gerepileupto(av, vecsort(D, mkvecsmall(1)));
    8729         105 :     return gerepilecopy(av, D);
    8730             :   }
    8731         126 :   return utoi( mfdim_Nndkchi(N, k, dk, CHI, space) );
    8732             : }
    8733             : 
    8734             : GEN
    8735         315 : mfbasis(GEN NK, long space)
    8736             : {
    8737         315 :   pari_sp av = avma;
    8738             :   long N, k, dk;
    8739             :   GEN mf, CHI;
    8740         315 :   if ((mf = checkMF_i(NK))) return gconcat(gel(mf,2), gel(mf,3));
    8741           7 :   checkNK2(NK, &N, &k, &dk, &CHI, 0);
    8742           7 :   if (dk == 2) return gerepilecopy(av, mf2basis(N, k>>1, CHI, NULL, space));
    8743           7 :   mf = mfinit_Nkchi(N, k, CHI, space, 1);
    8744           7 :   return gerepilecopy(av, MF_get_basis(mf));
    8745             : }
    8746             : 
    8747             : static GEN
    8748          49 : deg1ser_shallow(GEN a1, GEN a0, long v, long e)
    8749          49 : { return RgX_to_ser(deg1pol_shallow(a1, a0, v), e+2); }
    8750             : /* r / x + O(1) */
    8751             : static GEN
    8752          49 : simple_pole(GEN r)
    8753             : {
    8754          49 :   GEN S = deg1ser_shallow(gen_0, r, 0, 1);
    8755          49 :   setvalp(S, -1); return S;
    8756             : }
    8757             : 
    8758             : /* F form, E embedding; mfa = mfatkininit or root number (eigenform case) */
    8759             : static GEN
    8760         161 : mflfuncreate(GEN mfa, GEN F, GEN E, GEN N, GEN gk)
    8761             : {
    8762         161 :   GEN LF = cgetg(8,t_VEC), polar = cgetg(1,t_COL), eps;
    8763         161 :   long k = itou(gk);
    8764         161 :   gel(LF,1) = lfuntag(t_LFUN_MFCLOS, mkvec3(F,E,gen_1));
    8765         161 :   if (typ(mfa) != t_VEC)
    8766          98 :     eps = mfa; /* cuspidal eigenform: root number; no poles */
    8767             :   else
    8768             :   { /* mfatkininit */
    8769          63 :     GEN a0, b0, vF, vG, G = NULL;
    8770          63 :     GEN M = gel(mfa,2), C = gel(mfa,3), mf = gel(mfa,4);
    8771          63 :     M = gdiv(mfmatembed(E, M), C);
    8772          63 :     vF = mfvecembed(E, mftobasis_i(mf, F));
    8773          63 :     vG = RgM_RgC_mul(M, vF);
    8774          63 :     if (gequal(vF,vG)) eps = gen_1;
    8775          49 :     else if (gequal(vF,gneg(vG))) eps = gen_m1;
    8776             :     else
    8777             :     { /* not self-dual */
    8778          42 :       eps = NULL;
    8779          42 :       G = mfatkin(mfa, F);
    8780          42 :       gel(LF,2) = lfuntag(t_LFUN_MFCLOS, mkvec3(G,E,ginv(C)));
    8781          42 :       gel(LF,6) = powIs(k);
    8782             :     }
    8783             :     /* polar part */
    8784          63 :     a0 = mfembed(E, mfcoef(F,0));
    8785          63 :     b0 = eps? gmul(eps,a0): gdiv(mfembed(E, mfcoef(G,0)), C);
    8786          63 :     if (!gequal0(b0))
    8787             :     {
    8788          28 :       b0 = mulcxpowIs(gmul2n(b0,1), k);
    8789          28 :       polar = vec_append(polar, mkvec2(gk, simple_pole(b0)));
    8790             :     }
    8791          63 :     if (!gequal0(a0))
    8792             :     {
    8793          21 :       a0 = gneg(gmul2n(a0,1));
    8794          21 :       polar = vec_append(polar, mkvec2(gen_0, simple_pole(a0)));
    8795             :     }
    8796             :   }
    8797         161 :   if (eps) /* self-dual */
    8798             :   {
    8799         119 :     gel(LF,2) = mfcharorder(mf_get_CHI(F)) <= 2? gen_0: gen_1;
    8800         119 :     gel(LF,6) = mulcxpowIs(eps,k);
    8801             :   }
    8802         161 :   gel(LF,3) = mkvec2(gen_0, gen_1);
    8803         161 :   gel(LF,4) = gk;
    8804         161 :   gel(LF,5) = N;
    8805         161 :   if (lg(polar) == 1) setlg(LF,7); else gel(LF,7) = polar;
    8806         161 :   return LF;
    8807             : }
    8808             : static GEN
    8809         133 : mflfuncreateall(long sd, GEN mfa, GEN F, GEN vE, GEN gN, GEN gk)
    8810             : {
    8811         133 :   long i, l = lg(vE);
    8812         133 :   GEN L = cgetg(l, t_VEC);
    8813         294 :   for (i = 1; i < l; i++)
    8814         161 :     gel(L,i) = mflfuncreate(sd? gel(mfa,i): mfa, F, gel(vE,i), gN, gk);
    8815         133 :   return L;
    8816             : }
    8817             : GEN
    8818          84 : lfunmf(GEN mf, GEN F, long bitprec)
    8819             : {
    8820          84 :   pari_sp av = avma;
    8821          84 :   long i, l, prec = nbits2prec(bitprec);
    8822             :   GEN L, gk, gN;
    8823          84 :   mf = checkMF(mf);
    8824          84 :   gk = MF_get_gk(mf);
    8825          84 :   gN = MF_get_gN(mf);
    8826          84 :   if (typ(gk)!=t_INT) pari_err_IMPL("half-integral weight");
    8827          84 :   if (F)
    8828             :   {
    8829             :     GEN v;
    8830          77 :     long s = MF_get_space(mf);
    8831          77 :     if (!checkmf_i(F)) pari_err_TYPE("lfunmf", F);
    8832          77 :     if (!mfisinspace_i(mf, F)) err_space(F);
    8833          77 :     L = NULL;
    8834          77 :     if ((s == mf_NEW || s == mf_CUSP || s == mf_FULL)
    8835          63 :         && gequal(mfcoefs_i(F,1,1), mkvec2(gen_0,gen_1)))
    8836             :     { /* check if eigenform */
    8837          35 :       GEN vP, vF, b = mftobasis_i(mf, F);
    8838          35 :       long lF, d = degpol(mf_get_field(F));
    8839          35 :       v = mfsplit(mf, d, 0);
    8840          35 :       vF = gel(v,1);
    8841          35 :       vP = gel(v,2); lF = lg(vF);
    8842          35 :       for (i = 1; i < lF; i++)
    8843          28 :         if (degpol(gel(vP,i)) == d && gequal(gel(vF,i), b))
    8844             :         {
    8845          28 :           GEN vE = mfgetembed(F, prec);
    8846          28 :           GEN Z = mffrickeeigen_i(mf, mkvec(b), mkvec(vE), prec);
    8847          28 :           L = mflfuncreateall(1, gel(Z,1), F, vE, gN, gk);
    8848          28 :           break;
    8849             :         }
    8850             :     }
    8851          77 :     if (!L)
    8852             :     { /* not an eigenform: costly general case */
    8853          49 :       GEN mfa = mfatkininit_i(mf, itou(gN), 1, prec);
    8854          49 :       L = mflfuncreateall(0,mfa, F, mfgetembed(F,prec), gN, gk);
    8855             :     }
    8856          77 :     if (lg(L) == 2) L = gel(L,1);
    8857             :   }
    8858             :   else
    8859             :   {
    8860           7 :     GEN M = mfeigenbasis(mf), vE = mfeigenembed(mf, prec);
    8861           7 :     GEN v = mffrickeeigen(mf, vE, prec);
    8862           7 :     l = lg(vE); L = cgetg(l, t_VEC);
    8863          63 :     for (i = 1; i < l; i++)
    8864          56 :       gel(L,i) = mflfuncreateall(1,gel(v,i), gel(M,i), gel(vE,i), gN, gk);
    8865             :   }
    8866          84 :   return gerepilecopy(av, L);
    8867             : }
    8868             : 
    8869             : GEN
    8870          28 : mffromell(GEN E)
    8871             : {
    8872          28 :   pari_sp av = avma;
    8873             :   GEN mf, F, z, v, S;
    8874             :   long N, i, l;
    8875             : 
    8876          28 :   checkell(E);
    8877          28 :   if (ell_get_type(E) != t_ELL_Q) pari_err_TYPE("mfffromell [E not over Q]", E);
    8878          28 :   N = itos(ellQ_get_N(E));
    8879          28 :   mf = mfinit_i(mkvec2(utoi(N), gen_2), mf_NEW);
    8880          28 :   v = split_i(mf, 1, 0);
    8881          28 :   S = gel(v,1); l = lg(S); /* rational newforms */
    8882          28 :   F = tag(t_MF_ELL, mkNK(N,2,mfchartrivial()), E);
    8883          28 :   z = mftobasis_i(mf, F);
    8884          28 :   for(i = 1; i < l; i++)
    8885          28 :     if (gequal(z, gel(S,i))) break;
    8886          28 :   if (i == l) pari_err_BUG("mffromell [E is not modular]");
    8887          28 :   return gerepilecopy(av, mkvec3(mf, F, z));
    8888             : }
    8889             : 
    8890             : /* returns -1 if not, degree otherwise */
    8891             : long
    8892         140 : polishomogeneous(GEN P)
    8893             : {
    8894             :   long i, D, l;
    8895         140 :   if (typ(P) != t_POL) return 0;
    8896          77 :   D = -1; l = lg(P);
    8897         322 :   for (i = 2; i < l; i++)
    8898             :   {
    8899         245 :     GEN c = gel(P,i);
    8900             :     long d;
    8901         245 :     if (gequal0(c)) continue;
    8902         112 :     d = polishomogeneous(c);
    8903         112 :     if (d < 0) return -1;
    8904         112 :     if (D < 0) D = d + i-2; else if (D != d + i-2) return -1;
    8905             :   }
    8906          77 :   return D;
    8907             : }
    8908             : 
    8909             : /* M a pp((Gram q)^(-1)) ZM; P a homogeneous t_POL, is P spherical ? */
    8910             : static int
    8911          28 : RgX_isspherical(GEN M, GEN P)
    8912             : {
    8913          28 :   pari_sp av = avma;
    8914          28 :   GEN S, v = variables_vecsmall(P);
    8915          28 :   long i, j, l = lg(v);
    8916          28 :   if (l > lg(M)) pari_err(e_MISC, "too many variables in mffromqf");
    8917          21 :   S = gen_0;
    8918          63 :   for (j = 1; j < l; j++)
    8919             :   {
    8920          42 :     GEN Mj = gel(M, j), Pj = deriv(P, v[j]);
    8921         105 :     for (i = 1; i <= j; i++)
    8922             :     {
    8923          63 :       GEN c = gel(Mj, i);
    8924          63 :       if (!signe(c)) continue;
    8925          42 :       if (i != j) c = shifti(c, 1);
    8926          42 :       S = gadd(S, gmul(c, deriv(Pj, v[i])));
    8927             :     }
    8928             :   }
    8929          21 :   return gc_bool(av, gequal0(S));
    8930             : }
    8931             : 
    8932             : static GEN
    8933          49 : c_QFsimple_i(long n, GEN Q, GEN P)
    8934             : {
    8935          49 :   GEN V, v = qfrep0(Q, utoi(n), 1);
    8936          49 :   long i, l = lg(v);
    8937          49 :   V = cgetg(l+1, t_VEC);
    8938          91 :   if (!P || equali1(P))
    8939             :   {
    8940          42 :     gel(V,1) = gen_1;
    8941         420 :     for (i = 2; i <= l; i++) gel(V,i) = utoi(v[i-1] << 1);
    8942             :   }
    8943             :   else
    8944             :   {
    8945           7 :     gel(V,1) = gcopy(P);
    8946           7 :     for (i = 2; i <= l; i++) gel(V,i) = gmulgs(P, v[i-1] << 1);
    8947             :   }
    8948          49 :   return V;
    8949             : }
    8950             : 
    8951             : /* v a t_VECSMALL of variable numbers, lg(r) >= lg(v), r is a vector of
    8952             :  * scalars [not involving any variable in v] */
    8953             : static GEN
    8954          14 : gsubstvec_i(GEN e, GEN v, GEN r)
    8955             : {
    8956          14 :   long i, l = lg(v);
    8957          42 :   for(i = 1; i < l; i++) e = gsubst(e, v[i], gel(r,i));
    8958          14 :   return e;
    8959             : }
    8960             : static GEN
    8961          56 : c_QF_i(long n, GEN Q, GEN P)
    8962             : {
    8963          56 :   pari_sp av = avma;
    8964             :   GEN V, v, va;
    8965             :   long i, l;
    8966          56 :   if (!P || typ(P) != t_POL) return gerepileupto(av, c_QFsimple_i(n, Q, P));
    8967           7 :   v = gel(minim(Q, utoi(2*n), NULL), 3);
    8968           7 :   va = variables_vecsmall(P);
    8969           7 :   V = zerovec(n + 1); l = lg(v);
    8970          21 :   for (i = 1; i < l; i++)
    8971             :   {
    8972          14 :     pari_sp av = avma;
    8973          14 :     GEN X = gel(v,i);
    8974          14 :     long c = (itos(qfeval(Q, X)) >> 1) + 1;
    8975          14 :     gel(V, c) = gerepileupto(av, gadd(gel(V, c), gsubstvec_i(P, va, X)));
    8976             :   }
    8977           7 :   return gmul2n(V, 1);
    8978             : }
    8979             : 
    8980             : GEN
    8981          77 : mffromqf(GEN Q, GEN P)
    8982             : {
    8983          77 :   pari_sp av = avma;
    8984             :   GEN G, Qi, F, D, N, mf, v, gk, chi;
    8985             :   long m, d, space;
    8986          77 :   if (typ(Q) != t_MAT) pari_err_TYPE("mffromqf", Q);
    8987          77 :   if (!RgM_is_ZM(Q) || !qfiseven(Q))
    8988           0 :     pari_err_TYPE("mffromqf [not integral or even]", Q);
    8989          77 :   m = lg(Q)-1;
    8990          77 :   Qi = ZM_inv(Q, &N);
    8991          77 :   if (!qfiseven(Qi)) N = shifti(N, 1);
    8992          77 :   d = 0;
    8993          77 :   if (!P || gequal1(P)) P = NULL;
    8994             :   else
    8995             :   {
    8996          35 :     P = simplify_shallow(P);
    8997          35 :     if (typ(P) == t_POL)
    8998             :     {
    8999          28 :       d = polishomogeneous(P);
    9000          28 :       if (d < 0) pari_err_TYPE("mffromqf [not homogeneous t_POL]", P);
    9001          28 :       if (!RgX_isspherical(Qi, P))
    9002           7 :         pari_err_TYPE("mffromqf [not a spherical t_POL]", P);
    9003             :     }
    9004             :   }
    9005          63 :   gk = sstoQ(m + 2*d, 2);
    9006          63 :   D = ZM_det(Q);
    9007          63 :   if (!odd(m)) { if ((m & 3) == 2) D = negi(D); } else D = shifti(D, 1);
    9008          63 :   space = d > 0 ? mf_CUSP : mf_FULL;
    9009          63 :   G = znstar0(N,1);
    9010          63 :   chi = mkvec2(G, znchar_quad(G,D));
    9011          63 :   mf = mfinit(mkvec3(N, gk, chi), space);
    9012          63 :   if (odd(d))
    9013             :   {
    9014           7 :     F = mftrivial();
    9015           7 :     v = zerocol(MF_get_dim(mf));
    9016             :   }
    9017             :   else
    9018             :   {
    9019          56 :     F = c_QF_i(mfsturm(mf), Q, P);
    9020          56 :     v = mftobasis_i(mf, F);
    9021          56 :     F = mflinear(mf, v);
    9022             :   }
    9023          63 :   return gerepilecopy(av, mkvec3(mf, F, v));
    9024             : }
    9025             : 
    9026             : /***********************************************************************/
    9027             : /*                          Eisenstein Series                          */
    9028             : /***********************************************************************/
    9029             : /* \sigma_{k-1}(\chi,n) */
    9030             : static GEN
    9031       24192 : sigchi(long k, GEN CHI, long n)
    9032             : {
    9033       24192 :   pari_sp av = avma;
    9034       24192 :   GEN S = gen_1, D = mydivisorsu(u_ppo(n,mfcharmodulus(CHI)));
    9035       24192 :   long i, l = lg(D), ord = mfcharorder(CHI), vt = varn(mfcharpol(CHI));
    9036       83671 :   for (i = 2; i < l; i++) /* skip D[1] = 1 */
    9037             :   {
    9038       59479 :     long d = D[i], a = mfcharevalord(CHI, d, ord);
    9039       59479 :     S = gadd(S, Qab_Czeta(a, ord, powuu(d, k-1), vt));
    9040             :   }
    9041       24192 :   return gerepileupto(av,S);
    9042             : }
    9043             : 
    9044             : /* write n = n0*n1*n2, (n0,N1*N2) = 1, n1 | N1^oo, n2 | N2^oo;
    9045             :  * return NULL if (n,N1,N2) > 1, else return factoru(n0) */
    9046             : static GEN
    9047      636601 : sigchi2_dec(long n, long N1, long N2, long *pn1, long *pn2)
    9048             : {
    9049      636601 :   GEN P0, E0, P, E, fa = myfactoru(n);
    9050             :   long i, j, l;
    9051      636601 :   *pn1 = 1;
    9052      636601 :   *pn2 = 1;
    9053      636601 :   if (N1 == 1 && N2 == 1) return fa;
    9054      623112 :   P = gel(fa,1); l = lg(P);
    9055      623112 :   E = gel(fa,2);
    9056      623112 :   P0 = cgetg(l, t_VECSMALL);
    9057      623112 :   E0 = cgetg(l, t_VECSMALL);
    9058     1441419 :   for (i = j = 1; i < l; i++)
    9059             :   {
    9060      922985 :     long p = P[i], e = E[i];
    9061      922985 :     if (N1 % p == 0)
    9062             :     {
    9063      140553 :       if (N2 % p == 0) return NULL;
    9064       35875 :       *pn1 *= upowuu(p,e);
    9065             :     }
    9066      782432 :     else if (N2 % p == 0)
    9067      125594 :       *pn2 *= upowuu(p,e);
    9068      656838 :     else { P0[j] = p; E0[j] = e; j++; }
    9069             :   }
    9070      518434 :   setlg(P0, j);
    9071      518434 :   setlg(E0, j); return mkvec2(P0,E0);
    9072             : }
    9073             : 
    9074             : /* sigma_{k-1}(\chi_1,\chi_2,n), ord multiple of lcm(ord(CHI1),ord(CHI2)) */
    9075             : static GEN
    9076      582113 : sigchi2(long k, GEN CHI1, GEN CHI2, long n, long ord)
    9077             : {
    9078      582113 :   pari_sp av = avma;
    9079             :   GEN S, D;
    9080      582113 :   long i, l, n1, n2, vt, N1 = mfcharmodulus(CHI1), N2 = mfcharmodulus(CHI2);
    9081      582113 :   D = sigchi2_dec(n, N1, N2, &n1, &n2); if (!D) return gc_const(av, gen_0);
    9082      482104 :   D = divisorsu_fact(D); l = lg(D);
    9083      482104 :   vt = varn(mfcharpol(CHI1));
    9084     2075815 :   for (i = 1, S = gen_0; i < l; i++)
    9085             :   { /* S += d^(k-1)*chi1(d)*chi2(n/d) */
    9086     1593711 :     long a, d = n2*D[i], nd = n1*D[l-i]; /* (d,N1)=1; (n/d,N2) = 1 */
    9087     1593711 :     a = mfcharevalord(CHI1, d, ord) + mfcharevalord(CHI2, nd, ord);
    9088     1593711 :     if (a >= ord) a -= ord;
    9089     1593711 :     S = gadd(S, Qab_Czeta(a, ord, powuu(d, k-1), vt));
    9090             :   }
    9091      482104 :   return gerepileupto(av, S);
    9092             : }
    9093             : 
    9094             : /**************************************************************************/
    9095             : /**           Dirichlet characters with precomputed values               **/
    9096             : /**************************************************************************/
    9097             : /* CHI mfchar */
    9098             : static GEN
    9099       13230 : mfcharcxinit(GEN CHI, long prec)
    9100             : {
    9101       13230 :   GEN G = gel(CHI,1), chi = gel(CHI,2), z, V;
    9102       13230 :   GEN v = ncharvecexpo(G, znconrey_normalized(G,chi));
    9103       13230 :   long n, l = lg(v), o = mfcharorder(CHI);
    9104       13230 :   V = cgetg(l, t_VEC);
    9105       13230 :   z = grootsof1(o, prec); /* Mod(t, Phi_o(t)) -> e(1/o) */
    9106      120085 :   for (n = 1; n < l; n++) gel(V,n) = v[n] < 0? gen_0: gel(z, v[n]+1);
    9107       13230 :   return mkvecn(6, G, chi, gmfcharorder(CHI), v, V, mfcharpol(CHI));
    9108             : }
    9109             : /* v a "CHIvec" */
    9110             : static long
    9111    23969960 : CHIvec_N(GEN v) { return itou(znstar_get_N(gel(v,1))); }
    9112             : static GEN
    9113       14406 : CHIvec_CHI(GEN v)
    9114       14406 : { return mkvec4(gel(v,1), gel(v,2), gel(v,3), gel(v,6)); }
    9115             : /* character order */
    9116             : static long
    9117       33929 : CHIvec_ord(GEN v) { return itou(gel(v,3)); }
    9118             : /* character exponents, i.e. t such that chi(n) = e(t) */
    9119             : static GEN
    9120      416829 : CHIvec_expo(GEN v) { return gel(v,4); }
    9121             : /* character values chi(n) */
    9122             : static GEN
    9123    23334311 : CHIvec_val(GEN v) { return gel(v,5); }
    9124             : /* CHI(n) */
    9125             : static GEN
    9126    23324910 : mychareval(GEN v, long n)
    9127             : {
    9128    23324910 :   long N = CHIvec_N(v), ind = n%N;
    9129    23324910 :   if (ind <= 0) ind += N;
    9130    23324910 :   return gel(CHIvec_val(v), ind);
    9131             : }
    9132             : /* return c such that CHI(n) = e(c / ordz) or -1 if (n,N) > 1 */
    9133             : static long
    9134      416829 : mycharexpo(GEN v, long n)
    9135             : {
    9136      416829 :   long N = CHIvec_N(v), ind = n%N;
    9137      416829 :   if (ind <= 0) ind += N;
    9138      416829 :   return CHIvec_expo(v)[ind];
    9139             : }
    9140             : /* faster than mfcharparity */
    9141             : static long
    9142       53823 : CHIvec_parity(GEN v) { return mycharexpo(v,-1) ? -1: 1; }
    9143             : /**************************************************************************/
    9144             : 
    9145             : static ulong
    9146       54488 : sigchi2_Fl(long k, GEN CHI1vec, GEN CHI2vec, long n, GEN vz, ulong p)
    9147             : {
    9148       54488 :   pari_sp av = avma;
    9149       54488 :   long ordz = lg(vz)-2, i, l, n1, n2;
    9150       54488 :   ulong S = 0;
    9151       54488 :   GEN D = sigchi2_dec(n, CHIvec_N(CHI1vec), CHIvec_N(CHI2vec), &n1, &n2);
    9152       54488 :   if (!D) return gc_ulong(av,S);
    9153       49819 :   D = divisorsu_fact(D);
    9154       49819 :   l = lg(D);
    9155      169470 :   for (i = 1; i < l; i++)
    9156             :   { /* S += d^(k-1)*chi1(d)*chi2(n/d) */
    9157      119651 :     long a, d = n2*D[i], nd = n1*D[l-i]; /* (d,N1)=1, (n/d,N2)=1 */
    9158      119651 :     a = mycharexpo(CHI2vec, nd) + mycharexpo(CHI1vec, d);
    9159      119651 :     if (a >= ordz) a -= ordz;
    9160      119651 :     S = Fl_add(S, Qab_Czeta_Fl(a, vz, Fl_powu(d,k-1,p), p), p);
    9161             :   }
    9162       49819 :   return gc_ulong(av,S);
    9163             : }
    9164             : 
    9165             : /**********************************************************************/
    9166             : /* Fourier expansions of Eisenstein series                            */
    9167             : /**********************************************************************/
    9168             : /* L(CHI_t,0) / 2, CHI_t(n) = CHI(n)(t/n) as a character modulo N*t,
    9169             :  * order(CHI) | ord != 0 */
    9170             : static GEN
    9171        2555 : charLFwt1(long N, GEN CHI, long ord, long t)
    9172             : {
    9173             :   GEN S;
    9174             :   long r, vt;
    9175             : 
    9176        2555 :   if (N == 1 && t == 1) return mkfrac(gen_m1,stoi(4));
    9177        2555 :   S = gen_0; vt = varn(mfcharpol(CHI));
    9178      289611 :   for (r = 1; r < N; r++)
    9179             :   { /* S += r*chi(r) */
    9180             :     long a, c;
    9181      287056 :     if (ugcd(N,r) != 1) continue;
    9182      230272 :     a = mfcharevalord(CHI,r,ord);
    9183      230272 :     c = (t != 1 && kross(t, r) < 0)? -r: r;
    9184      230272 :     S = gadd(S, Qab_Czeta(a, ord, stoi(c), vt));
    9185             :   }
    9186        2555 :   return gdivgs(S, -2*N);
    9187             : }
    9188             : /* L(CHI,0) / 2, mod p */
    9189             : static ulong
    9190        1960 : charLFwt1_Fl(GEN CHIvec, GEN vz, ulong p)
    9191             : {
    9192        1960 :   long r, m = CHIvec_N(CHIvec);
    9193             :   ulong S;
    9194        1960 :   if (m == 1) return Rg_to_Fl(mkfrac(gen_m1,stoi(4)), p);
    9195        1960 :   S = 0;
    9196       95746 :   for (r = 1; r < m; r++)
    9197             :   { /* S += r*chi(r) */
    9198       93786 :     long a = mycharexpo(CHIvec,r);
    9199       93786 :     if (a < 0) continue;
    9200       91490 :     S = Fl_add(S, Qab_Czeta_Fl(a, vz, r, p), p);
    9201             :   }
    9202        1960 :   return Fl_div(Fl_neg(S,p), 2*m, p);
    9203             : }
    9204             : /* L(CHI_t,1-k) / 2, CHI_t(n) = CHI(n) * (t/n), order(CHI) | ord != 0;
    9205             :  * assume conductor of CHI_t divides N */
    9206             : static GEN
    9207        4095 : charLFwtk(long N, long k, GEN CHI, long ord, long t)
    9208             : {
    9209             :   GEN S, P, dS;
    9210             :   long r, vt;
    9211             : 
    9212        4095 :   if (k == 1) return charLFwt1(N, CHI, ord, t);
    9213        1540 :   if (N == 1 && t == 1) return gdivgs(bernfrac(k),-2*k);
    9214         924 :   S = gen_0; vt = varn(mfcharpol(CHI));
    9215         924 :   P = ZX_rescale(Q_remove_denom(bernpol(k,0), &dS), utoi(N));
    9216         924 :   dS = mul_denom(dS, stoi(-2*N*k));
    9217       13398 :   for (r = 1; r < N; r++)
    9218             :   { /* S += P(r)*chi(r) */
    9219             :     long a;
    9220             :     GEN C;
    9221       12474 :     if (ugcd(r,N) != 1) continue;
    9222       10108 :     a = mfcharevalord(CHI,r,ord);
    9223       10108 :     C = ZX_Z_eval(P, utoi(r));
    9224       10108 :     if (t != 1 && kross(t, r) < 0) C = gneg(C);
    9225       10108 :     S = gadd(S, Qab_Czeta(a, ord, C, vt));
    9226             :   }
    9227         924 :   return gdiv(S, dS);
    9228             : }
    9229             : /* L(CHI,1-k) / 2, mod p */
    9230             : static ulong
    9231        2723 : charLFwtk_Fl(long k, GEN CHIvec, GEN vz, ulong p)
    9232             : {
    9233             :   GEN P;
    9234             :   long r, m;
    9235             :   ulong S;
    9236        2723 :   if (k == 1) return charLFwt1_Fl(CHIvec, vz, p);
    9237         763 :   m = CHIvec_N(CHIvec);
    9238         763 :   if (m == 1) return Rg_to_Fl(gdivgs(bernfrac(k),-2*k), p);
    9239         448 :   S = 0;
    9240         448 :   P = RgX_to_Flx(RgX_rescale(bernpol(k,0), utoi(m)), p);
    9241       10038 :   for (r = 1; r < m; r++)
    9242             :   { /* S += P(r)*chi(r) */
    9243        9590 :     long a = mycharexpo(CHIvec,r);
    9244        9590 :     if (a < 0) continue;
    9245        8470 :     S = Fl_add(S, Qab_Czeta_Fl(a, vz, Flx_eval(P,r,p), p), p);
    9246             :   }
    9247         448 :   return Fl_div(Fl_neg(S,p), 2*k*m, p);
    9248             : }
    9249             : 
    9250             : static GEN
    9251        7469 : mfeisenstein2_0(long k, GEN CHI1, GEN CHI2, long ord)
    9252             : {
    9253        7469 :   long N1 = mfcharmodulus(CHI1), N2 = mfcharmodulus(CHI2);
    9254        7469 :   if (k == 1 && N1 == 1) return charLFwtk(N2, 1, CHI2, ord, 1);
    9255        4921 :   if (N2 == 1) return charLFwtk(N1, k, CHI1, ord, 1);
    9256        3584 :   return gen_0;
    9257             : }
    9258             : static ulong
    9259        4137 : mfeisenstein2_0_Fl(long k, GEN CHI1vec, GEN CHI2vec, GEN vz, ulong p)
    9260             : {
    9261        4137 :   if (k == 1 && CHIvec_N(CHI1vec) == 1)
    9262        1960 :     return charLFwtk_Fl(k, CHI2vec, vz, p);
    9263        2177 :   else if (CHIvec_N(CHI2vec) == 1)
    9264         763 :     return charLFwtk_Fl(k, CHI1vec, vz, p);
    9265        1414 :   else return 0;
    9266             : }
    9267             : static GEN
    9268         133 : NK_eisen2(long k, GEN CHI1, GEN CHI2, long ord)
    9269             : {
    9270         133 :   long o, N = mfcharmodulus(CHI1)*mfcharmodulus(CHI2);
    9271         133 :   GEN CHI = mfcharmul(CHI1, CHI2);
    9272         133 :   o = mfcharorder(CHI);
    9273         133 :   if ((ord & 3) == 2) ord >>= 1;
    9274         133 :   if ((o & 3) == 2) o >>= 1;
    9275         133 :   if (ord != o) pari_err_IMPL("mfeisenstein for these characters");
    9276         126 :   return mkNK(N, k, CHI);
    9277             : }
    9278             : static GEN
    9279         364 : mfeisenstein_i(long k, GEN CHI1, GEN CHI2)
    9280             : {
    9281         364 :   long s = 1, ord, vt;
    9282             :   GEN E0, NK, vchi, T;
    9283         364 :   if (CHI2) { CHI2 = get_mfchar(CHI2); if (mfcharparity(CHI2) < 0) s = -s; }
    9284         364 :   if (CHI1) { CHI1 = get_mfchar(CHI1); if (mfcharparity(CHI1) < 0) s = -s; }
    9285         350 :   if (s != m1pk(k)) return mftrivial();
    9286         329 :   if (!CHI1) CHI1 = mfchartrivial();
    9287         329 :   if (!CHI2)
    9288             :   { /* E_k(chi1) */
    9289         196 :     vt = varn(mfcharpol(CHI1));
    9290         196 :     ord = mfcharorder(CHI1);
    9291         196 :     NK = mkNK(mfcharmodulus(CHI1), k, CHI1);
    9292         196 :     E0 = charLFwtk(mfcharmodulus(CHI1), k, CHI1, ord, 1);
    9293         196 :     vchi = mkvec3(E0, mkvec(mfcharpol(CHI1)), CHI1);
    9294         196 :     return tag(t_MF_EISEN, NK, vchi);
    9295             :   }
    9296             :   /* E_k(chi1,chi2) */
    9297         133 :   vt = varn(mfcharpol(CHI1));
    9298         133 :   ord = ulcm(mfcharorder(CHI1), mfcharorder(CHI2));
    9299         133 :   NK = NK_eisen2(k, CHI1, CHI2, ord);
    9300         126 :   E0 = mfeisenstein2_0(k, CHI1, CHI2, ord);
    9301         126 :   T = mkvec(polcyclo(ord, vt));
    9302         126 :   vchi = mkvec4(E0, T, CHI1, CHI2);
    9303         126 :   return tag2(t_MF_EISEN, NK, vchi, mkvecsmall2(ord,0));
    9304             : }
    9305             : GEN
    9306         364 : mfeisenstein(long k, GEN CHI1, GEN CHI2)
    9307             : {
    9308         364 :   pari_sp av = avma;
    9309         364 :   if (k < 1) pari_err_DOMAIN("mfeisenstein", "k", "<", gen_1, stoi(k));
    9310         364 :   return gerepilecopy(av, mfeisenstein_i(k, CHI1, CHI2));
    9311             : }
    9312             : 
    9313             : static GEN
    9314        2450 : mfeisenstein2all(long N0, GEN NK, long k, GEN CHI1, GEN CHI2, GEN T, long o)
    9315             : {
    9316        2450 :   GEN E, E0 = mfeisenstein2_0(k, CHI1,CHI2, o), vchi = mkvec4(E0, T, CHI1,CHI2);
    9317        2450 :   long j, d = (lg(T)==4)? itou(gmael(T,3,1)): 1;
    9318        2450 :   E = cgetg(d+1, t_VEC);
    9319        5019 :   for (j=1; j<=d; j++) gel(E,j) = tag2(t_MF_EISEN, NK,vchi,mkvecsmall2(o,j-1));
    9320        2450 :   return mfbdall(E, N0 / mf_get_N(gel(E,1)));
    9321             : }
    9322             : 
    9323             : /* list of characters on G = (Z/NZ)^*, v[i] = NULL if (i,N) > 1, else
    9324             :  * the conductor of Conrey label i, [conductor, primitive char].
    9325             :  * Trivial chi (label 1) comes first */
    9326             : static GEN
    9327        1099 : zncharsG(GEN G)
    9328             : {
    9329        1099 :   long i, l, N = itou(znstar_get_N(G));
    9330             :   GEN vCHI, V;
    9331        1099 :   if (N == 1) return mkvec2(gen_1,cgetg(1,t_COL));
    9332        1099 :   vCHI = const_vec(N,NULL);
    9333        1099 :   V = cyc2elts(znstar_get_conreycyc(G));
    9334        1099 :   l = lg(V);
    9335      204393 :   for (i = 1; i < l; i++)
    9336             :   {
    9337      203294 :     GEN chi0, chi = zc_to_ZC(gel(V,i)), n, F;
    9338      203294 :     F = znconreyconductor(G, chi, &chi0);
    9339      203294 :     if (typ(F) != t_INT) F = gel(F,1);
    9340      203294 :     n = znconreyexp(G, chi);
    9341      203294 :     gel(vCHI, itos(n)) = mkvec2(chi0, F);
    9342             :   }
    9343        1099 :   return vCHI;
    9344             : }
    9345             : 
    9346             : /* CHI primitive, f(CHI) | N. Return pairs (CHI1,CHI2) both primitive
    9347             :  * such that f(CHI1)*f(CHI2) | N and CHI1 * CHI2 = CHI;
    9348             :  * if k = 1, CHI1 is even; if k = 2, omit (1,1) if CHI = 1 */
    9349             : static GEN
    9350        1316 : mfeisensteinbasis_i(long N0, long k, GEN CHI)
    9351             : {
    9352        1316 :   GEN G = gel(CHI,1), chi = gel(CHI,2), vT = const_vec(myeulerphiu(N0), NULL);
    9353        1316 :   GEN CHI0, GN, chiN, Lchi, LG, V, RES, NK, T, C = mfcharpol(CHI);
    9354        1316 :   long i, j, l, n, n1, N, ord = mfcharorder(CHI);
    9355        1316 :   long F = mfcharmodulus(CHI), vt = varn(mfcharpol(CHI));
    9356             : 
    9357        1316 :   CHI0 = (F == 1)? CHI: mfchartrivial();
    9358        1316 :   j = 1; RES = cgetg(N0+1, t_VEC);
    9359        1316 :   T = gel(vT,ord) = Qab_trace_init(ord, ord, C, C);
    9360        1316 :   if (F != 1 || k != 2)
    9361             :   { /* N1 = 1 */
    9362        1162 :     NK = mkNK(F, k, CHI);
    9363        1162 :     gel(RES, j++) = mfeisenstein2all(N0, NK, k, CHI0, CHI, T, ord);
    9364        1162 :     if (F != 1 && k != 1)
    9365         273 :       gel(RES, j++) = mfeisenstein2all(N0, NK, k, CHI, CHI0, T, ord);
    9366             :   }
    9367        1316 :   if (N0 == 1) { setlg(RES,j); return RES; }
    9368        1246 :   GN = G; chiN = chi;
    9369        1246 :   if (F == N0) N = N0;
    9370             :   else
    9371             :   {
    9372         679 :     GEN faN = myfactoru(N0), P = gel(faN,1), E = gel(faN,2);
    9373         679 :     long lP = lg(P);
    9374        1771 :     for (i = N = 1; i < lP; i++)
    9375             :     {
    9376        1092 :       long p = P[i];
    9377        1092 :       N *= upowuu(p, maxuu(E[i]/2, z_lval(F,p)));
    9378             :     }
    9379         679 :     if ((N & 3) == 2) N >>= 1;
    9380         679 :     if (N == 1) { setlg(RES,j); return RES; }
    9381         532 :     if (F != N)
    9382             :     {
    9383         126 :       GN = znstar0(utoipos(N),1);
    9384         126 :       chiN = zncharinduce(G, chi, GN);
    9385             :     }
    9386             :   }
    9387        1099 :   LG = const_vec(N, NULL); /* LG[d] = znstar(d,1) or NULL */
    9388        1099 :   gel(LG,1) = gel(CHI0,1);
    9389        1099 :   gel(LG,F) = G;
    9390        1099 :   gel(LG,N) = GN;
    9391        1099 :   Lchi = coprimes_zv(N);
    9392        1099 :   n = itou(znconreyexp(GN,chiN));
    9393        1099 :   V = zncharsG(GN); l = lg(V);
    9394      256963 :   for (n1 = 2; n1 < l; n1++) /* skip 1 (trivial char) */
    9395             :   {
    9396      255864 :     GEN v = gel(V,n1), w, chi1, chi2, G1, G2, CHI1, CHI2;
    9397             :     long N12, N1, N2, no, o12, t, m;
    9398      255864 :     if (!Lchi[n1] || n1 == n) continue; /* skip trivial chi2 */
    9399      201061 :     chi1 = gel(v,1); N1 = itou(gel(v,2)); /* conductor of chi1 */
    9400      201061 :     w = gel(V, Fl_div(n,n1,N));
    9401      201061 :     chi2 = gel(w,1); N2 = itou(gel(w,2)); /* conductor of chi2 */
    9402      201061 :     N12 = N1 * N2;
    9403      201061 :     if (N0 % N12) continue;
    9404             : 
    9405        1715 :     G1 = gel(LG,N1); if (!G1) gel(LG,N1) = G1 = znstar0(utoipos(N1), 1);
    9406        1715 :     if (k == 1 && zncharisodd(G1,chi1)) continue;
    9407        1015 :     G2 = gel(LG,N2); if (!G2) gel(LG,N2) = G2 = znstar0(utoipos(N2), 1);
    9408        1015 :     CHI1 = mfcharGL(G1, chi1);
    9409        1015 :     CHI2 = mfcharGL(G2, chi2);
    9410        1015 :     o12 = ulcm(mfcharorder(CHI1), mfcharorder(CHI2));
    9411             :     /* remove Galois orbit: same trace */
    9412        1015 :     no = Fl_powu(n1, ord, N);
    9413        1386 :     for (t = 1+ord, m = n1; t <= o12; t += ord)
    9414             :     { /* m <-> CHI1^t, if t in Gal(Q(chi1,chi2)/Q), omit (CHI1^t,CHI2^t) */
    9415         371 :       m = Fl_mul(m, no, N); if (!m) break;
    9416         371 :       if (ugcd(t, o12) == 1) Lchi[m] = 0;
    9417             :     }
    9418        1015 :     T = gel(vT,o12);
    9419        1015 :     if (!T) T = gel(vT,o12) = Qab_trace_init(o12, ord, polcyclo(o12,vt), C);
    9420        1015 :     NK = mkNK(N12, k, CHI);
    9421        1015 :     gel(RES, j++) = mfeisenstein2all(N0, NK, k, CHI1, CHI2, T, o12);
    9422             :   }
    9423        1099 :   setlg(RES,j); return RES;
    9424             : }
    9425             : 
    9426             : static GEN
    9427         721 : mfbd_E2(GEN E2, long d, GEN CHI)
    9428             : {
    9429         721 :   GEN E2d = mfbd_i(E2, d);
    9430         721 :   GEN F = mkvec2(E2, E2d), L = mkvec2(gen_1, utoineg(d));
    9431             :   /* cannot use mflinear_i: E2 and E2d do not have the same level */
    9432         721 :   return tag3(t_MF_LINEAR, mkNK(d,2,CHI), F, L, gen_1);
    9433             : }
    9434             : /* C-basis of E_k(Gamma_0(N),chi). If k = 1, the first basis element must not
    9435             :  * vanish at oo [used in mf1basis]. Here E_1(CHI), whose q^0 coefficient
    9436             :  * does not vanish (since L(CHI,0) does not) *if* CHI is not trivial; which
    9437             :  * must be the case in weight 1.
    9438             :  *
    9439             :  * (k>=3): In weight k >= 3, basis is B(d) E(CHI1,(CHI/CHI1)_prim), where
    9440             :  * CHI1 is primitive modulo N1, and if N2 is the conductor of CHI/CHI1
    9441             :  * then d*N1*N2 | N.
    9442             :  * (k=2): In weight k=2, same if CHI is nontrivial. If CHI is trivial, must
    9443             :  * not take CHI1 trivial, and must add E_2(tau)-dE_2(d tau)), where
    9444             :  * d|N, d > 1.
    9445             :  * (k=1): In weight k=1, same as k >= 3 except that we restrict to CHI1 even */
    9446             : static GEN
    9447        1344 : mfeisensteinbasis(long N, long k, GEN CHI)
    9448             : {
    9449             :   long i, F;
    9450             :   GEN L;
    9451        1344 :   if (badchar(N, k, CHI)) return cgetg(1, t_VEC);
    9452        1344 :   if (k == 0) return mfcharistrivial(CHI)? mkvec(mf1()): cgetg(1, t_VEC);
    9453        1316 :   CHI = mfchartoprimitive(CHI, &F);
    9454        1316 :   L = mfeisensteinbasis_i(N, k, CHI);
    9455        1316 :   if (F == 1 && k == 2)
    9456             :   {
    9457         154 :     GEN v, E2 = mfeisenstein(2, NULL, NULL), D = mydivisorsu(N);
    9458         154 :     long nD = lg(D)-1;
    9459         154 :     v = cgetg(nD, t_VEC); L = vec_append(L,v);
    9460         868 :     for (i = 1; i < nD; i++) gel(v,i) = mfbd_E2(E2, D[i+1], CHI);
    9461             :   }
    9462        1316 :   return lg(L) == 1? L: shallowconcat1(L);
    9463             : }
    9464             : 
    9465             : static GEN
    9466          77 : not_in_space(GEN F, long flag)
    9467             : {
    9468          77 :   if (!flag) err_space(F);
    9469          70 :   return cgetg(1, t_COL);
    9470             : }
    9471             : /* when flag set, no error */
    9472             : GEN
    9473         819 : mftobasis(GEN mf, GEN F, long flag)
    9474             : {
    9475         819 :   pari_sp av2, av = avma;
    9476             :   GEN G, v, y, gk;
    9477         819 :   long N, B, ismf = checkmf_i(F);
    9478             : 
    9479         819 :   mf = checkMF(mf);
    9480         819 :   if (ismf)
    9481             :   {
    9482         728 :     if (mfistrivial(F)) return zerocol(MF_get_dim(mf));
    9483         721 :     if (!mf_same_k(mf, F) || !mf_same_CHI(mf, F)) return not_in_space(F, flag);
    9484             :   }
    9485         770 :   N = MF_get_N(mf);
    9486         770 :   gk = MF_get_gk(mf);
    9487         770 :   if (ismf)
    9488             :   {
    9489         679 :     long NF = mf_get_N(F);
    9490         679 :     B = maxuu(mfsturmNgk(NF,gk), mfsturmNgk(N,gk)) + 1;
    9491         679 :     v = mfcoefs_i(F,B,1);
    9492             :   }
    9493             :   else
    9494             :   {
    9495          91 :     B = mfsturmNgk(N, gk) + 1;
    9496          91 :     switch(typ(F))
    9497             :     { /* F(0),...,F(lg(v)-2) */
    9498          63 :       case t_SER: v = sertocol(F); settyp(v,t_VEC); break;
    9499          14 :       case t_VEC: v = F; break;
    9500           7 :       case t_COL: v = shallowtrans(F); break;
    9501           7 :       default: pari_err_TYPE("mftobasis",F);
    9502             :                v = NULL;/*LCOV_EXCL_LINE*/
    9503             :     }
    9504          84 :     if (flag) B = minss(B, lg(v)-2);
    9505             :   }
    9506         763 :   y = mftobasis_i(mf, v);
    9507         763 :   if (typ(y) == t_VEC)
    9508             :   {
    9509          21 :     if (flag) return gerepilecopy(av, y);
    9510           0 :     pari_err(e_MISC, "not enough coefficients in mftobasis");
    9511             :   }
    9512         742 :   av2 = avma;
    9513         742 :   if (MF_get_space(mf) == mf_FULL || mfsturm(mf)+1 == B) return y;
    9514         287 :   G = mflinear(mf, y);
    9515         287 :   if (!gequal(v, mfcoefs_i(G, lg(v)-2,1))) y = NULL;
    9516         287 :   if (!y) { set_avma(av); return not_in_space(F, flag); }
    9517         252 :   set_avma(av2); return gerepileupto(av, y);
    9518             : }
    9519             : 
    9520             : /* assume N > 0; first cusp is always 0 */
    9521             : static GEN
    9522          49 : mfcusps_i(long N)
    9523             : {
    9524             :   long i, c, l;
    9525             :   GEN D, v;
    9526             : 
    9527          49 :   if (N == 1) return mkvec(gen_0);
    9528          49 :   D = mydivisorsu(N); l = lg(D); /* left on stack */
    9529          49 :   c = mfnumcuspsu_fact(myfactoru(N));
    9530          49 :   v = cgetg(c + 1, t_VEC);
    9531         350 :   for (i = c = 1; i < l; i++)
    9532             :   {
    9533         301 :     long C = D[i], NC = D[l-i], lima = ugcd(C, NC), A0, A;
    9534         889 :     for (A0 = 0; A0 < lima; A0++)
    9535         588 :       if (ugcd(A0, lima) == 1)
    9536             :       {
    9537         539 :         A = A0; while (ugcd(A,C) > 1) A += lima;
    9538         392 :         gel(v, c++) = sstoQ(A, C);
    9539             :       }
    9540             :   }
    9541          49 :   return v;
    9542             : }
    9543             : /* List of cusps of Gamma_0(N) */
    9544             : GEN
    9545          28 : mfcusps(GEN gN)
    9546             : {
    9547             :   long N;
    9548             :   GEN mf;
    9549          28 :   if (typ(gN) == t_INT) N = itos(gN);
    9550          14 :   else if ((mf = checkMF_i(gN))) N = MF_get_N(mf);
    9551           0 :   else { pari_err_TYPE("mfcusps", gN); N = 0; }
    9552          28 :   if (N <= 0) pari_err_DOMAIN("mfcusps", "N", "<=", gen_0, stoi(N));
    9553          28 :   return mfcusps_i(N);
    9554             : }
    9555             : 
    9556             : long
    9557         315 : mfcuspisregular(GEN NK, GEN cusp)
    9558             : {
    9559             :   long v, N, dk, nk, t, o;
    9560             :   GEN mf, CHI, go, A, C, g, c, d;
    9561         315 :   if ((mf = checkMF_i(NK)))
    9562             :   {
    9563          49 :     GEN gk = MF_get_gk(mf);
    9564          49 :     N = MF_get_N(mf);
    9565          49 :     CHI = MF_get_CHI(mf);
    9566          49 :     Qtoss(gk, &nk, &dk);
    9567             :   }
    9568             :   else
    9569         266 :     checkNK2(NK, &N, &nk, &dk, &CHI, 0);
    9570         315 :   if (typ(cusp) == t_INFINITY) return 1;
    9571         315 :   if (typ(cusp) == t_FRAC) { A = gel(cusp,1); C = gel(cusp,2); }
    9572          28 :   else { A = cusp; C = gen_1; }
    9573         315 :   g = diviuexact(mului(N,C), ugcd(N, Fl_sqr(umodiu(C,N), N)));
    9574         315 :   c = mulii(negi(C),g);
    9575         315 :   d = addiu(mulii(A,g), 1);
    9576         315 :   if (!CHI) return 1;
    9577         315 :   go = gmfcharorder(CHI);
    9578         315 :   v = vali(go); if (v < 2) go = shifti(go, 2-v);
    9579         315 :   t = itou( znchareval(gel(CHI,1), gel(CHI,2), d, go) );
    9580         315 :   if (dk == 1) return t == 0;
    9581         154 :   o = itou(go);
    9582         154 :   if (kronecker(c,d) < 0) t = Fl_add(t, o/2, o);
    9583         154 :   if (Mod4(d) == 1) return t == 0;
    9584          14 :   t = Fl_sub(t, Fl_mul(o/4, nk, o), o);
    9585          14 :   return t == 0;
    9586             : }
    9587             : 
    9588             : /* Some useful closures */
    9589             : 
    9590             : /* sum_{d|n} d^k */
    9591             : static GEN
    9592       37660 : mysumdivku(ulong n, ulong k)
    9593             : {
    9594       37660 :   GEN fa = myfactoru(n);
    9595       37660 :   return k == 1? usumdiv_fact(fa): usumdivk_fact(fa,k);
    9596             : }
    9597             : static GEN
    9598         812 : c_Ek(long n, long d, GEN F)
    9599             : {
    9600         812 :   GEN E = cgetg(n + 2, t_VEC), C = gel(F,2);
    9601         812 :   long i, k = mf_get_k(F);
    9602         812 :   gel (E, 1) = gen_1;
    9603       25270 :   for (i = 1; i <= n; i++)
    9604             :   {
    9605       24458 :     pari_sp av = avma;
    9606       24458 :     gel(E, i+1) = gerepileupto(av, gmul(C, mysumdivku(i*d, k-1)));
    9607             :   }
    9608         812 :   return E;
    9609             : }
    9610             : 
    9611             : GEN
    9612         364 : mfEk(long k)
    9613             : {
    9614         364 :   pari_sp av = avma;
    9615             :   GEN E0, NK;
    9616         364 :   if (k < 0 || odd(k)) pari_err_TYPE("mfEk [incorrect k]", stoi(k));
    9617         364 :   if (!k) return mf1();
    9618         357 :   E0 = gdivsg(-2*k, bernfrac(k));
    9619         357 :   NK = mkNK(1,k,mfchartrivial());
    9620         357 :   return gerepilecopy(av, tag(t_MF_Ek, NK, E0));
    9621             : }
    9622             : 
    9623             : GEN
    9624          56 : mfDelta(void)
    9625             : {
    9626          56 :   pari_sp av = avma;
    9627          56 :   return gerepilecopy(av, tag0(t_MF_DELTA, mkNK(1,12,mfchartrivial())));
    9628             : }
    9629             : 
    9630             : GEN
    9631         749 : mfTheta(GEN psi)
    9632             : {
    9633         749 :   pari_sp av = avma;
    9634             :   GEN N, gk, psi2;
    9635             :   long par;
    9636         749 :   if (!psi) { psi = mfchartrivial(); N = utoipos(4); par = 1; }
    9637             :   else
    9638             :   {
    9639             :     long FC;
    9640          21 :     psi = get_mfchar(psi);
    9641          21 :     FC = mfcharconductor(psi);
    9642          21 :     if (mfcharmodulus(psi) != FC)
    9643           0 :       pari_err_TYPE("mfTheta [nonprimitive character]", psi);
    9644          21 :     par = mfcharparity(psi);
    9645          21 :     N = shifti(sqru(FC),2);
    9646             :   }
    9647         749 :   if (par > 0) { gk = ghalf; psi2 = psi; }
    9648           7 :   else { gk = gsubsg(2, ghalf); psi2 = mfcharmul(psi, get_mfchar(stoi(-4))); }
    9649         749 :   return gerepilecopy(av, tag(t_MF_THETA, mkgNK(N, gk, psi2, pol_x(1)), psi));
    9650             : }
    9651             : 
    9652             : /* Output 0 if not desired eta product: if flag=0 (default) require
    9653             :  * holomorphic at cusps. If flag set, accept meromorphic, but sill in some
    9654             :  * modular function space */
    9655             : GEN
    9656         210 : mffrometaquo(GEN eta, long flag)
    9657             : {
    9658         210 :   pari_sp av = avma;
    9659             :   GEN NK, N, k, BR, P;
    9660         210 :   long v, cusp = 0;
    9661         210 :   if (!etaquotype(&eta, &N,&k,&P, &v, NULL, flag? NULL: &cusp) || cusp < 0)
    9662          14 :     return gc_const(av, gen_0);
    9663         196 :   if (lg(gel(eta,1)) == 1) { set_avma(av); return mf1(); }
    9664         189 :   BR = mkvec2(ZV_to_zv(gel(eta,1)), ZV_to_zv(gel(eta,2)));
    9665         189 :   if (v < 0) v = 0;
    9666         189 :   NK = mkgNK(N, k, get_mfchar(P), pol_x(1));
    9667         189 :   return gerepilecopy(av, tag2(t_MF_ETAQUO, NK, BR, utoi(v)));
    9668             : }
    9669             : 
    9670             : /* Q^(-r) */
    9671             : static GEN
    9672         375 : RgXn_negpow(GEN Q, long r, long L)
    9673             : {
    9674         375 :   if (r < 0) r = -r; else Q = RgXn_inv_i(Q, L);
    9675         375 :   if (r != 1) Q = RgXn_powu_i(Q, r, L);
    9676         375 :   return Q;
    9677             : }
    9678             : /* flag same as in mffrometaquo: if set, accept meromorphic. */
    9679             : static GEN
    9680          49 : mfisetaquo_i(GEN F, long flag)
    9681             : {
    9682             :   GEN gk, P, E, M, S, G, CHI, v, w;
    9683             :   long b, l, L, N, vS, m, j;
    9684          49 :   const long bextra = 10;
    9685             : 
    9686          49 :   if (!checkmf_i(F)) pari_err_TYPE("mfisetaquo",F);
    9687          49 :   CHI = mf_get_CHI(F); if (mfcharorder(CHI) > 2) return NULL;
    9688          49 :   N = mf_get_N(F);
    9689          49 :   gk = mf_get_gk(F);
    9690          49 :   b = mfsturmNgk(N, gk);
    9691          49 :   L = maxss(N, b) + bextra;
    9692          49 :   S = mfcoefs_i(F, L, 1);
    9693          49 :   if (!RgV_is_ZV(S)) return NULL;
    9694         889 :   for (vS = 1; vS <= L+1; vS++)
    9695         889 :     if (signe(gel(S,vS))) break;
    9696          49 :   vS--;
    9697          49 :   if (vS >= bextra - 1) { L += vS; S = mfcoefs_i(F, L, 1); }
    9698          49 :   if (vS) { S = vecslice(S, vS+1, L+1); L -= vS; }
    9699          49 :   S = RgV_to_RgX(S, 0); l = lg(S)-2;
    9700          49 :   P = cgetg(l, t_COL);
    9701          49 :   E = cgetg(l, t_COL); w = v = gen_0; /* w = weight, v = valuation */
    9702        1908 :   for (m = j = 1; m+2 < lg(S); m++)
    9703             :   {
    9704        1866 :     GEN c = gel(S,m+2);
    9705             :     long r;
    9706        1866 :     if (is_bigint(c)) return NULL;
    9707        1859 :     r = -itos(c);
    9708        1859 :     if (r)
    9709             :     {
    9710         375 :       S = ZXn_mul(S, RgXn_negpow(eta_ZXn(m, L), r, L), L);
    9711         375 :       gel(P,j) = utoipos(m);
    9712         375 :       gel(E,j) = stoi(r);
    9713         375 :       v = addmuliu(v, gel(E,j), m);
    9714         375 :       w = addis(w, r);
    9715         375 :       j++;
    9716             :     }
    9717             :   }
    9718          42 :   if (!equalii(w, gmul2n(gk, 1)) || (!flag && !equalii(v, muluu(24,vS))))
    9719           7 :     return NULL;
    9720          35 :   setlg(P, j);
    9721          35 :   setlg(E, j); M = mkmat2(P, E); G = mffrometaquo(M, flag);
    9722          35 :   return (typ(G) != t_INT
    9723          35 :           && (mfsturmmf(G) <= b + bextra || mfisequal(F, G, b)))? M: NULL;
    9724             : }
    9725             : GEN
    9726          49 : mfisetaquo(GEN F, long flag)
    9727             : {
    9728          49 :   pari_sp av = avma;
    9729          49 :   GEN M = mfisetaquo_i(F, flag);
    9730          49 :   return M? gerepilecopy(av, M): gc_const(av, gen_0);
    9731             : }
    9732             : 
    9733             : #if 0
    9734             : /* number of primitive characters modulo N */
    9735             : static ulong
    9736             : numprimchars(ulong N)
    9737             : {
    9738             :   GEN fa, P, E;
    9739             :   long i, l;
    9740             :   ulong n;
    9741             :   if ((N & 3) == 2) return 0;
    9742             :   fa = myfactoru(N);
    9743             :   P = gel(fa,1); l = lg(P);
    9744             :   E = gel(fa,2);
    9745             :   for (i = n = 1; i < l; i++)
    9746             :   {
    9747             :     ulong p = P[i], e = E[i];
    9748             :     if (e == 2) n *= p-2; else n *= (p-1)*(p-1)*upowuu(p,e-2);
    9749             :   }
    9750             :   return n;
    9751             : }
    9752             : #endif
    9753             : 
    9754             : /* Space generated by products of two Eisenstein series */
    9755             : 
    9756             : static int
    9757       73122 : cmp_small_priority(void *E, GEN a, GEN b)
    9758             : {
    9759       73122 :   GEN prio = (GEN)E;
    9760       73122 :   return cmpss(prio[(long)a], prio[(long)b]);
    9761             : }
    9762             : static long
    9763        1148 : znstar_get_expo(GEN G) { return itou(cyc_get_expo(znstar_get_cyc(G))); }
    9764             : 
    9765             : /* Return [vchi, bymod, vG]:
    9766             :  * vG[f] = znstar(f,1) for f a conductor of (at least) a char mod N; else NULL
    9767             :  * bymod[f] = vecsmall of conrey indexes of chars modulo f | N; else NULL
    9768             :  * vchi[n] = a list of CHIvec [G0,chi0,o,ncharvecexpo(G0,nchi0),...]:
    9769             :  *   chi0 = primitive char attached to Conrey Mod(n,N)
    9770             :  * (resp. NULL if (n,N) > 1) */
    9771             : static GEN
    9772         574 : charsmodN(long N)
    9773             : {
    9774         574 :   GEN D, G, prio, phio, dummy = cgetg(1,t_VEC);
    9775         574 :   GEN vP, vG = const_vec(N,NULL), vCHI  = const_vec(N,NULL);
    9776         574 :   GEN bymod = const_vec(N,NULL);
    9777         574 :   long pn, i, l, vt = fetch_user_var("t");
    9778         574 :   D = mydivisorsu(N); l = lg(D);
    9779        3598 :   for (i = 1; i < l; i++)
    9780        3024 :     gel(bymod, D[i]) = vecsmalltrunc_init(myeulerphiu(D[i])+1);
    9781         574 :   gel(vG,N) = G = znstar0(utoipos(N),1);
    9782         574 :   pn = znstar_get_expo(G);  /* exponent(Z/NZ)^* */
    9783         574 :   vP = const_vec(pn,NULL);
    9784       26152 :   for (i = 1; i <= N; i++)
    9785             :   {
    9786             :     GEN P, gF, G0, chi0, nchi0, chi, v, go;
    9787             :     long j, F, o;
    9788       25578 :     if (ugcd(i,N) != 1) continue;
    9789       13601 :     chi = znconreylog(G, utoipos(i));
    9790       13601 :     gF = znconreyconductor(G, chi, &chi0);
    9791       13601 :     F = (typ(gF) == t_INT)? itou(gF): itou(gel(gF,1));
    9792       13601 :     G0 = gel(vG, F); if (!G0) G0 = gel(vG,F) = znstar0(gF, 1);
    9793       13601 :     nchi0 = znconreylog_normalize(G0,chi0);
    9794       13601 :     go = gel(nchi0,1); o = itou(go); /* order(chi0) */
    9795       13601 :     v = ncharvecexpo(G0, nchi0);
    9796       13601 :     if (!equaliu(go, pn)) v = zv_z_mul(v, pn / o);
    9797       13601 :     P = gel(vP, o); if (!P) P = gel(vP,o) = polcyclo(o,vt);
    9798             :     /* mfcharcxinit with dummy complex powers */
    9799       13601 :     gel(vCHI,i) = mkvecn(6, G0, chi0, go, v, dummy, P);
    9800       13601 :     D = mydivisorsu(N / F); l = lg(D);
    9801       39179 :     for (j = 1; j < l; j++) vecsmalltrunc_append(gel(bymod, F*D[j]), i);
    9802             :   }
    9803         574 :   phio = zero_zv(pn); l = lg(vCHI); prio = cgetg(l, t_VEC);
    9804       26152 :   for (i = 1; i < l; i++)
    9805             :   {
    9806       25578 :     GEN CHI = gel(vCHI,i);
    9807             :     long o;
    9808       25578 :     if (!CHI) continue;
    9809       13601 :     o = CHIvec_ord(CHI);
    9810       13601 :     if (!phio[o]) phio[o] = myeulerphiu(o);
    9811       13601 :     prio[i] = phio[o];
    9812             :   }
    9813         574 :   l = lg(bymod);
    9814             :   /* sort characters by increasing value of phi(order) */
    9815       26152 :   for (i = 1; i < l; i++)
    9816             :   {
    9817       25578 :     GEN z = gel(bymod,i);
    9818       25578 :     if (z) gen_sort_inplace(z, (void*)prio, &cmp_small_priority, NULL);
    9819             :   }
    9820         574 :   return mkvec3(vCHI, bymod, vG);
    9821             : }
    9822             : 
    9823             : static GEN
    9824        4893 : mfeisenstein2pure(long k, GEN CHI1, GEN CHI2, long ord, GEN P, long lim)
    9825             : {
    9826        4893 :   GEN c, V = cgetg(lim+2, t_COL);
    9827             :   long n;
    9828        4893 :   c = mfeisenstein2_0(k, CHI1, CHI2, ord);
    9829        4893 :   if (P) c = grem(c, P);
    9830        4893 :   gel(V,1) = c;
    9831       97587 :   for (n=1; n <= lim; n++)
    9832             :   {
    9833       92694 :     c = sigchi2(k, CHI1, CHI2, n, ord);
    9834       92694 :     if (P) c = grem(c, P);
    9835       92694 :     gel(V,n+1) = c;
    9836             :   }
    9837        4893 :   return V;
    9838             : }
    9839             : static GEN
    9840        4137 : mfeisenstein2pure_Fl(long k, GEN CHI1vec, GEN CHI2vec, GEN vz, ulong p, long lim)
    9841             : {
    9842        4137 :   GEN V = cgetg(lim+2, t_VECSMALL);
    9843             :   long n;
    9844        4137 :   V[1] = mfeisenstein2_0_Fl(k, CHI1vec, CHI2vec, vz, p);
    9845       58625 :   for (n=1; n <= lim; n++) V[n+1] = sigchi2_Fl(k, CHI1vec, CHI2vec, n, vz, p);
    9846        4137 :   return V;
    9847             : }
    9848             : 
    9849             : static GEN
    9850         224 : getcolswt2(GEN M, GEN D, ulong p)
    9851             : {
    9852         224 :   GEN R, v = gel(M,1);
    9853         224 :   long i, l = lg(M) - 1;
    9854         224 :   R = cgetg(l, t_MAT); /* skip D[1] = 1 */
    9855         896 :   for (i = 1; i < l; i++)
    9856             :   {
    9857         672 :     GEN w = Flv_Fl_mul(gel(M,i+1), D[i+1], p);
    9858         672 :     gel(R,i) = Flv_sub(v, w, p);
    9859             :   }
    9860         224 :   return R;
    9861             : }
    9862             : static GEN
    9863        5131 : expandbd(GEN V, long d)
    9864             : {
    9865             :   long L, n, nd;
    9866             :   GEN W;
    9867        5131 :   if (d == 1) return V;
    9868        1995 :   L = lg(V)-1; W = zerocol(L); /* nd = n/d */
    9869       17521 :   for (n = nd = 0; n < L; n += d, nd++) gel(W, n+1) = gel(V, nd+1);
    9870        1995 :   return W;
    9871             : }
    9872             : static GEN
    9873        6587 : expandbd_Fl(GEN V, long d)
    9874             : {
    9875             :   long L, n, nd;
    9876             :   GEN W;
    9877        6587 :   if (d == 1) return V;
    9878        2450 :   L = lg(V)-1; W = zero_Flv(L); /* nd = n/d */
    9879       15729 :   for (n = nd = 0; n < L; n += d, nd++) W[n+1] = V[nd+1];
    9880        2450 :   return W;
    9881             : }
    9882             : static void
    9883        4137 : getcols_i(GEN *pM, GEN *pvj, GEN gk, GEN CHI1vec, GEN CHI2vec, long NN1, GEN vz,
    9884             :           ulong p, long lim)
    9885             : {
    9886        4137 :   GEN CHI1 = CHIvec_CHI(CHI1vec), CHI2 = CHIvec_CHI(CHI2vec);
    9887        4137 :   long N2 = CHIvec_N(CHI2vec);
    9888        4137 :   GEN vj, M, D = mydivisorsu(NN1/N2);
    9889        4137 :   long i, l = lg(D), k = gk[2];
    9890        4137 :   GEN V = mfeisenstein2pure_Fl(k, CHI1vec, CHI2vec, vz, p, lim);
    9891        4137 :   M = cgetg(l, t_MAT);
    9892       10724 :   for (i = 1; i < l; i++) gel(M,i) = expandbd_Fl(V, D[i]);
    9893        4137 :   if (k == 2 && N2 == 1 && CHIvec_N(CHI1vec) == 1)
    9894             :   {
    9895         224 :     M = getcolswt2(M, D, p); l--;
    9896         224 :     D = vecslice(D, 2, l);
    9897             :   }
    9898        4137 :   *pM = M;
    9899        4137 :   *pvj = vj = cgetg(l, t_VEC);
    9900       10500 :   for (i = 1; i < l; i++) gel(vj,i) = mkvec4(gk, CHI1, CHI2, utoipos(D[i]));
    9901        4137 : }
    9902             : 
    9903             : /* find all CHI1, CHI2 mod N such that CHI1*CHI2 = CHI, f(CHI1)*f(CHI2) | N.
    9904             :  * set M = mfcoefs(B_e E(CHI1,CHI2), lim), vj = [e,i1,i2] */
    9905             : static void
    9906        1687 : getcols(GEN *pM, GEN *pv, long k, long nCHI, GEN allN, GEN vz, ulong p,
    9907             :         long lim)
    9908             : {
    9909        1687 :   GEN vCHI = gel(allN,1), gk = utoi(k);
    9910        1687 :   GEN M = cgetg(1,t_MAT), v = cgetg(1,t_VEC);
    9911        1687 :   long i1, N = lg(vCHI)-1;
    9912       84938 :   for (i1 = 1; i1 <= N; i1++)
    9913             :   {
    9914       83251 :     GEN CHI1vec = gel(vCHI, i1), CHI2vec, M1, v1;
    9915             :     long NN1, i2;
    9916      145859 :     if (!CHI1vec) continue;
    9917       65632 :     if (k == 1 && CHIvec_parity(CHI1vec) == -1) continue;
    9918       40936 :     NN1 = N/CHIvec_N(CHI1vec); /* N/f(chi1) */;
    9919       40936 :     i2 = Fl_div(nCHI,i1, N);
    9920       40936 :     if (!i2) i2 = 1;
    9921       40936 :     CHI2vec = gel(vCHI,i2);
    9922       40936 :     if (NN1 % CHIvec_N(CHI2vec)) continue; /* f(chi1)f(chi2) | N ? */
    9923        3024 :     getcols_i(&M1, &v1, gk, CHI1vec, CHI2vec, NN1, vz, p, lim);
    9924        3024 :     M = shallowconcat(M, M1);
    9925        3024 :     v = shallowconcat(v, v1);
    9926             :   }
    9927        1687 :   *pM = M;
    9928        1687 :   *pv = v;
    9929        1687 : }
    9930             : 
    9931             : static void
    9932        1120 : update_Mj(GEN *M, GEN *vecj, GEN *pz, ulong p)
    9933             : {
    9934             :   GEN perm;
    9935        1120 :   *pz = Flm_indexrank(*M, p); perm = gel(*pz,2);
    9936        1120 :   *M = vecpermute(*M, perm);
    9937        1120 :   *vecj = vecpermute(*vecj, perm);
    9938        1120 : }
    9939             : static int
    9940         357 : getcolsgen(long dim, GEN *pM, GEN *pvj, GEN *pz, long k, long ell, long nCHI,
    9941             :            GEN allN, GEN vz, ulong p, long lim)
    9942             : {
    9943         357 :   GEN vCHI = gel(allN,1), bymod = gel(allN,2), gell = utoi(ell);
    9944         357 :   long i1, N = lg(vCHI)-1;
    9945         357 :   long L = lim+1;
    9946         357 :   if (lg(*pvj)-1 >= dim) update_Mj(pM, pvj, pz, p);
    9947         357 :   if (lg(*pvj)-1 == dim) return 1;
    9948        1526 :   for (i1 = 1; i1 <= N; i1++)
    9949             :   {
    9950        1505 :     GEN CHI1vec = gel(vCHI, i1), T;
    9951             :     long par1, j, l, N1, NN1;
    9952             : 
    9953        1505 :     if (!CHI1vec) continue;
    9954        1484 :     par1 = CHIvec_parity(CHI1vec);
    9955        1484 :     if (ell == 1 && par1 == -1) continue;
    9956         903 :     if (odd(ell)) par1 = -par1;
    9957         903 :     N1 = CHIvec_N(CHI1vec);
    9958         903 :     NN1 = N/N1;
    9959         903 :     T = gel(bymod, NN1); l = lg(T);
    9960        3528 :     for (j = 1; j < l; j++)
    9961             :     {
    9962        2947 :       long i2 = T[j], l1, l2, j1, s, nC;
    9963        2947 :       GEN M, M1, M2, vj, vj1, vj2, CHI2vec = gel(vCHI, i2);
    9964        2947 :       if (CHIvec_parity(CHI2vec) != par1) continue;
    9965        1113 :       nC = Fl_div(nCHI, Fl_mul(i1,i2,N), N);
    9966        1113 :       getcols(&M2, &vj2, k-ell, nC, allN, vz, p, lim);
    9967        1113 :       l2 = lg(M2); if (l2 == 1) continue;
    9968        1113 :       getcols_i(&M1, &vj1, gell, CHI1vec, CHI2vec, NN1, vz, p, lim);
    9969        1113 :       l1 = lg(M1);
    9970        1113 :       M1 = Flm_to_FlxV(M1, 0);
    9971        1113 :       M2 = Flm_to_FlxV(M2, 0);
    9972        1113 :       M  = cgetg((l1-1)*(l2-1) + 1, t_MAT);
    9973        1113 :       vj = cgetg((l1-1)*(l2-1) + 1, t_VEC);
    9974        2737 :       for (j1 = s = 1; j1 < l1; j1++)
    9975             :       {
    9976        1624 :         GEN E = gel(M1,j1), v = gel(vj1,j1);
    9977             :         long j2;
    9978        6811 :         for (j2 = 1; j2 < l2; j2++, s++)
    9979             :         {
    9980        5187 :           GEN c = Flx_to_Flv(Flxn_mul(E, gel(M2,j2), L, p), L);
    9981        5187 :           gel(M,s) = c;
    9982        5187 :           gel(vj,s) = mkvec2(v, gel(vj2,j2));
    9983             :         }
    9984             :       }
    9985        1113 :       *pM = shallowconcat(*pM, M);
    9986        1113 :       *pvj = shallowconcat(*pvj, vj);
    9987        1113 :       if (lg(*pvj)-1 >= dim) update_Mj(pM, pvj, pz, p);
    9988        1113 :       if (lg(*pvj)-1 == dim) return 1;
    9989             :     }
    9990             :   }
    9991          21 :   if (ell == 1)
    9992             :   {
    9993          21 :     update_Mj(pM, pvj, pz, p);
    9994          21 :     return (lg(*pvj)-1 == dim);
    9995             :   }
    9996           0 :   return 0;
    9997             : }
    9998             : 
    9999             : static GEN
   10000        1456 : mkF2bd(long d, long lim)
   10001             : {
   10002        1456 :   GEN V = zerovec(lim + 1);
   10003             :   long n;
   10004        1456 :   gel(V, 1) = ginv(stoi(-24));
   10005       14623 :   for (n = 1; n <= lim/d; n++) gel(V, n*d + 1) = mysumdivku(n, 1);
   10006        1456 :   return V;
   10007             : }
   10008             : 
   10009             : static GEN
   10010        5467 : mkeisen(GEN E, long ord, GEN P, long lim)
   10011             : {
   10012        5467 :   long k = itou(gel(E,1)), e = itou(gel(E,4));
   10013        5467 :   GEN CHI1 = gel(E,2), CHI2 = gel(E,3);
   10014        5467 :   if (k == 2 && mfcharistrivial(CHI1) && mfcharistrivial(CHI2))
   10015         574 :     return gsub(mkF2bd(1,lim), gmulgs(mkF2bd(e,lim), e));
   10016             :   else
   10017             :   {
   10018        4893 :     GEN V = mfeisenstein2pure(k, CHI1, CHI2, ord, P, lim);
   10019        4893 :     return expandbd(V, e);
   10020             :   }
   10021             : }
   10022             : static GEN
   10023         532 : mkM(GEN vj, long pn, GEN P, long lim)
   10024             : {
   10025         532 :   long j, l = lg(vj), L = lim+1;
   10026         532 :   GEN M = cgetg(l, t_MAT);
   10027        4508 :   for (j = 1; j < l; j++)
   10028             :   {
   10029             :     GEN E1, E2;
   10030        3976 :     parse_vecj(gel(vj,j), &E1,&E2);
   10031        3976 :     E1 = RgV_to_RgX(mkeisen(E1, pn, P, lim), 0);
   10032        3976 :     if (E2)
   10033             :     {
   10034        1491 :       E2 = RgV_to_RgX(mkeisen(E2, pn, P, lim), 0);
   10035        1491 :       E1 = RgXn_mul(E1, E2, L);
   10036             :     }
   10037        3976 :     E1 = RgX_to_RgC(E1, L);
   10038        3976 :     if (P && E2) E1 = RgXQV_red(E1, P);
   10039        3976 :     gel(M,j) = E1;
   10040             :   }
   10041         532 :   return M;
   10042             : }
   10043             : 
   10044             : /* assume N > 2 */
   10045             : static GEN
   10046          35 : mffindeisen1(long N)
   10047             : {
   10048          35 :   GEN G = znstar0(utoipos(N), 1), L = chargalois(G, NULL), chi0 = NULL;
   10049          35 :   long j, m = N, l = lg(L);
   10050         259 :   for (j = 1; j < l; j++)
   10051             :   {
   10052         245 :     GEN chi = gel(L,j);
   10053         245 :     long r = myeulerphiu(itou(zncharorder(G,chi)));
   10054         245 :     if (r >= m) continue;
   10055         182 :     chi = znconreyfromchar(G, chi);
   10056         182 :     if (zncharisodd(G,chi)) { m = r; chi0 = chi; if (r == 1) break; }
   10057             :   }
   10058          35 :   if (!chi0) pari_err_BUG("mffindeisen1 [no Eisenstein series found]");
   10059          35 :   chi0 = znchartoprimitive(G,chi0);
   10060          35 :   return mfcharGL(gel(chi0,1), gel(chi0,2));
   10061             : }
   10062             : 
   10063             : static GEN
   10064         574 : mfeisensteinspaceinit_i(long N, long k, GEN CHI)
   10065             : {
   10066         574 :   GEN M, Minv, vj, vG, GN, allN, P, vz, z = NULL;
   10067         574 :   long nCHI, lim, ell, ord, dim = mffulldim(N, k, CHI);
   10068             :   ulong r, p;
   10069             : 
   10070         574 :   if (!dim) retmkvec3(cgetg(1,t_VECSMALL),
   10071             :                       mkvec2(cgetg(1,t_MAT),gen_1),cgetg(1,t_VEC));
   10072         574 :   lim = mfsturmNk(N, k) + 1;
   10073         574 :   allN = charsmodN(N);
   10074         574 :   vG = gel(allN,3);
   10075         574 :   GN = gel(vG,N);
   10076         574 :   ord = znstar_get_expo(GN);
   10077         574 :   P = ord <= 2? NULL: polcyclo(ord, varn(mfcharpol(CHI)));
   10078         574 :   CHI = induce(GN, CHI); /* lift CHI mod N before mfcharno*/
   10079         574 :   nCHI = mfcharno(CHI);
   10080         574 :   r = QabM_init(ord, &p);
   10081         574 :   vz = Fl_powers(r, ord, p);
   10082         574 :   getcols(&M, &vj, k, nCHI, allN, vz, p, lim);
   10083         595 :   for (ell = k>>1; ell >= 1; ell--)
   10084         357 :     if (getcolsgen(dim, &M, &vj, &z, k, ell, nCHI, allN, vz, p, lim)) break;
   10085         574 :   if (!z) update_Mj(&M, &vj, &z, p);
   10086         574 :   if (lg(vj) - 1 < dim) return NULL;
   10087         532 :   M = mkM(vj, ord, P, lim);
   10088         532 :   Minv = QabM_Minv(rowpermute(M, gel(z,1)), P, ord);
   10089         532 :   return mkvec4(gel(z,1), Minv, vj, utoi(ord));
   10090             : }
   10091             : /* true mf */
   10092             : static GEN
   10093         532 : mfeisensteinspaceinit(GEN mf)
   10094             : {
   10095         532 :   pari_sp av = avma;
   10096         532 :   GEN z, CHI = MF_get_CHI(mf);
   10097         532 :   long N = MF_get_N(mf), k = MF_get_k(mf);
   10098         532 :   if (!CHI) CHI = mfchartrivial();
   10099         532 :   z = mfeisensteinspaceinit_i(N, k, CHI);
   10100         532 :   if (!z)
   10101             :   {
   10102          35 :     GEN E, CHIN = mffindeisen1(N), CHI0 = mfchartrivial();
   10103          35 :     z = mfeisensteinspaceinit_i(N, k+1, mfcharmul(CHI, CHIN));
   10104          35 :     if (z) E = mkvec4(gen_1, CHI0, CHIN, gen_1);
   10105             :     else
   10106             :     {
   10107           7 :       z = mfeisensteinspaceinit_i(N, k+2, CHI);
   10108           7 :       E = mkvec4(gen_2, CHI0, CHI0, utoipos(N));
   10109             :     }
   10110          35 :     z = mkvec2(z, E);
   10111             :   }
   10112         532 :   return gerepilecopy(av, z);
   10113             : }
   10114             : 
   10115             : /* decomposition of modular form on eisenspace */
   10116             : static GEN
   10117         945 : mfeisensteindec(GEN mf, GEN F)
   10118             : {
   10119         945 :   pari_sp av = avma;
   10120             :   GEN M, Mindex, Mvecj, V, B, CHI;
   10121             :   long o, ord;
   10122             : 
   10123         945 :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
   10124         945 :   if (lg(Mvecj) < 5)
   10125             :   {
   10126          56 :     GEN E, e = gel(Mvecj,2), gkE = gel(e,1);
   10127          56 :     long dE = itou(gel(e,4));
   10128          56 :     Mvecj = gel(Mvecj,1);
   10129          56 :     E = mfeisenstein(itou(gkE), NULL, gel(e,3));
   10130          56 :     if (dE != 1) E = mfbd_E2(E, dE, gel(e,2)); /* here k = 2 */
   10131          56 :     F = mfmul(F, E);
   10132             :   }
   10133         945 :   M = gel(Mvecj, 2);
   10134         945 :   if (lg(M) == 1) return cgetg(1, t_VEC);
   10135         945 :   Mindex = gel(Mvecj, 1);
   10136         945 :   ord = itou(gel(Mvecj,4));
   10137         945 :   V = mfcoefs(F, Mindex[lg(Mindex)-1]-1, 1); settyp(V, t_COL);
   10138         945 :   CHI = mf_get_CHI(F);
   10139         945 :   o = mfcharorder(CHI);
   10140         945 :   if (o > 2 && o != ord)
   10141             :   { /* convert Mod(.,polcyclo(o)) to Mod(., polcyclo(N)) for o | N,
   10142             :      * o and N both != 2 (mod 4) */
   10143          84 :     GEN z, P = gel(M,4); /* polcyclo(ord) */
   10144          84 :     long vt = varn(P);
   10145          84 :     z = gmodulo(pol_xn(ord/o, vt), P);
   10146          84 :     if (ord % o) pari_err_TYPE("mfeisensteindec", V);
   10147          84 :     V = gsubst(liftpol_shallow(V), vt, z);
   10148             :   }
   10149         945 :   B = Minv_RgC_mul(M, vecpermute(V, Mindex));
   10150         945 :   return gerepileupto(av, B);
   10151             : }
   10152             : 
   10153             : /*********************************************************************/
   10154             : /*                        END EISENSPACE                             */
   10155             : /*********************************************************************/
   10156             : 
   10157             : static GEN
   10158          70 : sertocol2(GEN S, long l)
   10159             : {
   10160          70 :   GEN C = cgetg(l + 2, t_COL);
   10161             :   long i;
   10162         420 :   for (i = 0; i <= l; i++) gel(C, i+1) = polcoef_i(S, i, -1);
   10163          70 :   return C;
   10164             : }
   10165             : 
   10166             : /* Compute polynomial P0 such that F=E4^(k/4)P0(E6/E4^(3/2)). */
   10167             : static GEN
   10168          14 : mfcanfindp0(GEN F, long k)
   10169             : {
   10170          14 :   pari_sp ltop = avma;
   10171             :   GEN E4, E6, V, V1, Q, W, res, M, B;
   10172             :   long l, j;
   10173          14 :   l = k/6 + 2;
   10174          14 :   V = mfcoefsser(F,l);
   10175          14 :   E4 = mfcoefsser(mfEk(4),l);
   10176          14 :   E6 = mfcoefsser(mfEk(6),l);
   10177          14 :   V1 = gdiv(V, gpow(E4, sstoQ(k,4), 0));
   10178          14 :   Q = gdiv(E6, gpow(E4, sstoQ(3,2), 0));
   10179          14 :   W = gpowers(Q, l - 1);
   10180          14 :   M = cgetg(l + 1, t_MAT);
   10181          70 :   for (j = 1; j <= l; j++) gel(M,j) = sertocol2(gel(W,j), l);
   10182          14 :   B = sertocol2(V1, l);
   10183          14 :   res = inverseimage(M, B);
   10184          14 :   if (lg(res) == 1) err_space(F);
   10185          14 :   return gerepilecopy(ltop, gtopolyrev(res, 0));
   10186             : }
   10187             : 
   10188             : /* Compute the first n+1 Taylor coeffs at tau=I of a modular form
   10189             :  * on SL_2(Z). */
   10190             : GEN
   10191          14 : mftaylor(GEN F, long n, long flreal, long prec)
   10192             : {
   10193          14 :   pari_sp ltop = avma;
   10194          14 :   GEN P0, Pm1 = gen_0, v;
   10195          14 :   GEN X2 = mkpoln(3, ghalf,gen_0,gneg(ghalf)); /* (x^2-1) / 2 */
   10196             :   long k, m;
   10197          14 :   if (!checkmf_i(F)) pari_err_TYPE("mftaylor",F);
   10198          14 :   k = mf_get_k(F);
   10199          14 :   if (mf_get_N(F) != 1 || k < 0) pari_err_IMPL("mftaylor for this form");
   10200          14 :   P0 = mfcanfindp0(F, k);
   10201          14 :   v = cgetg(n+2, t_VEC); gel(v, 1) = RgX_coeff(P0,0);
   10202         154 :   for (m = 0; m < n; m++)
   10203             :   {
   10204         140 :     GEN P1 = gdivgs(gmulsg(-(k + 2*m), RgX_shift(P0,1)), 12);
   10205         140 :     P1 = gadd(P1, gmul(X2, RgX_deriv(P0)));
   10206         140 :     if (m) P1 = gsub(P1, gdivgs(gmulsg(m*(m+k-1), Pm1), 144));
   10207         140 :     Pm1 = P0; P0 = P1;
   10208         140 :     gel(v, m+2) = RgX_coeff(P0, 0);
   10209             :   }
   10210          14 :   if (flreal)
   10211             :   {
   10212           7 :     GEN pi2 = Pi2n(1, prec), pim4 = gmulsg(-2, pi2), VPC;
   10213           7 :     GEN C = gmulsg(3, gdiv(gpowgs(ggamma(ginv(utoi(4)), prec), 8), gpowgs(pi2, 6)));
   10214             :     /* E_4(i): */
   10215           7 :     GEN facn = gen_1;
   10216           7 :     VPC = gpowers(gmul(pim4, gsqrt(C, prec)), n);
   10217           7 :     C = gpow(C, sstoQ(k,4), prec);
   10218          84 :     for (m = 0; m <= n; m++)
   10219             :     {
   10220          77 :       gel(v, m+1) = gdiv(gmul(C, gmul(gel(v, m+1), gel(VPC, m+1))), facn);
   10221          77 :       facn = gmulgs(facn, m+1);
   10222             :     }
   10223             :   }
   10224          14 :   return gerepilecopy(ltop, v);
   10225             : }
   10226             : 
   10227             : #if 0
   10228             : /* To be used in mfeigensearch() */
   10229             : GEN
   10230             : mfreadratfile()
   10231             : {
   10232             :   GEN eqn;
   10233             :   pariFILE *F = pari_fopengz("rateigen300.gp");
   10234             :   eqn = gp_readvec_stream(F->file);
   10235             :   pari_fclose(F);
   10236             :   return eqn;
   10237             : }
   10238             : #endif
   10239             :  /*****************************************************************/
   10240             : /*           EISENSTEIN CUSPS: COMPLEX DIRECTLY: one F_k         */
   10241             : /*****************************************************************/
   10242             : 
   10243             : /* CHIvec = charinit(CHI); data = [N1g/g1,N2g/g2,g1/g,g2/g,C/g1,C/g2,
   10244             :  * (N1g/g1)^{-1},(N2g/g2)^{-1}] */
   10245             : 
   10246             : /* nm = n/m;
   10247             :  * z1 = powers of \z_{C/g}^{(Ae/g)^{-1}},
   10248             :  * z2 = powers of \z_N^{A^{-1}(g1g2/C)}]
   10249             :  * N.B. : we compute value and conjugate at the end, so it is (Ae/g)^{-1}
   10250             :  * and not -(Ae/g)^{-1} */
   10251             : static GEN
   10252     7643496 : eiscnm(long nm, long m, GEN CHI1vec, GEN CHI2vec, GEN data, GEN z1)
   10253             : {
   10254     7643496 :   long Cg1 = data[5], s10 = (nm*data[7]) % Cg1, r10 = (nm - data[1]*s10) / Cg1;
   10255     7643496 :   long Cg2 = data[6], s20 = (m *data[8]) % Cg2, r20 = (m  - data[2]*s20) / Cg2;
   10256             :   long j1, r1, s1;
   10257     7643496 :   GEN T = gen_0;
   10258    18404232 :   for (j1 = 0, r1 = r10, s1 = s10; j1 < data[3]; j1++, r1 -= data[1], s1 += Cg1)
   10259             :   {
   10260    10760736 :     GEN c1 = mychareval(CHI1vec, r1);
   10261    10760736 :     if (!gequal0(c1))
   10262             :     {
   10263             :       long j2, r2, s2;
   10264     7907396 :       GEN S = gen_0;
   10265    20468378 :       for (j2 = 0, r2 = r20, s2 = s20; j2 < data[4]; j2++, r2 -= data[2], s2 += Cg2)
   10266             :       {
   10267    12560982 :         GEN c2 = mychareval(CHI2vec, r2);
   10268    12560982 :         if (!gequal0(c2)) S = gadd(S, gmul(c2, rootsof1pow(z1, s1*s2)));
   10269             :       }
   10270     7907396 :       T = gadd(T, gmul(c1, S));
   10271             :     }
   10272             :   }
   10273     7643496 :   return conj_i(T);
   10274             : }
   10275             : 
   10276             : static GEN
   10277      593467 : fg1g2n(long n, long k, GEN CHI1vec, GEN CHI2vec, GEN data, GEN z1, GEN z2)
   10278             : {
   10279      593467 :   pari_sp av = avma;
   10280      593467 :   GEN S = gen_0, D = mydivisorsu(n);
   10281      593467 :   long i, l = lg(D);
   10282     4415215 :   for (i = 1; i < l; i++)
   10283             :   {
   10284     3821748 :     long m = D[i], nm = D[l-i]; /* n/m */
   10285     3821748 :     GEN u = eiscnm( nm,  m, CHI1vec, CHI2vec, data, z1);
   10286     3821748 :     GEN v = eiscnm(-nm, -m, CHI1vec, CHI2vec, data, z1);
   10287     3821748 :     GEN w = odd(k) ? gsub(u, v) : gadd(u, v);
   10288     3821748 :     S = gadd(S, gmul(powuu(m, k-1), w));
   10289             :   }
   10290      593467 :   return gerepileupto(av, gmul(S, rootsof1pow(z2, n)));
   10291             : }
   10292             : 
   10293             : static GEN
   10294       13230 : gausssumcx(GEN CHIvec, long prec)
   10295             : {
   10296             :   GEN z, S, V;
   10297       13230 :   long m, N = CHIvec_N(CHIvec);
   10298       13230 :   if (N == 1) return gen_1;
   10299        7210 :   V = CHIvec_val(CHIvec);
   10300        7210 :   z = rootsof1u_cx(N, prec);
   10301        7210 :   S = gmul(z, gel(V, N));
   10302      100835 :   for (m = N-1; m >= 1; m--) S = gmul(z, gadd(gel(V, m), S));
   10303        7210 :   return S;
   10304             : }
   10305             : 
   10306             : /* Computation of Q_k(\z_N^s) as a polynomial in \z_N^s. FIXME: explicit
   10307             :  * formula ? */
   10308             : static GEN
   10309        2191 : mfqk(long k, long N)
   10310             : {
   10311             :   GEN X, P, ZI, Q, Xm1, invden;
   10312             :   long i;
   10313        2191 :   ZI = gdivgs(RgX_shift_shallow(RgV_to_RgX(identity_ZV(N-1), 0), 1), N);
   10314        2191 :   if (k == 1) return ZI;
   10315        1113 :   P = gsubgs(pol_xn(N,0), 1);
   10316        1113 :   invden = RgXQ_powu(ZI, k, P);
   10317        1113 :   X = pol_x(0); Q = gneg(X); Xm1 = gsubgs(X, 1);
   10318        2765 :   for (i = 2; i < k; i++)
   10319        1652 :     Q = RgX_shift_shallow(ZX_add(gmul(Xm1, ZX_deriv(Q)), gmulsg(-i, Q)), 1);
   10320        1113 :   return RgXQ_mul(Q, invden, P);
   10321             : }
   10322             : 
   10323             : /* CHI mfchar; M is a multiple of the conductor of CHI, but is NOT
   10324             :  * necessarily its modulus */
   10325             : static GEN
   10326        3066 : mfskcx(long k, GEN CHI, long M, long prec)
   10327             : {
   10328             :   GEN S, CHIvec, P;
   10329             :   long F, m, i, l;
   10330        3066 :   CHI = mfchartoprimitive(CHI, &F);
   10331        3066 :   CHIvec = mfcharcxinit(CHI, prec);
   10332        3066 :   if (F == 1) S = gdivgs(bernfrac(k), k);
   10333             :   else
   10334             :   {
   10335        2191 :     GEN Q = mfqk(k, F), V = CHIvec_val(CHIvec);
   10336        2191 :     S = gmul(gel(V, F), RgX_coeff(Q, 0));
   10337       38682 :     for (m = 1; m < F; m++) S = gadd(S, gmul(gel(V, m), RgX_coeff(Q, m)));
   10338        2191 :     S = conj_i(S);
   10339             :   }
   10340             :   /* prime divisors of M not dividing f(chi) */
   10341        3066 :   P = gel(myfactoru(u_ppo(M/F,F)), 1); l = lg(P);
   10342        3192 :   for (i = 1; i < l; i++)
   10343             :   {
   10344         126 :     long p = P[i];
   10345         126 :     S = gmul(S, gsubsg(1, gdiv(mychareval(CHIvec, p), powuu(p, k))));
   10346             :   }
   10347        3066 :   return gmul(gmul(gausssumcx(CHIvec, prec), S), powuu(M/F, k));
   10348             : }
   10349             : 
   10350             : static GEN
   10351        5684 : f00_i(long k, GEN CHI1vec, GEN CHI2vec, GEN G2, GEN S, long prec)
   10352             : {
   10353             :   GEN c, a;
   10354        5684 :   long N1 = CHIvec_N(CHI1vec), N2 = CHIvec_N(CHI2vec);
   10355        5684 :   if (S[2] != N1) return gen_0;
   10356        3066 :   c = mychareval(CHI1vec, S[3]);
   10357        3066 :   if (isintzero(c)) return gen_0;
   10358        3066 :   a = mfskcx(k, mfchardiv(CHIvec_CHI(CHI2vec), CHIvec_CHI(CHI1vec)), N1*N2, prec);
   10359        3066 :   a = gmul(a, conj_i(gmul(c,G2)));
   10360        3066 :   return gdiv(a, mulsi(-N2, powuu(S[1], k-1)));
   10361             : }
   10362             : 
   10363             : static GEN
   10364        4487 : f00(long k, GEN CHI1vec,GEN CHI2vec, GEN G1,GEN G2, GEN data, long prec)
   10365             : {
   10366             :   GEN T1, T2;
   10367        4487 :   T2 = f00_i(k, CHI1vec, CHI2vec, G2, data, prec);
   10368        4487 :   if (k > 1) return T2;
   10369        1197 :   T1 = f00_i(k, CHI2vec, CHI1vec, G1, data, prec);
   10370        1197 :   return gadd(T1, T2);
   10371             : }
   10372             : 
   10373             : /* ga in SL_2(Z), find beta [a,b;c,d] in Gamma_0(N) and mu in Z such that
   10374             :  * beta * ga * T^u = [A',B';C',D'] with C' | N and N | B', C' > 0 */
   10375             : static void
   10376        5082 : mfgatogap(GEN ga, long N, long *pA, long *pC, long *pD, long *pd, long *pmu)
   10377             : {
   10378        5082 :   GEN A = gcoeff(ga,1,1), B = gcoeff(ga,1,2);
   10379        5082 :   GEN C = gcoeff(ga,2,1), D = gcoeff(ga,2,2), a, b, c, d;
   10380             :   long t, Ap, Cp, B1, D1, mu;
   10381        5082 :   Cp = itou(bezout(muliu(A,N), C, &c, &d)); /* divides N */
   10382        5082 :   t = 0;
   10383        5082 :   if (Cp > 1)
   10384             :   { /* (d, N/Cp) = 1, find t such that (d - t*(A*N/Cp), N) = 1 */
   10385        2408 :     long dN = umodiu(d,Cp), Q = (N/Cp * umodiu(A,Cp)) % Cp;
   10386        2779 :     while (ugcd(dN, Cp) > 1) { t++; dN = Fl_sub(dN, Q, Cp); }
   10387             :   }
   10388        5082 :   if (t)
   10389             :   {
   10390         371 :     c = addii(c, mului(t, diviuexact(C,Cp)));
   10391         371 :     d = subii(d, mului(t, muliu(A, N/Cp))); /* (d,N) = 1 */
   10392             :   }
   10393        5082 :   D1 = umodiu(mulii(d,D), N);
   10394        5082 :   (void)bezout(d, mulis(c,-N), &a, &b); /* = 1 */
   10395        5082 :   t = 0; Ap = umodiu(addii(mulii(a,A), mulii(b,C)), N); /* (Ap,Cp) = 1 */
   10396        6937 :   while (ugcd(Ap, N) > 1) { t++; Ap = Fl_add(Ap, Cp, N); }
   10397        5082 :   B1 = umodiu(a,N)*umodiu(B,N) + umodiu(b,N)*umodiu(D,N) + t*D1;
   10398        5082 :   B1 %= N;
   10399        5082 :   *pmu = mu = Fl_neg(Fl_div(B1, Ap, N), N);
   10400             :   /* A', D' and d only needed modulo N */
   10401        5082 :   *pd = umodiu(d, N);
   10402        5082 :   *pA = Ap;
   10403        5082 :   *pC = Cp; *pD = (D1 + Cp*mu) % N;
   10404        5082 : }
   10405             : 
   10406             : #if 0
   10407             : /* CHI is a mfchar, return alpha(CHI) */
   10408             : static long
   10409             : mfalchi(GEN CHI, long AN, long cg)
   10410             : {
   10411             :   GEN G = gel(CHI,1), chi = gel(CHI,2), go = gmfcharorder(CHI);
   10412             :   long o = itou(go), a = itos( znchareval(G, chi, stoi(1 + AN/cg), go) );
   10413             :   if (a < 0 || (cg * a) % o) pari_err_BUG("mfalchi");
   10414             :   return (cg * a) / o;
   10415             : }
   10416             : #endif
   10417             : /* return A such that CHI1(t) * CHI2(t) = e(A) or NULL if (t,N1*N2) > 1 */
   10418             : static GEN
   10419       10164 : mfcharmuleval(GEN CHI1vec, GEN CHI2vec, long t)
   10420             : {
   10421       10164 :   long a1 = mycharexpo(CHI1vec, t), o1 = CHIvec_ord(CHI1vec);
   10422       10164 :   long a2 = mycharexpo(CHI2vec, t), o2 = CHIvec_ord(CHI2vec);;
   10423       10164 :   if (a1 < 0 || a2 < 0) return NULL;
   10424       10164 :   return sstoQ(a1*o2 + a2*o1, o1*o2);
   10425             : }
   10426             : static GEN
   10427        5082 : mfcharmulcxeval(GEN CHI1vec, GEN CHI2vec, long t, long prec)
   10428             : {
   10429        5082 :   GEN A = mfcharmuleval(CHI1vec, CHI2vec, t);
   10430             :   long n, d;
   10431        5082 :   if (!A) return gen_0;
   10432        5082 :   Qtoss(A, &n,&d); return rootsof1q_cx(n, d, prec);
   10433             : }
   10434             : /* alpha(CHI1 * CHI2) */
   10435             : static long
   10436        5082 : mfalchi2(GEN CHI1vec, GEN CHI2vec, long AN, long cg)
   10437             : {
   10438        5082 :   GEN A = mfcharmuleval(CHI1vec, CHI2vec, 1 + AN/cg);
   10439             :   long a;
   10440        5082 :   if (!A) pari_err_BUG("mfalchi2");
   10441        5082 :   A = gmulsg(cg, A);
   10442        5082 :   if (typ(A) != t_INT) pari_err_BUG("mfalchi2");
   10443        5082 :   a = itos(A) % cg; if (a < 0) a += cg;
   10444        5082 :   return a;
   10445             : }
   10446             : 
   10447             : /* return g = (a,b), set u >= 0 s.t. g = a * u (mod b) */
   10448             : static long
   10449       20328 : mybezout(long a, long b, long *pu)
   10450             : {
   10451       20328 :   long junk, g = cbezout(a, b, pu, &junk);
   10452       20328 :   if (*pu < 0) *pu += b/g;
   10453       20328 :   return g;
   10454             : }
   10455             : 
   10456             : /* E = [k, CHI1,CHI2, e], CHI1 and CHI2 primitive mfchars such that,
   10457             :  * CHI1(-1)*CHI2(-1) = (-1)^k; expansion of (B_e (E_k(CHI1,CHI2))) | ga.
   10458             :  * w is the width for the space of the calling function. */
   10459             : static GEN
   10460        5082 : mfeisensteingacx(GEN E, long w, GEN ga, long lim, long prec)
   10461             : {
   10462        5082 :   GEN CHI1vec, CHI2vec, CHI1 = gel(E,2), CHI2 = gel(E,3), v, S, ALPHA;
   10463             :   GEN G1, G2, z1, z2, data;
   10464        5082 :   long k = itou(gel(E,1)), e = itou(gel(E,4));
   10465        5082 :   long N1 = mfcharmodulus(CHI1);
   10466        5082 :   long N2 = mfcharmodulus(CHI2), N = e * N1 * N2;
   10467             :   long NsurC, cg, wN, A, C, Ai, d, mu, alchi, na, da;
   10468             :   long eg, g, gH, U, u0, u1, u2, Aig, H, m, n, t, Cg, NC1, NC2;
   10469             : 
   10470        5082 :   mfgatogap(ga, N, &A, &C, &Ai, &d, &mu);
   10471        5082 :   CHI1vec = mfcharcxinit(CHI1, prec);
   10472        5082 :   CHI2vec = mfcharcxinit(CHI2, prec);
   10473        5082 :   NsurC = N/C; cg  = ugcd(C, NsurC); wN = NsurC / cg;
   10474        5082 :   if (w%wN) pari_err_BUG("mfeisensteingacx [wN does not divide w]");
   10475        5082 :   alchi = mfalchi2(CHI1vec, CHI2vec, A*N, cg);
   10476        5082 :   ALPHA = sstoQ(alchi, NsurC);
   10477             : 
   10478        5082 :   g = mybezout(A*e, C, &u0); Cg = C/g; eg = e/g;
   10479        5082 :   NC1 = mybezout(N1, Cg, &u1);
   10480        5082 :   NC2 = mybezout(N2, Cg, &u2);
   10481        5082 :   H = (NC1*NC2*g)/Cg;
   10482        5082 :   Aig = (Ai*H)%N; if (Aig < 0) Aig += N;
   10483        5082 :   z1 = rootsof1powinit(u0, Cg, prec);
   10484        5082 :   z2 = rootsof1powinit(Aig, N, prec);
   10485        5082 :   data = mkvecsmalln(8, N1/NC1, N2/NC2, NC1, NC2, Cg/NC1, Cg/NC2, u1, u2);
   10486        5082 :   v = zerovec(lim + 1);
   10487             :   /* need n*H = alchi (mod cg) */
   10488        5082 :   gH = mybezout(H, cg, &U);
   10489        5082 :   if (gH > 1)
   10490             :   {
   10491         399 :     if (alchi % gH) return mkvec2(gen_0, v);
   10492         399 :     alchi /= gH; cg /= gH; H /= gH;
   10493             :   }
   10494        5082 :   G1 = gausssumcx(CHI1vec, prec);
   10495        5082 :   G2 = gausssumcx(CHI2vec, prec);
   10496        5082 :   if (!alchi)
   10497        4487 :     gel(v,1) = f00(k, CHI1vec,CHI2vec,G1,G2, mkvecsmall3(NC2,Cg,A*eg), prec);
   10498        5082 :   n = Fl_mul(alchi,U,cg); if (!n) n = cg;
   10499        5082 :   m = (n*H - alchi) / cg; /* positive, exact division */
   10500      598549 :   for (; m <= lim; n+=cg, m+=H)
   10501      593467 :     gel(v, m+1) = fg1g2n(n, k, CHI1vec, CHI2vec, data, z1,z2);
   10502        5082 :   t = (2*e)/g; if (odd(k)) t = -t;
   10503        5082 :   v = gdiv(v, gmul(conj_i(gmul(G1,G2)), mulsi(t, powuu(eg*N2/NC2, k-1))));
   10504        5082 :   if (k == 2 && N1 == 1 && N2 == 1) v = gsub(mkF2bd(wN,lim), gmulsg(e,v));
   10505             : 
   10506        5082 :   Qtoss(ALPHA, &na,&da);
   10507        5082 :   S = conj_i( mfcharmulcxeval(CHI1vec,CHI2vec,d,prec) ); /* CHI(1/d) */
   10508        5082 :   if (wN > 1)
   10509             :   {
   10510        3927 :     GEN z = rootsof1powinit(-mu, wN, prec);
   10511        3927 :     long i, l = lg(v);
   10512      571123 :     for (i = 1; i < l; i++) gel(v,i) = gmul(gel(v,i), rootsof1pow(z,i-1));
   10513             :   }
   10514        5082 :   v = RgV_Rg_mul(v, gmul(S, rootsof1q_cx(-mu*na, da, prec)));
   10515        5082 :   return mkvec2(ALPHA, bdexpand(v, w/wN));
   10516             : }
   10517             : 
   10518             : /*****************************************************************/
   10519             : /*                       END EISENSTEIN CUSPS                    */
   10520             : /*****************************************************************/
   10521             : 
   10522             : static GEN
   10523        1596 : mfchisimpl(GEN CHI)
   10524             : {
   10525             :   GEN G, chi;
   10526        1596 :   if (typ(CHI) == t_INT) return CHI;
   10527        1596 :   G = gel(CHI, 1); chi = gel(CHI, 2);
   10528        1596 :   switch(mfcharorder(CHI))
   10529             :   {
   10530        1148 :     case 1: chi = gen_1; break;
   10531         427 :     case 2: chi = znchartokronecker(G,chi,1); break;
   10532          21 :     default:chi = mkintmod(znconreyexp(G,chi), znstar_get_N(G)); break;
   10533             :   }
   10534        1596 :   return chi;
   10535             : }
   10536             : 
   10537             : GEN
   10538         700 : mfparams(GEN F)
   10539             : {
   10540         700 :   pari_sp av = avma;
   10541             :   GEN z, mf, CHI;
   10542         700 :   if ((mf = checkMF_i(F)))
   10543             :   {
   10544          14 :     long N = MF_get_N(mf);
   10545          14 :     GEN gk = MF_get_gk(mf);
   10546          14 :     CHI = MF_get_CHI(mf);
   10547          14 :     z = mkvec5(utoi(N), gk, CHI, utoi(MF_get_space(mf)), mfcharpol(CHI));
   10548             :   }
   10549             :   else
   10550             :   {
   10551         686 :     if (!checkmf_i(F)) pari_err_TYPE("mfparams", F);
   10552         686 :     z = vec_append(mf_get_NK(F), mfcharpol(mf_get_CHI(F)));
   10553             :   }
   10554         700 :   gel(z,3) = mfchisimpl(gel(z,3));
   10555         700 :   return gerepilecopy(av, z);
   10556             : }
   10557             : 
   10558             : GEN
   10559          14 : mfisCM(GEN F)
   10560             : {
   10561          14 :   pari_sp av = avma;
   10562             :   forprime_t S;
   10563             :   GEN D, v;
   10564             :   long N, k, lD, sb, p, i;
   10565          14 :   if (!checkmf_i(F)) pari_err_TYPE("mfisCM", F);
   10566          14 :   N = mf_get_N(F);
   10567          14 :   k = mf_get_k(F); if (N < 0 || k < 0) pari_err_IMPL("mfisCM for this F");
   10568          14 :   D = mfunram(N, -1);
   10569          14 :   lD = lg(D);
   10570          14 :   sb = maxss(mfsturmNk(N, k), 4*N);
   10571          14 :   v = mfcoefs_i(F, sb, 1);
   10572          14 :   u_forprime_init(&S, 2, sb);
   10573         504 :   while ((p = u_forprime_next(&S)))
   10574             :   {
   10575         490 :     GEN ap = gel(v, p+1);
   10576         490 :     if (!gequal0(ap))
   10577         406 :       for (i = 1; i < lD; i++)
   10578         245 :         if (kross(D[i], p) == -1)