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.18.0 lcov report (development 29806-4d001396c7) Lines: 7496 7702 97.3 %
Date: 2024-12-21 09:08:57 Functions: 769 775 99.2 %
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       36967 : mkgNK(GEN N, GEN k, GEN CHI, GEN P) { return mkvec4(N, k, CHI, P); }
      82             : static GEN
      83       15015 : mkNK(long N, long k, GEN CHI) { return mkgNK(stoi(N), stoi(k), CHI, pol_x(1)); }
      84             : GEN
      85        8561 : MF_get_CHI(GEN mf) { return gmael(mf,1,3); }
      86             : GEN
      87       20265 : MF_get_gN(GEN mf) { return gmael(mf,1,1); }
      88             : long
      89       19173 : MF_get_N(GEN mf) { return itou(MF_get_gN(mf)); }
      90             : GEN
      91       14574 : MF_get_gk(GEN mf) { return gmael(mf,1,2); }
      92             : long
      93        7021 : MF_get_k(GEN mf)
      94             : {
      95        7021 :   GEN gk = MF_get_gk(mf);
      96        7021 :   if (typ(gk)!=t_INT) pari_err_IMPL("half-integral weight");
      97        7021 :   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       14665 : MF_get_space(GEN mf) { return itos(gmael(mf,1,4)); }
     108             : GEN
     109        4389 : MF_get_E(GEN mf) { return gel(mf,2); }
     110             : GEN
     111       21077 : MF_get_S(GEN mf) { return gel(mf,3); }
     112             : GEN
     113        1855 : MF_get_basis(GEN mf) { return shallowconcat(gel(mf,2), gel(mf,3)); }
     114             : long
     115        5502 : MF_get_dim(GEN mf)
     116             : {
     117        5502 :   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        4669 :     default: /* mf_NEW, mf_CUSP, mf_OLD */
     124        4669 :       return lg(MF_get_S(mf)) - 1;
     125             :   }
     126             : }
     127             : GEN
     128        7175 : MFnew_get_vj(GEN mf) { return gel(mf,4); }
     129             : GEN
     130         672 : MFcusp_get_vMjd(GEN mf) { return gel(mf,4); }
     131             : GEN
     132        6790 : MF_get_M(GEN mf) { return gmael(mf,5,3); }
     133             : GEN
     134        4774 : MF_get_Minv(GEN mf) { return gmael(mf,5,2); }
     135             : GEN
     136       10409 : MF_get_Mindex(GEN mf) { return gmael(mf,5,1); }
     137             : 
     138             : /* ordinary gtocol forgets about initial 0s */
     139             : GEN
     140        2387 : sertocol(GEN S) { return gtocol0(S, -(lg(S) - 2 + valser(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        1225 : QabM_init(long n, ulong *p)
     147             : {
     148        1225 :   ulong pinit = 1000000007;
     149             :   forprime_t T;
     150        1225 :   if (n <= 1) { *p = pinit; return 0; }
     151        1218 :   u_forprime_arith_init(&T, pinit, ULONG_MAX, 1, n);
     152        1218 :   *p = u_forprime_next(&T);
     153        1218 :   return Flx_oneroot(ZX_to_Flx(polcyclo(n, 0), *p), *p);
     154             : }
     155             : static ulong
     156     8534960 : Qab_to_Fl(GEN P, ulong r, ulong p)
     157             : {
     158             :   ulong t;
     159             :   GEN den;
     160     8534960 :   P = Q_remove_denom(liftpol_shallow(P), &den);
     161     8534960 :   if (typ(P) == t_POL) { GEN Pp = ZX_to_Flx(P, p); t = Flx_eval(Pp, r, p); }
     162     8399335 :   else t = umodiu(P, p);
     163     8534960 :   if (den) t = Fl_div(t, umodiu(den, p), p);
     164     8534960 :   return t;
     165             : }
     166             : static GEN
     167       38164 : QabC_to_Flc(GEN x, ulong r, ulong p)
     168     8341333 : { pari_APPLY_long( Qab_to_Fl(gel(x,i), r, p)); }
     169             : static GEN
     170         595 : QabM_to_Flm(GEN x, ulong r, ulong p)
     171       38759 : { pari_APPLY_same(QabC_to_Flc(gel(x, i), r, p);) }
     172             : /* A a t_POL */
     173             : static GEN
     174        1484 : QabX_to_Flx(GEN A, ulong r, ulong p)
     175             : {
     176        1484 :   long i, l = lg(A);
     177        1484 :   GEN a = cgetg(l, t_VECSMALL);
     178        1484 :   a[1] = ((ulong)A[1])&VARNBITS;
     179      233023 :   for (i = 2; i < l; i++) uel(a,i) = Qab_to_Fl(gel(A,i), r, p);
     180        1484 :   return Flx_renormalize(a, l);
     181             : }
     182             : 
     183             : /* FIXME: remove */
     184             : static GEN
     185        1092 : ZabM_pseudoinv_i(GEN M, GEN P, long n, GEN *pv, GEN *den, int ratlift)
     186             : {
     187        1092 :   GEN v = ZabM_indexrank(M, P, n);
     188        1092 :   if (pv) *pv = v;
     189        1092 :   M = shallowmatextract(M,gel(v,1),gel(v,2));
     190        1092 :   return ratlift? ZabM_inv_ratlift(M, P, n, den): ZabM_inv(M, P, n, den);
     191             : }
     192             : 
     193             : /* M matrix with coeff in Q(\chi)), where Q(\chi) = Q(X)/(P) for
     194             :  * P = cyclotomic Phi_n. Assume M rational if n <= 2 */
     195             : static GEN
     196        1561 : QabM_ker(GEN M, GEN P, long n)
     197             : {
     198        1561 :   if (n <= 2) return QM_ker(M);
     199         378 :   return ZabM_ker(row_Q_primpart(liftpol_shallow(M)), P, n);
     200             : }
     201             : /* pseudo-inverse of M. FIXME: should replace QabM_pseudoinv */
     202             : static GEN
     203        1274 : QabM_pseudoinv_i(GEN M, GEN P, long n, GEN *pv, GEN *pden)
     204             : {
     205             :   GEN cM, Mi;
     206        1274 :   if (n <= 2)
     207             :   {
     208        1134 :     M = Q_primitive_part(M, &cM);
     209        1134 :     Mi = ZM_pseudoinv(M, pv, pden); /* M^(-1) = Mi / (cM * den) */
     210             :   }
     211             :   else
     212             :   {
     213         140 :     M = Q_primitive_part(liftpol_shallow(M), &cM);
     214         140 :     Mi = ZabM_pseudoinv(M, P, n, pv, pden);
     215             :   }
     216        1274 :   *pden = mul_content(*pden, cM);
     217        1274 :   return Mi;
     218             : }
     219             : /* FIXME: delete */
     220             : static GEN
     221        1015 : QabM_pseudoinv(GEN M, GEN P, long n, GEN *pv, GEN *pden)
     222             : {
     223        1015 :   GEN Mi = QabM_pseudoinv_i(M, P, n, pv, pden);
     224        1015 :   return P? gmodulo(Mi, P): Mi;
     225             : }
     226             : 
     227             : static GEN
     228       10388 : QabM_indexrank(GEN M, GEN P, long n)
     229             : {
     230             :   GEN z;
     231       10388 :   if (n <= 2)
     232             :   {
     233        9233 :     M = vec_Q_primpart(M);
     234        9233 :     z = ZM_indexrank(M); /* M^(-1) = Mi / (cM * den) */
     235             :   }
     236             :   else
     237             :   {
     238        1155 :     M = vec_Q_primpart(liftpol_shallow(M));
     239        1155 :     z = ZabM_indexrank(M, P, n);
     240             :   }
     241       10388 :   return z;
     242             : }
     243             : 
     244             : /*********************************************************************/
     245             : /*                    Simple arithmetic functions                    */
     246             : /*********************************************************************/
     247             : /* TODO: most of these should be exported and used in ifactor1.c */
     248             : /* phi(n) */
     249             : static ulong
     250      109662 : myeulerphiu(ulong n)
     251             : {
     252             :   pari_sp av;
     253      109662 :   if (n == 1) return 1;
     254       90902 :   av = avma; return gc_ulong(av, eulerphiu_fact(myfactoru(n)));
     255             : }
     256             : static long
     257       65709 : mymoebiusu(ulong n)
     258             : {
     259             :   pari_sp av;
     260       65709 :   if (n == 1) return 1;
     261       54194 :   av = avma; return gc_long(av, moebiusu_fact(myfactoru(n)));
     262             : }
     263             : 
     264             : static long
     265        2975 : mynumdivu(long N)
     266             : {
     267             :   pari_sp av;
     268        2975 :   if (N == 1) return 1;
     269        2870 :   av = avma; return gc_long(av, numdivu_fact(myfactoru(N)));
     270             : }
     271             : 
     272             : /* N\prod_{p|N} (1+1/p) */
     273             : static long
     274      393960 : mypsiu(ulong N)
     275             : {
     276             :   pari_sp av;
     277             :   GEN P;
     278             :   long j, l, a;
     279      393960 :   if (N == 1) return 1;
     280      310436 :   av = avma; P = gel(myfactoru(N), 1); l = lg(P);
     281      739452 :   for (a = N, j = 1; j < l; j++) a += a / P[j];
     282      310436 :   return gc_long(av, a);
     283             : }
     284             : /* write n = mf^2. Return m, set f. */
     285             : static ulong
     286          70 : mycore(ulong n, long *pf)
     287             : {
     288          70 :   pari_sp av = avma;
     289          70 :   GEN fa = myfactoru(n), P = gel(fa,1), E = gel(fa,2);
     290          70 :   long i, l = lg(P), m = 1, f = 1;
     291         266 :   for (i = 1; i < l; i++)
     292             :   {
     293         196 :     long j, p = P[i], e = E[i];
     294         196 :     if (e & 1) m *= p;
     295         455 :     for (j = 2; j <= e; j+=2) f *= p;
     296             :   }
     297          70 :   *pf = f; return gc_long(av,m);
     298             : }
     299             : 
     300             : /* fa = factorization of -D > 0, return -D0 > 0 (where D0 is fundamental) */
     301             : static long
     302     5200863 : corediscs_fact(GEN fa)
     303             : {
     304     5200863 :   GEN P = gel(fa,1), E = gel(fa,2);
     305     5200863 :   long i, l = lg(P), m = 1;
     306    17215227 :   for (i = 1; i < l; i++)
     307             :   {
     308    12014364 :     long p = P[i], e = E[i];
     309    12014364 :     if (e & 1) m *= p;
     310             :   }
     311     5200863 :   if ((m&3L) != 3) m <<= 2;
     312     5200863 :   return m;
     313             : }
     314             : static long
     315        6993 : mubeta(long n)
     316             : {
     317        6993 :   pari_sp av = avma;
     318        6993 :   GEN E = gel(myfactoru(n), 2);
     319        6993 :   long i, s = 1, l = lg(E);
     320       14511 :   for (i = 1; i < l; i++)
     321             :   {
     322        7518 :     long e = E[i];
     323        7518 :     if (e >= 3) return gc_long(av,0);
     324        7518 :     if (e == 1) s *= -2;
     325             :   }
     326        6993 :   return gc_long(av,s);
     327             : }
     328             : 
     329             : /* n = n1*n2, n1 = ppo(n, m); return mubeta(n1)*moebiusu(n2).
     330             :  * N.B. If n from newt_params we, in fact, never return 0 */
     331             : static long
     332     7650494 : mubeta2(long n, long m)
     333             : {
     334     7650494 :   pari_sp av = avma;
     335     7650494 :   GEN fa = myfactoru(n), P = gel(fa,1), E = gel(fa,2);
     336     7650494 :   long i, s = 1, l = lg(P);
     337    15381428 :   for (i = 1; i < l; i++)
     338             :   {
     339     7730934 :     long p = P[i], e = E[i];
     340     7730934 :     if (m % p)
     341             :     { /* p^e in n1 */
     342     6565567 :       if (e >= 3) return gc_long(av,0);
     343     6565567 :       if (e == 1) s *= -2;
     344             :     }
     345             :     else
     346             :     { /* in n2 */
     347     1165367 :       if (e >= 2) return gc_long(av,0);
     348     1165367 :       s = -s;
     349             :     }
     350             :   }
     351     7650494 :   return gc_long(av,s);
     352             : }
     353             : 
     354             : /* write N = prod p^{ep} and n = df^2, d squarefree.
     355             :  * set g  = ppo(gcd(sqfpart(N), f), FC)
     356             :  *     N2 = prod p^if(e==1 || p|n, ep-1, ep-2) */
     357             : static void
     358     1874765 : newt_params(long N, long n, long FC, long *pg, long *pN2)
     359             : {
     360     1874765 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
     361     1874765 :   long i, g = 1, N2 = 1, l = lg(P);
     362     5004483 :   for (i = 1; i < l; i++)
     363             :   {
     364     3129718 :     long p = P[i], e = E[i];
     365     3129718 :     if (e == 1)
     366     2732919 :     { if (FC % p && n % (p*p) == 0) g *= p; }
     367             :     else
     368      396799 :       N2 *= upowuu(p,(n % p)? e-2: e-1);
     369             :   }
     370     1874765 :   *pg = g; *pN2 = N2;
     371     1874765 : }
     372             : /* simplified version of newt_params for n = 1 (newdim) */
     373             : static void
     374       41510 : newd_params(long N, long *pN2)
     375             : {
     376       41510 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
     377       41510 :   long i, N2 = 1, l = lg(P);
     378      103999 :   for (i = 1; i < l; i++)
     379             :   {
     380       62489 :     long p = P[i], e = E[i];
     381       62489 :     if (e > 2) N2 *= upowuu(p, e-2);
     382             :   }
     383       41510 :   *pN2 = N2;
     384       41510 : }
     385             : 
     386             : static long
     387          21 : newd_params2(long N)
     388             : {
     389          21 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
     390          21 :   long i, N2 = 1, l = lg(P);
     391          56 :   for (i = 1; i < l; i++)
     392             :   {
     393          35 :     long p = P[i], e = E[i];
     394          35 :     if (e >= 2) N2 *= upowuu(p, e);
     395             :   }
     396          21 :   return N2;
     397             : }
     398             : 
     399             : /*******************************************************************/
     400             : /*   Relative trace between cyclotomic fields (TODO: export this)  */
     401             : /*******************************************************************/
     402             : /* g>=1; return g * prod_{p | g, (p,q) = 1} (1-1/p) */
     403             : static long
     404       36869 : phipart(long g, long q)
     405             : {
     406       36869 :   if (g > 1)
     407             :   {
     408       19670 :     GEN P = gel(myfactoru(g), 1);
     409       19670 :     long i, l = lg(P);
     410       40194 :     for (i = 1; i < l; i++) { long p = P[i]; if (q % p) g -= g / p; }
     411             :   }
     412       36869 :   return g;
     413             : }
     414             : /* Set s,v s.t. Trace(zeta_N^k) from Q(zeta_N) to Q(\zeta_N) = s * zeta_M^v
     415             :  * With k > 0, N = M*d and N, M != 2 mod 4 */
     416             : static long
     417       84756 : tracerelz(long *pv, long d, long M, long k)
     418             : {
     419             :   long s, g, q, muq;
     420       84756 :   if (d == 1) { *pv = k; return 1; }
     421       65618 :   *pv = 0; g = ugcd(k, d); q = d / g;
     422       65618 :   muq = mymoebiusu(q); if (!muq) return 0;
     423       47173 :   if (M != 1)
     424             :   {
     425       37828 :     long v = Fl_invsafe(q % M, M);
     426       37828 :     if (!v) return 0;
     427       27524 :     *pv = (v * (k/g)) % M;
     428             :   }
     429       36869 :   s = phipart(g, M*q); if (muq < 0) s = -s;
     430       36869 :   return s;
     431             : }
     432             : /* Pi = polcyclo(i), i = m or n. Let Ki = Q(zeta_i), initialize Tr_{Kn/Km} */
     433             : GEN
     434       34013 : Qab_trace_init(long n, long m, GEN Pn, GEN Pm)
     435             : {
     436             :   long a, i, j, N, M, vt, d, D;
     437             :   GEN T, G;
     438             : 
     439       34013 :   if (m == n || n <= 2) return mkvec(Pm);
     440       16555 :   vt = varn(Pn);
     441       16555 :   d = degpol(Pn);
     442             :   /* if (N != n) zeta_N = zeta_n^2 and zeta_n = - zeta_N^{(N+1)/2} */
     443       16555 :   N = ((n & 3) == 2)? n >> 1: n;
     444       16555 :   M = ((m & 3) == 2)? m >> 1: m; /* M | N | n */
     445       16555 :   a = N / M;
     446       16555 :   T = const_vec(d, NULL);
     447       16555 :   D = d / degpol(Pm); /* relative degree */
     448       16555 :   if (D == 1) G = NULL;
     449             :   else
     450             :   { /* zeta_M = zeta_n^A; s_j(zeta_M) = zeta_M <=> j = 1 (mod J) */
     451       15281 :     long lG, A = (N == n)? a: (a << 1), J = n / ugcd(n, A);
     452       15281 :     G = coprimes_zv(n);
     453      150276 :     for (j = lG = 1; j < n; j += J)
     454      134995 :       if (G[j]) G[lG++] = j;
     455       15281 :     setlg(G, lG); /* Gal(Q(zeta_n) / Q(zeta_m)) */
     456             :   }
     457       16555 :   T = const_vec(d, NULL);
     458       16555 :   gel(T,1) = utoipos(D); /* Tr 1 */
     459      140140 :   for (i = 1; i < d; i++)
     460             :   { /* if n = 2N, zeta_n^i = (-1)^i zeta_N^k */
     461             :     long s, v, k;
     462             :     GEN t;
     463             : 
     464      123585 :     if (gel(T, i+1)) continue;
     465       84756 :     k = (N == n)? i: ((odd(i)? i + N: i) >> 1);
     466       84756 :     if ((s = tracerelz(&v, a, M, k)))
     467             :     {
     468       56007 :       if (m != M) v *= 2;/* Tr = s * zeta_m^v */
     469       56007 :       if (n != N && odd(i)) s = -s;
     470       56007 :       t = Qab_Czeta(v, m, stoi(s), vt);
     471             :     }
     472             :     else
     473       28749 :       t = gen_0;
     474             :     /* t = Tr_{Kn/Km} zeta_n^i; fill using Galois action */
     475       84756 :     if (!G)
     476       19138 :       gel(T, i + 1) = t;
     477             :     else
     478      370874 :       for (j = 1; j <= D; j++)
     479             :       {
     480      305256 :         long z = Fl_mul(i,G[j], n);
     481      305256 :         if (z < d) gel(T, z + 1) = t;
     482             :       }
     483             :   }
     484       16555 :   return mkvec3(Pm, Pn, T);
     485             : }
     486             : /* x a t_POL modulo Phi_n */
     487             : static GEN
     488       80255 : tracerel_i(GEN T, GEN x)
     489             : {
     490       80255 :   long k, l = lg(x);
     491             :   GEN S;
     492       80255 :   if (l == 2) return gen_0;
     493       80255 :   S = gmul(gel(T,1), gel(x,2));
     494      283290 :   for (k = 3; k < l; k++) S = gadd(S, gmul(gel(T,k-1), gel(x,k)));
     495       80255 :   return S;
     496             : }
     497             : static GEN
     498      253855 : tracerel(GEN a, GEN v, GEN z)
     499             : {
     500      253855 :   a = liftpol_shallow(a);
     501      253855 :   a = simplify_shallow(z? gmul(z,a): a);
     502      253855 :   if (typ(a) == t_POL)
     503             :   {
     504       80255 :     GEN T = gel(v,3);
     505       80255 :     long degrel = itou(gel(T,1));
     506       80255 :     a = tracerel_i(T, RgX_rem(a, gel(v,2)));
     507       80255 :     if (degrel != 1) a = gdivgu(a, degrel);
     508       80255 :     if (typ(a) == t_POL) a = RgX_rem(a, gel(v,1));
     509             :   }
     510      253855 :   return a;
     511             : }
     512             : static GEN
     513        6944 : tracerel_z(GEN v, long t)
     514             : {
     515        6944 :   GEN Pn = gel(v,2);
     516        6944 :   return t? pol_xn(t, varn(Pn)): NULL;
     517             : }
     518             : /* v = Qab_trace_init(n,m); x is a t_VEC of polmodulo Phi_n; Kn = Q(zeta_n)
     519             :  * [Kn:Km]^(-1) Tr_{Kn/Km} (zeta_n^t * x); 0 <= t < [Kn:Km] */
     520             : GEN
     521           0 : Qab_tracerel(GEN v, long t, GEN a)
     522             : {
     523           0 :   if (lg(v) != 4) return a; /* => t = 0 */
     524           0 :   return tracerel(a, v, tracerel_z(v, t));
     525             : }
     526             : GEN
     527       16093 : QabV_tracerel(GEN v, long t, GEN x)
     528             : {
     529             :   GEN z;
     530       16093 :   if (lg(v) != 4) return x; /* => t = 0 */
     531        6944 :   z = tracerel_z(v, t);
     532      260799 :   pari_APPLY_same(tracerel(gel(x,i), v, z));
     533             : }
     534             : GEN
     535         147 : QabM_tracerel(GEN v, long t, GEN x)
     536             : {
     537         147 :   if (lg(v) != 4) return x;
     538         105 :   pari_APPLY_same(QabV_tracerel(v, t, gel(x,i)));
     539             : }
     540             : 
     541             : /* C*zeta_o^k mod X^o - 1 */
     542             : static GEN
     543     2189572 : Qab_Czeta(long k, long o, GEN C, long vt)
     544             : {
     545     2189572 :   if (!k) return C;
     546     1456077 :   if (!odd(o))
     547             :   { /* optimization: reduce max degree by a factor 2 for free */
     548     1404970 :     o >>= 1;
     549     1404970 :     if (k >= o) { k -= o; C = gneg(C); if (!k) return C; }
     550             :   }
     551     1109451 :   return monomial(C, k, vt);
     552             : }
     553             : /* zeta_o^k */
     554             : static GEN
     555      200242 : Qab_zeta(long k, long o, long vt) { return Qab_Czeta(k, o, gen_1, vt); }
     556             : 
     557             : /*              Operations on Dirichlet characters                       */
     558             : 
     559             : /* A Dirichlet character can be given in GP in different formats, but in this
     560             :  * package, it will be a vector CHI=[G,chi,ord], where G is the (Z/MZ)^* to
     561             :  * which the character belongs, chi is the character in Conrey format, ord is
     562             :  * the order */
     563             : 
     564             : static GEN
     565     3722761 : gmfcharorder(GEN CHI) { return gel(CHI, 3); }
     566             : long
     567     3684408 : mfcharorder(GEN CHI) { return itou(gmfcharorder(CHI)); }
     568             : static long
     569        2709 : mfcharistrivial(GEN CHI) { return !CHI || mfcharorder(CHI) == 1; }
     570             : static GEN
     571     1560188 : gmfcharmodulus(GEN CHI) { return gmael3(CHI, 1, 1, 1); }
     572             : long
     573     1560188 : mfcharmodulus(GEN CHI) { return itou(gmfcharmodulus(CHI)); }
     574             : GEN
     575      564011 : mfcharpol(GEN CHI) { return gel(CHI,4); }
     576             : 
     577             : /* vz[i+1] = image of (zeta_o)^i in Fp */
     578             : static ulong
     579      221459 : Qab_Czeta_Fl(long k, GEN vz, ulong C, ulong p)
     580             : {
     581             :   long o;
     582      221459 :   if (!k) return C;
     583      148911 :   o = lg(vz)-2;
     584      148911 :   if ((k << 1) == o) return Fl_neg(C,p);
     585      123123 :   return Fl_mul(C, vz[k+1], p);
     586             : }
     587             : 
     588             : static long
     589     2508317 : znchareval_i(GEN CHI, long n, GEN ord)
     590     2508317 : { return itos(znchareval(gel(CHI,1), gel(CHI,2), stoi(n), ord)); }
     591             : 
     592             : /* n coprime with the modulus of CHI */
     593             : static GEN
     594       14217 : mfchareval(GEN CHI, long n)
     595             : {
     596       14217 :   GEN Pn, C, go = gmfcharorder(CHI);
     597       14217 :   long k, o = go[2];
     598       14217 :   if (o == 1) return gen_1;
     599        7378 :   k = znchareval_i(CHI, n, go);
     600        7378 :   Pn = mfcharpol(CHI);
     601        7378 :   C = Qab_zeta(k, o, varn(Pn));
     602        7378 :   if (typ(C) != t_POL) return C;
     603        5320 :   return gmodulo(C, Pn);
     604             : }
     605             : /* d a multiple of ord(CHI); n coprime with char modulus;
     606             :  * return x s.t. CHI(n) = \zeta_d^x] */
     607             : static long
     608     3563112 : mfcharevalord(GEN CHI, long n, long d)
     609             : {
     610     3563112 :   if (mfcharorder(CHI) == 1) return 0;
     611     2497243 :   return znchareval_i(CHI, n, utoi(d));
     612             : }
     613             : 
     614             : /* G a znstar, L a Conrey log: return a 'mfchar' */
     615             : static GEN
     616      373877 : mfcharGL(GEN G, GEN L)
     617             : {
     618      373877 :   GEN o = zncharorder(G,L);
     619      373877 :   long ord = itou(o), vt = fetch_user_var("t");
     620      373877 :   return mkvec4(G, L, o, polcyclo(ord,vt));
     621             : }
     622             : static GEN
     623        5775 : mfchartrivial()
     624        5775 : { return mfcharGL(znstar0(gen_1,1), cgetg(1,t_COL)); }
     625             : /* convert a generic character into an 'mfchar' */
     626             : static GEN
     627        4039 : get_mfchar(GEN CHI)
     628             : {
     629             :   GEN G, L;
     630        4039 :   if (typ(CHI) != t_VEC) CHI = znchar(CHI);
     631             :   else
     632             :   {
     633         889 :     long l = lg(CHI);
     634         889 :     if ((l != 3 && l != 5) || !checkznstar_i(gel(CHI,1)))
     635           7 :       pari_err_TYPE("checkNF [chi]", CHI);
     636         882 :     if (l == 5) return CHI;
     637             :   }
     638        3969 :   G = gel(CHI,1);
     639        3969 :   L = gel(CHI,2); if (typ(L) != t_COL) L = znconreylog(G,L);
     640        3969 :   return mfcharGL(G, L);
     641             : }
     642             : 
     643             : /* parse [N], [N,k], [N,k,CHI]. If 'joker' is set, allow wildcard for CHI */
     644             : static GEN
     645        9191 : checkCHI(GEN NK, long N, int joker)
     646             : {
     647             :   GEN CHI;
     648        9191 :   if (lg(NK) == 3)
     649         693 :     CHI = mfchartrivial();
     650             :   else
     651             :   {
     652             :     long i, l;
     653        8498 :     CHI = gel(NK,3); l = lg(CHI);
     654        8498 :     if (isintzero(CHI) && joker)
     655        4116 :       CHI = NULL; /* all character orbits */
     656        4382 :     else if (isintm1(CHI) && joker > 1)
     657        2373 :       CHI = gen_m1; /* sum over all character orbits */
     658        2009 :     else if ((typ(CHI) == t_VEC &&
     659         217 :              (l == 1 || l != 3 || !checkznstar_i(gel(CHI,1)))) && joker)
     660             :     {
     661         133 :       CHI = shallowtrans(CHI); /* list of characters */
     662         952 :       for (i = 1; i < l; i++) gel(CHI,i) = get_mfchar(gel(CHI,i));
     663             :     }
     664             :     else
     665             :     {
     666        1876 :       CHI = get_mfchar(CHI); /* single char */
     667        1876 :       if (N % mfcharmodulus(CHI)) pari_err_TYPE("checkNF [chi]", NK);
     668             :     }
     669             :   }
     670        9177 :   return CHI;
     671             : }
     672             : /* support half-integral weight */
     673             : static void
     674        9198 : checkNK2(GEN NK, long *N, long *nk, long *dk, GEN *CHI, int joker)
     675             : {
     676        9198 :   long l = lg(NK);
     677             :   GEN T;
     678        9198 :   if (typ(NK) != t_VEC || l < 3 || l > 4) pari_err_TYPE("checkNK", NK);
     679        9198 :   T = gel(NK,1); if (typ(T) != t_INT) pari_err_TYPE("checkNF [N]", NK);
     680        9198 :   *N = itos(T); if (*N <= 0) pari_err_TYPE("checkNF [N <= 0]", NK);
     681        9198 :   T = gel(NK,2);
     682        9198 :   switch(typ(T))
     683             :   {
     684        5817 :     case t_INT:  *nk = itos(T); *dk = 1; break;
     685        3374 :     case t_FRAC:
     686        3374 :       *nk = itos(gel(T,1));
     687        3374 :       *dk = itou(gel(T,2)); if (*dk == 2) break;
     688           7 :     default: pari_err_TYPE("checkNF [k]", NK);
     689             :   }
     690        9191 :   *CHI = checkCHI(NK, *N, joker);
     691        9177 : }
     692             : /* don't support half-integral weight */
     693             : static void
     694         133 : checkNK(GEN NK, long *N, long *k, GEN *CHI, int joker)
     695             : {
     696             :   long d;
     697         133 :   checkNK2(NK, N, k, &d, CHI, joker);
     698         133 :   if (d != 1) pari_err_TYPE("checkNF [k]", NK);
     699         133 : }
     700             : 
     701             : static GEN
     702        4872 : mfchargalois(long N, int odd, GEN flagorder)
     703             : {
     704        4872 :   GEN G = znstar0(utoi(N), 1), L = chargalois(G, flagorder);
     705        4872 :   long l = lg(L), i, j;
     706      113526 :   for (i = j = 1; i < l; i++)
     707             :   {
     708      108654 :     GEN chi = znconreyfromchar(G, gel(L,i));
     709      108654 :     if (zncharisodd(G,chi) == odd) gel(L,j++) = mfcharGL(G,chi);
     710             :   }
     711        4872 :   setlg(L, j); return L;
     712             : }
     713             : /* possible characters for nontrivial S_1(N, chi) */
     714             : static GEN
     715        1729 : mf1chars(long N, GEN vCHI)
     716             : {
     717        1729 :   if (vCHI) return vCHI; /*do not filter, user knows best*/
     718             :   /* Tate's theorem */
     719        1659 :   return mfchargalois(N, 1, uisprime(N)? mkvecsmall2(2,4): NULL);
     720             : }
     721             : static GEN
     722        3255 : mfchars(long N, long k, long dk, GEN vCHI)
     723        3255 : { return vCHI? vCHI: mfchargalois(N, (dk == 2)? 0: (k & 1), NULL); }
     724             : 
     725             : /* wrappers from mfchar to znchar */
     726             : static long
     727       68369 : mfcharparity(GEN CHI)
     728             : {
     729       68369 :   if (!CHI) return 1;
     730       68369 :   return zncharisodd(gel(CHI,1), gel(CHI,2)) ? -1 : 1;
     731             : }
     732             : /* if CHI is primitive, return CHI itself, not a copy */
     733             : static GEN
     734       76307 : mfchartoprimitive(GEN CHI, long *pF)
     735             : {
     736             :   pari_sp av;
     737             :   GEN chi, F;
     738       76307 :   if (!CHI) { if (pF) *pF = 1; return mfchartrivial(); }
     739       76307 :   av = avma; F = znconreyconductor(gel(CHI,1), gel(CHI,2), &chi);
     740       76307 :   if (typ(F) == t_INT) set_avma(av);
     741             :   else
     742             :   {
     743        7840 :     CHI = leafcopy(CHI);
     744        7840 :     gel(CHI,1) = znstar0(F, 1);
     745        7840 :     gel(CHI,2) = chi;
     746             :   }
     747       76307 :   if (pF) *pF = mfcharmodulus(CHI);
     748       76307 :   return CHI;
     749             : }
     750             : static long
     751      397327 : mfcharconductor(GEN CHI)
     752             : {
     753      397327 :   pari_sp av = avma;
     754      397327 :   GEN res = znconreyconductor(gel(CHI,1), gel(CHI,2), NULL);
     755      397327 :   if (typ(res) == t_VEC) res = gel(res, 1);
     756      397327 :   return gc_long(av, itos(res));
     757             : }
     758             : 
     759             : /*                      Operations on mf closures                    */
     760             : static GEN
     761       62615 : tagparams(long t, GEN NK) { return mkvec2(mkvecsmall(t), NK); }
     762             : static GEN
     763        1141 : lfuntag(long t, GEN x) { return mkvec2(mkvecsmall(t), x); }
     764             : static GEN
     765          56 : tag0(long t, GEN NK) { retmkvec(tagparams(t,NK)); }
     766             : static GEN
     767       10220 : tag(long t, GEN NK, GEN x) { retmkvec2(tagparams(t,NK), x); }
     768             : static GEN
     769       36127 : tag2(long t, GEN NK, GEN x, GEN y) { retmkvec3(tagparams(t,NK), x,y); }
     770             : static GEN
     771       16086 : tag3(long t, GEN NK, GEN x,GEN y,GEN z) { retmkvec4(tagparams(t,NK), x,y,z); }
     772             : static GEN
     773           0 : tag4(long t, GEN NK, GEN x,GEN y,GEN z,GEN a)
     774           0 : { retmkvec5(tagparams(t,NK), x,y,z,a); }
     775             : /* is F a "modular form" ? */
     776             : int
     777       18627 : checkmf_i(GEN F)
     778       18627 : { return typ(F) == t_VEC
     779       17808 :     && lg(F) > 1 && typ(gel(F,1)) == t_VEC
     780       13111 :     && lg(gel(F,1)) == 3
     781       12950 :     && typ(gmael(F,1,1)) == t_VECSMALL
     782       36435 :     && typ(gmael(F,1,2)) == t_VEC; }
     783      231581 : long mf_get_type(GEN F) { return gmael(F,1,1)[1]; }
     784      183960 : GEN mf_get_gN(GEN F) { return gmael3(F,1,2,1); }
     785      138943 : GEN mf_get_gk(GEN F) { return gmael3(F,1,2,2); }
     786             : /* k - 1/2, assume k in 1/2 + Z */
     787         441 : long mf_get_r(GEN F) { return itou(gel(mf_get_gk(F),1)) >> 1; }
     788      118860 : long mf_get_N(GEN F) { return itou(mf_get_gN(F)); }
     789       71694 : long mf_get_k(GEN F)
     790             : {
     791       71694 :   GEN gk = mf_get_gk(F);
     792       71694 :   if (typ(gk)!=t_INT) pari_err_IMPL("half-integral weight");
     793       71694 :   return itou(gk);
     794             : }
     795       61894 : GEN mf_get_CHI(GEN F) { return gmael3(F,1,2,3); }
     796       24262 : GEN mf_get_field(GEN F) { return gmael3(F,1,2,4); }
     797       18767 : GEN mf_get_NK(GEN F) { return gmael(F,1,2); }
     798             : static void
     799         525 : mf_setfield(GEN f, GEN P)
     800             : {
     801         525 :   gel(f,1) = leafcopy(gel(f,1));
     802         525 :   gmael(f,1,2) = leafcopy(gmael(f,1,2));
     803         525 :   gmael3(f,1,2,4) = P;
     804         525 : }
     805             : 
     806             : /* UTILITY FUNCTIONS */
     807             : GEN
     808        9065 : mftocol(GEN F, long lim, long d)
     809        9065 : { GEN c = mfcoefs_i(F, lim, d); settyp(c,t_COL); return c; }
     810             : GEN
     811        2093 : mfvectomat(GEN vF, long lim, long d)
     812             : {
     813        2093 :   long j, l = lg(vF);
     814        2093 :   GEN M = cgetg(l, t_MAT);
     815       10339 :   for (j = 1; j < l; j++) gel(M,j) = mftocol(gel(vF,j), lim, d);
     816        2093 :   return M;
     817             : }
     818             : 
     819             : static GEN
     820        4655 : RgV_to_ser_full(GEN x) { return RgV_to_ser(x, 0, lg(x)+1); }
     821             : /* TODO: delete */
     822             : static GEN
     823         665 : mfcoefsser(GEN F, long n) { return RgV_to_ser_full(mfcoefs_i(F,n,1)); }
     824             : static GEN
     825         833 : sertovecslice(GEN S, long n)
     826             : {
     827         833 :   GEN v = gtovec0(S, -(lg(S) - 2 + valser(S)));
     828         833 :   long l = lg(v), n2 = n + 2;
     829         833 :   if (l < n2) pari_err_BUG("sertovecslice [n too large]");
     830         833 :   return (l == n2)? v: vecslice(v, 1, n2-1);
     831             : }
     832             : 
     833             : /* a, b two RgV of the same length, multiply as truncated power series */
     834             : static GEN
     835        3381 : RgV_mul_RgXn(GEN a, GEN b)
     836             : {
     837        3381 :   long n = lg(a)-1;
     838             :   GEN c;
     839        3381 :   a = RgV_to_RgX(a,0);
     840        3381 :   b = RgV_to_RgX(b,0); c = RgXn_mul(a, b, n);
     841        3381 :   c = RgX_to_RgC(c,n); settyp(c,t_VEC); return c;
     842             : }
     843             : /* divide as truncated power series */
     844             : static GEN
     845         399 : RgV_div_RgXn(GEN a, GEN b)
     846             : {
     847         399 :   long n = lg(a)-1;
     848             :   GEN c;
     849         399 :   a = RgV_to_RgX(a,0);
     850         399 :   b = RgV_to_RgX(b,0); c = RgXn_div_i(a, b, n);
     851         399 :   c = RgX_to_RgC(c,n); settyp(c,t_VEC); return c;
     852             : }
     853             : /* a^b */
     854             : static GEN
     855         112 : RgV_pows_RgXn(GEN a, long b)
     856             : {
     857         112 :   long n = lg(a)-1;
     858             :   GEN c;
     859         112 :   a = RgV_to_RgX(a,0);
     860         112 :   if (b < 0) { a = RgXn_inv(a, n); b = -b; }
     861         112 :   c = RgXn_powu_i(a,b,n);
     862         112 :   c = RgX_to_RgC(c,n); settyp(c,t_VEC); return c;
     863             : }
     864             : 
     865             : /* assume lg(V) >= n*d + 2 */
     866             : static GEN
     867        8778 : c_deflate(long n, long d, GEN v)
     868             : {
     869        8778 :   long i, id, l = n+2;
     870             :   GEN w;
     871        8778 :   if (d == 1) return lg(v) == l ? v: vecslice(v, 1, l-1);
     872         574 :   w = cgetg(l, typ(v));
     873       11123 :   for (i = id = 1; i < l; i++, id += d) gel(w, i) = gel(v, id);
     874         574 :   return w;
     875             : }
     876             : 
     877             : static void
     878          14 : err_cyclo(void)
     879          14 : { pari_err_IMPL("changing cyclotomic fields in mf"); }
     880             : /* Q(zeta_a) = Q(zeta_b) ? */
     881             : static int
     882         616 : same_cyc(long a, long b)
     883         616 : { return (a == b) || (odd(a) && b == (a<<1)) || (odd(b) && a == (b<<1)); }
     884             : /* need to combine elements in Q(CHI1) and Q(CHI2) with result in Q(CHI),
     885             :  * CHI = CHI1 * CHI2 or CHI / CHI2 times some character of order 2 */
     886             : static GEN
     887        2723 : chicompat(GEN CHI, GEN CHI1, GEN CHI2)
     888             : {
     889        2723 :   long o1 = mfcharorder(CHI1);
     890        2723 :   long o2 = mfcharorder(CHI2), O, o;
     891             :   GEN T1, T2, P, Po;
     892        2723 :   if (o1 <= 2 && o2 <= 2) return NULL;
     893         623 :   o = mfcharorder(CHI);
     894         623 :   Po = mfcharpol(CHI);
     895         623 :   P = mfcharpol(CHI1);
     896         623 :   if (o1 == o2)
     897             :   {
     898          21 :     if (o1 == o) return NULL;
     899          14 :     if (!same_cyc(o1,o)) err_cyclo();
     900           0 :     return mkvec4(P, gen_1,gen_1, Qab_trace_init(o1, o, P, Po));
     901             :   }
     902         602 :   O = ulcm(o1, o2);
     903         602 :   if (!same_cyc(O,o)) err_cyclo();
     904         602 :   if (O != o1) P = (O == o2)? mfcharpol(CHI2): polcyclo(O, varn(P));
     905         602 :   T1 = o1 <= 2? gen_1: utoipos(O / o1);
     906         602 :   T2 = o2 <= 2? gen_1: utoipos(O / o2);
     907         602 :   return mkvec4(P, T1, T2, O == o? gen_1: Qab_trace_init(O, o, P, Po));
     908             : }
     909             : static GEN
     910          49 : inflatemod(GEN f, long o, GEN P)
     911             : {
     912          49 :   f = lift_shallow(f);
     913          49 :   return gmodulo(typ(f)==t_POL? RgX_inflate(f,o): f, P);
     914             : }
     915             : static GEN
     916           7 : RgV_inflatemod(GEN x, long o, GEN P)
     917          56 : { pari_APPLY_same(inflatemod(gel(x,i), o, P)); }
     918             : /* *F a vector of cyclotomic numbers */
     919             : static void
     920         651 : chicompatlift(GEN T, GEN *F, GEN *G)
     921             : {
     922         651 :   long o1 = itou(gel(T,2)), o2 = itou(gel(T,3));
     923         651 :   GEN P = gel(T,1);
     924         651 :   if (o1 != 1) *F = RgV_inflatemod(*F, o1, P);
     925         651 :   if (o2 != 1 && G) *G = RgV_inflatemod(*G, o2, P);
     926         651 : }
     927             : static GEN
     928         651 : chicompatfix(GEN T, GEN F)
     929             : {
     930         651 :   GEN V = gel(T,4);
     931         651 :   if (typ(V) == t_VEC) F = gmodulo(QabV_tracerel(V, 0, F), gel(V,1));
     932         651 :   return F;
     933             : }
     934             : 
     935             : static GEN
     936         637 : c_mul(long n, long d, GEN S)
     937             : {
     938         637 :   pari_sp av = avma;
     939         637 :   long nd = n*d;
     940         637 :   GEN F = gel(S,2), G = gel(S,3);
     941         637 :   F = mfcoefs_i(F, nd, 1);
     942         637 :   G = mfcoefs_i(G, nd, 1);
     943         637 :   if (lg(S) == 5) chicompatlift(gel(S,4),&F,&G);
     944         637 :   F = c_deflate(n, d, RgV_mul_RgXn(F,G));
     945         637 :   if (lg(S) == 5) F = chicompatfix(gel(S,4), F);
     946         637 :   return gerepilecopy(av, F);
     947             : }
     948             : static GEN
     949         112 : c_pow(long n, long d, GEN S)
     950             : {
     951         112 :   pari_sp av = avma;
     952         112 :   long nd = n*d;
     953         112 :   GEN F = gel(S,2), a = gel(S,3), f = mfcoefs_i(F,nd,1);
     954         112 :   if (lg(S) == 5) chicompatlift(gel(S,4),&F, NULL);
     955         112 :   f = RgV_pows_RgXn(f, itos(a));
     956         112 :   f = c_deflate(n, d, f);
     957         112 :   if (lg(S) == 5) f = chicompatfix(gel(S,4), f);
     958         112 :   return gerepilecopy(av, f);
     959             : }
     960             : 
     961             : /* F * Theta */
     962             : static GEN
     963         448 : mfmultheta(GEN F)
     964             : {
     965         448 :   if (typ(mf_get_gk(F)) == t_FRAC && mf_get_type(F) == t_MF_DIV)
     966             :   {
     967         154 :     GEN T = gel(F,3); /* hopefully mfTheta() */
     968         154 :     if (mf_get_type(T) == t_MF_THETA && mf_get_N(T) == 4) return gel(F,2);
     969             :   }
     970         294 :   return mfmul(F, mfTheta(NULL));
     971             : }
     972             : 
     973             : static GEN
     974          42 : c_bracket(long n, long d, GEN S)
     975             : {
     976          42 :   pari_sp av = avma;
     977          42 :   long i, nd = n*d;
     978          42 :   GEN F = gel(S,2), G = gel(S,3), tF, tG, C, mpow, res, gk, gl;
     979          42 :   GEN VF = mfcoefs_i(F, nd, 1);
     980          42 :   GEN VG = mfcoefs_i(G, nd, 1);
     981          42 :   ulong j, m = itou(gel(S,4));
     982             : 
     983          42 :   if (!n)
     984             :   {
     985          14 :     if (m > 0) { set_avma(av); return mkvec(gen_0); }
     986           7 :     return gerepilecopy(av, mkvec(gmul(gel(VF, 1), gel(VG, 1))));
     987             :   }
     988          28 :   tF = cgetg(nd+2, t_VEC);
     989          28 :   tG = cgetg(nd+2, t_VEC);
     990          28 :   res = NULL; gk = mf_get_gk(F); gl = mf_get_gk(G);
     991             :   /* pow[i,j+1] = i^j */
     992          28 :   if (lg(S) == 6) chicompatlift(gel(S,5),&VF,&VG);
     993          28 :   mpow = cgetg(m+2, t_MAT);
     994          28 :   gel(mpow,1) = const_col(nd, gen_1);
     995          56 :   for (j = 1; j <= m; j++)
     996             :   {
     997          28 :     GEN c = cgetg(nd+1, t_COL);
     998          28 :     gel(mpow,j+1) = c;
     999         245 :     for (i = 1; i <= nd; i++) gel(c,i) = muliu(gcoeff(mpow,i,j), i);
    1000             :   }
    1001          28 :   C = binomial(gaddgs(gk, m-1), m);
    1002          28 :   if (odd(m)) C = gneg(C);
    1003          84 :   for (j = 0; j <= m; j++)
    1004             :   { /* C = (-1)^(m-j) binom(m+l-1, j) binom(m+k-1,m-j) */
    1005             :     GEN c;
    1006          56 :     gel(tF,1) = j == 0? gel(VF,1): gen_0;
    1007          56 :     gel(tG,1) = j == m? gel(VG,1): gen_0;
    1008          56 :     gel(tF,2) = gel(VF,2); /* assume nd >= 1 */
    1009          56 :     gel(tG,2) = gel(VG,2);
    1010         518 :     for (i = 2; i <= nd; i++)
    1011             :     {
    1012         462 :       gel(tF, i+1) = gmul(gcoeff(mpow,i,j+1),   gel(VF, i+1));
    1013         462 :       gel(tG, i+1) = gmul(gcoeff(mpow,i,m-j+1), gel(VG, i+1));
    1014             :     }
    1015          56 :     c = gmul(C, c_deflate(n, d, RgV_mul_RgXn(tF, tG)));
    1016          56 :     res = res? gadd(res, c): c;
    1017          56 :     if (j < m)
    1018          56 :       C = gdiv(gmul(C, gmulsg(m-j, gaddgs(gl,m-j-1))),
    1019          28 :                gmulsg(-(j+1), gaddgs(gk,j)));
    1020             :   }
    1021          28 :   if (lg(S) == 6) res = chicompatfix(gel(S,5), res);
    1022          28 :   return gerepileupto(av, res);
    1023             : }
    1024             : /* linear combination \sum L[j] vecF[j] */
    1025             : static GEN
    1026        2975 : c_linear(long n, long d, GEN F, GEN L, GEN dL)
    1027             : {
    1028        2975 :   pari_sp av = avma;
    1029        2975 :   long j, l = lg(L);
    1030        2975 :   GEN S = NULL;
    1031       10640 :   for (j = 1; j < l; j++)
    1032             :   {
    1033        7665 :     GEN c = gel(L,j);
    1034        7665 :     if (gequal0(c)) continue;
    1035        6909 :     c = gmul(c, mfcoefs_i(gel(F,j), n, d));
    1036        6909 :     S = S? gadd(S,c): c;
    1037             :   }
    1038        2975 :   if (!S) return zerovec(n+1);
    1039        2975 :   if (!is_pm1(dL)) S = gdiv(S, dL);
    1040        2975 :   return gerepileupto(av, S);
    1041             : }
    1042             : 
    1043             : /* B_d(T_j Trace^new) as t_MF_BD(t_MF_HECKE(t_MF_NEWTRACE)) or
    1044             :  * t_MF_HECKE(t_MF_NEWTRACE)
    1045             :  * or t_MF_NEWTRACE in level N. Set d and j, return t_MF_NEWTRACE component*/
    1046             : static GEN
    1047       82481 : bhn_parse(GEN f, long *d, long *j)
    1048             : {
    1049       82481 :   long t = mf_get_type(f);
    1050       82481 :   *d = *j = 1;
    1051       82481 :   if (t == t_MF_BD) { *d = itos(gel(f,3)); f = gel(f,2); t = mf_get_type(f); }
    1052       82481 :   if (t == t_MF_HECKE) { *j = gel(f,2)[1]; f = gel(f,3); }
    1053       82481 :   return f;
    1054             : }
    1055             : /* f as above, return the t_MF_NEWTRACE component */
    1056             : static GEN
    1057       32494 : bhn_newtrace(GEN f)
    1058             : {
    1059       32494 :   long t = mf_get_type(f);
    1060       32494 :   if (t == t_MF_BD) { f = gel(f,2); t = mf_get_type(f); }
    1061       32494 :   if (t == t_MF_HECKE) f = gel(f,3);
    1062       32494 :   return f;
    1063             : }
    1064             : static int
    1065        3976 : ok_bhn_linear(GEN vf)
    1066             : {
    1067        3976 :   long i, N0 = 0, l = lg(vf);
    1068             :   GEN CHI, gk;
    1069        3976 :   if (l == 1) return 1;
    1070        3976 :   gk = mf_get_gk(gel(vf,1));
    1071        3976 :   CHI = mf_get_CHI(gel(vf,1));
    1072       27321 :   for (i = 1; i < l; i++)
    1073             :   {
    1074       25676 :     GEN f = bhn_newtrace(gel(vf,i));
    1075       25676 :     long N = mf_get_N(f);
    1076       25676 :     if (mf_get_type(f) != t_MF_NEWTRACE) return 0;
    1077       23345 :     if (N < N0) return 0; /* largest level must come last */
    1078       23345 :     N0 = N;
    1079       23345 :     if (!gequal(gk,mf_get_gk(f))) return 0; /* same k */
    1080       23345 :     if (!gequal(gel(mf_get_CHI(f),2), gel(CHI,2))) return 0; /* same CHI */
    1081             :   }
    1082        1645 :   return 1;
    1083             : }
    1084             : 
    1085             : /* vF not empty, same hypotheses as bhnmat_extend */
    1086             : static GEN
    1087        6923 : bhnmat_extend_nocache(GEN M, long N, long n, long d, GEN vF)
    1088             : {
    1089             :   cachenew_t cache;
    1090        6923 :   long l = lg(vF);
    1091             :   GEN f;
    1092        6923 :   if (l == 1) return M? M: cgetg(1, t_MAT);
    1093        6818 :   f = bhn_newtrace(gel(vF,1)); /* N.B. mf_get_N(f) divides N */
    1094        6818 :   init_cachenew(&cache, n*d, N, f);
    1095        6818 :   M = bhnmat_extend(M, n, d, vF, &cache);
    1096        6818 :   dbg_cachenew(&cache); return M;
    1097             : }
    1098             : /* c_linear of "bhn" mf closures, same hypotheses as bhnmat_extend */
    1099             : static GEN
    1100        2233 : c_linear_bhn(long n, long d, GEN F)
    1101             : {
    1102             :   pari_sp av;
    1103        2233 :   GEN M, v, vF = gel(F,2), L = gel(F,3), dL = gel(F,4);
    1104        2233 :   if (lg(L) == 1) return zerovec(n+1);
    1105        2233 :   av = avma;
    1106        2233 :   M = bhnmat_extend_nocache(NULL, mf_get_N(F), n, d, vF);
    1107        2233 :   v = RgM_RgC_mul(M,L); settyp(v, t_VEC);
    1108        2233 :   if (!is_pm1(dL)) v = gdiv(v, dL);
    1109        2233 :   return gerepileupto(av, v);
    1110             : }
    1111             : 
    1112             : /* c in K, K := Q[X]/(T) vz = vector of consecutive powers of root z of T
    1113             :  * attached to an embedding s: K -> C. Return s(c) in C */
    1114             : static GEN
    1115       84658 : Rg_embed1(GEN c, GEN vz)
    1116             : {
    1117       84658 :   long t = typ(c);
    1118       84658 :   if (t == t_POLMOD) { c = gel(c,2); t = typ(c); }
    1119       84658 :   if (t == t_POL) c = RgX_RgV_eval(c, vz);
    1120       84658 :   return c;
    1121             : }
    1122             : /* return s(x) in C[X] */
    1123             : static GEN
    1124       14203 : RgX_embed1(GEN x, GEN vz)
    1125       42042 : { pari_APPLY_pol(Rg_embed1(gel(x,i), vz)); }
    1126             : /* return s(x) in C^n */
    1127             : static GEN
    1128         798 : vecembed1(GEN x, GEN vz)
    1129       39858 : { pari_APPLY_same(Rg_embed1(gel(x,i), vz)); }
    1130             : /* P in L = K[X]/(U), K = Q[t]/T; s an embedding of K -> C attached
    1131             :  * to a root of T, extended to an embedding of L -> C attached to a root
    1132             :  * of s(U); vT powers of the root of T, vU powers of the root of s(U).
    1133             :  * Return s(P) in C^n */
    1134             : static GEN
    1135       13328 : Rg_embed2(GEN P, long vt, GEN vT, GEN vU)
    1136             : {
    1137       13328 :   P = liftpol_shallow(P);
    1138       13328 :   if (typ(P) != t_POL) return P;
    1139       13300 :   if (varn(P) == vt) return Rg_embed1(P, vT);
    1140       13293 :   return Rg_embed1(RgX_embed1(P, vT), vU); /* varn(P) == vx */
    1141             : }
    1142             : static GEN
    1143          42 : vecembed2(GEN x, long vt, GEN vT, GEN vU)
    1144        1050 : { pari_APPLY_same(Rg_embed2(gel(x,i), vt, vT, vU)); }
    1145             : static GEN
    1146         532 : RgX_embed2(GEN x, long vt, GEN vT, GEN vU)
    1147        3724 : { pari_APPLY_pol(Rg_embed2(gel(x,i), vt, vT, vU)); }
    1148             : /* embed polynomial f in variable 0 [ may be a scalar ], E from getembed */
    1149             : static GEN
    1150        1673 : RgX_embed(GEN f, GEN E)
    1151             : {
    1152             :   GEN vT;
    1153        1673 :   if (typ(f) != t_POL || varn(f) != 0) return mfembed(E, f);
    1154        1631 :   if (lg(E) == 1) return f;
    1155        1407 :   vT = gel(E,2);
    1156        1407 :   if (lg(E) == 3)
    1157         875 :     f = RgX_embed1(f, vT);
    1158             :   else
    1159         532 :     f = RgX_embed2(f, varn(gel(E,1)), vT, gel(E,3));
    1160        1407 :   return f;
    1161             : }
    1162             : /* embed vector, E from getembed */
    1163             : GEN
    1164        1708 : mfvecembed(GEN E, GEN v)
    1165             : {
    1166             :   GEN vT;
    1167        1708 :   if (lg(E) == 1) return v;
    1168         840 :   vT = gel(E,2);
    1169         840 :   if (lg(E) == 3)
    1170         798 :     v = vecembed1(v, vT);
    1171             :   else
    1172          42 :     v = vecembed2(v, varn(gel(E,1)), vT, gel(E,3));
    1173         840 :   return v;
    1174             : }
    1175             : GEN
    1176          70 : mfmatembed(GEN E, GEN x)
    1177             : {
    1178          70 :   if (lg(E) == 1) return x;
    1179         168 :   pari_APPLY_same(mfvecembed(E, gel(x,i)));
    1180             : }
    1181             : /* embed vector of polynomials in var 0 */
    1182             : static GEN
    1183          98 : RgXV_embed(GEN x, GEN E)
    1184             : {
    1185          98 :   if (lg(E) == 1) return x;
    1186        1358 :   pari_APPLY_same(RgX_embed(gel(x,i), E));
    1187             : }
    1188             : 
    1189             : /* embed scalar */
    1190             : GEN
    1191      100670 : mfembed(GEN E, GEN f)
    1192             : {
    1193             :   GEN vT;
    1194      100670 :   if (lg(E) == 1) return f;
    1195       13587 :   vT = gel(E,2);
    1196       13587 :   if (lg(E) == 3)
    1197        4459 :     f = Rg_embed1(f, vT);
    1198             :   else
    1199        9128 :     f = Rg_embed2(f, varn(gel(E,1)), vT, gel(E,3));
    1200       13587 :   return f;
    1201             : }
    1202             : /* vector of the sigma(f), sigma in vE */
    1203             : static GEN
    1204         350 : RgX_embedall(GEN f, GEN vE)
    1205             : {
    1206         350 :   long i, l = lg(vE);
    1207             :   GEN v;
    1208         350 :   if (l == 2) return RgX_embed(f, gel(vE,1));
    1209          35 :   v = cgetg(l, t_VEC);
    1210         105 :   for (i = 1; i < l; i++) gel(v,i) = RgX_embed(f, gel(vE,i));
    1211          35 :   return v;
    1212             : }
    1213             : /* matrix whose colums are the sigma(v), sigma in vE */
    1214             : static GEN
    1215         350 : RgC_embedall(GEN v, GEN vE)
    1216             : {
    1217         350 :   long j, l = lg(vE);
    1218         350 :   GEN M = cgetg(l, t_MAT);
    1219         875 :   for (j = 1; j < l; j++) gel(M,j) = mfvecembed(gel(vE,j), v);
    1220         350 :   return M;
    1221             : }
    1222             : /* vector of the sigma(v), sigma in vE */
    1223             : static GEN
    1224        4907 : Rg_embedall_i(GEN v, GEN vE)
    1225             : {
    1226        4907 :   long j, l = lg(vE);
    1227        4907 :   GEN M = cgetg(l, t_VEC);
    1228       14735 :   for (j = 1; j < l; j++) gel(M,j) = mfembed(gel(vE,j), v);
    1229        4907 :   return M;
    1230             : }
    1231             : /* vector of the sigma(v), sigma in vE; if #vE == 1, return v */
    1232             : static GEN
    1233       95000 : Rg_embedall(GEN v, GEN vE)
    1234       95000 : { return (lg(vE) == 2)? mfembed(gel(vE,1), v): Rg_embedall_i(v, vE); }
    1235             : 
    1236             : static GEN
    1237         833 : c_div_i(long n, GEN S)
    1238             : {
    1239         833 :   GEN F = gel(S,2), G = gel(S,3);
    1240             :   GEN a0, a0i, H;
    1241         833 :   F = mfcoefs_i(F, n, 1);
    1242         833 :   G = mfcoefs_i(G, n, 1);
    1243         833 :   if (lg(S) == 5) chicompatlift(gel(S,4),&F,&G);
    1244         833 :   F = RgV_to_ser_full(F);
    1245         833 :   G = RgV_to_ser_full(G);
    1246         833 :   a0 = polcoef_i(G, 0, -1); /* != 0 */
    1247         833 :   if (gequal1(a0)) a0 = a0i = NULL;
    1248             :   else
    1249             :   {
    1250         602 :     a0i = ginv(a0);
    1251         602 :     G = gmul(ser_unscale(G,a0), a0i);
    1252         602 :     F = gmul(ser_unscale(F,a0), a0i);
    1253             :   }
    1254         833 :   H = gdiv(F, G);
    1255         833 :   if (a0) H = ser_unscale(H,a0i);
    1256         833 :   H = sertovecslice(H, n);
    1257         833 :   if (lg(S) == 5) H = chicompatfix(gel(S,4), H);
    1258         833 :   return H;
    1259             : }
    1260             : static GEN
    1261         833 : c_div(long n, long d, GEN S)
    1262             : {
    1263         833 :   pari_sp av = avma;
    1264         833 :   GEN D = (d==1)? c_div_i(n, S): c_deflate(n, d, c_div_i(n*d, S));
    1265         833 :   return gerepilecopy(av, D);
    1266             : }
    1267             : 
    1268             : static GEN
    1269          35 : c_shift(long n, long d, GEN F, GEN gsh)
    1270             : {
    1271          35 :   pari_sp av = avma;
    1272             :   GEN vF;
    1273          35 :   long sh = itos(gsh), n1 = n*d + sh;
    1274          35 :   if (n1 < 0) return zerovec(n+1);
    1275          35 :   vF = mfcoefs_i(F, n1, 1);
    1276          35 :   if (sh < 0) vF = shallowconcat(zerovec(-sh), vF);
    1277          35 :   else vF = vecslice(vF, sh+1, n1+1);
    1278          35 :   return gerepilecopy(av, c_deflate(n, d, vF));
    1279             : }
    1280             : 
    1281             : static GEN
    1282         175 : c_deriv(long n, long d, GEN F, GEN gm)
    1283             : {
    1284         175 :   pari_sp av = avma;
    1285         175 :   GEN V = mfcoefs_i(F, n, d), res;
    1286         175 :   long i, m = itos(gm);
    1287         175 :   if (!m) return V;
    1288         175 :   res = cgetg(n+2, t_VEC); gel(res,1) = gen_0;
    1289         175 :   if (m < 0)
    1290          49 :   { for (i=1; i <= n; i++) gel(res, i+1) = gdiv(gel(V, i+1), powuu(i,-m)); }
    1291             :   else
    1292        2457 :   { for (i=1; i <= n; i++) gel(res, i+1) = gmul(gel(V,i+1), powuu(i,m)); }
    1293         175 :   return gerepileupto(av, res);
    1294             : }
    1295             : 
    1296             : static GEN
    1297          14 : c_derivE2(long n, long d, GEN F, GEN gm)
    1298             : {
    1299          14 :   pari_sp av = avma;
    1300             :   GEN VF, VE, res, tmp, gk;
    1301          14 :   long i, m = itos(gm), nd;
    1302          14 :   if (m == 0) return mfcoefs_i(F, n, d);
    1303          14 :   nd = n*d;
    1304          14 :   VF = mfcoefs_i(F, nd, 1); VE = mfcoefs_i(mfEk(2), nd, 1);
    1305          14 :   gk = mf_get_gk(F);
    1306          14 :   if (m == 1)
    1307             :   {
    1308           7 :     res = cgetg(n+2, t_VEC);
    1309          56 :     for (i = 0; i <= n; i++) gel(res, i+1) = gmulsg(i, gel(VF, i*d+1));
    1310           7 :     tmp = c_deflate(n, d, RgV_mul_RgXn(VF, VE));
    1311           7 :     return gerepileupto(av, gsub(res, gmul(gdivgu(gk, 12), tmp)));
    1312             :   }
    1313             :   else
    1314             :   {
    1315             :     long j;
    1316          35 :     for (j = 1; j <= m; j++)
    1317             :     {
    1318          28 :       tmp = RgV_mul_RgXn(VF, VE);
    1319         140 :       for (i = 0; i <= nd; i++) gel(VF, i+1) = gmulsg(i, gel(VF, i+1));
    1320          28 :       VF = gsub(VF, gmul(gdivgu(gaddgs(gk, 2*(j-1)), 12), tmp));
    1321             :     }
    1322           7 :     return gerepilecopy(av, c_deflate(n, d, VF));
    1323             :   }
    1324             : }
    1325             : 
    1326             : /* Twist by the character (D/.) */
    1327             : static GEN
    1328         161 : c_twist(long n, long d, GEN F, GEN D)
    1329             : {
    1330         161 :   pari_sp av = avma;
    1331         161 :   GEN v = mfcoefs_i(F, n, d), z = cgetg(n+2, t_VEC);
    1332             :   long i;
    1333         707 :   for (i = 0; i <= n; i++)
    1334             :   {
    1335             :     long s;
    1336         546 :     GEN a = gel(v, i+1);
    1337         546 :     if (d == 1) s = krois(D, i);
    1338             :     else
    1339             :     {
    1340         266 :       pari_sp av2 = avma;
    1341         266 :       s = kronecker(D, muluu(i, d)); set_avma(av2);
    1342             :     }
    1343         546 :     switch(s)
    1344             :     {
    1345         147 :       case 1: a = gcopy(a); break;
    1346         140 :       case -1: a = gneg(a); break;
    1347         259 :       default: a = gen_0; break;
    1348             :     }
    1349         546 :     gel(z, i+1) = a;
    1350             :   }
    1351         161 :   return gerepileupto(av, z);
    1352             : }
    1353             : 
    1354             : /* form F given by closure, compute T(n)(F) as closure */
    1355             : static GEN
    1356        1050 : c_hecke(long m, long l, GEN DATA, GEN F)
    1357             : {
    1358        1050 :   pari_sp av = avma;
    1359        1050 :   return gerepilecopy(av, hecke_i(m, l, NULL, F, DATA));
    1360             : }
    1361             : static GEN
    1362         140 : c_const(long n, long d, GEN C)
    1363             : {
    1364         140 :   GEN V = zerovec(n+1);
    1365         140 :   long i, j, l = lg(C);
    1366         140 :   if (l > d*n+2) l = d*n+2;
    1367         189 :   for (i = j = 1; i < l; i+=d, j++) gel(V, j) = gcopy(gel(C,i));
    1368         140 :   return V;
    1369             : }
    1370             : 
    1371             : /* m > 0 */
    1372             : static GEN
    1373         469 : eta3_ZXn(long m)
    1374             : {
    1375         469 :   long l = m+2, n, k;
    1376         469 :   GEN P = cgetg(l,t_POL);
    1377         469 :   P[1] = evalsigne(1)|evalvarn(0);
    1378        6489 :   for (n = 2; n < l; n++) gel(P,n) = gen_0;
    1379         469 :   for (n = k = 0;; n++)
    1380             :   {
    1381        2611 :     if (k + n >= m) { setlg(P, k+3); return P; }
    1382        2142 :     k += n;
    1383             :     /* now k = n(n+1) / 2 */
    1384        2142 :     gel(P, k+2) = odd(n)? utoineg(2*n+1): utoipos(2*n+1);
    1385             :   }
    1386             : }
    1387             : 
    1388             : static GEN
    1389         476 : c_delta(long n, long d)
    1390             : {
    1391         476 :   pari_sp ltop = avma;
    1392         476 :   long N = n*d;
    1393             :   GEN e;
    1394         476 :   if (!N) return mkvec(gen_0);
    1395         469 :   e = eta3_ZXn(N);
    1396         469 :   e = ZXn_sqr(e,N);
    1397         469 :   e = ZXn_sqr(e,N);
    1398         469 :   e = ZXn_sqr(e,N); /* eta(x)^24 */
    1399         469 :   settyp(e, t_VEC);
    1400         469 :   gel(e,1) = gen_0; /* Delta(x) = x*eta(x)^24 as a t_VEC */
    1401         469 :   return gerepilecopy(ltop, c_deflate(n, d, e));
    1402             : }
    1403             : 
    1404             : /* return s(d) such that s|f <=> d | f^2 */
    1405             : static long
    1406          56 : mysqrtu(ulong d)
    1407             : {
    1408          56 :   GEN fa = myfactoru(d), P = gel(fa,1), E = gel(fa,2);
    1409          56 :   long l = lg(P), i, s = 1;
    1410         140 :   for (i = 1; i < l; i++) s *= upowuu(P[i], (E[i]+1)>>1);
    1411          56 :   return s;
    1412             : }
    1413             : static GEN
    1414        1911 : c_theta(long n, long d, GEN psi)
    1415             : {
    1416        1911 :   long lim = usqrt(n*d), F = mfcharmodulus(psi), par = mfcharparity(psi);
    1417        1911 :   long f, d2 = d == 1? 1: mysqrtu(d);
    1418        1911 :   GEN V = zerovec(n + 1);
    1419        8414 :   for (f = d2; f <= lim; f += d2)
    1420        6503 :     if (ugcd(F, f) == 1)
    1421             :     {
    1422        6496 :       pari_sp av = avma;
    1423        6496 :       GEN c = mfchareval(psi, f);
    1424        6496 :       gel(V, f*f/d + 1) = gerepileupto(av, par < 0? gmulgu(c,2*f): gmul2n(c,1));
    1425             :     }
    1426        1911 :   if (F == 1) gel(V, 1) = gen_1;
    1427        1911 :   return V; /* no gerepile needed */
    1428             : }
    1429             : 
    1430             : static GEN
    1431         203 : c_etaquo(long n, long d, GEN eta, GEN gs)
    1432             : {
    1433         203 :   pari_sp av = avma;
    1434         203 :   long s = itos(gs), nd = n*d, nds = nd - s + 1;
    1435             :   GEN c;
    1436         203 :   if (nds <= 0) return zerovec(n+1);
    1437         182 :   c = RgX_to_RgC(eta_product_ZXn(eta, nds), nds); settyp(c, t_VEC);
    1438         182 :   if (s > 0) c = shallowconcat(zerovec(s), c);
    1439         182 :   return gerepilecopy(av, c_deflate(n, d, c));
    1440             : }
    1441             : 
    1442             : static GEN
    1443          77 : c_ell(long n, long d, GEN E)
    1444             : {
    1445          77 :   pari_sp av = avma;
    1446             :   GEN v;
    1447          77 :   if (d == 1) return gconcat(gen_0, ellan(E, n));
    1448           7 :   v = vec_prepend(ellan(E, n*d), gen_0);
    1449           7 :   return gerepilecopy(av, c_deflate(n, d, v));
    1450             : }
    1451             : 
    1452             : static GEN
    1453          21 : c_cusptrace(long n, long d, GEN F)
    1454             : {
    1455          21 :   pari_sp av = avma;
    1456          21 :   GEN D = gel(F,2), res = cgetg(n+2, t_VEC);
    1457          21 :   long i, N = mf_get_N(F), k = mf_get_k(F);
    1458          21 :   gel(res, 1) = gen_0;
    1459         140 :   for (i = 1; i <= n; i++)
    1460         119 :     gel(res, i+1) = mfcusptrace_i(N, k, i*d, mydivisorsu(i*d), D);
    1461          21 :   return gerepilecopy(av, res);
    1462             : }
    1463             : 
    1464             : static GEN
    1465        1897 : c_newtrace(long n, long d, GEN F)
    1466             : {
    1467        1897 :   pari_sp av = avma;
    1468             :   cachenew_t cache;
    1469        1897 :   long N = mf_get_N(F);
    1470             :   GEN v;
    1471        1897 :   init_cachenew(&cache, n == 1? 1: n*d, N, F);
    1472        1897 :   v = colnewtrace(0, n, d, N, mf_get_k(F), &cache);
    1473        1897 :   settyp(v, t_VEC); return gerepilecopy(av, v);
    1474             : }
    1475             : 
    1476             : static GEN
    1477        7462 : c_Bd(long n, long d, GEN F, GEN A)
    1478             : {
    1479        7462 :   pari_sp av = avma;
    1480        7462 :   long a = itou(A), ad = ugcd(a,d), aad = a/ad, i, j;
    1481        7462 :   GEN w, v = mfcoefs_i(F, n/aad, d/ad);
    1482        7462 :   if (a == 1) return v;
    1483        7462 :   n++; w = zerovec(n);
    1484      213087 :   for (i = j = 1; j <= n; i++, j += aad) gel(w,j) = gcopy(gel(v,i));
    1485        7462 :   return gerepileupto(av, w);
    1486             : }
    1487             : 
    1488             : static GEN
    1489        5579 : c_dihedral(long n, long d, GEN F)
    1490             : {
    1491        5579 :   pari_sp av = avma;
    1492        5579 :   GEN CHI = mf_get_CHI(F);
    1493        5579 :   GEN w = gel(F,3), V = dihan(gel(F,2), w, gel(F,4), mfcharorder(CHI), n*d);
    1494        5579 :   GEN Tinit = gel(w,3), Pm = gel(Tinit,1);
    1495        5579 :   GEN A = c_deflate(n, d, V);
    1496        5579 :   if (degpol(Pm) == 1 || RgV_is_ZV(A)) return gerepilecopy(av, A);
    1497        1043 :   return gerepileupto(av, gmodulo(A, Pm));
    1498             : }
    1499             : 
    1500             : static GEN
    1501         343 : c_mfEH(long n, long d, GEN F)
    1502             : {
    1503         343 :   pari_sp av = avma;
    1504             :   GEN v, M, A;
    1505         343 :   long i, r = mf_get_r(F);
    1506         343 :   if (n == 1)
    1507          14 :     return gerepilecopy(av, mkvec2(mfEHcoef(r,0),mfEHcoef(r,d)));
    1508             :   /* speedup mfcoef */
    1509         329 :   if (r == 1)
    1510             :   {
    1511          70 :     v = cgetg(n+2, t_VEC);
    1512          70 :     gel(v,1) = sstoQ(-1,12);
    1513       83258 :     for (i = 1; i <= n; i++)
    1514             :     {
    1515       83188 :       long id = i*d, a = id & 3;
    1516       83188 :       gel(v,i+1) = (a==1 || a==2)? gen_0: uutoQ(hclassno6u(id), 6);
    1517             :     }
    1518          70 :     return v; /* no gerepile needed */
    1519             :   }
    1520         259 :   M = mfEHmat(n*d+1,r);
    1521         259 :   if (d > 1)
    1522             :   {
    1523          35 :     long l = lg(M);
    1524         119 :     for (i = 1; i < l; i++) gel(M,i) = c_deflate(n, d, gel(M,i));
    1525             :   }
    1526         259 :   A = gel(F,2); /* [num(B), den(B)] */
    1527         259 :   v = RgC_Rg_div(RgM_RgC_mul(M, gel(A,1)), gel(A,2));
    1528         259 :   settyp(v,t_VEC); return gerepileupto(av, v);
    1529             : }
    1530             : 
    1531             : static GEN
    1532       11256 : c_mfeisen(long n, long d, GEN F)
    1533             : {
    1534       11256 :   pari_sp av = avma;
    1535       11256 :   GEN v, vchi, E0, P, T, CHI, gk = mf_get_gk(F);
    1536             :   long i, k;
    1537       11256 :   if (typ(gk) != t_INT) return c_mfEH(n, d, F);
    1538       10913 :   k = itou(gk);
    1539       10913 :   vchi = gel(F,2);
    1540       10913 :   E0 = gel(vchi,1);
    1541       10913 :   T = gel(vchi,2);
    1542       10913 :   P = gel(T,1);
    1543       10913 :   CHI = gel(vchi,3);
    1544       10913 :   v = cgetg(n+2, t_VEC);
    1545       10913 :   gel(v, 1) = gcopy(E0); /* E(0) */
    1546       10913 :   if (lg(vchi) == 5)
    1547             :   { /* E_k(chi1,chi2) */
    1548        8820 :     GEN CHI2 = gel(vchi,4), F3 = gel(F,3);
    1549        8820 :     long ord = F3[1], j = F3[2];
    1550      508634 :     for (i = 1; i <= n; i++) gel(v, i+1) = sigchi2(k, CHI, CHI2, i*d, ord);
    1551        8820 :     v = QabV_tracerel(T, j, v);
    1552             :   }
    1553             :   else
    1554             :   { /* E_k(chi) */
    1555       26285 :     for (i = 1; i <= n; i++) gel(v, i+1) = sigchi(k, CHI, i*d);
    1556             :   }
    1557       10913 :   if (degpol(P) != 1 && !RgV_is_QV(v)) return gerepileupto(av, gmodulo(v, P));
    1558        7980 :   return gerepilecopy(av, v);
    1559             : }
    1560             : 
    1561             : /* N^k * (D * B_k)(x/N), set D = denom(B_k) */
    1562             : static GEN
    1563        1589 : bern_init(long N, long k, GEN *pD)
    1564        1589 : { return ZX_rescale(Q_remove_denom(bernpol(k, 0), pD), utoi(N)); }
    1565             : 
    1566             : /* L(chi_D, 1-k) */
    1567             : static GEN
    1568          28 : lfunquadneg_naive(long D, long k)
    1569             : {
    1570             :   GEN B, dS, S;
    1571          28 :   long r, N = labs(D);
    1572             :   pari_sp av;
    1573          28 :   if (k == 1 && N == 1) return gneg(ghalf);
    1574          28 :   B = bern_init(N, k, &dS);
    1575          28 :   dS = mul_denom(dS, stoi(-N*k));
    1576          28 :   av = avma;
    1577        7175 :   for (r = 0, S = gen_0; r < N; r++)
    1578             :   {
    1579        7147 :     long c = kross(D, r);
    1580        7147 :     if (c)
    1581             :     {
    1582        5152 :       GEN t = ZX_Z_eval(B, utoi(r));
    1583        5152 :       S = c > 0 ? addii(S, t) : subii(S, t);
    1584        5152 :       S = gerepileuptoint(av, S);
    1585             :     }
    1586             :   }
    1587          28 :   return gdiv(S, dS);
    1588             : }
    1589             : 
    1590             : /* Returns vector of coeffs from F[0], F[d], ..., F[d*n] */
    1591             : static GEN
    1592       37863 : mfcoefs_i(GEN F, long n, long d)
    1593             : {
    1594       37863 :   if (n < 0) return gen_0;
    1595       37863 :   switch(mf_get_type(F))
    1596             :   {
    1597         140 :     case t_MF_CONST: return c_const(n, d, gel(F,2));
    1598       11256 :     case t_MF_EISEN: return c_mfeisen(n, d, F);
    1599         840 :     case t_MF_Ek: return c_Ek(n, d, F);
    1600         476 :     case t_MF_DELTA: return c_delta(n, d);
    1601        1645 :     case t_MF_THETA: return c_theta(n, d, gel(F,2));
    1602         203 :     case t_MF_ETAQUO: return c_etaquo(n, d, gel(F,2), gel(F,3));
    1603          77 :     case t_MF_ELL: return c_ell(n, d, gel(F,2));
    1604         637 :     case t_MF_MUL: return c_mul(n, d, F);
    1605         112 :     case t_MF_POW: return c_pow(n, d, F);
    1606          42 :     case t_MF_BRACKET: return c_bracket(n, d, F);
    1607        2975 :     case t_MF_LINEAR: return c_linear(n, d, gel(F,2), gel(F,3), gel(F,4));
    1608        2233 :     case t_MF_LINEAR_BHN: return c_linear_bhn(n, d, F);
    1609         833 :     case t_MF_DIV: return c_div(n, d, F);
    1610          35 :     case t_MF_SHIFT: return c_shift(n, d, gel(F,2), gel(F,3));
    1611         175 :     case t_MF_DERIV: return c_deriv(n, d, gel(F,2), gel(F,3));
    1612          14 :     case t_MF_DERIVE2: return c_derivE2(n, d, gel(F,2), gel(F,3));
    1613         161 :     case t_MF_TWIST: return c_twist(n, d, gel(F,2), gel(F,3));
    1614        1050 :     case t_MF_HECKE: return c_hecke(n, d, gel(F,2), gel(F,3));
    1615        7462 :     case t_MF_BD: return c_Bd(n, d, gel(F,2), gel(F,3));
    1616          21 :     case t_MF_TRACE: return c_cusptrace(n, d, F);
    1617        1897 :     case t_MF_NEWTRACE: return c_newtrace(n, d, F);
    1618        5579 :     case t_MF_DIHEDRAL: return c_dihedral(n, d, F);
    1619             :     default: pari_err_TYPE("mfcoefs",F); return NULL;/*LCOV_EXCL_LINE*/
    1620             :   }
    1621             : }
    1622             : 
    1623             : static GEN
    1624         385 : matdeflate(long n, long d, GEN x)
    1625        1575 : { pari_APPLY_same(c_deflate(n,d,gel(x,i))); }
    1626             : static int
    1627        5992 : space_is_cusp(long space) { return space != mf_FULL && space != mf_EISEN; }
    1628             : /* safe with flraw mf */
    1629             : static GEN
    1630        2590 : mfcoefs_mf(GEN mf, long n, long d)
    1631             : {
    1632        2590 :   GEN MS, ME, E = MF_get_E(mf), S = MF_get_S(mf), M = MF_get_M(mf);
    1633        2590 :   long lE = lg(E), lS = lg(S), l = lE+lS-1;
    1634             : 
    1635        2590 :   if (l == 1) return cgetg(1, t_MAT);
    1636        2478 :   if (typ(M) == t_MAT && lg(M) != 1 && (n+1)*d < nbrows(M))
    1637          21 :     return matdeflate(n, d, M); /*cached; lg = 1 is possible from mfinit */
    1638        2457 :   ME = (lE == 1)? cgetg(1, t_MAT): mfvectomat(E, n, d);
    1639        2457 :   if (lS == 1)
    1640         448 :     MS = cgetg(1, t_MAT);
    1641        2009 :   else if (mf_get_type(gel(S,1)) == t_MF_DIV) /*k 1/2-integer or k=1 (exotic)*/
    1642         364 :     MS = matdeflate(n,d, mflineardivtomat(MF_get_N(mf), S, n*d));
    1643        1645 :   else if (MF_get_k(mf) == 1) /* k = 1 (dihedral) */
    1644             :   {
    1645         308 :     GEN M = mfvectomat(gmael(S,1,2), n, d);
    1646             :     long i;
    1647         308 :     MS = cgetg(lS, t_MAT);
    1648        1589 :     for (i = 1; i < lS; i++)
    1649             :     {
    1650        1281 :       GEN f = gel(S,i), dc = gel(f,4), c = RgM_RgC_mul(M, gel(f,3));
    1651        1281 :       if (!equali1(dc)) c = RgC_Rg_div(c,dc);
    1652        1281 :       gel(MS,i) = c;
    1653             :     }
    1654             :   }
    1655             :   else /* k >= 2 integer */
    1656        1337 :     MS = bhnmat_extend_nocache(NULL, MF_get_N(mf), n, d, S);
    1657        2457 :   return shallowconcat(ME,MS);
    1658             : }
    1659             : GEN
    1660        3864 : mfcoefs(GEN F, long n, long d)
    1661             : {
    1662        3864 :   if (!checkmf_i(F))
    1663             :   {
    1664          42 :     pari_sp av = avma;
    1665          42 :     GEN mf = checkMF_i(F); if (!mf) pari_err_TYPE("mfcoefs", F);
    1666          42 :     return gerepilecopy(av, mfcoefs_mf(mf,n,d));
    1667             :   }
    1668        3822 :   if (d <= 0) pari_err_DOMAIN("mfcoefs", "d", "<=", gen_0, stoi(d));
    1669        3822 :   if (n < 0) return cgetg(1, t_VEC);
    1670        3822 :   return mfcoefs_i(F, n, d);
    1671             : }
    1672             : 
    1673             : /* assume k >= 0 */
    1674             : static GEN
    1675         455 : mfak_i(GEN F, long k)
    1676             : {
    1677         455 :   if (!k) return gel(mfcoefs_i(F,0,1), 1);
    1678         294 :   return gel(mfcoefs_i(F,1,k), 2);
    1679             : }
    1680             : GEN
    1681         301 : mfcoef(GEN F, long n)
    1682             : {
    1683         301 :   pari_sp av = avma;
    1684         301 :   if (!checkmf_i(F)) pari_err_TYPE("mfcoef",F);
    1685         301 :   return n < 0? gen_0: gerepilecopy(av, mfak_i(F, n));
    1686             : }
    1687             : 
    1688             : static GEN
    1689         126 : paramconst() { return tagparams(t_MF_CONST, mkNK(1,0,mfchartrivial())); }
    1690             : static GEN
    1691          84 : mftrivial(void) { retmkvec2(paramconst(), cgetg(1,t_VEC)); }
    1692             : static GEN
    1693          42 : mf1(void) { retmkvec2(paramconst(), mkvec(gen_1)); }
    1694             : 
    1695             : /* induce mfchar CHI to G */
    1696             : static GEN
    1697      307909 : induce(GEN G, GEN CHI)
    1698             : {
    1699             :   GEN o, chi;
    1700      307909 :   if (typ(CHI) == t_INT) /* Kronecker */
    1701             :   {
    1702      300776 :     chi = znchar_quad(G, CHI);
    1703      300776 :     o = ZV_equal0(chi)? gen_1: gen_2;
    1704      300776 :     CHI = mkvec4(G,chi,o,cgetg(1,t_VEC));
    1705             :   }
    1706             :   else
    1707             :   {
    1708        7133 :     if (mfcharmodulus(CHI) == itos(znstar_get_N(G))) return CHI;
    1709        6510 :     CHI = leafcopy(CHI);
    1710        6510 :     chi = zncharinduce(gel(CHI,1), gel(CHI,2), G);
    1711        6510 :     gel(CHI,1) = G;
    1712        6510 :     gel(CHI,2) = chi;
    1713             :   }
    1714      307286 :   return CHI;
    1715             : }
    1716             : /* induce mfchar CHI to znstar(N) */
    1717             : static GEN
    1718       42364 : induceN(long N, GEN CHI)
    1719             : {
    1720       42364 :   if (mfcharmodulus(CHI) != N) CHI = induce(znstar0(utoipos(N),1), CHI);
    1721       42364 :   return CHI;
    1722             : }
    1723             : /* *pCHI1 and *pCHI2 are mfchar, induce to common modulus */
    1724             : static void
    1725        6615 : char2(GEN *pCHI1, GEN *pCHI2)
    1726             : {
    1727        6615 :   GEN CHI1 = *pCHI1, G1 = gel(CHI1,1), N1 = znstar_get_N(G1);
    1728        6615 :   GEN CHI2 = *pCHI2, G2 = gel(CHI2,1), N2 = znstar_get_N(G2);
    1729        6615 :   if (!equalii(N1,N2))
    1730             :   {
    1731        4935 :     GEN G, d = gcdii(N1,N2);
    1732        4935 :     if      (equalii(N2,d)) *pCHI2 = induce(G1, CHI2);
    1733        1568 :     else if (equalii(N1,d)) *pCHI1 = induce(G2, CHI1);
    1734             :     else
    1735             :     {
    1736         154 :       if (!equali1(d)) N2 = diviiexact(N2,d);
    1737         154 :       G = znstar0(mulii(N1,N2), 1);
    1738         154 :       *pCHI1 = induce(G, CHI1);
    1739         154 :       *pCHI2 = induce(G, CHI2);
    1740             :     }
    1741             :   }
    1742        6615 : }
    1743             : /* mfchar or charinit wrt same modulus; outputs a mfchar */
    1744             : static GEN
    1745      301861 : mfcharmul_i(GEN CHI1, GEN CHI2)
    1746             : {
    1747      301861 :   GEN G = gel(CHI1,1), chi3 = zncharmul(G, gel(CHI1,2), gel(CHI2,2));
    1748      301861 :   return mfcharGL(G, chi3);
    1749             : }
    1750             : /* mfchar or charinit; outputs a mfchar */
    1751             : static GEN
    1752        1106 : mfcharmul(GEN CHI1, GEN CHI2)
    1753             : {
    1754        1106 :   char2(&CHI1, &CHI2); return mfcharmul_i(CHI1,CHI2);
    1755             : }
    1756             : /* mfchar or charinit; outputs a mfchar */
    1757             : static GEN
    1758         147 : mfcharpow(GEN CHI, GEN n)
    1759             : {
    1760             :   GEN G, chi;
    1761         147 :   G = gel(CHI,1); chi = zncharpow(G, gel(CHI,2), n);
    1762         147 :   return mfchartoprimitive(mfcharGL(G, chi), NULL);
    1763             : }
    1764             : /* mfchar or charinit wrt same modulus; outputs a mfchar */
    1765             : static GEN
    1766        5509 : mfchardiv_i(GEN CHI1, GEN CHI2)
    1767             : {
    1768        5509 :   GEN G = gel(CHI1,1), chi3 = znchardiv(G, gel(CHI1,2), gel(CHI2,2));
    1769        5509 :   return mfcharGL(G, chi3);
    1770             : }
    1771             : /* mfchar or charinit; outputs a mfchar */
    1772             : static GEN
    1773        5509 : mfchardiv(GEN CHI1, GEN CHI2)
    1774             : {
    1775        5509 :   char2(&CHI1, &CHI2); return mfchardiv_i(CHI1,CHI2);
    1776             : }
    1777             : static GEN
    1778          56 : mfcharconj(GEN CHI)
    1779             : {
    1780          56 :   CHI = leafcopy(CHI);
    1781          56 :   gel(CHI,2) = zncharconj(gel(CHI,1), gel(CHI,2));
    1782          56 :   return CHI;
    1783             : }
    1784             : 
    1785             : /* CHI mfchar, assume 4 | N. Multiply CHI by \chi_{-4} */
    1786             : static GEN
    1787         980 : mfchilift(GEN CHI, long N)
    1788             : {
    1789         980 :   CHI = induceN(N, CHI);
    1790         980 :   return mfcharmul_i(CHI, induce(gel(CHI,1), stoi(-4)));
    1791             : }
    1792             : /* CHI defined mod N, N4 = N/4;
    1793             :  * if CHI is defined mod N4 return CHI;
    1794             :  * else if CHI' = CHI*(-4,.) is defined mod N4, return CHI' (primitive)
    1795             :  * else error */
    1796             : static GEN
    1797          35 : mfcharchiliftprim(GEN CHI, long N4)
    1798             : {
    1799          35 :   long FC = mfcharconductor(CHI);
    1800             :   GEN CHIP;
    1801          35 :   if (N4 % FC == 0) return CHI;
    1802          14 :   CHIP = mfchartoprimitive(mfchilift(CHI, N4 << 2), &FC);
    1803          14 :   if (N4 % FC) pari_err_TYPE("mfkohnenbasis [incorrect CHI]", CHI);
    1804          14 :   return CHIP;
    1805             : }
    1806             : /* ensure CHI(-1) = (-1)^k [k integer] or 1 [half-integer], by multiplying
    1807             :  * by (-4/.) if needed */
    1808             : static GEN
    1809        2821 : mfchiadjust(GEN CHI, GEN gk, long N)
    1810             : {
    1811        2821 :   long par = mfcharparity(CHI);
    1812        2821 :   if (typ(gk) == t_INT &&  mpodd(gk)) par = -par;
    1813        2821 :   return par == 1 ? CHI : mfchilift(CHI, N);
    1814             : }
    1815             : 
    1816             : static GEN
    1817        3906 : mfsamefield(GEN T, GEN P, GEN Q)
    1818             : {
    1819        3906 :   if (degpol(P) == 1) return Q;
    1820         602 :   if (degpol(Q) == 1) return P;
    1821         511 :   if (!gequal(P,Q)) pari_err_TYPE("mfsamefield [different fields]",mkvec2(P,Q));
    1822         504 :   if (T) err_cyclo();
    1823         504 :   return P;
    1824             : }
    1825             : 
    1826             : GEN
    1827         455 : mfmul(GEN f, GEN g)
    1828             : {
    1829         455 :   pari_sp av = avma;
    1830             :   GEN T, N, K, NK, CHI, CHIf, CHIg;
    1831         455 :   if (!checkmf_i(f)) pari_err_TYPE("mfmul",f);
    1832         455 :   if (!checkmf_i(g)) pari_err_TYPE("mfmul",g);
    1833         455 :   N = lcmii(mf_get_gN(f), mf_get_gN(g));
    1834         455 :   K = gadd(mf_get_gk(f), mf_get_gk(g));
    1835         455 :   CHIf = mf_get_CHI(f);
    1836         455 :   CHIg = mf_get_CHI(g);
    1837         455 :   CHI = mfchiadjust(mfcharmul(CHIf,CHIg), K, itos(N));
    1838         455 :   T = chicompat(CHI, CHIf, CHIg);
    1839         455 :   NK = mkgNK(N, K, CHI, mfsamefield(T, mf_get_field(f), mf_get_field(g)));
    1840         448 :   return gerepilecopy(av, T? tag3(t_MF_MUL,NK,f,g,T): tag2(t_MF_MUL,NK,f,g));
    1841             : }
    1842             : GEN
    1843          77 : mfpow(GEN f, long n)
    1844             : {
    1845          77 :   pari_sp av = avma;
    1846             :   GEN T, KK, NK, gn, CHI, CHIf;
    1847          77 :   if (!checkmf_i(f)) pari_err_TYPE("mfpow",f);
    1848          77 :   if (!n) return mf1();
    1849          77 :   if (n == 1) return gcopy(f);
    1850          77 :   KK = gmulsg(n,mf_get_gk(f));
    1851          77 :   gn = stoi(n);
    1852          77 :   CHIf = mf_get_CHI(f);
    1853          77 :   CHI = mfchiadjust(mfcharpow(CHIf,gn), KK, mf_get_N(f));
    1854          77 :   T = chicompat(CHI, CHIf, CHIf);
    1855          70 :   NK = mkgNK(mf_get_gN(f), KK, CHI, mf_get_field(f));
    1856          70 :   return gerepilecopy(av, T? tag3(t_MF_POW,NK,f,gn,T): tag2(t_MF_POW,NK,f,gn));
    1857             : }
    1858             : GEN
    1859          28 : mfbracket(GEN f, GEN g, long m)
    1860             : {
    1861          28 :   pari_sp av = avma;
    1862             :   GEN T, N, K, NK, CHI, CHIf, CHIg;
    1863          28 :   if (!checkmf_i(f)) pari_err_TYPE("mfbracket",f);
    1864          28 :   if (!checkmf_i(g)) pari_err_TYPE("mfbracket",g);
    1865          28 :   if (m < 0) pari_err_TYPE("mfbracket [m<0]",stoi(m));
    1866          28 :   K = gaddgs(gadd(mf_get_gk(f), mf_get_gk(g)), 2*m);
    1867          28 :   if (gsigne(K) < 0) pari_err_IMPL("mfbracket for this form");
    1868          28 :   N = lcmii(mf_get_gN(f), mf_get_gN(g));
    1869          28 :   CHIf = mf_get_CHI(f);
    1870          28 :   CHIg = mf_get_CHI(g);
    1871          28 :   CHI = mfcharmul(CHIf, CHIg);
    1872          28 :   CHI = mfchiadjust(CHI, K, itou(N));
    1873          28 :   T = chicompat(CHI, CHIf, CHIg);
    1874          28 :   NK = mkgNK(N, K, CHI, mfsamefield(T, mf_get_field(f), mf_get_field(g)));
    1875          56 :   return gerepilecopy(av, T? tag4(t_MF_BRACKET, NK, f, g, utoi(m), T)
    1876          28 :                            : tag3(t_MF_BRACKET, NK, f, g, utoi(m)));
    1877             : }
    1878             : 
    1879             : /* remove 0 entries in L */
    1880             : static int
    1881        1820 : mflinear_strip(GEN *pF, GEN *pL)
    1882             : {
    1883        1820 :   pari_sp av = avma;
    1884        1820 :   GEN F = *pF, L = *pL;
    1885        1820 :   long i, j, l = lg(L);
    1886        1820 :   GEN F2 = cgetg(l, t_VEC), L2 = cgetg(l, t_VEC);
    1887       10108 :   for (i = j = 1; i < l; i++)
    1888             :   {
    1889        8288 :     if (gequal0(gel(L,i))) continue;
    1890        4207 :     gel(F2,j) = gel(F,i);
    1891        4207 :     gel(L2,j) = gel(L,i); j++;
    1892             :   }
    1893        1820 :   if (j == l) set_avma(av);
    1894             :   else
    1895             :   {
    1896         546 :     setlg(F2,j); *pF = F2;
    1897         546 :     setlg(L2,j); *pL = L2;
    1898             :   }
    1899        1820 :   return (j > 1);
    1900             : }
    1901             : static GEN
    1902        6741 : taglinear_i(long t, GEN NK, GEN F, GEN L)
    1903             : {
    1904             :   GEN dL;
    1905        6741 :   L = Q_remove_denom(L, &dL); if (!dL) dL = gen_1;
    1906        6741 :   return tag3(t, NK, F, L, dL);
    1907             : }
    1908             : static GEN
    1909        2765 : taglinear(GEN NK, GEN F, GEN L)
    1910             : {
    1911        2765 :   long t = ok_bhn_linear(F)? t_MF_LINEAR_BHN: t_MF_LINEAR;
    1912        2765 :    return taglinear_i(t, NK, F, L);
    1913             : }
    1914             : /* assume F has parameters NK = [N,K,CHI] */
    1915             : static GEN
    1916         476 : mflinear_i(GEN NK, GEN F, GEN L)
    1917             : {
    1918         476 :   if (!mflinear_strip(&F,&L)) return mftrivial();
    1919         476 :   return taglinear(NK, F,L);
    1920             : }
    1921             : static GEN
    1922         700 : mflinear_bhn(GEN mf, GEN L)
    1923             : {
    1924             :   long i, l;
    1925         700 :   GEN P, NK, F = MF_get_S(mf);
    1926         700 :   if (!mflinear_strip(&F,&L)) return mftrivial();
    1927         693 :   l = lg(L); P = pol_x(1);
    1928        3031 :   for (i = 1; i < l; i++)
    1929             :   {
    1930        2338 :     GEN c = gel(L,i);
    1931        2338 :     if (typ(c) == t_POLMOD && varn(gel(c,1)) == 1)
    1932         518 :       P = mfsamefield(NULL, P, gel(c,1));
    1933             :   }
    1934         693 :   NK = mkgNK(MF_get_gN(mf), MF_get_gk(mf), MF_get_CHI(mf), P);
    1935         693 :   return taglinear_i(t_MF_LINEAR_BHN,  NK, F,L);
    1936             : }
    1937             : 
    1938             : /* F vector of forms with same weight and character but varying level, return
    1939             :  * global [N,k,chi,P] */
    1940             : static GEN
    1941        3227 : vecmfNK(GEN F)
    1942             : {
    1943        3227 :   long i, l = lg(F);
    1944             :   GEN N, f;
    1945        3227 :   if (l == 1) return mkNK(1, 0, mfchartrivial());
    1946        3227 :   f = gel(F,1); N = mf_get_gN(f);
    1947       45255 :   for (i = 2; i < l; i++) N = lcmii(N, mf_get_gN(gel(F,i)));
    1948        3227 :   return mkgNK(N, mf_get_gk(f), mf_get_CHI(f), mf_get_field(f));
    1949             : }
    1950             : /* do not use mflinear: mflineardivtomat rely on F being constant across the
    1951             :  * basis where mflinear strips the ones matched by 0 coeffs. Assume k and CHI
    1952             :  * constant, N is allowed to vary. */
    1953             : static GEN
    1954        1211 : vecmflinear(GEN F, GEN C)
    1955             : {
    1956        1211 :   long i, t, l = lg(C);
    1957        1211 :   GEN NK, v = cgetg(l, t_VEC);
    1958        1211 :   if (l == 1) return v;
    1959        1211 :   t = ok_bhn_linear(F)? t_MF_LINEAR_BHN: t_MF_LINEAR;
    1960        1211 :   NK = vecmfNK(F);
    1961        4494 :   for (i = 1; i < l; i++) gel(v,i) = taglinear_i(t, NK, F, gel(C,i));
    1962        1211 :   return v;
    1963             : }
    1964             : /* vecmflinear(F,C), then divide everything by E, which has valuation 0 */
    1965             : static GEN
    1966         427 : vecmflineardiv0(GEN F, GEN C, GEN E)
    1967             : {
    1968         427 :   GEN v = vecmflinear(F, C);
    1969         427 :   long i, l = lg(v);
    1970         427 :   if (l == 1) return v;
    1971         427 :   gel(v,1) = mfdiv_val(gel(v,1), E, 0);
    1972        1631 :   for (i = 2; i < l; i++)
    1973             :   { /* v[i] /= E */
    1974        1204 :     GEN f = shallowcopy(gel(v,1));
    1975        1204 :     gel(f,2) = gel(v,i);
    1976        1204 :     gel(v,i) = f;
    1977             :   }
    1978         427 :   return v;
    1979             : }
    1980             : 
    1981             : /* Non empty linear combination of linear combinations of same
    1982             :  * F_j=\sum_i \mu_{i,j}G_i so R = \sum_i (\sum_j(\la_j\mu_{i,j})) G_i */
    1983             : static GEN
    1984        2016 : mflinear_linear(GEN F, GEN L, int strip)
    1985             : {
    1986        2016 :   long l = lg(F), j;
    1987        2016 :   GEN vF, M = cgetg(l, t_MAT);
    1988        2016 :   L = shallowcopy(L);
    1989       18522 :   for (j = 1; j < l; j++)
    1990             :   {
    1991       16506 :     GEN f = gel(F,j), c = gel(f,3), d = gel(f,4);
    1992       16506 :     if (typ(c) == t_VEC) c = shallowtrans(c);
    1993       16506 :     if (!isint1(d)) gel(L,j) = gdiv(gel(L,j),d);
    1994       16506 :     gel(M,j) = c;
    1995             :   }
    1996        2016 :   vF = gmael(F,1,2); L = RgM_RgC_mul(M,L);
    1997        2016 :   if (strip && !mflinear_strip(&vF,&L)) return mftrivial();
    1998        2016 :   return taglinear(vecmfNK(vF), vF, L);
    1999             : }
    2000             : /* F nonempty vector of forms of the form mfdiv(mflinear(B,v), E) where E
    2001             :  * does not vanish at oo, or mflinear(B,v). Apply mflinear(F, L) */
    2002             : static GEN
    2003        2016 : mflineardiv_linear(GEN F, GEN L, int strip)
    2004             : {
    2005        2016 :   long l = lg(F), j;
    2006             :   GEN v, E, f;
    2007        2016 :   if (lg(L) != l) pari_err_DIM("mflineardiv_linear");
    2008        2016 :   f = gel(F,1); /* l > 1 */
    2009        2016 :   if (mf_get_type(f) != t_MF_DIV) return mflinear_linear(F,L,strip);
    2010        1708 :   E = gel(f,3);
    2011        1708 :   v = cgetg(l, t_VEC);
    2012       17059 :   for (j = 1; j < l; j++) { GEN f = gel(F,j); gel(v,j) = gel(f,2); }
    2013        1708 :   return mfdiv_val(mflinear_linear(v,L,strip), E, 0);
    2014             : }
    2015             : static GEN
    2016         476 : vecmflineardiv_linear(GEN F, GEN M)
    2017             : {
    2018         476 :   long i, l = lg(M);
    2019         476 :   GEN v = cgetg(l, t_VEC);
    2020        1918 :   for (i = 1; i < l; i++) gel(v,i) = mflineardiv_linear(F, gel(M,i), 0);
    2021         476 :   return v;
    2022             : }
    2023             : 
    2024             : static GEN
    2025        1001 : tobasis(GEN mf, GEN F, GEN L)
    2026             : {
    2027        1001 :   if (checkmf_i(L) && mf) return mftobasis(mf, L, 0);
    2028         994 :   if (typ(F) != t_VEC) pari_err_TYPE("mflinear",F);
    2029         994 :   if (!is_vec_t(typ(L))) pari_err_TYPE("mflinear",L);
    2030         994 :   if (lg(L) != lg(F)) pari_err_DIM("mflinear");
    2031         994 :   return L;
    2032             : }
    2033             : GEN
    2034        1043 : mflinear(GEN F, GEN L)
    2035             : {
    2036        1043 :   pari_sp av = avma;
    2037        1043 :   GEN G, NK, P, mf = checkMF_i(F), N = NULL, K = NULL, CHI = NULL;
    2038             :   long i, l;
    2039        1043 :   if (mf)
    2040             :   {
    2041         707 :     GEN gk = MF_get_gk(mf);
    2042         707 :     F = MF_get_basis(F);
    2043         707 :     if (typ(gk) != t_INT)
    2044          42 :       return gerepilecopy(av, mflineardiv_linear(F, L, 1));
    2045         665 :     if (itou(gk) > 1 && space_is_cusp(MF_get_space(mf)))
    2046             :     {
    2047         448 :       L = tobasis(mf, F, L);
    2048         448 :       return gerepilecopy(av, mflinear_bhn(mf, L));
    2049             :     }
    2050             :   }
    2051         553 :   L = tobasis(mf, F, L);
    2052         553 :   if (!mflinear_strip(&F,&L)) return mftrivial();
    2053             : 
    2054         546 :   l = lg(F);
    2055         546 :   if (l == 2 && gequal1(gel(L,1))) return gerepilecopy(av, gel(F,1));
    2056         287 :   P = pol_x(1);
    2057         910 :   for (i = 1; i < l; i++)
    2058             :   {
    2059         630 :     GEN f = gel(F,i), c = gel(L,i), Ni, Ki;
    2060         630 :     if (!checkmf_i(f)) pari_err_TYPE("mflinear", f);
    2061         630 :     Ni = mf_get_gN(f); N = N? lcmii(N, Ni): Ni;
    2062         630 :     Ki = mf_get_gk(f);
    2063         630 :     if (!K) K = Ki;
    2064         343 :     else if (!gequal(K, Ki))
    2065           7 :       pari_err_TYPE("mflinear [different weights]", mkvec2(K,Ki));
    2066         623 :     P = mfsamefield(NULL, P, mf_get_field(f));
    2067         623 :     if (typ(c) == t_POLMOD && varn(gel(c,1)) == 1)
    2068         126 :       P = mfsamefield(NULL, P, gel(c,1));
    2069             :   }
    2070         280 :   G = znstar0(N,1);
    2071         889 :   for (i = 1; i < l; i++)
    2072             :   {
    2073         616 :     GEN CHI2 = mf_get_CHI(gel(F,i));
    2074         616 :     CHI2 = induce(G, CHI2);
    2075         616 :     if (!CHI) CHI = CHI2;
    2076         336 :     else if (!gequal(CHI, CHI2))
    2077           7 :       pari_err_TYPE("mflinear [different characters]", mkvec2(CHI,CHI2));
    2078             :   }
    2079         273 :   NK = mkgNK(N, K, CHI, P);
    2080         273 :   return gerepilecopy(av, taglinear(NK,F,L));
    2081             : }
    2082             : 
    2083             : GEN
    2084          42 : mfshift(GEN F, long sh)
    2085             : {
    2086          42 :   pari_sp av = avma;
    2087          42 :   if (!checkmf_i(F)) pari_err_TYPE("mfshift",F);
    2088          42 :   return gerepilecopy(av, tag2(t_MF_SHIFT, mf_get_NK(F), F, stoi(sh)));
    2089             : }
    2090             : static long
    2091          49 : mfval(GEN F)
    2092             : {
    2093          49 :   pari_sp av = avma;
    2094          49 :   long i = 0, n, sb;
    2095             :   GEN gk, gN;
    2096          49 :   if (!checkmf_i(F)) pari_err_TYPE("mfval", F);
    2097          49 :   gN = mf_get_gN(F);
    2098          49 :   gk = mf_get_gk(F);
    2099          49 :   sb = mfsturmNgk(itou(gN), gk);
    2100          70 :   for (n = 1; n <= sb;)
    2101             :   {
    2102             :     GEN v;
    2103          63 :     if (n > 0.5*sb) n = sb+1;
    2104          63 :     v = mfcoefs_i(F, n, 1);
    2105         119 :     for (; i <= n; i++)
    2106          98 :       if (!gequal0(gel(v, i+1))) return gc_long(av,i);
    2107          21 :     n <<= 1;
    2108             :   }
    2109           7 :   return gc_long(av,-1);
    2110             : }
    2111             : 
    2112             : GEN
    2113        2163 : mfdiv_val(GEN f, GEN g, long vg)
    2114             : {
    2115             :   GEN T, N, K, NK, CHI, CHIf, CHIg;
    2116        2163 :   if (vg) { f = mfshift(f,vg); g = mfshift(g,vg); }
    2117        2163 :   N = lcmii(mf_get_gN(f), mf_get_gN(g));
    2118        2163 :   K = gsub(mf_get_gk(f), mf_get_gk(g));
    2119        2163 :   CHIf = mf_get_CHI(f);
    2120        2163 :   CHIg = mf_get_CHI(g);
    2121        2163 :   CHI = mfchiadjust(mfchardiv(CHIf, CHIg), K, itos(N));
    2122        2163 :   T = chicompat(CHI, CHIf, CHIg);
    2123        2156 :   NK = mkgNK(N, K, CHI, mfsamefield(T, mf_get_field(f), mf_get_field(g)));
    2124        2156 :   return T? tag3(t_MF_DIV, NK, f, g, T): tag2(t_MF_DIV, NK, f, g);
    2125             : }
    2126             : GEN
    2127          49 : mfdiv(GEN F, GEN G)
    2128             : {
    2129          49 :   pari_sp av = avma;
    2130          49 :   long v = mfval(G);
    2131          49 :   if (!checkmf_i(F)) pari_err_TYPE("mfdiv", F);
    2132          42 :   if (v < 0 || (v && !gequal0(mfcoefs(F, v-1, 1))))
    2133          14 :     pari_err_DOMAIN("mfdiv", "ord(G)", ">", strtoGENstr("ord(F)"),
    2134             :                     mkvec2(F, G));
    2135          28 :   return gerepilecopy(av, mfdiv_val(F, G, v));
    2136             : }
    2137             : GEN
    2138         182 : mfderiv(GEN F, long m)
    2139             : {
    2140         182 :   pari_sp av = avma;
    2141             :   GEN NK, gk;
    2142         182 :   if (!checkmf_i(F)) pari_err_TYPE("mfderiv",F);
    2143         182 :   gk = gaddgs(mf_get_gk(F), 2*m);
    2144         182 :   NK = mkgNK(mf_get_gN(F), gk, mf_get_CHI(F), mf_get_field(F));
    2145         182 :   return gerepilecopy(av, tag2(t_MF_DERIV, NK, F, stoi(m)));
    2146             : }
    2147             : GEN
    2148          21 : mfderivE2(GEN F, long m)
    2149             : {
    2150          21 :   pari_sp av = avma;
    2151             :   GEN NK, gk;
    2152          21 :   if (!checkmf_i(F)) pari_err_TYPE("mfderivE2",F);
    2153          21 :   if (m < 0) pari_err_DOMAIN("mfderivE2","m","<",gen_0,stoi(m));
    2154          21 :   gk = gaddgs(mf_get_gk(F), 2*m);
    2155          21 :   NK = mkgNK(mf_get_gN(F), gk, mf_get_CHI(F), mf_get_field(F));
    2156          21 :   return gerepilecopy(av, tag2(t_MF_DERIVE2, NK, F, stoi(m)));
    2157             : }
    2158             : 
    2159             : GEN
    2160          21 : mftwist(GEN F, GEN D)
    2161             : {
    2162          21 :   pari_sp av = avma;
    2163             :   GEN NK, CHI, NT, Da;
    2164             :   long q;
    2165          21 :   if (!checkmf_i(F)) pari_err_TYPE("mftwist", F);
    2166          21 :   if (typ(D) != t_INT) pari_err_TYPE("mftwist", D);
    2167          21 :   Da = mpabs_shallow(D);
    2168          21 :   CHI = mf_get_CHI(F); q = mfcharconductor(CHI);
    2169          21 :   NT = glcm(glcm(mf_get_gN(F), mulsi(q, Da)), sqri(Da));
    2170          21 :   NK = mkgNK(NT, mf_get_gk(F), CHI, mf_get_field(F));
    2171          21 :   return gerepilecopy(av, tag2(t_MF_TWIST, NK, F, D));
    2172             : }
    2173             : 
    2174             : /***************************************************************/
    2175             : /*                 Generic cache handling                      */
    2176             : /***************************************************************/
    2177             : enum { cache_FACT, cache_DIV, cache_H, cache_D, cache_DIH };
    2178             : typedef struct {
    2179             :   const char *name;
    2180             :   GEN cache;
    2181             :   ulong minself, maxself;
    2182             :   void (*init)(long);
    2183             :   ulong miss, maxmiss;
    2184             :   long compressed;
    2185             : } cache;
    2186             : 
    2187             : static void constfact(long lim);
    2188             : static void constdiv(long lim);
    2189             : static void consttabh(long lim);
    2190             : static void consttabdihedral(long lim);
    2191             : static void constcoredisc(long lim);
    2192             : static THREAD cache caches[] = {
    2193             : { "Factors",  NULL,  50000,    50000, &constfact, 0, 0, 0 },
    2194             : { "Divisors", NULL,  50000,    50000, &constdiv, 0, 0, 0 },
    2195             : { "H",        NULL, 100000, 10000000, &consttabh, 0, 0, 1 },
    2196             : { "CorediscF",NULL, 100000, 10000000, &constcoredisc, 0, 0, 0 },
    2197             : { "Dihedral", NULL,   1000,     3000, &consttabdihedral, 0, 0, 0 },
    2198             : };
    2199             : 
    2200             : static void
    2201         501 : cache_reset(long id) { caches[id].miss = caches[id].maxmiss = 0; }
    2202             : static void
    2203        9310 : cache_delete(long id) { guncloneNULL(caches[id].cache); }
    2204             : static void
    2205         515 : cache_set(long id, GEN S)
    2206             : {
    2207         515 :   GEN old = caches[id].cache;
    2208         515 :   caches[id].cache = gclone(S);
    2209         515 :   guncloneNULL(old);
    2210         515 : }
    2211             : 
    2212             : /* handle a cache miss: store stats, possibly reset table; return value
    2213             :  * if (now) cached; return NULL on failure. HACK: some caches contain an
    2214             :  * ulong where the 0 value is impossible, and return it (typecast to GEN) */
    2215             : static GEN
    2216   448759798 : cache_get(long id, ulong D)
    2217             : {
    2218   448759798 :   cache *S = &caches[id];
    2219   448759798 :   const ulong d = S->compressed? D>>1: D;
    2220             :   ulong max, l;
    2221             : 
    2222   448759798 :   if (!S->cache)
    2223             :   {
    2224         375 :     max = maxuu(minuu(D, S->maxself), S->minself);
    2225         375 :     S->init(max);
    2226         375 :     l = lg(S->cache);
    2227             :   }
    2228             :   else
    2229             :   {
    2230   448759423 :     l = lg(S->cache);
    2231   448759423 :     if (l <= d)
    2232             :     {
    2233         351 :       if (D > S->maxmiss) S->maxmiss = D;
    2234         351 :       if (DEBUGLEVEL >= 3)
    2235           0 :         err_printf("miss in cache %s: %lu, max = %lu\n",
    2236             :                    S->name, D, S->maxmiss);
    2237         351 :       if (S->miss++ >= 5 && D < S->maxself)
    2238             :       {
    2239          16 :         max = minuu(S->maxself, (long)(S->maxmiss * 1.2));
    2240          16 :         if (max <= S->maxself)
    2241             :         {
    2242          16 :           if (DEBUGLEVEL >= 3)
    2243           0 :             err_printf("resetting cache %s to %lu\n", S->name, max);
    2244          16 :           S->init(max); l = lg(S->cache);
    2245             :         }
    2246             :       }
    2247             :     }
    2248             :   }
    2249   448759798 :   return (l <= d)? NULL: gel(S->cache, d);
    2250             : }
    2251             : static GEN
    2252          70 : cache_report(long id)
    2253             : {
    2254          70 :   cache *S = &caches[id];
    2255          70 :   GEN v = zerocol(5);
    2256          70 :   gel(v,1) = strtoGENstr(S->name);
    2257          70 :   if (S->cache)
    2258             :   {
    2259          35 :     gel(v,2) = utoi(lg(S->cache)-1);
    2260          35 :     gel(v,3) = utoi(S->miss);
    2261          35 :     gel(v,4) = utoi(S->maxmiss);
    2262          35 :     gel(v,5) = utoi(gsizebyte(S->cache));
    2263             :   }
    2264          70 :   return v;
    2265             : }
    2266             : GEN
    2267          14 : getcache(void)
    2268             : {
    2269          14 :   pari_sp av = avma;
    2270          14 :   GEN M = cgetg(6, t_MAT);
    2271          14 :   gel(M,1) = cache_report(cache_FACT);
    2272          14 :   gel(M,2) = cache_report(cache_DIV);
    2273          14 :   gel(M,3) = cache_report(cache_H);
    2274          14 :   gel(M,4) = cache_report(cache_D);
    2275          14 :   gel(M,5) = cache_report(cache_DIH);
    2276          14 :   return gerepilecopy(av, shallowtrans(M));
    2277             : }
    2278             : 
    2279             : void
    2280        1862 : pari_close_mf(void)
    2281             : {
    2282        1862 :   cache_delete(cache_FACT);
    2283        1862 :   cache_delete(cache_DIV);
    2284        1862 :   cache_delete(cache_H);
    2285        1862 :   cache_delete(cache_D);
    2286        1862 :   cache_delete(cache_DIH);
    2287        1862 : }
    2288             : 
    2289             : /*************************************************************************/
    2290             : /* a odd, update local cache (recycle memory) */
    2291             : static GEN
    2292        2201 : update_factor_cache(long a, long lim, long *pb)
    2293             : {
    2294        2201 :   const long step = 16000; /* even; don't increase this: RAM cache thrashing */
    2295        2201 :   if (a + 2*step > lim)
    2296         204 :     *pb = lim; /* fuse last 2 chunks */
    2297             :   else
    2298        1997 :     *pb = a + step;
    2299        2201 :   return vecfactoroddu_i(a, *pb);
    2300             : }
    2301             : /* assume lim < MAX_LONG/8 */
    2302             : static void
    2303          50 : constcoredisc(long lim)
    2304             : {
    2305          50 :   pari_sp av2, av = avma;
    2306          50 :   GEN D = caches[cache_D].cache, CACHE = NULL;
    2307          50 :   long cachea, cacheb, N, LIM = !D ? 4 : lg(D)-1;
    2308          50 :   if (lim <= 0) lim = 5;
    2309          50 :   if (lim <= LIM) return;
    2310          50 :   cache_reset(cache_D);
    2311          50 :   D = zero_zv(lim);
    2312          36 :   av2 = avma;
    2313          36 :   cachea = cacheb = 0;
    2314     5201083 :   for (N = 1; N <= lim; N+=2)
    2315             :   { /* N odd */
    2316             :     long i, d, d2;
    2317             :     GEN F;
    2318     5201033 :     if (N > cacheb)
    2319             :     {
    2320         639 :       set_avma(av2); cachea = N;
    2321         639 :       CACHE = update_factor_cache(N, lim, &cacheb);
    2322             :     }
    2323     5201033 :     F = gel(CACHE, ((N-cachea)>>1)+1); /* factoru(N) */
    2324     5201033 :     D[N] = d = corediscs_fact(F); /* = 3 mod 4 or 4 mod 16 */
    2325     5201117 :     d2 = odd(d)? d<<3: d<<1;
    2326     5204899 :     for (i = 1;;)
    2327             :     {
    2328     6917671 :       if ((N << i) > lim) break;
    2329     3445135 :       D[N<<i] = d2; i++;
    2330     3445135 :       if ((N << i) > lim) break;
    2331     1712772 :       D[N<<i] = d; i++;
    2332             :     }
    2333             :   }
    2334          50 :   cache_set(cache_D, D);
    2335          50 :   set_avma(av);
    2336             : }
    2337             : 
    2338             : static void
    2339         173 : constfact(long lim)
    2340             : {
    2341             :   pari_sp av;
    2342         173 :   GEN VFACT = caches[cache_FACT].cache;
    2343         173 :   long LIM = VFACT? lg(VFACT)-1: 4;
    2344         173 :   if (lim <= 0) lim = 5;
    2345         173 :   if (lim <= LIM) return;
    2346         152 :   cache_reset(cache_FACT); av = avma;
    2347         152 :   cache_set(cache_FACT, vecfactoru_i(1,lim)); set_avma(av);
    2348             : }
    2349             : static void
    2350         145 : constdiv(long lim)
    2351             : {
    2352             :   pari_sp av;
    2353         145 :   GEN VFACT, VDIV = caches[cache_DIV].cache;
    2354         145 :   long N, LIM = VDIV? lg(VDIV)-1: 4;
    2355         145 :   if (lim <= 0) lim = 5;
    2356         145 :   if (lim <= LIM) return;
    2357         145 :   constfact(lim);
    2358         145 :   VFACT = caches[cache_FACT].cache;
    2359         145 :   cache_reset(cache_DIV); av = avma;
    2360         145 :   VDIV  = cgetg(lim+1, t_VEC);
    2361     6984680 :   for (N = 1; N <= lim; N++) gel(VDIV,N) = divisorsu_fact(gel(VFACT,N));
    2362         145 :   cache_set(cache_DIV, VDIV); set_avma(av);
    2363             : }
    2364             : 
    2365             : /* n > 1, D = divisors(n); sets L = 2*lambda(n), S = sigma(n) */
    2366             : static void
    2367    14089494 : lamsig(GEN D, long *pL, long *pS)
    2368             : {
    2369    14089494 :   pari_sp av = avma;
    2370    14089494 :   long i, l = lg(D), L = 1, S = D[l-1]+1;
    2371    50891849 :   for (i = 2; i < l; i++) /* skip d = 1 */
    2372             :   {
    2373    51611375 :     long d = D[i], nd = D[l-i]; /* nd = n/d */
    2374    51611375 :     if (d < nd) { L += d; S += d + nd; }
    2375             :     else
    2376             :     {
    2377    14809020 :       L <<= 1; if (d == nd) { L += d; S += d; }
    2378    14809020 :       break;
    2379             :     }
    2380             :   }
    2381    14089494 :   set_avma(av); *pL = L; *pS = S;
    2382    14929053 : }
    2383             : /* table of 6 * Hurwitz class numbers D <= lim */
    2384             : static void
    2385         154 : consttabh(long lim)
    2386             : {
    2387         154 :   pari_sp av = avma, av2;
    2388         154 :   GEN VHDH0, VDIV, CACHE = NULL;
    2389         154 :   GEN VHDH = caches[cache_H].cache;
    2390         154 :   long r, N, cachea, cacheb, lim0 = VHDH? lg(VHDH)-1: 2, LIM = lim0 << 1;
    2391             : 
    2392         154 :   if (lim <= 0) lim = 5;
    2393         154 :   if (lim <= LIM) return;
    2394         154 :   cache_reset(cache_H);
    2395         154 :   r = lim&3L; if (r) lim += 4-r;
    2396         154 :   cache_get(cache_DIV, lim);
    2397         154 :   VDIV = caches[cache_DIV].cache;
    2398         154 :   VHDH0 = cgetg(lim/2 + 1, t_VECSMALL);
    2399         154 :   VHDH0[1] = 2;
    2400         154 :   VHDH0[2] = 3;
    2401      863058 :   for (N = 3; N <= lim0; N++) VHDH0[N] = VHDH[N];
    2402         154 :   av2 = avma;
    2403         154 :   cachea = cacheb = 0;
    2404     7715523 :   for (N = LIM + 3; N <= lim; N += 4)
    2405             :   {
    2406     7739395 :     long s = 0, limt = usqrt(N>>2), flsq = 0, ind, t, L, S;
    2407             :     GEN DN, DN2;
    2408     7778700 :     if (N + 2 >= lg(VDIV))
    2409             :     { /* use local cache */
    2410             :       GEN F;
    2411     5999342 :       if (N + 2 > cacheb)
    2412             :       {
    2413        1562 :         set_avma(av2); cachea = N;
    2414        1562 :         CACHE = update_factor_cache(N, lim+2, &cacheb);
    2415             :       }
    2416     5999342 :       F = gel(CACHE, ((N-cachea)>>1)+1); /* factoru(N) */
    2417     5999342 :       DN = divisorsu_fact(F);
    2418     5979482 :       F = gel(CACHE, ((N-cachea)>>1)+2); /* factoru(N+2) */
    2419     5979482 :       DN2 = divisorsu_fact(F);
    2420             :     }
    2421             :     else
    2422             :     { /* use global cache */
    2423     1779358 :       DN = gel(VDIV,N);
    2424     1779358 :       DN2 = gel(VDIV,N+2);
    2425             :     }
    2426     7651832 :     ind = N >> 1;
    2427   893566373 :     for (t = 1; t <= limt; t++)
    2428             :     {
    2429   885914541 :       ind -= (t<<2)-2; /* N/2 - 2t^2 */
    2430   885914541 :       if (ind) s += VHDH0[ind]; else flsq = 1;
    2431             :     }
    2432     7651832 :     lamsig(DN, &L,&S);
    2433     7612658 :     VHDH0[N >> 1] = 2*S - 3*L - 2*s + flsq;
    2434     7612658 :     s = 0; flsq = 0; limt = (usqrt(N+2) - 1) >> 1;
    2435     7447713 :     ind = (N+1) >> 1;
    2436   897751584 :     for (t = 1; t <= limt; t++)
    2437             :     {
    2438   890303871 :       ind -= t<<2; /* (N+1)/2 - 2t(t+1) */
    2439   890303871 :       if (ind) s += VHDH0[ind]; else flsq = 1;
    2440             :     }
    2441     7447713 :     lamsig(DN2, &L,&S);
    2442     7715369 :     VHDH0[(N+1) >> 1] = S - 3*(L >> 1) - s - flsq;
    2443             :   }
    2444          89 :   cache_set(cache_H, VHDH0); set_avma(av);
    2445             : }
    2446             : 
    2447             : /*************************************************************************/
    2448             : /* Core functions using factorizations, divisors of class numbers caches */
    2449             : /* TODO: myfactoru and factorization cache should be exported */
    2450             : static GEN
    2451    33684624 : myfactoru(long N)
    2452             : {
    2453    33684624 :   GEN z = cache_get(cache_FACT, N);
    2454    33684624 :   return z? gcopy(z): factoru(N);
    2455             : }
    2456             : static GEN
    2457    69087138 : mydivisorsu(long N)
    2458             : {
    2459    69087138 :   GEN z = cache_get(cache_DIV, N);
    2460    69087138 :   return z? leafcopy(z): divisorsu(N);
    2461             : }
    2462             : /* write -n = Df^2, D < 0 fundamental discriminant. Return D, set f. */
    2463             : static long
    2464   176460473 : mycoredisc2neg(ulong n, long *pf)
    2465             : {
    2466   176460473 :   ulong m, D = (ulong)cache_get(cache_D, n);
    2467   176460473 :   if (D) { *pf = usqrt(n/D); return -(long)D; }
    2468          56 :   m = mycore(n, pf);
    2469          56 :   if ((m&3) != 3) { m <<= 2; *pf >>= 1; }
    2470          56 :   return (long)-m;
    2471             : }
    2472             : /* write n = Df^2, D > 0 fundamental discriminant. Return D, set f. */
    2473             : static long
    2474          14 : mycoredisc2pos(ulong n, long *pf)
    2475             : {
    2476          14 :   ulong m = mycore(n, pf);
    2477          14 :   if ((m&3) != 1) { m <<= 2; *pf >>= 1; }
    2478          14 :   return (long)m;
    2479             : }
    2480             : 
    2481             : /* D < 0 fundamental. Return 6*hclassno(-D); faster than quadclassunit up
    2482             :  * to 5*10^5 or so */
    2483             : static ulong
    2484          56 : hclassno6_count(long D)
    2485             : {
    2486          56 :   ulong a, b, b2, h = 0, d = -D;
    2487          56 :   int f = 0;
    2488             : 
    2489          56 :   if (d > 500000) return 6 * quadclassnos(D);
    2490             :   /* this part would work with -d non fundamental */
    2491          49 :   b = d&1; b2 = (1+d)>>2;
    2492          49 :   if (!b)
    2493             :   {
    2494        1165 :     for (a=1; a*a<b2; a++)
    2495        1161 :       if (b2%a == 0) h++;
    2496           4 :     f = (a*a==b2); b=2; b2=(4+d)>>2;
    2497             :   }
    2498        9502 :   while (b2*3 < d)
    2499             :   {
    2500        9453 :     if (b2%b == 0) h++;
    2501     1522009 :     for (a=b+1; a*a < b2; a++)
    2502     1512556 :       if (b2%a == 0) h += 2;
    2503        9453 :     if (a*a == b2) h++;
    2504        9453 :     b += 2; b2 = (b*b+d)>>2;
    2505             :   }
    2506          49 :   if (b2*3 == d) return 6*h+2;
    2507          49 :   if (f) return 6*h+3;
    2508          49 :   return 6*h;
    2509             : }
    2510             : /* D0 < 0; 6 * hclassno(-D), using D = D0*F^2 */
    2511             : static long
    2512          81 : hclassno6u_2(long D0, long F)
    2513             : {
    2514             :   long h;
    2515          81 :   if (F == 1) h = hclassno6_count(D0);
    2516             :   else
    2517             :   { /* second chance */
    2518          25 :     h = (ulong)cache_get(cache_H, -D0);
    2519          25 :     if (!h) h = hclassno6_count(D0);
    2520          25 :     h *= uhclassnoF_fact(myfactoru(F), D0);
    2521             :   }
    2522          81 :   return h;
    2523             : }
    2524             : /* D > 0; 6 * hclassno(D) (6*Hurwitz). Beware, cached value for D (=0,3 mod 4)
    2525             :  * is stored at D>>1 */
    2526             : ulong
    2527     2427669 : hclassno6u(ulong D)
    2528             : {
    2529     2427669 :   ulong z = (ulong)cache_get(cache_H, D);
    2530             :   long D0, F;
    2531     2427669 :   if (z) return z;
    2532          81 :   D0 = mycoredisc2neg(D, &F);
    2533          81 :   return hclassno6u_2(D0,F);
    2534             : }
    2535             : /* same as hclassno6u without creating caches */
    2536             : ulong
    2537       86917 : hclassno6u_no_cache(ulong D)
    2538             : {
    2539       86917 :   cache *S = &caches[cache_H];
    2540             :   long D0, F;
    2541       86917 :   if (S->cache)
    2542             :   {
    2543       80025 :     const ulong d = D>>1; /* compressed */
    2544       80025 :     if ((ulong)lg(S->cache) > d) return S->cache[d];
    2545             :   }
    2546       86647 :   S = &caches[cache_D];
    2547       86647 :   if (!S->cache || (ulong)lg(S->cache) <= D) return 0;
    2548           0 :   D0 = mycoredisc2neg(D, &F);
    2549           0 :   return hclassno6u_2(D0,F);
    2550             : }
    2551             : /* same, where the decomposition D = D0*F^2 is already known */
    2552             : static ulong
    2553   156440563 : hclassno6u_i(ulong D, long D0, long F)
    2554             : {
    2555   156440563 :   ulong z = (ulong)cache_get(cache_H, D);
    2556   156440563 :   if (z) return z;
    2557           0 :   return hclassno6u_2(D0,F);
    2558             : }
    2559             : 
    2560             : /* D < -4 fundamental, h(D), ordinary class number */
    2561             : static long
    2562    10646797 : myh(long D)
    2563             : {
    2564    10646797 :   ulong z = (ulong)cache_get(cache_H, -D);
    2565    10646797 :   return z? z / 6: quadclassnos(D);
    2566             : }
    2567             : 
    2568             : /*************************************************************************/
    2569             : /*                          TRACE FORMULAS                               */
    2570             : /* CHIP primitive, initialize for t_POLMOD output */
    2571             : static GEN
    2572       32690 : mfcharinit(GEN CHIP)
    2573             : {
    2574       32690 :   long n, o, l, vt, N = mfcharmodulus(CHIP);
    2575             :   GEN c, v, V, G, Pn;
    2576       32690 :   if (N == 1) return mkvec2(mkvec(gen_1), pol_x(0));
    2577        5481 :   G = gel(CHIP,1);
    2578        5481 :   v = ncharvecexpo(G, znconrey_normalized(G, gel(CHIP,2)));
    2579        5481 :   l = lg(v); V = cgetg(l, t_VEC);
    2580        5481 :   o = mfcharorder(CHIP);
    2581        5481 :   Pn = mfcharpol(CHIP); vt = varn(Pn);
    2582        5481 :   if (o <= 2)
    2583             :   {
    2584       59248 :     for (n = 1; n < l; n++)
    2585             :     {
    2586       54719 :       if (v[n] < 0) c = gen_0; else c = v[n]? gen_m1: gen_1;
    2587       54719 :       gel(V,n) = c;
    2588             :     }
    2589             :   }
    2590             :   else
    2591             :   {
    2592       16835 :     for (n = 1; n < l; n++)
    2593             :     {
    2594       15883 :       if (v[n] < 0) c = gen_0;
    2595             :       else
    2596             :       {
    2597        8890 :         c = Qab_zeta(v[n], o, vt);
    2598        8890 :         if (typ(c) == t_POL && lg(c) >= lg(Pn)) c = RgX_rem(c, Pn);
    2599             :       }
    2600       15883 :       gel(V,n) = c;
    2601             :     }
    2602             :   }
    2603        5481 :   return mkvec2(V, Pn);
    2604             : }
    2605             : static GEN
    2606      410718 : vchip_lift(GEN VCHI, long x, GEN C)
    2607             : {
    2608      410718 :   GEN V = gel(VCHI,1);
    2609      410718 :   long F = lg(V)-1;
    2610      410718 :   if (F == 1) return C;
    2611       18368 :   x %= F;
    2612       18368 :   if (!x) return C;
    2613       18368 :   if (x <= 0) x += F;
    2614       18368 :   return gmul(C, gel(V, x));
    2615             : }
    2616             : static long
    2617   279516120 : vchip_FC(GEN VCHI) { return lg(gel(VCHI,1))-1; }
    2618             : static GEN
    2619     6440981 : vchip_mod(GEN VCHI, GEN S)
    2620     6440981 : { return (typ(S) == t_POL)? RgX_rem(S, gel(VCHI,2)): S; }
    2621             : static GEN
    2622     1913440 : vchip_polmod(GEN VCHI, GEN S)
    2623     1913440 : { return (typ(S) == t_POL)? mkpolmod(S, gel(VCHI,2)): S; }
    2624             : 
    2625             : /* contribution of scalar matrices in dimension formula */
    2626             : static GEN
    2627      359366 : A1(long N, long k) { return uutoQ(mypsiu(N)*(k-1), 12); }
    2628             : static long
    2629        7602 : ceilA1(long N, long k) { return ceildivuu(mypsiu(N) * (k-1), 12); }
    2630             : 
    2631             : /* sturm bound, slightly larger than dimension */
    2632             : long
    2633       21714 : mfsturmNk(long N, long k) { return (mypsiu(N) * k) / 12; }
    2634             : long
    2635        3262 : mfsturmNgk(long N, GEN k)
    2636             : {
    2637        3262 :   long n,d; Qtoss(k,&n,&d);
    2638        3262 :   return 1 + (mypsiu(N)*n)/(d == 1? 12: 24);
    2639             : }
    2640             : static long
    2641         427 : mfsturmmf(GEN F) { return mfsturmNgk(mf_get_N(F), mf_get_gk(F)); }
    2642             : 
    2643             : /* List of all solutions of x^2 + x + 1 = 0 modulo N, x modulo N */
    2644             : static GEN
    2645         539 : sqrtm3modN(long N)
    2646             : {
    2647             :   pari_sp av;
    2648             :   GEN fa, P, E, B, mB, A, Q, T, R, v, gen_m3;
    2649         539 :   long l, i, n, ct, fl3 = 0, Ninit;
    2650         539 :   if (!odd(N) || (N%9) == 0) return cgetg(1,t_VECSMALL);
    2651         511 :   Ninit = N;
    2652         511 :   if ((N%3) == 0) { N /= 3; fl3 = 1; }
    2653         511 :   fa = myfactoru(N); P = gel(fa, 1); E = gel(fa, 2);
    2654         511 :   l = lg(P);
    2655         707 :   for (i = 1; i < l; i++)
    2656         518 :     if ((P[i]%3) == 2) return cgetg(1,t_VECSMALL);
    2657         189 :   A = cgetg(l, t_VECSMALL);
    2658         189 :   B = cgetg(l, t_VECSMALL);
    2659         189 :   mB= cgetg(l, t_VECSMALL);
    2660         189 :   Q = cgetg(l, t_VECSMALL); gen_m3 = utoineg(3);
    2661         385 :   for (i = 1; i < l; i++)
    2662             :   {
    2663         196 :     long p = P[i], e = E[i];
    2664         196 :     Q[i] = upowuu(p,e);
    2665         196 :     B[i] = itou( Zp_sqrt(gen_m3, utoipos(p), e) );
    2666         196 :     mB[i]= Q[i] - B[i];
    2667             :   }
    2668         189 :   ct = 1 << (l-1);
    2669         189 :   T = ZV_producttree(Q);
    2670         189 :   R = ZV_chinesetree(Q,T);
    2671         189 :   v = cgetg(ct+1, t_VECSMALL);
    2672         189 :   av = avma;
    2673         581 :   for (n = 1; n <= ct; n++)
    2674             :   {
    2675         392 :     long m = n-1, r;
    2676         812 :     for (i = 1; i < l; i++)
    2677             :     {
    2678         420 :       A[i] = (m&1L)? mB[i]: B[i];
    2679         420 :       m >>= 1;
    2680             :     }
    2681         392 :     r = itou( ZV_chinese_tree(A, Q, T, R) );
    2682         462 :     if (fl3) while (r%3) r += N;
    2683         392 :     set_avma(av); v[n] = odd(r) ? (r-1) >> 1 : (r+Ninit-1) >> 1;
    2684             :   }
    2685         189 :   return v;
    2686             : }
    2687             : 
    2688             : /* number of elliptic points of order 3 in X0(N) */
    2689             : static long
    2690       10178 : nu3(long N)
    2691             : {
    2692             :   long i, l;
    2693             :   GEN P;
    2694       10178 :   if (!odd(N) || (N%9) == 0) return 0;
    2695        8953 :   if ((N%3) == 0) N /= 3;
    2696        8953 :   P = gel(myfactoru(N), 1); l = lg(P);
    2697       13118 :   for (i = 1; i < l; i++) if ((P[i]%3) == 2) return 0;
    2698        3983 :   return 1L<<(l-1);
    2699             : }
    2700             : /* number of elliptic points of order 2 in X0(N) */
    2701             : static long
    2702       17549 : nu2(long N)
    2703             : {
    2704             :   long i, l;
    2705             :   GEN P;
    2706       17549 :   if ((N&3L) == 0) return 0;
    2707       17549 :   if (!odd(N)) N >>= 1;
    2708       17549 :   P = gel(myfactoru(N), 1); l = lg(P);
    2709       21952 :   for (i = 1; i < l; i++) if ((P[i]&3L) == 3) return 0;
    2710        3941 :   return 1L<<(l-1);
    2711             : }
    2712             : 
    2713             : /* contribution of elliptic matrices of order 3 in dimension formula
    2714             :  * Only depends on CHIP the primitive char attached to CHI */
    2715             : static GEN
    2716       43701 : A21(long N, long k, GEN CHI)
    2717             : {
    2718             :   GEN res, G, chi, o;
    2719             :   long a21, i, limx, S;
    2720       43701 :   if ((N&1L) == 0) return gen_0;
    2721       21105 :   a21 = k%3 - 1;
    2722       21105 :   if (!a21) return gen_0;
    2723       20356 :   if (N <= 3) return sstoQ(a21, 3);
    2724       10717 :   if (!CHI) return sstoQ(nu3(N) * a21, 3);
    2725         539 :   res = sqrtm3modN(N); limx = (N - 1) >> 1;
    2726         539 :   G = gel(CHI,1); chi = gel(CHI,2);
    2727         539 :   o = gmfcharorder(CHI);
    2728         931 :   for (S = 0, i = 1; i < lg(res); i++)
    2729             :   { /* (x,N) = 1; S += chi(x) + chi(x^2) */
    2730         392 :     long x = res[i];
    2731         392 :     if (x <= limx)
    2732             :     { /* CHI(x)=e(c/o), 3rd-root of 1 */
    2733         196 :       GEN c = znchareval(G, chi, utoi(x), o);
    2734         196 :       if (!signe(c)) S += 2; else S--;
    2735             :     }
    2736             :   }
    2737         539 :   return sstoQ(a21 * S, 3);
    2738             : }
    2739             : 
    2740             : /* List of all square roots of -1 modulo N */
    2741             : static GEN
    2742         595 : sqrtm1modN(long N)
    2743             : {
    2744             :   pari_sp av;
    2745             :   GEN fa, P, E, B, mB, A, Q, T, R, v;
    2746         595 :   long l, i, n, ct, fleven = 0;
    2747         595 :   if ((N&3L) == 0) return cgetg(1,t_VECSMALL);
    2748         595 :   if ((N&1L) == 0) { N >>= 1; fleven = 1; }
    2749         595 :   fa = myfactoru(N); P = gel(fa,1); E = gel(fa,2);
    2750         595 :   l = lg(P);
    2751         945 :   for (i = 1; i < l; i++)
    2752         665 :     if ((P[i]&3L) == 3) return cgetg(1,t_VECSMALL);
    2753         280 :   A = cgetg(l, t_VECSMALL);
    2754         280 :   B = cgetg(l, t_VECSMALL);
    2755         280 :   mB= cgetg(l, t_VECSMALL);
    2756         280 :   Q = cgetg(l, t_VECSMALL);
    2757         574 :   for (i = 1; i < l; i++)
    2758             :   {
    2759         294 :     long p = P[i], e = E[i];
    2760         294 :     Q[i] = upowuu(p,e);
    2761         294 :     B[i] = itou( Zp_sqrt(gen_m1, utoipos(p), e) );
    2762         294 :     mB[i]= Q[i] - B[i];
    2763             :   }
    2764         280 :   ct = 1 << (l-1);
    2765         280 :   T = ZV_producttree(Q);
    2766         280 :   R = ZV_chinesetree(Q,T);
    2767         280 :   v = cgetg(ct+1, t_VECSMALL);
    2768         280 :   av = avma;
    2769         868 :   for (n = 1; n <= ct; n++)
    2770             :   {
    2771         588 :     long m = n-1, r;
    2772        1232 :     for (i = 1; i < l; i++)
    2773             :     {
    2774         644 :       A[i] = (m&1L)? mB[i]: B[i];
    2775         644 :       m >>= 1;
    2776             :     }
    2777         588 :     r = itou( ZV_chinese_tree(A, Q, T, R) );
    2778         588 :     if (fleven && !odd(r)) r += N;
    2779         588 :     set_avma(av); v[n] = r;
    2780             :   }
    2781         280 :   return v;
    2782             : }
    2783             : 
    2784             : /* contribution of elliptic matrices of order 4 in dimension formula.
    2785             :  * Only depends on CHIP the primitive char attached to CHI */
    2786             : static GEN
    2787       43701 : A22(long N, long k, GEN CHI)
    2788             : {
    2789             :   GEN G, chi, o, res;
    2790             :   long S, a22, i, limx, o2;
    2791       43701 :   if ((N&3L) == 0) return gen_0;
    2792       30086 :   a22 = (k & 3L) - 1; /* (k % 4) - 1 */
    2793       30086 :   if (!a22) return gen_0;
    2794       30086 :   if (N <= 2) return sstoQ(a22, 4);
    2795       18354 :   if (!CHI) return sstoQ(nu2(N)*a22, 4);
    2796         805 :   if (mfcharparity(CHI) == -1) return gen_0;
    2797         595 :   res = sqrtm1modN(N); limx = (N - 1) >> 1;
    2798         595 :   G = gel(CHI,1); chi = gel(CHI,2);
    2799         595 :   o = gmfcharorder(CHI);
    2800         595 :   o2 = itou(o)>>1;
    2801        1183 :   for (S = 0, i = 1; i < lg(res); i++)
    2802             :   { /* (x,N) = 1, S += real(chi(x)) */
    2803         588 :     long x = res[i];
    2804         588 :     if (x <= limx)
    2805             :     { /* CHI(x)=e(c/o), 4th-root of 1 */
    2806         294 :       long c = itou( znchareval(G, chi, utoi(x), o) );
    2807         294 :       if (!c) S++; else if (c == o2) S--;
    2808             :     }
    2809             :   }
    2810         595 :   return sstoQ(a22 * S, 2);
    2811             : }
    2812             : 
    2813             : /* sumdiv(N,d,eulerphi(gcd(d,N/d))) */
    2814             : static long
    2815       38892 : nuinf(long N)
    2816             : {
    2817       38892 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
    2818       38892 :   long i, t = 1, l = lg(P);
    2819       82656 :   for (i=1; i<l; i++)
    2820             :   {
    2821       43764 :     long p = P[i], e = E[i];
    2822       43764 :     if (odd(e))
    2823       34972 :       t *= upowuu(p,e>>1) << 1;
    2824             :     else
    2825        8792 :       t *= upowuu(p,(e>>1)-1) * (p+1);
    2826             :   }
    2827       38892 :   return t;
    2828             : }
    2829             : 
    2830             : /* contribution of hyperbolic matrices in dimension formula */
    2831             : static GEN
    2832       44149 : A3(long N, long FC)
    2833             : {
    2834             :   long i, S, NF, l;
    2835             :   GEN D;
    2836       44149 :   if (FC == 1) return uutoQ(nuinf(N),2);
    2837        5257 :   D = mydivisorsu(N); l = lg(D);
    2838        5257 :   S = 0; NF = N/FC;
    2839       41412 :   for (i = 1; i < l; i++)
    2840             :   {
    2841       36155 :     long g = ugcd(D[i], D[l-i]);
    2842       36155 :     if (NF%g == 0) S += myeulerphiu(g);
    2843             :   }
    2844        5257 :   return uutoQ(S, 2);
    2845             : }
    2846             : 
    2847             : /* special contribution in weight 2 in dimension formula */
    2848             : static long
    2849       43253 : A4(long k, long FC)
    2850       43253 : { return (k==2 && FC==1)? 1: 0; }
    2851             : /* gcd(x,N) */
    2852             : static long
    2853   283248301 : myugcd(GEN GCD, ulong x)
    2854             : {
    2855   283248301 :   ulong N = lg(GCD)-1;
    2856   283248301 :   if (x >= N) x %= N;
    2857   283248301 :   return GCD[x+1];
    2858             : }
    2859             : /* 1_{gcd(x,N) = 1} * chi(x), return NULL if 0 */
    2860             : static GEN
    2861   402384797 : mychicgcd(GEN GCD, GEN VCHI, long x)
    2862             : {
    2863   402384797 :   long N = lg(GCD)-1;
    2864   402384797 :   if (N == 1) return gen_1;
    2865   327947366 :   x = umodsu(x, N);
    2866   327947366 :   if (GCD[x+1] != 1) return NULL;
    2867   271862120 :   x %= vchip_FC(VCHI); if (!x) return gen_1;
    2868     4469213 :   return gel(gel(VCHI,1), x);
    2869             : }
    2870             : 
    2871             : /* contribution of scalar matrices to trace formula */
    2872             : static GEN
    2873     6390844 : TA1(long N, long k, GEN VCHI, GEN GCD, long n)
    2874             : {
    2875             :   GEN S;
    2876             :   ulong m;
    2877     6390844 :   if (!uissquareall(n, &m)) return gen_0;
    2878      385903 :   if (m == 1) return A1(N,k); /* common */
    2879      346094 :   S = mychicgcd(GCD, VCHI, m);
    2880      346094 :   return S? gmul(gmul(powuu(m, k-2), A1(N,k)), S): gen_0;
    2881             : }
    2882             : 
    2883             : /* All square roots modulo 4N, x modulo 2N, precomputed to accelerate TA2 */
    2884             : static GEN
    2885      127169 : mksqr(long N)
    2886             : {
    2887      127169 :   pari_sp av = avma;
    2888      127169 :   long x, N2 = N << 1, N4 = N << 2;
    2889      127169 :   GEN v = const_vec(N2, cgetg(1, t_VECSMALL));
    2890      127169 :   gel(v, N2) = mkvecsmall(0); /* x = 0 */
    2891     3490627 :   for (x = 1; x <= N; x++)
    2892             :   {
    2893     3363458 :     long r = (((x*x - 1)%N4) >> 1) + 1;
    2894     3363458 :     gel(v,r) = vecsmall_append(gel(v,r), x);
    2895             :   }
    2896      127169 :   return gerepilecopy(av, v);
    2897             : }
    2898             : 
    2899             : static GEN
    2900      127169 : mkgcd(long N)
    2901             : {
    2902             :   GEN GCD, d;
    2903             :   long i, N2;
    2904      127169 :   if (N == 1) return mkvecsmall(N);
    2905      104755 :   GCD = cgetg(N + 1, t_VECSMALL);
    2906      104755 :   d = GCD+1; /* GCD[i+1] = d[i] = gcd(i,N) = gcd(N-i,N), i = 0..N-1 */
    2907      104755 :   d[0] = N; d[1] = d[N-1] = 1; N2 = N>>1;
    2908     1649809 :   for (i = 2; i <= N2; i++) d[i] = d[N-i] = ugcd(N, i);
    2909      104755 :   return GCD;
    2910             : }
    2911             : 
    2912             : /* Table of \sum_{x^2-tx+n=0 mod Ng}chi(x) for all g dividing gcd(N,F),
    2913             :  * F^2 largest such that (t^2-4n)/F^2=0 or 1 mod 4; t >= 0 */
    2914             : static GEN
    2915    15215785 : mutglistall(long t, long N, long NF, GEN VCHI, long n, GEN MUP, GEN li, GEN GCD)
    2916             : {
    2917    15215785 :   long i, lx = lg(li);
    2918    15215785 :   GEN DNF = mydivisorsu(NF), v = zerovec(NF);
    2919    15215785 :   long j, g, lDNF = lg(DNF);
    2920    42452890 :   for (i = 1; i < lx; i++)
    2921             :   {
    2922    27237105 :     long x = (li[i] + t) >> 1, y, lD;
    2923    27237105 :     GEN D, c = mychicgcd(GCD, VCHI, x);
    2924    27237105 :     if (li[i] && li[i] != N)
    2925             :     {
    2926    18094600 :       GEN c2 = mychicgcd(GCD, VCHI, t - x);
    2927    18094600 :       if (c2) c = c? gadd(c, c2): c2;
    2928             :     }
    2929    27237105 :     if (!c) continue;
    2930    22096466 :     y = (x*(x - t) + n) / N; /* exact division */
    2931    22096466 :     D = mydivisorsu(ugcd(labs(y), NF)); lD = lg(D);
    2932    59531880 :     for (j=1; j < lD; j++) { g = D[j]; gel(v,g) = gadd(gel(v,g), c); }
    2933             :   }
    2934             :   /* j = 1 corresponds to g = 1, and MUP[1] = 1 */
    2935    35145437 :   for (j=2; j < lDNF; j++) { g = DNF[j]; gel(v,g) = gmulsg(MUP[g], gel(v,g)); }
    2936    15215785 :   return v;
    2937             : }
    2938             : 
    2939             : /* special case (N,F) = 1: easier */
    2940             : static GEN
    2941   161244593 : mutg1(long t, long N, GEN VCHI, GEN li, GEN GCD)
    2942             : { /* (N,F) = 1 */
    2943   161244593 :   GEN S = NULL;
    2944   161244593 :   long i, lx = lg(li);
    2945   338364024 :   for (i = 1; i < lx; i++)
    2946             :   {
    2947   177119431 :     long x = (li[i] + t) >> 1;
    2948   177119431 :     GEN c = mychicgcd(GCD, VCHI, x);
    2949   177119431 :     if (c) S = S? gadd(S, c): c;
    2950   177119431 :     if (li[i] && li[i] != N)
    2951             :     {
    2952    97946933 :       c = mychicgcd(GCD, VCHI, t - x);
    2953    97946933 :       if (c) S = S? gadd(S, c): c;
    2954             :     }
    2955   177119431 :     if (S && !signe(S)) S = NULL; /* strive hard to add gen_0 */
    2956             :   }
    2957   161244593 :   return S; /* single value */
    2958             : }
    2959             : 
    2960             : /* Gegenbauer pol; n > 2, P = \sum_{0<=j<=n/2} (-1)^j (n-j)!/j!(n-2*j)! X^j */
    2961             : GEN
    2962      361541 : mfrhopol(long n)
    2963             : {
    2964             : #ifdef LONG_IS_64BIT
    2965      309936 :   const long M = 2642249;
    2966             : #else
    2967       51605 :   const long M = 1629;
    2968             : #endif
    2969      361541 :   long j, d = n >> 1; /* >= 1 */
    2970      361541 :   GEN P = cgetg(d + 3, t_POL);
    2971             : 
    2972      361541 :   if (n > M) pari_err_IMPL("mfrhopol for large weight"); /* avoid overflow */
    2973      361541 :   P[1] = evalvarn(0)|evalsigne(1);
    2974      361541 :   gel(P,2) = gen_1;
    2975      361541 :   gel(P,3) = utoineg(n-1); /* j = 1 */
    2976      361541 :   if (d > 1) gel(P,4) = utoipos(((n-3)*(n-2)) >> 1); /* j = 2 */
    2977      361541 :   if (d > 2) gel(P,5) = utoineg(((n-5)*(n-4)*(n-3)) / 6); /* j = 3 */
    2978     1516390 :   for (j = 4; j <= d; j++)
    2979     1154849 :     gel(P,j+2) = divis(mulis(gel(P,j+1), (n-2*j+1)*(n-2*j+2)), (n-j+1)*(-j));
    2980      361541 :   return P;
    2981             : }
    2982             : 
    2983             : /* polrecip(Q)(t2), assume Q(0) = 1 */
    2984             : GEN
    2985     3251068 : mfrhopol_u_eval(GEN Q, ulong t2)
    2986             : {
    2987     3251068 :   GEN T = addiu(gel(Q,3), t2);
    2988     3251067 :   long l = lg(Q), j;
    2989    37887456 :   for (j = 4; j < l; j++) T = addii(gel(Q,j), mului(t2, T));
    2990     3251068 :   return T;
    2991             : }
    2992             : GEN
    2993       56620 : mfrhopol_eval(GEN Q, GEN t2)
    2994             : {
    2995             :   long l, j;
    2996             :   GEN T;
    2997       56620 :   if (lgefint(t2) == 3) return mfrhopol_u_eval(Q, t2[2]);
    2998           0 :   l = lg(Q); T = addii(gel(Q,3), t2);
    2999           0 :   for (j = 4; j < l; j++) T = addii(gel(Q,j), mulii(t2, T));
    3000           0 :   return T;
    3001             : }
    3002             : /* return sh * sqrt(n)^nu * G_nu(t/(2*sqrt(n))) for t != 0
    3003             :  * else (sh/2) * sqrt(n)^nu * G_nu(0) [ implies nu is even ]
    3004             :  * G_nu(z) = \sum_{0<=j<=nu/2} (-1)^j (nu-j)!/j!(nu-2*j)! * (2z)^(nu-2*j)) */
    3005             : static GEN
    3006   168051054 : mfrhopowsimp(GEN Q, GEN sh, long nu, long t, long t2, long n)
    3007             : {
    3008             :   GEN T;
    3009   168051054 :   switch (nu)
    3010             :   {
    3011   162107834 :     case 0: return t? sh: gmul2n(sh,-1);
    3012     1125446 :     case 1: return gmulsg(t, sh);
    3013     1576750 :     case 2: return t? gmulsg(t2 - n, sh): gmul(gmul2n(stoi(-n), -1), sh);
    3014         427 :     case 3: return gmul(mulss(t, t2 - 2*n), sh);
    3015     3240597 :     default:
    3016     3240597 :       if (!t) return gmul(gmul2n(gel(Q, lg(Q) - 1), -1), sh);
    3017     3194448 :       T = mfrhopol_u_eval(Q, t2); if (odd(nu)) T = mulsi(t, T);
    3018     3194448 :       return gmul(T, sh);
    3019             :   }
    3020             : }
    3021             : 
    3022             : /* contribution of elliptic matrices to trace formula */
    3023             : static GEN
    3024     6390844 : TA2(long N, long k, GEN VCHI, long n, GEN SQRTS, GEN MUP, GEN GCD)
    3025             : {
    3026     6390844 :   const long n4 = n << 2, N4 = N << 2, nu = k - 2;
    3027     6390844 :   const long st = (!odd(N) && odd(n)) ? 2 : 1;
    3028             :   long limt, t;
    3029             :   GEN S, Q;
    3030             : 
    3031     6390844 :   limt = usqrt(n4);
    3032     6390844 :   if (limt*limt == n4) limt--;
    3033     6390844 :   Q = nu > 3 ? ZX_z_unscale(mfrhopol(nu), n) : NULL;
    3034     6390844 :   S = gen_0;
    3035   325844922 :   for (t = odd(k)? st: 0; t <= limt; t += st) /* t^2 < 4n */
    3036             :   {
    3037   319454078 :     pari_sp av = avma;
    3038   319454078 :     long t2 = t*t, D = n4 - t2, F, D0, NF;
    3039             :     GEN sh, li;
    3040             : 
    3041   319454078 :     li = gel(SQRTS, (umodsu(-D - 1, N4) >> 1) + 1);
    3042   327863402 :     if (lg(li) == 1) continue;
    3043   176460378 :     D0 = mycoredisc2neg(D, &F);
    3044   176460378 :     NF = myugcd(GCD, F);
    3045   176460378 :     if (NF == 1)
    3046             :     { /* (N,F) = 1 => single value in mutglistall */
    3047   161244593 :       GEN mut = mutg1(t, N, VCHI, li, GCD);
    3048   161244593 :       if (!mut) { set_avma(av); continue; }
    3049   156440563 :       sh = gmul(uutoQ(hclassno6u_i(D,D0,F),6), mut);
    3050             :     }
    3051             :     else
    3052             :     {
    3053    15215785 :       GEN v = mutglistall(t, N, NF, VCHI, n, MUP, li, GCD);
    3054    15215785 :       GEN DF = mydivisorsu(F);
    3055    15215785 :       long i, lDF = lg(DF);
    3056    15215785 :       sh = gen_0;
    3057    61336626 :       for (i = 1; i < lDF; i++)
    3058             :       {
    3059    46120841 :         long Ff, f = DF[i], g = myugcd(GCD, f);
    3060    46120841 :         GEN mut = gel(v, g);
    3061    46120841 :         if (gequal0(mut)) continue;
    3062    31198055 :         Ff = DF[lDF-i]; /* F/f */
    3063    31198055 :         if (Ff == 1) sh = gadd(sh, mut);
    3064             :         else
    3065             :         {
    3066    22357666 :           GEN P = gel(myfactoru(Ff), 1);
    3067    22357666 :           long j, lP = lg(P);
    3068    49309307 :           for (j = 1; j < lP; j++) { long p = P[j]; Ff -= kross(D0, p)*Ff/p; }
    3069    22357666 :           sh = gadd(sh, gmulsg(Ff, mut));
    3070             :         }
    3071             :       }
    3072    15215785 :       if (gequal0(sh)) { set_avma(av); continue; }
    3073    11610491 :       if (D0 == -3) sh = gdivgu(sh, 3);
    3074    11117981 :       else if (D0 == -4) sh = gdivgu(sh, 2);
    3075    10646797 :       else sh = gmulgu(sh, myh(D0));
    3076             :     }
    3077   168051054 :     S = gerepileupto(av, gadd(S, mfrhopowsimp(Q,sh,nu,t,t2,n)));
    3078             :   }
    3079     6390844 :   return S;
    3080             : }
    3081             : 
    3082             : /* compute global auxiliary data for TA3 */
    3083             : static GEN
    3084      127169 : mkbez(long N, long FC)
    3085             : {
    3086      127169 :   long ct, i, NF = N/FC;
    3087      127169 :   GEN w, D = mydivisorsu(N);
    3088      127169 :   long l = lg(D);
    3089             : 
    3090      127169 :   w = cgetg(l, t_VEC);
    3091      368788 :   for (i = ct = 1; i < l; i++)
    3092             :   {
    3093      346374 :     long u, v, h, c = D[i], Nc = D[l-i];
    3094      346374 :     if (c > Nc) break;
    3095      241619 :     h = cbezout(c, Nc, &u, &v);
    3096      241619 :     if (h == 1) /* shortcut */
    3097      174251 :       gel(w, ct++) = mkvecsmall4(1,u*c,1,i);
    3098       67368 :     else if (!(NF%h))
    3099       57456 :       gel(w, ct++) = mkvecsmall4(h,u*(c/h),myeulerphiu(h),i);
    3100             :   }
    3101      127169 :   setlg(w,ct); stackdummy((pari_sp)(w+ct),(pari_sp)(w+l));
    3102      127169 :   return w;
    3103             : }
    3104             : 
    3105             : /* contribution of hyperbolic matrices to trace formula, d * nd = n,
    3106             :  * DN = divisorsu(N) */
    3107             : static GEN
    3108    33177353 : auxsum(GEN VCHI, GEN GCD, long d, long nd, GEN DN, GEN BEZ)
    3109             : {
    3110    33177353 :   GEN S = gen_0;
    3111    33177353 :   long ct, g = nd - d, lDN = lg(DN), lBEZ = lg(BEZ);
    3112    85198288 :   for (ct = 1; ct < lBEZ; ct++)
    3113             :   {
    3114    52020935 :     GEN y, B = gel(BEZ, ct);
    3115    52020935 :     long ic, c, Nc, uch, h = B[1];
    3116    52020935 :     if (g%h) continue;
    3117    50813883 :     uch = B[2];
    3118    50813883 :     ic  = B[4];
    3119    50813883 :     c = DN[ic];
    3120    50813883 :     Nc= DN[lDN - ic]; /* Nc = N/c */
    3121    50813883 :     if (ugcd(Nc, nd) == 1)
    3122    43379701 :       y = mychicgcd(GCD, VCHI, d + uch*g); /* 0 if (c,d) > 1 */
    3123             :     else
    3124     7434182 :       y = NULL;
    3125    50813883 :     if (c != Nc && ugcd(Nc, d) == 1)
    3126             :     {
    3127    38260933 :       GEN y2 = mychicgcd(GCD, VCHI, nd - uch*g); /* 0 if (c,nd) > 1 */
    3128    38260933 :       if (y2) y = y? gadd(y, y2): y2;
    3129             :     }
    3130    50813883 :     if (y) S = gadd(S, gmulsg(B[3], y));
    3131             :   }
    3132    33177353 :   return S;
    3133             : }
    3134             : 
    3135             : static GEN
    3136     6390844 : TA3(long N, long k, GEN VCHI, GEN GCD, GEN Dn, GEN BEZ)
    3137             : {
    3138     6390844 :   GEN S = gen_0, DN = mydivisorsu(N);
    3139     6390844 :   long i, l = lg(Dn);
    3140    39568197 :   for (i = 1; i < l; i++)
    3141             :   {
    3142    39528388 :     long d = Dn[i], nd = Dn[l-i]; /* = n/d */
    3143             :     GEN t, u;
    3144    39528388 :     if (d > nd) break;
    3145    33177353 :     t = auxsum(VCHI, GCD, d, nd, DN, BEZ);
    3146    33177353 :     if (isintzero(t)) continue;
    3147    32059621 :     u = powuu(d,k-1); if (d == nd) u = gmul2n(u,-1);
    3148    32059621 :     S = gadd(S, gmul(u,t));
    3149             :   }
    3150     6390844 :   return S;
    3151             : }
    3152             : 
    3153             : /* special contribution in weight 2 in trace formula */
    3154             : static long
    3155     6390844 : TA4(long k, GEN VCHIP, GEN Dn, GEN GCD)
    3156             : {
    3157             :   long i, l, S;
    3158     6390844 :   if (k != 2 || vchip_FC(VCHIP) != 1) return 0;
    3159     5687416 :   l = lg(Dn); S = 0;
    3160    66354498 :   for (i = 1; i < l; i++)
    3161             :   {
    3162    60667082 :     long d = Dn[i]; /* gcd(N,n/d) == 1? */
    3163    60667082 :     if (myugcd(GCD, Dn[l-i]) == 1) S += d;
    3164             :   }
    3165     5687416 :   return S;
    3166             : }
    3167             : 
    3168             : /* precomputation of products occurring im mutg, again to accelerate TA2 */
    3169             : static GEN
    3170      127169 : mkmup(long N)
    3171             : {
    3172      127169 :   GEN fa = myfactoru(N), P = gel(fa,1), D = divisorsu_fact(fa);
    3173      127169 :   long i, lP = lg(P), lD = lg(D);
    3174      127169 :   GEN MUP = zero_zv(N);
    3175      127169 :   MUP[1] = 1;
    3176      445613 :   for (i = 2; i < lD; i++)
    3177             :   {
    3178      318444 :     long j, g = D[i], Ng = D[lD-i]; /*  N/g */
    3179      871731 :     for (j = 1; j < lP; j++) { long p = P[j]; if (Ng%p) g += g/p; }
    3180      318444 :     MUP[D[i]] = g;
    3181             :   }
    3182      127169 :   return MUP;
    3183             : }
    3184             : 
    3185             : /* quadratic nonresidues mod p; p odd prime, p^2 fits in a long */
    3186             : static GEN
    3187        2814 : non_residues(long p)
    3188             : {
    3189        2814 :   long i, j, p2 = p >> 1;
    3190        2814 :   GEN v = cgetg(p2+1, t_VECSMALL), w = const_vecsmall(p-1, 1);
    3191        4571 :   for (i = 2; i <= p2; i++) w[(i*i) % p] = 0; /* no need to check 1 */
    3192        9142 :   for (i = 2, j = 1; i < p; i++) if (w[i]) v[j++] = i;
    3193        2814 :   return v;
    3194             : }
    3195             : 
    3196             : /* CHIP primitive. Return t_VECSMALL v of length q such that
    3197             :  * Tr^new_{N,CHIP}(n) = 0 whenever v[(n%q) + 1] is nonzero */
    3198             : static GEN
    3199       32788 : mfnewzerodata(long N, GEN CHIP)
    3200             : {
    3201       32788 :   GEN V, M, L, faN = myfactoru(N), PN = gel(faN,1), EN = gel(faN,2);
    3202       32788 :   GEN G = gel(CHIP,1), chi = gel(CHIP,2);
    3203       32788 :   GEN fa = znstar_get_faN(G), P = ZV_to_zv(gel(fa,1)), E = gel(fa,2);
    3204       32788 :   long i, mod, j = 1, l = lg(PN);
    3205             : 
    3206       32788 :   M = cgetg(l, t_VECSMALL); M[1] = 0;
    3207       32788 :   V = cgetg(l, t_VEC);
    3208             :   /* Tr^new(n) = 0 if (n mod M[i]) in V[i]  */
    3209       32788 :   if ((N & 3) == 0)
    3210             :   {
    3211       12929 :     long e = EN[1];
    3212       12929 :     long c = (lg(P) > 1 && P[1] == 2)? E[1]: 0; /* c = v_2(FC) */
    3213             :     /* e >= 2 */
    3214       12929 :     if (c == e-1) return NULL; /* Tr^new = 0 */
    3215       12824 :     if (c == e)
    3216             :     {
    3217        3717 :       if (e == 2)
    3218             :       { /* sc: -4 */
    3219        1785 :         gel(V,1) = mkvecsmall(3);
    3220        1785 :         M[1] = 4;
    3221             :       }
    3222        1932 :       else if (e == 3)
    3223             :       { /* sc: -8 (CHI_2(-1)=-1<=>chi[1]=1) and 8 (CHI_2(-1)=1 <=> chi[1]=0) */
    3224        1932 :         long t = signe(gel(chi,1))? 7: 3;
    3225        1932 :         gel(V,1) = mkvecsmall2(5, t);
    3226        1932 :         M[1] = 8;
    3227             :       }
    3228             :     }
    3229        9107 :     else if (e == 5 && c == 3)
    3230         154 :     { /* sc: -8 (CHI_2(-1)=-1<=>chi[1]=1) and 8 (CHI_2(-1)=1 <=> chi[1]=0) */
    3231         154 :       long t = signe(gel(chi,1))? 7: 3;
    3232         154 :       gel(V,1) = mkvecsmalln(6, 2L,4L,5L,6L,8L,t);
    3233         154 :       M[1] = 8;
    3234             :     }
    3235        8953 :     else if ((e == 4 && c == 2) || (e == 5 && c <= 2) || (e == 6 && c <= 2)
    3236        7378 :          || (e >= 7 && c == e - 3))
    3237             :     { /* sc: 4 */
    3238        1575 :       gel(V,1) = mkvecsmall3(0,2,3);
    3239        1575 :       M[1] = 4;
    3240             :     }
    3241        7378 :     else if ((e <= 4 && c == 0) || (e >= 5 && c == e - 2))
    3242             :     { /* sc: 2 */
    3243        7021 :       gel(V,1) = mkvecsmall(0);
    3244        7021 :       M[1] = 2;
    3245             :     }
    3246         357 :     else if ((e == 6 && c == 3) || (e >= 7 && c <= e - 4))
    3247             :     { /* sc: -2 */
    3248         357 :       gel(V,1) = mkvecsmalln(7, 0L,2L,3L,4L,5L,6L,7L);
    3249         357 :       M[1] = 8;
    3250             :     }
    3251             :   }
    3252       32683 :   j = M[1]? 2: 1;
    3253       69762 :   for (i = odd(N)? 1: 2; i < l; i++) /* skip p=2, done above */
    3254             :   {
    3255       37079 :     long p = PN[i], e = EN[i];
    3256       37079 :     long z = zv_search(P, p), c = z? E[z]: 0; /* c = v_p(FC) */
    3257       37079 :     if ((e <= 2 && c == 1 && itos(gel(chi,z)) == (p>>1)) /* ord(CHI_p)=2 */
    3258       34888 :         || (e >= 3 && c <= e - 2))
    3259        2814 :     { /* sc: -p */
    3260        2814 :       GEN v = non_residues(p);
    3261        2814 :       if (e != 1) v = vecsmall_prepend(v, 0);
    3262        2814 :       gel(V,j) = v;
    3263        2814 :       M[j] = p; j++;
    3264             :     }
    3265       34265 :     else if (e >= 2 && c < e)
    3266             :     { /* sc: p */
    3267        2590 :       gel(V,j) = mkvecsmall(0);
    3268        2590 :       M[j] = p; j++;
    3269             :     }
    3270             :   }
    3271       32683 :   if (j == 1) return cgetg(1, t_VECSMALL);
    3272       15309 :   setlg(V,j); setlg(M,j); mod = zv_prod(M);
    3273       15309 :   L = zero_zv(mod);
    3274       33537 :   for (i = 1; i < j; i++)
    3275             :   {
    3276       18228 :     GEN v = gel(V,i);
    3277       18228 :     long s, m = M[i], lv = lg(v);
    3278       47481 :     for (s = 1; s < lv; s++)
    3279             :     {
    3280       29253 :       long a = v[s] + 1;
    3281       56322 :       do { L[a] = 1; a += m; } while (a <= mod);
    3282             :     }
    3283             :   }
    3284       15309 :   return L;
    3285             : }
    3286             : /* v=mfnewzerodata(N,CHI); returns TRUE if newtrace(n) must be zero,
    3287             :  * (but newtrace(n) may still be zero if we return FALSE) */
    3288             : static long
    3289     2599588 : mfnewchkzero(GEN v, long n) { long q = lg(v)-1; return q && v[(n%q) + 1]; }
    3290             : 
    3291             : /* if (!VCHIP): from mftraceform_cusp;
    3292             :  * else from initnewtrace and CHI is known to be primitive */
    3293             : static GEN
    3294      127169 : inittrace(long N, GEN CHI, GEN VCHIP)
    3295             : {
    3296             :   long FC;
    3297      127169 :   if (VCHIP)
    3298      127162 :     FC = mfcharmodulus(CHI);
    3299             :   else
    3300           7 :     VCHIP = mfcharinit(mfchartoprimitive(CHI, &FC));
    3301      127169 :   return mkvecn(5, mksqr(N), mkmup(N), mkgcd(N), VCHIP, mkbez(N, FC));
    3302             : }
    3303             : 
    3304             : /* p > 2 prime; return a sorted t_VECSMALL of primes s.t Tr^new(p) = 0 for all
    3305             :  * weights > 2 */
    3306             : static GEN
    3307       32683 : inittrconj(long N, long FC)
    3308             : {
    3309             :   GEN fa, P, E, v;
    3310             :   long i, k, l;
    3311             : 
    3312       32683 :   if (FC != 1) return cgetg(1,t_VECSMALL);
    3313             : 
    3314       27202 :   fa = myfactoru(N >> vals(N));
    3315       27202 :   P = gel(fa,1); l = lg(P);
    3316       27202 :   E = gel(fa,2);
    3317       27202 :   v = cgetg(l, t_VECSMALL);
    3318       59318 :   for (i = k = 1; i < l; i++)
    3319             :   {
    3320       32116 :     long j, p = P[i]; /* > 2 */
    3321       77588 :     for (j = 1; j < l; j++)
    3322       45472 :       if (j != i && E[j] == 1 && kross(-p, P[j]) == 1) v[k++] = p;
    3323             :   }
    3324       27202 :   setlg(v,k); return v;
    3325             : }
    3326             : 
    3327             : /* assume CHIP primitive, f(CHIP) | N; NZ = mfnewzerodata(N,CHIP) */
    3328             : static GEN
    3329       32683 : initnewtrace_i(long N, GEN CHIP, GEN NZ)
    3330             : {
    3331       32683 :   GEN T = const_vec(N, cgetg(1,t_VEC)), D, VCHIP;
    3332       32683 :   long FC = mfcharmodulus(CHIP), N1, N2, i, l;
    3333             : 
    3334       32683 :   if (!NZ) NZ = mkvecsmall(1); /*Tr^new = 0; initialize data nevertheless*/
    3335       32683 :   VCHIP = mfcharinit(CHIP);
    3336       32683 :   N1 = N/FC; newd_params(N1, &N2);
    3337       32683 :   D = mydivisorsu(N1/N2); l = lg(D);
    3338       32683 :   N2 *= FC;
    3339      159845 :   for (i = 1; i < l; i++)
    3340             :   {
    3341      127162 :     long M = D[i]*N2;
    3342      127162 :     gel(T,M) = inittrace(M, CHIP, VCHIP);
    3343             :   }
    3344       32683 :   gel(T,N) = shallowconcat(gel(T,N), mkvec2(NZ, inittrconj(N,FC)));
    3345       32683 :   return T;
    3346             : }
    3347             : /* don't initialize if Tr^new = 0, return NULL */
    3348             : static GEN
    3349       32788 : initnewtrace(long N, GEN CHI)
    3350             : {
    3351       32788 :   GEN CHIP = mfchartoprimitive(CHI, NULL), NZ = mfnewzerodata(N,CHIP);
    3352       32788 :   return NZ? initnewtrace_i(N, CHIP, NZ): NULL;
    3353             : }
    3354             : 
    3355             : /* (-1)^k */
    3356             : static long
    3357        8155 : m1pk(long k) { return odd(k)? -1 : 1; }
    3358             : static long
    3359        7798 : badchar(long N, long k, GEN CHI)
    3360        7798 : { return mfcharparity(CHI) != m1pk(k) || (CHI && N % mfcharconductor(CHI)); }
    3361             : 
    3362             : 
    3363             : static long
    3364       43330 : mfcuspdim_i(long N, long k, GEN CHI, GEN vSP)
    3365             : {
    3366       43330 :   pari_sp av = avma;
    3367             :   long FC;
    3368             :   GEN s;
    3369       43330 :   if (k <= 0) return 0;
    3370       43330 :   if (k == 1) return CHI? mf1cuspdim(N, CHI, vSP): 0;
    3371       43071 :   FC = CHI? mfcharconductor(CHI): 1;
    3372       43071 :   if (FC == 1) CHI = NULL;
    3373       43071 :   s = gsub(A1(N, k), gadd(A21(N, k, CHI), A22(N, k, CHI)));
    3374       43071 :   s = gadd(s, gsubsg(A4(k, FC), A3(N, FC)));
    3375       43071 :   return gc_long(av, itos(s));
    3376             : }
    3377             : /* dimension of space of cusp forms S_k(\G_0(N),CHI)
    3378             :  * Only depends on CHIP the primitive char attached to CHI */
    3379             : long
    3380        3381 : mfcuspdim(long N, long k, GEN CHI) { return mfcuspdim_i(N, k, CHI, NULL); }
    3381             : 
    3382             : /* dimension of whole space M_k(\G_0(N),CHI)
    3383             :  * Only depends on CHIP the primitive char attached to CHI; assumes !badchar */
    3384             : long
    3385         847 : mffulldim(long N, long k, GEN CHI)
    3386             : {
    3387         847 :   pari_sp av = avma;
    3388         847 :   long FC = CHI? mfcharconductor(CHI): 1;
    3389             :   GEN s;
    3390         847 :   if (k <= 0) return (k == 0 && FC == 1)? 1: 0;
    3391         847 :   if (k == 1) return gc_long(av, itos(A3(N, FC)) + mf1cuspdim(N, CHI, NULL));
    3392         630 :   if (FC == 1) CHI = NULL;
    3393         630 :   s = gsub(A1(N, k), gadd(A21(N, k, CHI), A22(N, k, CHI)));
    3394         630 :   s = gadd(s, A3(N, FC));
    3395         630 :   return gc_long(av, itos(s));
    3396             : }
    3397             : 
    3398             : /* Dimension of the space of Eisenstein series */
    3399             : long
    3400         231 : mfeisensteindim(long N, long k, GEN CHI)
    3401             : {
    3402         231 :   pari_sp av = avma;
    3403         231 :   long s, FC = CHI? mfcharconductor(CHI): 1;
    3404         231 :   if (k <= 0) return (k == 0 && FC == 1)? 1: 0;
    3405         231 :   s = itos(gmul2n(A3(N, FC), 1));
    3406         231 :   if (k > 1) s -= A4(k, FC); else s >>= 1;
    3407         231 :   return gc_long(av,s);
    3408             : }
    3409             : 
    3410             : enum { _SQRTS = 1, _MUP, _GCD, _VCHIP, _BEZ, _NEWLZ, _TRCONJ };
    3411             : /* Trace of T(n) on space of cuspforms; only depends on CHIP the primitive char
    3412             :  * attached to CHI */
    3413             : static GEN
    3414     6390844 : mfcusptrace_i(long N, long k, long n, GEN Dn, GEN S)
    3415             : {
    3416     6390844 :   pari_sp av = avma;
    3417             :   GEN a, b, VCHIP, GCD;
    3418             :   long t;
    3419     6390844 :   if (!n) return gen_0;
    3420     6390844 :   VCHIP = gel(S,_VCHIP);
    3421     6390844 :   GCD = gel(S,_GCD);
    3422     6390844 :   t = TA4(k, VCHIP, Dn, GCD);
    3423     6390844 :   a = TA1(N, k, VCHIP, GCD, n); if (t) a = gaddgs(a,t);
    3424     6390844 :   b = TA2(N, k, VCHIP, n, gel(S,_SQRTS), gel(S,_MUP), GCD);
    3425     6390844 :   b = gadd(b, TA3(N, k, VCHIP, GCD, Dn, gel(S,_BEZ)));
    3426     6390844 :   b = gsub(a,b);
    3427     6390844 :   if (typ(b) != t_POL) return gerepileupto(av, b);
    3428       38675 :   return gerepilecopy(av, vchip_polmod(VCHIP, b));
    3429             : }
    3430             : 
    3431             : static GEN
    3432     7650494 : mfcusptracecache(long N, long k, long n, GEN Dn, GEN S, cachenew_t *cache)
    3433             : {
    3434     7650494 :   GEN C = NULL, T = gel(cache->vfull,N);
    3435     7650494 :   long lcache = lg(T);
    3436     7650494 :   if (n < lcache) C = gel(T, n);
    3437     7650494 :   if (C) cache->cuspHIT++; else C = mfcusptrace_i(N, k, n, Dn, S);
    3438     7650494 :   cache->cuspTOTAL++;
    3439     7650494 :   if (n < lcache) gel(T,n) = C;
    3440     7650494 :   return C;
    3441             : }
    3442             : 
    3443             : /* return the divisors of n, known to be among the elements of D */
    3444             : static GEN
    3445      322077 : div_restrict(GEN D, ulong n)
    3446             : {
    3447             :   long i, j, l;
    3448      322077 :   GEN v, VDIV = caches[cache_DIV].cache;
    3449      322077 :   if (lg(VDIV) > n) return gel(VDIV,n);
    3450           0 :   l = lg(D);
    3451           0 :   v = cgetg(l, t_VECSMALL);
    3452           0 :   for (i = j = 1; i < l; i++)
    3453             :   {
    3454           0 :     ulong d = D[i];
    3455           0 :     if (n % d == 0) v[j++] = d;
    3456             :   }
    3457           0 :   setlg(v,j); return v;
    3458             : }
    3459             : 
    3460             : /* for some prime divisors of N, Tr^new(p) = 0 */
    3461             : static int
    3462      204061 : trconj(GEN T, long N, long n)
    3463      204061 : { return (lg(T) > 1 && N % n == 0 && zv_search(T, n)); }
    3464             : 
    3465             : /* n > 0; trace formula on new space */
    3466             : static GEN
    3467     2599588 : mfnewtrace_i(long N, long k, long n, cachenew_t *cache)
    3468             : {
    3469     2599588 :   GEN VCHIP, s, Dn, DN1, SN, S = cache->DATA;
    3470             :   long FC, N1, N2, N1N2, g, i, j, lDN1;
    3471             : 
    3472     2599588 :   if (!S) return gen_0;
    3473     2599588 :   SN = gel(S,N);
    3474     2599588 :   if (mfnewchkzero(gel(SN,_NEWLZ), n)) return gen_0;
    3475     1874793 :   if (k > 2 && trconj(gel(SN,_TRCONJ), N, n)) return gen_0;
    3476     1874765 :   VCHIP = gel(SN, _VCHIP); FC = vchip_FC(VCHIP);
    3477     1874765 :   N1 = N/FC; newt_params(N1, n, FC, &g, &N2);
    3478     1874765 :   N1N2 = N1/N2;
    3479     1874765 :   DN1 = mydivisorsu(N1N2); lDN1 = lg(DN1);
    3480     1874765 :   N2 *= FC;
    3481     1874765 :   Dn = mydivisorsu(n); /* this one is probably out of cache */
    3482     1874765 :   s = gmulsg(mubeta2(N1N2,n), mfcusptracecache(N2, k, n, Dn, gel(S,N2), cache));
    3483     7328417 :   for (i = 2; i < lDN1; i++)
    3484             :   { /* skip M1 = 1, done above */
    3485     5453652 :     long M1 = DN1[i], N1M1 = DN1[lDN1-i];
    3486     5453652 :     GEN Dg = mydivisorsu(ugcd(M1, g));
    3487     5453652 :     M1 *= N2;
    3488     5453652 :     s = gadd(s, gmulsg(mubeta2(N1M1,n),
    3489     5453652 :                        mfcusptracecache(M1, k, n, Dn, gel(S,M1), cache)));
    3490     5775729 :     for (j = 2; j < lg(Dg); j++) /* skip d = 1, done above */
    3491             :     {
    3492      322077 :       long d = Dg[j], ndd = n/(d*d), M = M1/d;
    3493      322077 :       GEN z = mulsi(mubeta2(N1M1,ndd), powuu(d,k-1)), C = vchip_lift(VCHIP,d,z);
    3494      322077 :       GEN Dndd = div_restrict(Dn, ndd);
    3495      322077 :       s = gadd(s, gmul(C, mfcusptracecache(M, k, ndd, Dndd, gel(S,M), cache)));
    3496             :     }
    3497     5453652 :     s = vchip_mod(VCHIP, s);
    3498             :   }
    3499     1874765 :   return vchip_polmod(VCHIP, s);
    3500             : }
    3501             : 
    3502             : static GEN
    3503       12355 : get_DIH(long N)
    3504             : {
    3505       12355 :   GEN x = cache_get(cache_DIH, N);
    3506       12355 :   return x? gcopy(x): mfdihedral(N);
    3507             : }
    3508             : static GEN
    3509        2373 : get_vDIH(long N, GEN D)
    3510             : {
    3511        2373 :   GEN x = const_vec(N, NULL);
    3512             :   long i, l;
    3513        2373 :   if (!D) D = mydivisorsu(N);
    3514        2373 :   l = lg(D);
    3515       14504 :   for (i = 1; i < l; i++) { long d = D[i]; gel(x, d) = get_DIH(d); }
    3516        2373 :   return x;
    3517             : }
    3518             : 
    3519             : /* divisors of N which are multiple of F */
    3520             : static GEN
    3521         322 : divisorsNF(long N, long F)
    3522             : {
    3523         322 :   GEN D = mydivisorsu(N / F);
    3524         322 :   long l = lg(D), i;
    3525         833 :   for (i = 1; i < l; i++) D[i] = N / D[i];
    3526         322 :   return D;
    3527             : }
    3528             : /* mfcuspdim(N,k,CHI) - mfnewdim(N,k,CHI); CHIP primitive (for efficiency) */
    3529             : static long
    3530        8358 : mfolddim_i(long N, long k, GEN CHIP, GEN vSP)
    3531             : {
    3532        8358 :   long S, i, l, F = mfcharmodulus(CHIP), N1 = N / F, N2;
    3533             :   GEN D;
    3534        8358 :   newd_params(N1, &N2); /* will ensure mubeta != 0 */
    3535        8358 :   D = mydivisorsu(N1/N2); l = lg(D); S = 0;
    3536        8358 :   if (k == 1 && !vSP) vSP = get_vDIH(N, divisorsNF(N, F));
    3537       32480 :   for (i = 2; i < l; i++)
    3538             :   {
    3539       24122 :     long d = mfcuspdim_i(N / D[i], k, CHIP, vSP);
    3540       24122 :     if (d) S -= mubeta(D[i]) * d;
    3541             :   }
    3542        8358 :   return S;
    3543             : }
    3544             : long
    3545         224 : mfolddim(long N, long k, GEN CHI)
    3546             : {
    3547         224 :   pari_sp av = avma;
    3548         224 :   GEN CHIP = mfchartoprimitive(CHI, NULL);
    3549         224 :   return gc_long(av, mfolddim_i(N, k, CHIP, NULL));
    3550             : }
    3551             : /* Only depends on CHIP the primitive char attached to CHI; assumes !badchar */
    3552             : long
    3553       15827 : mfnewdim(long N, long k, GEN CHI)
    3554             : {
    3555             :   pari_sp av;
    3556             :   long S, F;
    3557       15827 :   GEN vSP, CHIP = mfchartoprimitive(CHI, &F);
    3558       15827 :   vSP = (k == 1)? get_vDIH(N, divisorsNF(N, F)): NULL;
    3559       15827 :   S = mfcuspdim_i(N, k, CHIP, vSP); if (!S) return 0;
    3560        7861 :   av = avma; return gc_long(av, S - mfolddim_i(N, k, CHIP, vSP));
    3561             : }
    3562             : 
    3563             : /* trace form, given as closure */
    3564             : static GEN
    3565         952 : mftraceform_new(long N, long k, GEN CHI)
    3566             : {
    3567             :   GEN T;
    3568         952 :   if (k == 1) return initwt1newtrace(mfinit_Nkchi(N, 1, CHI, mf_CUSP, 0));
    3569         931 :   T = initnewtrace(N,CHI); if (!T) return mftrivial();
    3570         931 :   return tag(t_MF_NEWTRACE, mkNK(N,k,CHI), T);
    3571             : }
    3572             : static GEN
    3573          14 : mftraceform_cusp(long N, long k, GEN CHI)
    3574             : {
    3575          14 :   if (k == 1) return initwt1trace(mfinit_Nkchi(N, 1, CHI, mf_CUSP, 0));
    3576           7 :   return tag(t_MF_TRACE, mkNK(N,k,CHI), inittrace(N,CHI,NULL));
    3577             : }
    3578             : static GEN
    3579          98 : mftraceform_i(GEN NK, long space)
    3580             : {
    3581             :   GEN CHI;
    3582             :   long N, k;
    3583          98 :   checkNK(NK, &N, &k, &CHI, 0);
    3584          98 :   if (!mfdim_Nkchi(N, k, CHI, space)) return mftrivial();
    3585          77 :   switch(space)
    3586             :   {
    3587          56 :     case mf_NEW: return mftraceform_new(N, k, CHI);
    3588          14 :     case mf_CUSP:return mftraceform_cusp(N, k, CHI);
    3589             :   }
    3590           7 :   pari_err_DOMAIN("mftraceform", "space", "=", utoi(space), NK);
    3591             :   return NULL;/*LCOV_EXCL_LINE*/
    3592             : }
    3593             : GEN
    3594          98 : mftraceform(GEN NK, long space)
    3595          98 : { pari_sp av = avma; return gerepilecopy(av, mftraceform_i(NK,space)); }
    3596             : 
    3597             : static GEN
    3598       17542 : hecke_data(long N, long n)
    3599       17542 : { return mkvecsmall3(n, u_ppo(n, N), N); }
    3600             : /* 1/2-integral weight */
    3601             : static GEN
    3602          84 : heckef2_data(long N, long n)
    3603             : {
    3604             :   ulong f, fN, fN2;
    3605          84 :   if (!uissquareall(n, &f)) return NULL;
    3606          77 :   fN = u_ppo(f, N); fN2 = fN*fN;
    3607          77 :   return mkvec2(myfactoru(fN), mkvecsmall4(n, N, fN2, n/fN2));
    3608             : }
    3609             : /* N = mf_get_N(F) or a multiple */
    3610             : static GEN
    3611       24619 : mfhecke_i(long n, long N, GEN F)
    3612             : {
    3613       24619 :   if (n == 1) return F;
    3614       17171 :   return tag2(t_MF_HECKE, mf_get_NK(F), hecke_data(N,n), F);
    3615             : }
    3616             : 
    3617             : GEN
    3618         105 : mfhecke(GEN mf, GEN F, long n)
    3619             : {
    3620         105 :   pari_sp av = avma;
    3621             :   GEN NK, CHI, gk, DATA;
    3622             :   long N, nk, dk;
    3623         105 :   mf = checkMF(mf);
    3624         105 :   if (!checkmf_i(F)) pari_err_TYPE("mfhecke",F);
    3625         105 :   if (n <= 0) pari_err_TYPE("mfhecke [n <= 0]", stoi(n));
    3626         105 :   if (n == 1) return gcopy(F);
    3627         105 :   gk = mf_get_gk(F);
    3628         105 :   Qtoss(gk,&nk,&dk);
    3629         105 :   CHI = mf_get_CHI(F);
    3630         105 :   N = MF_get_N(mf);
    3631         105 :   if (dk == 2)
    3632             :   {
    3633          77 :     DATA = heckef2_data(N,n);
    3634          77 :     if (!DATA) return mftrivial();
    3635             :   }
    3636             :   else
    3637          28 :     DATA = hecke_data(N,n);
    3638          98 :   NK = mkgNK(lcmii(stoi(N), mf_get_gN(F)), gk, CHI, mf_get_field(F));
    3639          98 :   return gerepilecopy(av, tag2(t_MF_HECKE, NK, DATA, F));
    3640             : }
    3641             : 
    3642             : /* form F given by closure, compute B(d)(F) as closure (q -> q^d) */
    3643             : static GEN
    3644       35805 : mfbd_i(GEN F, long d)
    3645             : {
    3646             :   GEN D, NK, gk, CHI;
    3647       35805 :   if (d == 1) return F;
    3648       13482 :   if (d <= 0) pari_err_TYPE("mfbd [d <= 0]", stoi(d));
    3649       13482 :   if (mf_get_type(F) != t_MF_BD) D = utoi(d);
    3650           7 :   else { D = mului(d, gel(F,3)); F = gel(F,2); }
    3651       13482 :   gk = mf_get_gk(F); CHI = mf_get_CHI(F);
    3652       13482 :   if (typ(gk) != t_INT) CHI = mfcharmul(CHI, get_mfchar(utoi(d << 2)));
    3653       13482 :   NK = mkgNK(muliu(mf_get_gN(F), d), gk, CHI, mf_get_field(F));
    3654       13482 :   return tag2(t_MF_BD, NK, F, D);
    3655             : }
    3656             : GEN
    3657         252 : mfbd(GEN F, long d)
    3658             : {
    3659         252 :   pari_sp av = avma;
    3660         252 :   if (!checkmf_i(F)) pari_err_TYPE("mfbd",F);
    3661         252 :   return gerepilecopy(av, mfbd_i(F, d));
    3662             : }
    3663             : 
    3664             : /* A[i+1] = a(t*i^2) */
    3665             : static GEN
    3666         105 : RgV_shimura(GEN A, long n, long t, long N, long r, GEN CHI)
    3667             : {
    3668         105 :   GEN R, a0, Pn = mfcharpol(CHI);
    3669         105 :   long m, st, ord = mfcharorder(CHI), vt = varn(Pn), Nt = t == 1? N: ulcm(N,t);
    3670             : 
    3671         105 :   R = cgetg(n + 2, t_VEC);
    3672         105 :   st = odd(r)? -t: t;
    3673         105 :   a0 = gel(A, 1);
    3674         105 :   if (!gequal0(a0))
    3675             :   {
    3676          14 :     long o = mfcharorder(CHI);
    3677          14 :     if (st != 1 && odd(o)) o <<= 1;
    3678          14 :     a0 = gmul(a0, charLFwtk(Nt, r, CHI, o, st));
    3679             :   }
    3680         105 :   gel(R, 1) = a0;
    3681         637 :   for (m = 1; m <= n; m++)
    3682             :   {
    3683         532 :     GEN Dm = mydivisorsu(u_ppo(m, Nt)), S = gel(A, m*m + 1);
    3684         532 :     long i, l = lg(Dm);
    3685         805 :     for (i = 2; i < l; i++)
    3686             :     { /* (e,Nt) = 1; skip i = 1: e = 1, done above */
    3687         273 :       long e = Dm[i], me = m / e, a = mfcharevalord(CHI, e, ord);
    3688         273 :       GEN c, C = powuu(e, r - 1);
    3689         273 :       if (kross(st, e) == -1) C = negi(C);
    3690         273 :       c = Qab_Czeta(a, ord, C, vt);
    3691         273 :       S = gadd(S, gmul(c, gel(A, me*me + 1)));
    3692             :     }
    3693         532 :     gel(R, m+1) = S;
    3694             :   }
    3695         105 :   return degpol(Pn) > 1? gmodulo(R, Pn): R;
    3696             : }
    3697             : 
    3698             : static long
    3699          28 : mfisinkohnen(GEN mf, GEN F)
    3700             : {
    3701          28 :   GEN v, gk = MF_get_gk(mf), CHI = MF_get_CHI(mf);
    3702          28 :   long i, eps, N4 = MF_get_N(mf) >> 2, sb = mfsturmNgk(N4 << 4, gk) + 1;
    3703          28 :   eps = N4 % mfcharconductor(CHI)? -1 : 1;
    3704          28 :   if (odd(MF_get_r(mf))) eps = -eps;
    3705          28 :   v = mfcoefs(F, sb, 1);
    3706         686 :   for (i = 2;     i <= sb; i+=4) if (!gequal0(gel(v,i+1))) return 0;
    3707         245 :   for (i = 2+eps; i <= sb; i+=4) if (!gequal0(gel(v,i+1))) return 0;
    3708          14 :   return 1;
    3709             : }
    3710             : 
    3711             : static long
    3712          42 : mfshimura_space_cusp(GEN mf)
    3713             : {
    3714             :   long N4;
    3715          42 :   if (MF_get_r(mf) == 1 && (N4 = MF_get_N(mf) >> 2) >= 4)
    3716             :   {
    3717          21 :     GEN E = gel(myfactoru(N4), 2);
    3718          21 :     long ma = vecsmall_max(E);
    3719          21 :     if (ma > 2 || (ma == 2 && !mfcharistrivial(MF_get_CHI(mf)))) return 0;
    3720             :   }
    3721          28 :   return 1;
    3722             : }
    3723             : 
    3724             : /* D is either a discriminant (not necessarily fundamental) with
    3725             :    sign(D)=(-1)^{k-1/2}*eps, or a positive squarefree integer t, which is then
    3726             :    transformed into a fundamental discriminant of the correct sign. */
    3727             : GEN
    3728          49 : mfshimura(GEN mf, GEN F, long t)
    3729             : {
    3730          49 :   pari_sp av = avma;
    3731             :   GEN G, res, mf2, CHI;
    3732          49 :   long sb, M, r, N, space = mf_FULL;
    3733             : 
    3734          49 :   if (!checkmf_i(F)) pari_err_TYPE("mfshimura",F);
    3735          49 :   mf = checkMF(mf);
    3736          49 :   r = MF_get_r(mf);
    3737          49 :   if (r <= 0) pari_err_DOMAIN("mfshimura", "weight", "<=", ghalf, mf_get_gk(F));
    3738          49 :   if (t <= 0 || !uissquarefree(t)) pari_err_TYPE("mfshimura [t]", stoi(t));
    3739          42 :   N = MF_get_N(mf); M = N >> 1;
    3740          42 :   if (mfiscuspidal(mf,F))
    3741             :   {
    3742          28 :     if (mfshimura_space_cusp(mf)) space = mf_CUSP;
    3743          28 :     if (mfisinkohnen(mf,F)) M = N >> 2;
    3744             :   }
    3745          42 :   CHI = MF_get_CHI(mf);
    3746          42 :   mf2 = mfinit_Nkchi(M, r << 1, mfcharpow(CHI, gen_2), space, 0);
    3747          42 :   sb = mfsturm(mf2);
    3748          42 :   G = RgV_shimura(mfcoefs_i(F, sb*sb, t), sb, t, N, r, CHI);
    3749          42 :   res = mftobasis_i(mf2, G);
    3750             :   /* not mflinear(mf2,): we want lowest possible level */
    3751          42 :   G = mflinear(MF_get_basis(mf2), res);
    3752          42 :   return gerepilecopy(av, mkvec3(mf2, G, res));
    3753             : }
    3754             : 
    3755             : /* W ZabM (ZM if n = 1), a t_INT or NULL, b t_INT, ZXQ mod P or NULL.
    3756             :  * Write a/b = A/d with d t_INT and A Zab return [W,d,A,P] */
    3757             : static GEN
    3758        7700 : mkMinv(GEN W, GEN a, GEN b, GEN P)
    3759             : {
    3760        7700 :   GEN A = (b && typ(b) == t_POL)? Q_remove_denom(QXQ_inv(b,P), &b): NULL;
    3761        7700 :   if (a && b)
    3762             :   {
    3763        1295 :     a = Qdivii(a,b);
    3764        1295 :     if (typ(a) == t_INT) b = gen_1; else { b = gel(a,2); a = gel(a,1); }
    3765        1295 :     if (is_pm1(a)) a = NULL;
    3766             :   }
    3767        7700 :   if (a) A = A? ZX_Z_mul(A,a): a; else if (!A) A = gen_1;
    3768        7700 :   if (!b) b = gen_1;
    3769        7700 :   if (!P) P = gen_0;
    3770        7700 :   return mkvec4(W,b,A,P);
    3771             : }
    3772             : /* M square invertible QabM, return [M',d], M*M' = d*Id */
    3773             : static GEN
    3774         588 : QabM_Minv(GEN M, GEN P, long n)
    3775             : {
    3776             :   GEN dW, W, dM;
    3777         588 :   M = Q_remove_denom(M, &dM);
    3778         588 :   W = P? ZabM_inv(liftpol_shallow(M), P, n, &dW): ZM_inv(M, &dW);
    3779         588 :   return mkMinv(W, dM, dW, P);
    3780             : }
    3781             : /* Simplified form of mfclean, after a QabM_indexrank: M a ZabM with full
    3782             :  * column rank and z = indexrank(M) is known */
    3783             : static GEN
    3784         847 : mfclean2(GEN M, GEN z, GEN P, long n)
    3785             : {
    3786         847 :   GEN d, Minv, y = gel(z,1), W = rowpermute(M, y);
    3787         847 :   W = P? ZabM_inv(liftpol_shallow(W), P, n, &d): ZM_inv(W, &d);
    3788         847 :   M = rowslice(M, 1, y[lg(y)-1]);
    3789         847 :   Minv = mkMinv(W, NULL, d, P);
    3790         847 :   return mkvec3(y, Minv, M);
    3791             : }
    3792             : /* M QabM, lg(M)>1 and [y,z] its rank profile. Let Minv be the inverse of the
    3793             :  * invertible square matrix in mkMinv format. Return [y,Minv, M[..y[#y],]]
    3794             :  * P cyclotomic polynomial of order n > 2 or NULL */
    3795             : static GEN
    3796        4977 : mfclean(GEN M, GEN P, long n, int ratlift)
    3797             : {
    3798        4977 :   GEN W, v, y, z, d, Minv, dM, MdM = Q_remove_denom(M, &dM);
    3799        4977 :   if (n <= 2)
    3800        3885 :     W = ZM_pseudoinv(MdM, &v, &d);
    3801             :   else
    3802        1092 :     W = ZabM_pseudoinv_i(liftpol_shallow(MdM), P, n, &v, &d, ratlift);
    3803        4977 :   y = gel(v,1);
    3804        4977 :   z = gel(v,2);
    3805        4977 :   if (lg(z) != lg(MdM)) M = vecpermute(M,z);
    3806        4977 :   M = rowslice(M, 1, y[lg(y)-1]);
    3807        4977 :   Minv = mkMinv(W, dM, d, P);
    3808        4977 :   return mkvec3(y, Minv, M);
    3809             : }
    3810             : /* call mfclean using only CHI */
    3811             : static GEN
    3812        4025 : mfcleanCHI(GEN M, GEN CHI, int ratlift)
    3813             : {
    3814        4025 :   long n = mfcharorder(CHI);
    3815        4025 :   GEN P = (n <= 2)? NULL: mfcharpol(CHI);
    3816        4025 :   return mfclean(M, P, n, ratlift);
    3817             : }
    3818             : 
    3819             : /* DATA component of a t_MF_NEWTRACE. Was it stripped to save memory ? */
    3820             : static int
    3821       33712 : newtrace_stripped(GEN DATA)
    3822       33712 : { return DATA && (lg(DATA) == 5 && typ(gel(DATA,3)) == t_INT); }
    3823             : /* f a t_MF_NEWTRACE */
    3824             : static GEN
    3825       33712 : newtrace_DATA(long N, GEN f)
    3826             : {
    3827       33712 :   GEN DATA = gel(f,2);
    3828       33712 :   return newtrace_stripped(DATA)? initnewtrace(N, DATA): DATA;
    3829             : }
    3830             : /* reset cachenew for new level incorporating new DATA, tf a t_MF_NEWTRACE
    3831             :  * (+ possibly initialize 'full' for new allowed levels) */
    3832             : static void
    3833       33712 : reset_cachenew(cachenew_t *cache, long N, GEN tf)
    3834             : {
    3835             :   long i, n, l;
    3836       33712 :   GEN v, DATA = newtrace_DATA(N,tf);
    3837       33712 :   cache->DATA = DATA;
    3838       33712 :   if (!DATA) return;
    3839       33607 :   n = cache->n;
    3840       33607 :   v = cache->vfull; l = N+1; /* = lg(DATA) */
    3841     2198980 :   for (i = 1; i < l; i++)
    3842     2165373 :     if (typ(gel(v,i)) == t_INT && lg(gel(DATA,i)) != 1)
    3843       53452 :       gel(v,i) = const_vec(n, NULL);
    3844       33607 :   cache->VCHIP = gel(gel(DATA,N),_VCHIP);
    3845             : }
    3846             : /* initialize a cache of newtrace / cusptrace up to index n and level | N;
    3847             :  * DATA may be NULL (<=> Tr^new = 0). tf a t_MF_NEWTRACE */
    3848             : static void
    3849       13202 : init_cachenew(cachenew_t *cache, long n, long N, GEN tf)
    3850             : {
    3851       13202 :   long i, l = N+1; /* = lg(tf.DATA) when DATA != NULL */
    3852             :   GEN v;
    3853       13202 :   cache->n = n;
    3854       13202 :   cache->vnew = v = cgetg(l, t_VEC);
    3855      942872 :   for (i = 1; i < l; i++) gel(v,i) = (N % i)? gen_0: const_vec(n, NULL);
    3856       13202 :   cache->newHIT = cache->newTOTAL = cache->cuspHIT = cache->cuspTOTAL = 0;
    3857       13202 :   cache->vfull = v = zerovec(N);
    3858       13202 :   reset_cachenew(cache, N, tf);
    3859       13202 : }
    3860             : static void
    3861       17213 : dbg_cachenew(cachenew_t *C)
    3862             : {
    3863       17213 :   if (DEBUGLEVEL >= 2 && C)
    3864           0 :     err_printf("newtrace cache hits: new = %ld/%ld, cusp = %ld/%ld\n",
    3865             :                     C->newHIT, C->newTOTAL, C->cuspHIT, C->cuspTOTAL);
    3866       17213 : }
    3867             : 
    3868             : /* newtrace_{N,k}(d*i), i = n0, ..., n */
    3869             : static GEN
    3870      179235 : colnewtrace(long n0, long n, long d, long N, long k, cachenew_t *cache)
    3871             : {
    3872      179235 :   GEN v = cgetg(n-n0+2, t_COL);
    3873             :   long i;
    3874     4680354 :   for (i = n0; i <= n; i++) gel(v, i-n0+1) = mfnewtracecache(N, k, i*d, cache);
    3875      179235 :   return v;
    3876             : }
    3877             : /* T_n(l*m0, l*(m0+1), ..., l*m) F, F = t_MF_NEWTRACE [N,k],DATA, cache
    3878             :  * contains DATA != NULL as well as cached values of F */
    3879             : static GEN
    3880       88697 : heckenewtrace(long m0, long m, long l, long N, long NBIG, long k, long n, cachenew_t *cache)
    3881             : {
    3882       88697 :   long lD, a, k1, nl = n*l;
    3883       88697 :   GEN D, V, v = colnewtrace(m0, m, nl, N, k, cache); /* d=1 */
    3884             :   GEN VCHIP;
    3885       88697 :   if (n == 1) return v;
    3886       60886 :   VCHIP = cache->VCHIP;
    3887       60886 :   D = mydivisorsu(u_ppo(n, NBIG)); lD = lg(D);
    3888       60886 :   k1 = k - 1;
    3889      149527 :   for (a = 2; a < lD; a++)
    3890             :   { /* d > 1, (d,NBIG) = 1 */
    3891       88641 :     long i, j, d = D[a], c = ugcd(l, d), dl = d/c, m0d = ceildivuu(m0, dl);
    3892       88641 :     GEN C = vchip_lift(VCHIP, d, powuu(d, k1));
    3893             :     /* m0=0: i = 1 => skip F(0) = 0 */
    3894       88641 :     if (!m0) { i = 1; j = dl; } else { i = 0; j = m0d*dl; }
    3895       88641 :     V = colnewtrace(m0d, m/dl, nl/(d*c), N, k, cache);
    3896             :     /* C = chi(d) d^(k-1) */
    3897     1075970 :     for (; j <= m; i++, j += dl)
    3898      987329 :       gel(v,j-m0+1) = gadd(gel(v,j-m0+1), vchip_mod(VCHIP, gmul(C,gel(V,i+1))));
    3899             :   }
    3900       60886 :   return v;
    3901             : }
    3902             : 
    3903             : /* Given v = an[i], return an[d*i], i=0..n */
    3904             : static GEN
    3905        2618 : anextract(GEN v, long n, long d)
    3906             : {
    3907        2618 :   long i, id, l = n + 2;
    3908        2618 :   GEN w = cgetg(l, t_VEC);
    3909        2618 :   if (d == 1)
    3910        7245 :     for (i = 1; i < l; i++) gel(w, i) = gel(v, i);
    3911             :   else
    3912       22036 :     for (i = id = 1; i < l; i++, id += d) gel(w, i) = gel(v, id);
    3913        2618 :   return w;
    3914             : }
    3915             : /* T_n(F)(0, l, ..., l*m) */
    3916             : static GEN
    3917        2527 : hecke_i(long m, long l, GEN V, GEN F, GEN DATA)
    3918             : {
    3919             :   long k, n, nNBIG, NBIG, lD, M, a, t, nl;
    3920             :   GEN D, v, CHI;
    3921        2527 :   if (typ(DATA) == t_VEC)
    3922             :   { /* 1/2-integral k */
    3923          98 :     if (!V) { GEN S = gel(DATA,2); V = mfcoefs_i(F, m*l*S[3], S[4]); }
    3924          98 :     return RgV_heckef2(m, l, V, F, DATA);
    3925             :   }
    3926        2429 :   k = mf_get_k(F);
    3927        2429 :   n = DATA[1]; nl = n*l;
    3928        2429 :   nNBIG = DATA[2];
    3929        2429 :   NBIG = DATA[3];
    3930        2429 :   if (nNBIG == 1) return V? V: mfcoefs_i(F,m,nl);
    3931        1673 :   if (!V && mf_get_type(F) == t_MF_NEWTRACE)
    3932             :   { /* inline F to allow cache, T_n at level NBIG acting on Tr^new(N,k,CHI) */
    3933             :     cachenew_t cache;
    3934         364 :     long N = mf_get_N(F);
    3935         364 :     init_cachenew(&cache, m*nl, N, F);
    3936         364 :     v = heckenewtrace(0, m, l, N, NBIG, k, n, &cache);
    3937         364 :     dbg_cachenew(&cache);
    3938         364 :     settyp(v, t_VEC); return v;
    3939             :   }
    3940        1309 :   CHI = mf_get_CHI(F);
    3941        1309 :   D = mydivisorsu(nNBIG); lD = lg(D);
    3942        1309 :   M = m + 1;
    3943        1309 :   t = nNBIG * ugcd(nNBIG, l);
    3944        1309 :   if (!V) V = mfcoefs_i(F, m * t, nl / t); /* usually nl = t */
    3945        1309 :   v = anextract(V, m, t); /* mfcoefs(F, m, nl); d = 1 */
    3946        2618 :   for (a = 2; a < lD; a++)
    3947             :   { /* d > 1, (d, NBIG) = 1 */
    3948        1309 :     long d = D[a], c = ugcd(l, d), dl = d/c, i, idl;
    3949        1309 :     GEN C = gmul(mfchareval(CHI, d), powuu(d, k-1));
    3950        1309 :     GEN w = anextract(V, m/dl, t/(d*c)); /* mfcoefs(F, m/dl, nl/(d*c)) */
    3951        7245 :     for (i = idl = 1; idl <= M; i++, idl += dl)
    3952        5936 :       gel(v,idl) = gadd(gel(v,idl), gmul(C, gel(w,i)));
    3953             :   }
    3954        1309 :   return v;
    3955             : }
    3956             : 
    3957             : static GEN
    3958       12306 : mkmf(GEN x1, GEN x2, GEN x3, GEN x4, GEN x5)
    3959             : {
    3960       12306 :   GEN MF = obj_init(5, MF_SPLITN);
    3961       12306 :   gel(MF,1) = x1;
    3962       12306 :   gel(MF,2) = x2;
    3963       12306 :   gel(MF,3) = x3;
    3964       12306 :   gel(MF,4) = x4;
    3965       12306 :   gel(MF,5) = x5; return MF;
    3966             : }
    3967             : 
    3968             : /* return an integer b such that p | b => T_p^k Tr^new = 0, for all k > 0 */
    3969             : static long
    3970        7602 : get_badj(long N, long FC)
    3971             : {
    3972        7602 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
    3973        7602 :   long i, b = 1, l = lg(P);
    3974       20237 :   for (i = 1; i < l; i++)
    3975       12635 :     if (E[i] > 1 && u_lval(FC, P[i]) < E[i]) b *= P[i];
    3976        7602 :   return b;
    3977             : }
    3978             : /* in place, assume perm strictly increasing */
    3979             : static void
    3980        1330 : vecpermute_inplace(GEN v, GEN perm)
    3981             : {
    3982        1330 :   long i, l = lg(perm);
    3983       11522 :   for (i = 1; i < l; i++) gel(v,i) = gel(v,perm[i]);
    3984        1330 : }
    3985             : 
    3986             : /* Find basis of newspace using closures; assume k >= 2 and !badchar.
    3987             :  * Return NULL if space is empty, else
    3988             :  * [mf1, list of closures T(j)traceform, list of corresponding j, matrix] */
    3989             : static GEN
    3990       15582 : mfnewinit(long N, long k, GEN CHI, cachenew_t *cache, long init)
    3991             : {
    3992             :   GEN S, vj, M, CHIP, mf1, listj, P, tf;
    3993             :   long j, ct, ctlj, dim, jin, SB, sb, two, ord, FC, badj;
    3994             : 
    3995       15582 :   dim = mfnewdim(N, k, CHI);
    3996       15582 :   if (!dim && !init) return NULL;
    3997        7602 :   sb = mfsturmNk(N, k);
    3998        7602 :   CHIP = mfchartoprimitive(CHI, &FC);
    3999             :   /* remove newtrace data from S to save space in output: negligible slowdown */
    4000        7602 :   tf = tag(t_MF_NEWTRACE, mkNK(N,k,CHIP), CHIP);
    4001        7602 :   badj = get_badj(N, FC);
    4002             :   /* try sbsmall first: Sturm bound not sharp for new space */
    4003        7602 :   SB = ceilA1(N, k);
    4004        7602 :   listj = cgetg(2*sb + 3, t_VECSMALL);
    4005      372533 :   for (j = ctlj = 1; ctlj < 2*sb + 3; j++)
    4006      364931 :     if (ugcd(j, badj) == 1) listj[ctlj++] = j;
    4007        7602 :   if (init)
    4008             :   {
    4009        4123 :     init_cachenew(cache, (SB+1)*listj[dim+1], N, tf);
    4010        4123 :     if (init == -1 || !dim) return NULL; /* old space or dim = 0 */
    4011             :   }
    4012             :   else
    4013        3479 :     reset_cachenew(cache, N, tf);
    4014             :   /* cache.DATA is not NULL */
    4015        7133 :   ord = mfcharorder(CHIP);
    4016        7133 :   P = ord <= 2? NULL: mfcharpol(CHIP);
    4017        7133 :   vj = cgetg(dim+1, t_VECSMALL);
    4018        7133 :   M = cgetg(dim+1, t_MAT);
    4019        7140 :   for (two = 1, ct = 0, jin = 1; two <= 2; two++)
    4020             :   {
    4021        7140 :     long a, jlim = jin + sb;
    4022       21952 :     for (a = jin; a <= jlim; a++)
    4023             :     {
    4024             :       GEN z, vecz;
    4025       21945 :       ct++; vj[ct] = listj[a];
    4026       21945 :       gel(M, ct) = heckenewtrace(0, SB, 1, N, N, k, vj[ct], cache);
    4027       21945 :       if (ct < dim) continue;
    4028             : 
    4029        7798 :       z = QabM_indexrank(M, P, ord);
    4030        7798 :       vecz = gel(z, 2); ct = lg(vecz) - 1;
    4031        7798 :       if (ct == dim) { M = mkvec3(z, gen_0, M); break; } /*maximal rank, done*/
    4032         665 :       vecpermute_inplace(M, vecz);
    4033         665 :       vecpermute_inplace(vj, vecz);
    4034             :     }
    4035        7140 :     if (a <= jlim) break;
    4036             :     /* sbsmall was not sufficient, use Sturm bound: must extend M */
    4037          70 :     for (j = 1; j <= ct; j++)
    4038             :     {
    4039          63 :       GEN t = heckenewtrace(SB + 1, sb, 1, N, N, k, vj[j], cache);
    4040          63 :       gel(M,j) = shallowconcat(gel(M, j), t);
    4041             :     }
    4042           7 :     jin = jlim + 1; SB = sb;
    4043             :   }
    4044        7133 :   S = cgetg(dim + 1, t_VEC);
    4045       28371 :   for (j = 1; j <= dim; j++) gel(S, j) = mfhecke_i(vj[j], N, tf);
    4046        7133 :   dbg_cachenew(cache);
    4047        7133 :   mf1 = mkvec4(utoipos(N), utoipos(k), CHI, utoi(mf_NEW));
    4048        7133 :   return mkmf(mf1, cgetg(1,t_VEC), S, vj, M);
    4049             : }
    4050             : /* k > 1 integral, mf space is mf_CUSP or mf_FULL */
    4051             : static GEN
    4052          42 : mfinittonew(GEN mf)
    4053             : {
    4054          42 :   GEN CHI = MF_get_CHI(mf), S = MF_get_S(mf), vMjd = MFcusp_get_vMjd(mf);
    4055          42 :   GEN M = MF_get_M(mf), vj, mf1;
    4056          42 :   long i, j, l, l0 = lg(S), N0 = MF_get_N(mf);
    4057         203 :   for (i = l0-1; i > 0; i--)
    4058             :   {
    4059         189 :     long N = gel(vMjd,i)[1];
    4060         189 :     if (N != N0) break;
    4061             :   }
    4062          42 :   if (i == l0-1) return NULL;
    4063          35 :   S = vecslice(S, i+1, l0-1); /* forms of conductor N0 */
    4064          35 :   l = lg(S); vj = cgetg(l, t_VECSMALL);
    4065         196 :   for (j = 1; j < l; j++) vj[j] = gel(vMjd,j+i)[2];
    4066          35 :   M = vecslice(M, lg(M)-lg(S)+1, lg(M)-1); /* their coefficients */
    4067          35 :   M = mfcleanCHI(M, CHI, 0);
    4068          35 :   mf1 = mkvec4(utoipos(N0), MF_get_gk(mf), CHI, utoi(mf_NEW));
    4069          35 :   return mkmf(mf1, cgetg(1,t_VEC), S, vj, M);
    4070             : }
    4071             : 
    4072             : /* Bd(f)[m0..m], v = f[ceil(m0/d)..floor(m/d)], m0d = ceil(m0/d) */
    4073             : static GEN
    4074       82481 : RgC_Bd_expand(long m0, long m, GEN v, long d, long m0d)
    4075             : {
    4076             :   long i, j;
    4077             :   GEN w;
    4078       82481 :   if (d == 1) return v;
    4079       23660 :   w = zerocol(m-m0+1);
    4080       23660 :   if (!m0) { i = 1; j = d; } else { i = 0; j = m0d*d; }
    4081      469399 :   for (; j <= m; i++, j += d) gel(w,j-m0+1) = gel(v,i+1);
    4082       23660 :   return w;
    4083             : }
    4084             : /* S a nonempty vector of t_MF_BD(t_MF_HECKE(t_MF_NEWTRACE)); M the matrix
    4085             :  * of their coefficients r*0, r*1, ..., r*m0 (~ mfvectomat) or NULL (empty),
    4086             :  * extend it to coeffs up to m > m0. The forms B_d(T_j(tf_N))in S should be
    4087             :  * sorted by level N, then j, then increasing d. No reordering here. */
    4088             : static GEN
    4089        9065 : bhnmat_extend(GEN M, long m, long r, GEN S, cachenew_t *cache)
    4090             : {
    4091        9065 :   long i, mr, m0, m0r, Nold = 0, jold = 0, l = lg(S);
    4092        9065 :   GEN MAT = cgetg(l, t_MAT), v = NULL;
    4093        9065 :   if (M) { m0 = nbrows(M); m0r = m0 * r; } else m0 = m0r = 0;
    4094        9065 :   mr = m*r;
    4095       91546 :   for (i = 1; i < l; i++)
    4096             :   {
    4097             :     long d, j, md, N;
    4098       82481 :     GEN c, f = bhn_parse(gel(S,i), &d,&j); /* t_MF_NEWTRACE */
    4099       82481 :     N = mf_get_N(f);
    4100       82481 :     md = ceildivuu(m0r,d);
    4101       82481 :     if (N != Nold) { reset_cachenew(cache, N, f); Nold = N; jold = 0; }
    4102       82481 :     if (!cache->DATA) { gel(MAT,i) = zerocol(m+1); continue; }
    4103       82481 :     if (j != jold || md)
    4104       66325 :     { v = heckenewtrace(md, mr/d, 1, N, N, mf_get_k(f), j,cache); jold=j; }
    4105       82481 :     c = RgC_Bd_expand(m0r, mr, v, d, md);
    4106       82481 :     if (r > 1) c = c_deflate(m-m0, r, c);
    4107       82481 :     if (M) c = shallowconcat(gel(M,i), c);
    4108       82481 :     gel(MAT,i) = c;
    4109             :   }
    4110        9065 :   return MAT;
    4111             : }
    4112             : 
    4113             : /* k > 1 */
    4114             : static GEN
    4115        3199 : mfinitcusp(long N, long k, GEN CHI, cachenew_t *cache, long space)
    4116             : {
    4117             :   long L, l, lDN1, FC, N1, d1, i, init;
    4118        3199 :   GEN vS, vMjd, DN1, vmf, CHIP = mfchartoprimitive(CHI, &FC);
    4119             : 
    4120        3199 :   d1 = (space == mf_OLD)? mfolddim_i(N, k, CHIP, NULL): mfcuspdim(N, k, CHIP);
    4121        3199 :   if (!d1) return NULL;
    4122        2898 :   N1 = N/FC; DN1 = mydivisorsu(N1); lDN1 = lg(DN1);
    4123        2898 :   init = (space == mf_OLD)? -1: 1;
    4124        2898 :   vmf = cgetg(lDN1, t_VEC);
    4125       17255 :   for (i = lDN1 - 1, l = 1; i; i--)
    4126             :   { /* by decreasing level to allow cache */
    4127       14357 :     GEN mf = mfnewinit(FC*DN1[i], k, CHIP, cache, init);
    4128       14357 :     if (mf) gel(vmf, l++) = mf;
    4129       14357 :     init = 0;
    4130             :   }
    4131        2898 :   setlg(vmf,l); vmf = vecreverse(vmf); /* reorder by increasing level */
    4132             : 
    4133        2898 :   L = mfsturmNk(N, k)+1;
    4134        2898 :   vS = vectrunc_init(L);
    4135        2898 :   vMjd = vectrunc_init(L);
    4136        9177 :   for (i = 1; i < l; i++)
    4137             :   {
    4138        6279 :     GEN DNM, mf = gel(vmf,i), S = MF_get_S(mf), vj = MFnew_get_vj(mf);
    4139        6279 :     long a, lDNM, lS = lg(S), M = MF_get_N(mf);
    4140        6279 :     DNM = mydivisorsu(N / M); lDNM = lg(DNM);
    4141       25424 :     for (a = 1; a < lS; a++)
    4142             :     {
    4143       19145 :       GEN tf = gel(S,a);
    4144       19145 :       long b, j = vj[a];
    4145       47635 :       for (b = 1; b < lDNM; b++)
    4146             :       {
    4147       28490 :         long d = DNM[b];
    4148       28490 :         vectrunc_append(vS, mfbd_i(tf, d));
    4149       28490 :         vectrunc_append(vMjd, mkvecsmall3(M, j, d));
    4150             :       }
    4151             :     }
    4152             :   }
    4153        2898 :   return mkmf(NULL, cgetg(1, t_VEC), vS, vMjd, NULL);
    4154             : }
    4155             : 
    4156             : long
    4157        4536 : mfsturm_mf(GEN mf)
    4158             : {
    4159        4536 :   GEN Mindex = MF_get_Mindex(mf);
    4160        4536 :   long n = lg(Mindex)-1;
    4161        4536 :   return n? Mindex[n]-1: 0;
    4162             : }
    4163             : 
    4164             : long
    4165         805 : mfsturm(GEN T)
    4166             : {
    4167             :   long N, nk, dk;
    4168         805 :   GEN CHI, mf = checkMF_i(T);
    4169         805 :   if (mf) return mfsturm_mf(mf);
    4170           7 :   checkNK2(T, &N, &nk, &dk, &CHI, 0);
    4171           7 :   return dk == 1 ? mfsturmNk(N, nk) : mfsturmNk(N, (nk + 1) >> 1);
    4172             : }
    4173             : long
    4174         196 : mfisequal(GEN F, GEN G, long lim)
    4175             : {
    4176         196 :   pari_sp av = avma;
    4177             :   long b;
    4178         196 :   if (!checkmf_i(F)) pari_err_TYPE("mfisequal",F);
    4179         196 :   if (!checkmf_i(G)) pari_err_TYPE("mfisequal",G);
    4180         196 :   b = lim? lim: maxss(mfsturmmf(F), mfsturmmf(G));
    4181         196 :   return gc_long(av, gequal(mfcoefs_i(F, b, 1), mfcoefs_i(G, b, 1)));
    4182             : }
    4183             : 
    4184             : GEN
    4185          35 : mffields(GEN mf)
    4186             : {
    4187          35 :   if (checkmf_i(mf)) return gcopy(mf_get_field(mf));
    4188          35 :   mf = checkMF(mf); return gcopy(MF_get_fields(mf));
    4189             : }
    4190             : 
    4191             : GEN
    4192         343 : mfeigenbasis(GEN mf)
    4193             : {
    4194         343 :   pari_sp ltop = avma;
    4195             :   GEN F, S, v, vP;
    4196             :   long i, l, k, dS;
    4197             : 
    4198         343 :   mf = checkMF(mf);
    4199         343 :   k = MF_get_k(mf);
    4200         343 :   S = MF_get_S(mf); dS = lg(S)-1;
    4201         343 :   if (!dS) return cgetg(1, t_VEC);
    4202         336 :   F = MF_get_newforms(mf);
    4203         336 :   vP = MF_get_fields(mf);
    4204         336 :   if (k == 1)
    4205             :   {
    4206         210 :     if (MF_get_space(mf) == mf_FULL)
    4207             :     {
    4208          14 :       long dE = lg(MF_get_E(mf)) - 1;
    4209          14 :       if (dE) F = rowslice(F, dE+1, dE+dS);
    4210             :     }
    4211         210 :     v = vecmflineardiv_linear(S, F);
    4212         210 :     l = lg(v);
    4213             :   }
    4214             :   else
    4215             :   {
    4216         126 :     GEN (*L)(GEN, GEN) = (MF_get_space(mf) == mf_FULL)? mflinear: mflinear_bhn;
    4217         126 :     l = lg(F); v = cgetg(l, t_VEC);
    4218         427 :     for (i = 1; i < l; i++) gel(v,i) = L(mf, gel(F,i));
    4219             :   }
    4220         861 :   for (i = 1; i < l; i++) mf_setfield(gel(v,i), gel(vP,i));
    4221         336 :   return gerepilecopy(ltop, v);
    4222             : }
    4223             : 
    4224             : /* Minv = [M, d, A], v a t_COL; A a Zab, d a t_INT; return (A/d) * M*v */
    4225             : static GEN
    4226        7294 : Minv_RgC_mul(GEN Minv, GEN v)
    4227             : {
    4228        7294 :   GEN M = gel(Minv,1), d = gel(Minv,2), A = gel(Minv,3);
    4229        7294 :   v = RgM_RgC_mul(M, v);
    4230        7294 :   if (!equali1(A))
    4231             :   {
    4232        1771 :     if (typ(A) == t_POL && degpol(A) > 0) A = mkpolmod(A, gel(Minv,4));
    4233        1771 :     v = RgC_Rg_mul(v, A);
    4234             :   }
    4235        7294 :   if (!equali1(d)) v = RgC_Rg_div(v, d);
    4236        7294 :   return v;
    4237             : }
    4238             : static GEN
    4239        1281 : Minv_RgM_mul(GEN Minv, GEN B)
    4240             : {
    4241        1281 :   long j, l = lg(B);
    4242        1281 :   GEN M = cgetg(l, t_MAT);
    4243        5915 :   for (j = 1; j < l; j++) gel(M,j) = Minv_RgC_mul(Minv, gel(B,j));
    4244        1281 :   return M;
    4245             : }
    4246             : /* B * Minv; allow B = NULL for Id */
    4247             : static GEN
    4248        2436 : RgM_Minv_mul(GEN B, GEN Minv)
    4249             : {
    4250        2436 :   GEN M = gel(Minv,1), d = gel(Minv,2), A = gel(Minv,3);
    4251        2436 :   if (B) M = RgM_mul(B, M);
    4252        2436 :   if (!equali1(A))
    4253             :   {
    4254         980 :     if (typ(A) == t_POL) A = mkpolmod(A, gel(Minv,4));
    4255         980 :     M = RgM_Rg_mul(M, A);
    4256             :   }
    4257        2436 :   if (!equali1(d)) M = RgM_Rg_div(M,d);
    4258        2436 :   return M;
    4259             : }
    4260             : 
    4261             : /* perm vector of strictly increasing indices, v a vector or arbitrary length;
    4262             :  * the last r entries of perm fall beyond v.
    4263             :  * Return v o perm[1..(-r)], discarding the last r entries of v */
    4264             : static GEN
    4265        1358 : vecpermute_partial(GEN v, GEN perm, long *r)
    4266             : {
    4267        1358 :   long i, n = lg(v)-1, l = lg(perm);
    4268             :   GEN w;
    4269        1358 :   if (perm[l-1] <= n) { *r = 0; return vecpermute(v,perm); }
    4270          63 :   for (i = 1; i < l; i++)
    4271          63 :     if (perm[i] > n) break;
    4272          21 :   *r = l - i; l = i;
    4273          21 :   w = cgetg(l, typ(v));
    4274          63 :   for (i = 1; i < l; i++) gel(w,i) = gel(v,perm[i]);
    4275          21 :   return w;
    4276             : }
    4277             : 
    4278             : /* given form F, find coeffs of F on mfbasis(mf). If power series, not
    4279             :  * guaranteed correct if precision less than Sturm bound */
    4280             : static GEN
    4281        1386 : mftobasis_i(GEN mf, GEN F)
    4282             : {
    4283             :   GEN v, Mindex, Minv;
    4284        1386 :   if (!MF_get_dim(mf)) return cgetg(1, t_COL);
    4285        1386 :   Mindex = MF_get_Mindex(mf);
    4286        1386 :   Minv = MF_get_Minv(mf);
    4287        1386 :   if (checkmf_i(F))
    4288             :   {
    4289         266 :     long n = Mindex[lg(Mindex)-1];
    4290         266 :     v = vecpermute(mfcoefs_i(F, n, 1), Mindex);
    4291         266 :     return Minv_RgC_mul(Minv, v);
    4292             :   }
    4293             :   else
    4294             :   {
    4295        1120 :     GEN A = gel(Minv,1), d = gel(Minv,2);
    4296             :     long r;
    4297        1120 :     v = F;
    4298        1120 :     switch(typ(F))
    4299             :     {
    4300           0 :       case t_SER: v = sertocol(v);
    4301        1120 :       case t_VEC: case t_COL: break;
    4302           0 :       default: pari_err_TYPE("mftobasis", F);
    4303             :     }
    4304        1120 :     if (lg(v) == 1) pari_err_TYPE("mftobasis",v);
    4305        1120 :     v = vecpermute_partial(v, Mindex, &r);
    4306        1120 :     if (!r) return Minv_RgC_mul(Minv, v); /* single solution */
    4307             :     /* affine space of dimension r */
    4308          21 :     v = RgM_RgC_mul(vecslice(A, 1, lg(v)-1), v);
    4309          21 :     if (!equali1(d)) v = RgC_Rg_div(v,d);
    4310          21 :     return mkvec2(v, vecslice(A, lg(A)-r, lg(A)-1));
    4311             :   }
    4312             : }
    4313             : 
    4314             : static GEN
    4315         896 : const_mat(long n, GEN x)
    4316             : {
    4317         896 :   long j, l = n+1;
    4318         896 :   GEN A = cgetg(l,t_MAT);
    4319        6832 :   for (j = 1; j < l; j++) gel(A,j) = const_col(n, x);
    4320         896 :   return A;
    4321             : }
    4322             : 
    4323             : /* L is the mftobasis of a form on CUSP space. We allow mf_FULL or mf_CUSP */
    4324             : static GEN
    4325         448 : mftonew_i(GEN mf, GEN L, long *plevel)
    4326             : {
    4327             :   GEN S, listMjd, CHI, res, Aclos, Acoef, D, perm;
    4328         448 :   long N1, LC, lD, i, l, t, level, N = MF_get_N(mf);
    4329             : 
    4330         448 :   if (MF_get_k(mf) == 1) pari_err_IMPL("mftonew in weight 1");
    4331         448 :   listMjd = MFcusp_get_vMjd(mf);
    4332         448 :   CHI = MF_get_CHI(mf); LC = mfcharconductor(CHI);
    4333         448 :   S = MF_get_S(mf);
    4334             : 
    4335         448 :   N1 = N/LC;
    4336         448 :   D = mydivisorsu(N1); lD = lg(D);
    4337         448 :   perm = cgetg(N1+1, t_VECSMALL);
    4338        3416 :   for (i = 1; i < lD; i++) perm[D[i]] = i;
    4339         448 :   Aclos = const_mat(lD-1, cgetg(1,t_VEC));
    4340         448 :   Acoef = const_mat(lD-1, cgetg(1,t_VEC));
    4341         448 :   l = lg(listMjd);
    4342        4494 :   for (i = 1; i < l; i++)
    4343             :   {
    4344             :     long M, d;
    4345             :     GEN v;
    4346        4046 :     if (gequal0(gel(L,i))) continue;
    4347         441 :     v = gel(listMjd, i);
    4348         441 :     M = perm[ v[1]/LC ];
    4349         441 :     d = perm[ v[3] ];
    4350         441 :     gcoeff(Aclos,M,d) = vec_append(gcoeff(Aclos,M,d), gel(S,i));
    4351         441 :     gcoeff(Acoef,M,d) = shallowconcat(gcoeff(Acoef,M,d), gel(L,i));
    4352             :   }
    4353         448 :   res = cgetg(l, t_VEC); level = 1;
    4354        3416 :   for (i = t = 1; i < lD; i++)
    4355             :   {
    4356        2968 :     long j, M = D[i]*LC;
    4357        2968 :     GEN gM = utoipos(M);
    4358       26390 :     for (j = 1; j < lD; j++)
    4359             :     {
    4360       23422 :       GEN f = gcoeff(Aclos,i,j), C, NK;
    4361             :       long d;
    4362       23422 :       if (lg(f) == 1) continue;
    4363         413 :       d = D[j];
    4364         413 :       C = gcoeff(Acoef,i,j);
    4365         413 :       NK = mf_get_NK(gel(f, 1));
    4366         413 :       if (d > 1)
    4367             :       {
    4368         168 :           if (lg(f) > 2) pari_err_BUG("should be only one form");
    4369         168 :           f = gel(f, 1);
    4370         168 :           if (mf_get_type(f) == t_MF_BD)
    4371             :           {
    4372         168 :             if (!equaliu(gel(f,3), d))
    4373           0 :               pari_err_BUG("inconsistent multiplier");
    4374         168 :             f = gel(f, 2);
    4375             :           }
    4376         168 :           f = mkvec(f);
    4377             :       }
    4378         413 :       level = ulcm(level, M*d);
    4379         413 :       gel(res,t++) = mkvec3(gM, utoipos(d), mflinear_i(NK,f,C));
    4380             :     }
    4381             :   }
    4382         448 :   if (plevel) *plevel = level;
    4383         448 :   setlg(res, t); return res;
    4384             : }
    4385             : GEN
    4386         210 : mftonew(GEN mf, GEN F)
    4387             : {
    4388         210 :   pari_sp av = avma;
    4389             :   GEN ES;
    4390             :   long s;
    4391         210 :   mf = checkMF(mf);
    4392         210 :   s = MF_get_space(mf);
    4393         210 :   if (s != mf_FULL && s != mf_CUSP)
    4394           7 :     pari_err_TYPE("mftonew [not a full or cuspidal space]", mf);
    4395         203 :   ES = mftobasisES(mf,F);
    4396         196 :   if (!gequal0(gel(ES,1)))
    4397           0 :     pari_err_TYPE("mftonew [not a cuspidal form]", F);
    4398         196 :   F = gel(ES,2);
    4399         196 :   return gerepilecopy(av, mftonew_i(mf,F, NULL));
    4400             : }
    4401             : 
    4402             : static GEN mfeisenstein_i(long k, GEN CHI1, GEN CHI2);
    4403             : 
    4404             : /* mfinit(F * Theta) */
    4405             : static GEN
    4406          98 : mf2init(GEN mf)
    4407             : {
    4408          98 :   GEN CHI = MF_get_CHI(mf), gk = gadd(MF_get_gk(mf), ghalf);
    4409          98 :   long N = MF_get_N(mf);
    4410          98 :   return mfinit_Nkchi(N, itou(gk), mfchiadjust(CHI, gk, N), mf_FULL, 0);
    4411             : }
    4412             : 
    4413             : static long
    4414         623 : mfvec_first_cusp(GEN v)
    4415             : {
    4416         623 :   long i, l = lg(v);
    4417        1519 :   for (i = 1; i < l; i++)
    4418             :   {
    4419        1414 :     GEN F = gel(v,i);
    4420        1414 :     long t = mf_get_type(F);
    4421        1414 :     if (t == t_MF_BD) { F = gel(F,2); t = mf_get_type(F); }
    4422        1414 :     if (t == t_MF_HECKE) { F = gel(F,3); t = mf_get_type(F); }
    4423        1414 :     if (t == t_MF_NEWTRACE) break;
    4424             :   }
    4425         623 :   return i;
    4426             : }
    4427             : /* vF a vector of mf F of type DIV(LINEAR(BAS,L), f) in (lcm) level N,
    4428             :  * F[2]=LINEAR(BAS,L), F[2][2]=BAS=fixed basis (Eisenstein or bhn type),
    4429             :  * F[2][3]=L, F[3]=f; mfvectomat(vF, n) */
    4430             : static GEN
    4431         630 : mflineardivtomat(long N, GEN vF, long n)
    4432             : {
    4433         630 :   GEN F, M, f, fc, ME, dB, B, a0, V = NULL;
    4434         630 :   long lM, lF = lg(vF), j;
    4435             : 
    4436         630 :   if (lF == 1) return cgetg(1,t_MAT);
    4437         623 :   F = gel(vF,1);
    4438         623 :   if (lg(F) == 5)
    4439             :   { /* chicompat */
    4440         273 :     V = gmael(F,4,4);
    4441         273 :     if (typ(V) == t_INT) V = NULL;
    4442             :   }
    4443         623 :   M = gmael(F,2,2); /* BAS */
    4444         623 :   lM = lg(M);
    4445         623 :   j = mfvec_first_cusp(M);
    4446         623 :   if (j == 1) ME = NULL;
    4447             :   else
    4448             :   { /* BAS starts by Eisenstein */
    4449         161 :     ME = mfvectomat(vecslice(M,1,j-1), n, 1);
    4450         161 :     M = vecslice(M, j,lM-1);
    4451             :   }
    4452         623 :   M = bhnmat_extend_nocache(NULL, N, n, 1, M);
    4453         623 :   if (ME) M = shallowconcat(ME,M);
    4454             :   /* M = mfcoefs of BAS */
    4455         623 :   B = cgetg(lF, t_MAT);
    4456         623 :   dB= cgetg(lF, t_VEC);
    4457        2947 :   for (j = 1; j < lF; j++)
    4458             :   {
    4459        2324 :     GEN g = gel(vF, j); /* t_MF_DIV */
    4460        2324 :     gel(B,j) = RgM_RgC_mul(M, gmael(g,2,3));
    4461        2324 :     gel(dB,j)= gmael(g,2,4);
    4462             :   }
    4463         623 :   f = mfcoefsser(gel(F,3),n);
    4464         623 :   a0 = polcoef_i(f, 0, -1);
    4465         623 :   if (gequal0(a0) || gequal1(a0))
    4466         322 :     a0 = NULL;
    4467             :   else
    4468         301 :     f = gdiv(ser_unscale(f, a0), a0);
    4469         623 :   fc = ginv(f);
    4470        2947 :   for (j = 1; j < lF; j++)
    4471             :   {
    4472        2324 :     pari_sp av = avma;
    4473        2324 :     GEN LISer = RgV_to_ser_full(gel(B,j)), f;
    4474        2324 :     if (a0) LISer = gdiv(ser_unscale(LISer, a0), a0);
    4475        2324 :     f = gmul(LISer, fc);
    4476        2324 :     if (a0) f = ser_unscale(f, ginv(a0));
    4477        2324 :     f = sertocol(f); setlg(f, n+2);
    4478        2324 :     if (!gequal1(gel(dB,j))) f = RgC_Rg_div(f, gel(dB,j));
    4479        2324 :     gel(B,j) = gerepileupto(av,f);
    4480             :   }
    4481         623 :   if (V) B = gmodulo(QabM_tracerel(V, 0, B), gel(V,1));
    4482         623 :   return B;
    4483             : }
    4484             : 
    4485             : static GEN
    4486         350 : mfheckemat_mfcoefs(GEN mf, GEN B, GEN DATA)
    4487             : {
    4488         350 :   GEN Mindex = MF_get_Mindex(mf), Minv = MF_get_Minv(mf);
    4489         350 :   long j, l = lg(B), sb = mfsturm_mf(mf);
    4490         350 :   GEN b = MF_get_basis(mf), Q = cgetg(l, t_VEC);
    4491        1827 :   for (j = 1; j < l; j++)
    4492             :   {
    4493        1477 :     GEN v = hecke_i(sb, 1, gel(B,j), gel(b,j), DATA); /* Tn b[j] */
    4494        1477 :     settyp(v,t_COL); gel(Q,j) = vecpermute(v, Mindex);
    4495             :   }
    4496         350 :   return Minv_RgM_mul(Minv,Q);
    4497             : }
    4498             : /* T_p^2, p prime, 1/2-integral weight; B = mfcoefs(mf,sb*p^2,1) or (mf,sb,p^2)
    4499             :  * if p|N */
    4500             : static GEN
    4501           7 : mfheckemat_mfcoefs_p2(GEN mf, long p, GEN B)
    4502             : {
    4503           7 :   pari_sp av = avma;
    4504           7 :   GEN DATA = heckef2_data(MF_get_N(mf), p*p);
    4505           7 :   return gerepileupto(av, mfheckemat_mfcoefs(mf, B, DATA));
    4506             : }
    4507             : /* convert Mindex from row-index to mfcoef indexation: a(n) is stored in
    4508             :  * mfcoefs()[n+1], so subtract 1 from all indices */
    4509             : static GEN
    4510          49 : Mindex_as_coef(GEN mf)
    4511             : {
    4512          49 :   GEN v, Mindex = MF_get_Mindex(mf);
    4513          49 :   long i, l = lg(Mindex);
    4514          49 :   v = cgetg(l, t_VECSMALL);
    4515         210 :   for (i = 1; i < l; i++) v[i] = Mindex[i]-1;
    4516          49 :   return v;
    4517             : }
    4518             : /* T_p, p prime; B = mfcoefs(mf,sb*p,1) or (mf,sb,p) if p|N; integral weight */
    4519             : static GEN
    4520          35 : mfheckemat_mfcoefs_p(GEN mf, long p, GEN B)
    4521             : {
    4522          35 :   pari_sp av = avma;
    4523          35 :   GEN vm, Q, C, Minv = MF_get_Minv(mf);
    4524          35 :   long lm, k, i, j, l = lg(B), N = MF_get_N(mf);
    4525             : 
    4526          35 :   if (N % p == 0) return Minv_RgM_mul(Minv, rowpermute(B, MF_get_Mindex(mf)));
    4527          21 :   k = MF_get_k(mf);
    4528          21 :   C = gmul(mfchareval(MF_get_CHI(mf), p), powuu(p, k-1));
    4529          21 :   vm = Mindex_as_coef(mf); lm = lg(vm);
    4530          21 :   Q = cgetg(l, t_MAT);
    4531         147 :   for (j = 1; j < l; j++) gel(Q,j) = cgetg(lm, t_COL);
    4532         147 :   for (i = 1; i < lm; i++)
    4533             :   {
    4534         126 :     long m = vm[i], mp = m*p;
    4535         126 :     GEN Cm = (m % p) == 0? C : NULL;
    4536        1260 :     for (j = 1; j < l; j++)
    4537             :     {
    4538        1134 :       GEN S = gel(B,j), s = gel(S, mp + 1);
    4539        1134 :       if (Cm) s = gadd(s, gmul(C, gel(S, m/p + 1)));
    4540        1134 :       gcoeff(Q, i, j) = s;
    4541             :     }
    4542             :   }
    4543          21 :   return gerepileupto(av, Minv_RgM_mul(Minv,Q));
    4544             : }
    4545             : /* Matrix of T(p), p prime, dim(mf) > 0 and integral weight */
    4546             : static GEN
    4547         343 : mfheckemat_p(GEN mf, long p)
    4548             : {
    4549         343 :   pari_sp av = avma;
    4550         343 :   long N = MF_get_N(mf), sb = mfsturm_mf(mf);
    4551         343 :   GEN B = (N % p)? mfcoefs_mf(mf, sb * p, 1): mfcoefs_mf(mf, sb, p);
    4552         343 :   return gerepileupto(av, mfheckemat_mfcoefs(mf, B, hecke_data(N,p)));
    4553             : }
    4554             : 
    4555             : /* mf_NEW != (0), weight > 1, p prime. Use
    4556             :  * T(p) T(j) = T(j*p) + p^{k-1} \chi(p) 1_{p | j, p \nmid N} T(j/p) */
    4557             : static GEN
    4558         896 : mfnewmathecke_p(GEN mf, long p)
    4559             : {
    4560         896 :   pari_sp av = avma;
    4561         896 :   GEN tf, vj = MFnew_get_vj(mf), CHI = MF_get_CHI(mf);
    4562         896 :   GEN Mindex = MF_get_Mindex(mf), Minv = MF_get_Minv(mf);
    4563         896 :   long N = MF_get_N(mf), k = MF_get_k(mf);
    4564         896 :   long i, j, lvj = lg(vj), lim = vj[lvj-1] * p;
    4565         896 :   GEN M, perm, V, need = zero_zv(lim);
    4566         896 :   GEN C = (N % p)? gmul(mfchareval(CHI,p), powuu(p,k-1)): NULL;
    4567         896 :   tf = mftraceform_new(N, k, CHI);
    4568        3829 :   for (i = 1; i < lvj; i++)
    4569             :   {
    4570        2933 :     j = vj[i]; need[j*p] = 1;
    4571        2933 :     if (N % p && j % p == 0) need[j/p] = 1;
    4572             :   }
    4573         896 :   perm = zero_zv(lim);
    4574         896 :   V = cgetg(lim+1, t_VEC);
    4575       12285 :   for (i = j = 1; i <= lim; i++)
    4576       11389 :     if (need[i]) { gel(V,j) = mfhecke_i(i, N, tf); perm[i] = j; j++; }
    4577         896 :   setlg(V, j);
    4578         896 :   V = bhnmat_extend_nocache(NULL, N, mfsturm_mf(mf), 1, V);
    4579         896 :   V = rowpermute(V, Mindex); /* V[perm[i]] = coeffs(T_i newtrace) */
    4580         896 :   M = cgetg(lvj, t_MAT);
    4581        3829 :   for (i = 1; i < lvj; i++)
    4582             :   {
    4583             :     GEN t;
    4584        2933 :     j = vj[i]; t = gel(V, perm[j*p]);
    4585        2933 :     if (C && j % p == 0) t = RgC_add(t, RgC_Rg_mul(gel(V, perm[j/p]),C));
    4586        2933 :     gel(M,i) = t;
    4587             :   }
    4588         896 :   return gerepileupto(av, Minv_RgM_mul(Minv, M));
    4589             : }
    4590             : 
    4591             : GEN
    4592          77 : mfheckemat(GEN mf, GEN vn)
    4593             : {
    4594          77 :   pari_sp av = avma;
    4595          77 :   long lv, lvP, i, N, dim, nk, dk, p, sb, flint = (typ(vn)==t_INT);
    4596             :   GEN CHI, res, vT, FA, B, vP;
    4597             : 
    4598          77 :   mf = checkMF(mf);
    4599          77 :   if (typ(vn) != t_VECSMALL) vn = gtovecsmall(vn);
    4600          77 :   N = MF_get_N(mf); CHI = MF_get_CHI(mf); Qtoss(MF_get_gk(mf), &nk, &dk);
    4601          77 :   dim = MF_get_dim(mf);
    4602          77 :   lv = lg(vn);
    4603          77 :   res = cgetg(lv, t_VEC);
    4604          77 :   FA = cgetg(lv, t_VEC);
    4605          77 :   vP = cgetg(lv, t_VEC);
    4606          77 :   vT = const_vec(vecsmall_max(vn), NULL);
    4607         182 :   for (i = 1; i < lv; i++)
    4608             :   {
    4609         105 :     ulong n = (ulong)labs(vn[i]);
    4610             :     GEN fa;
    4611         105 :     if (!n) pari_err_TYPE("mfheckemat", vn);
    4612         105 :     if (dk == 1 || uissquareall(n, &n)) fa = myfactoru(n);
    4613           0 :     else { n = 0; fa = myfactoru(1); } /* dummy: T_{vn[i]} = 0 */
    4614         105 :     vn[i] = n;
    4615         105 :     gel(FA,i) = fa;
    4616         105 :     gel(vP,i) = gel(fa,1);
    4617             :   }
    4618          77 :   vP = shallowconcat1(vP); vecsmall_sort(vP);
    4619          77 :   vP = vecsmall_uniq_sorted(vP); /* all primes occurring in vn */
    4620          77 :   lvP = lg(vP); if (lvP == 1) goto END;
    4621          56 :   p = vP[lvP-1];
    4622          56 :   sb = mfsturm_mf(mf);
    4623          56 :   if (dk == 1 && nk != 1 && MF_get_space(mf) == mf_NEW)
    4624          21 :     B = NULL; /* special purpose mfnewmathecke_p is faster */
    4625          35 :   else if (lvP == 2 && N % p == 0)
    4626          21 :     B = mfcoefs_mf(mf, sb, dk==2? p*p: p); /* single prime | N, can optimize */
    4627             :   else
    4628          14 :     B = mfcoefs_mf(mf, sb * (dk==2? p*p: p), 1); /* general initialization */
    4629         126 :   for (i = 1; i < lvP; i++)
    4630             :   {
    4631          70 :     long j, l, q, e = 1;
    4632             :     GEN C, Tp, u1, u0;
    4633          70 :     p = vP[i];
    4634         189 :     for (j = 1; j < lv; j++) e = maxss(e, z_lval(vn[j], p));
    4635          70 :     if (!B)
    4636          28 :       Tp = mfnewmathecke_p(mf, p);
    4637          42 :     else if (dk == 2)
    4638           7 :       Tp = mfheckemat_mfcoefs_p2(mf,p, (lvP==2||N%p)? B: matdeflate(sb,p*p,B));
    4639             :     else
    4640          35 :       Tp = mfheckemat_mfcoefs_p(mf, p, (lvP==2||N%p)? B: matdeflate(sb,p,B));
    4641          70 :     gel(vT, p) = Tp;
    4642          70 :     if (e == 1) continue;
    4643          14 :     u0 = gen_1;
    4644          14 :     if (dk == 2)
    4645             :     {
    4646           0 :       C = N % p? gmul(mfchareval(CHI,p*p), powuu(p, nk-2)): NULL;
    4647           0 :       if (e == 2) u0 = uutoQ(p+1,p); /* special case T_{p^4} */
    4648             :     }
    4649             :     else
    4650          14 :       C = N % p? gmul(mfchareval(CHI,p),   powuu(p, nk-1)): NULL;
    4651          28 :     for (u1=Tp, q=p, l=2; l <= e; l++)
    4652             :     { /* u0 = T_{p^{l-2}}, u1 = T_{p^{l-1}} for l > 2 */
    4653          14 :       GEN v = gmul(Tp, u1);
    4654          14 :       if (C) v = gsub(v, gmul(C, u0));
    4655             :       /* q = p^l, vT[q] = T_q for k integer else T_{q^2} */
    4656          14 :       q *= p; u0 = u1; gel(vT, q) = u1 = v;
    4657             :     }
    4658             :   }
    4659          56 : END:
    4660             :   /* vT[p^e] = T_{p^e} for all p^e occurring below */
    4661         182 :   for (i = 1; i < lv; i++)
    4662             :   {
    4663         105 :     long n = vn[i], j, lP;
    4664             :     GEN fa, P, E, M;
    4665         105 :     if (n == 0) { gel(res,i) = zeromat(dim,dim); continue; }
    4666         105 :     if (n == 1) { gel(res,i) = matid(dim); continue; }
    4667          77 :     fa = gel(FA,i);
    4668          77 :     P = gel(fa,1); lP = lg(P);
    4669          77 :     E = gel(fa,2); M = gel(vT, upowuu(P[1], E[1]));
    4670          84 :     for (j = 2; j < lP; j++) M = RgM_mul(M, gel(vT, upowuu(P[j], E[j])));
    4671          77 :     gel(res,i) = M;
    4672             :   }
    4673          77 :   if (flint) res = gel(res,1);
    4674          77 :   return gerepilecopy(av, res);
    4675             : }
    4676             : 
    4677             : /* f = \sum_i v[i] T_listj[i] (Trace Form) attached to v; replace by f/a_1(f) */
    4678             : static GEN
    4679        1477 : mf_normalize(GEN mf, GEN v)
    4680             : {
    4681        1477 :   GEN c, dc = NULL, M = MF_get_M(mf), Mindex = MF_get_Mindex(mf);
    4682        1477 :   v = Q_primpart(v);
    4683        1477 :   c = RgMrow_RgC_mul(M, v, 2); /* a_1(f) */
    4684        1477 :   if (gequal1(c)) return v;
    4685         882 :   if (typ(c) == t_POL) c = gmodulo(c, mfcharpol(MF_get_CHI(mf)));
    4686         882 :   if (typ(c) == t_POLMOD && varn(gel(c,1)) == 1 && degpol(gel(c,1)) >= 40
    4687           7 :                          && Mindex[1] == 2
    4688           7 :                          && mfcharorder(MF_get_CHI(mf)) <= 2)
    4689           7 :   { /* normalize using expansion at infinity (small coefficients) */
    4690           7 :     GEN w, P = gel(c,1), a1 = gel(c,2);
    4691           7 :     long i, l = lg(Mindex);
    4692           7 :     w = cgetg(l, t_COL);
    4693           7 :     gel(w,1) = gen_1;
    4694         280 :     for (i = 2; i < l; i++)
    4695             :     {
    4696         273 :       c = liftpol_shallow(RgMrow_RgC_mul(M, v, Mindex[i]));
    4697         273 :       gel(w,i) = QXQ_div(c, a1, P);
    4698             :     }
    4699             :     /* w = expansion at oo of normalized form */
    4700           7 :     v = Minv_RgC_mul(MF_get_Minv(mf), Q_remove_denom(w, &dc));
    4701           7 :     v = gmodulo(v, P); /* back to mfbasis coefficients */
    4702             :   }
    4703             :   else
    4704             :   {
    4705         875 :     c = ginv(c);
    4706         875 :     if (typ(c) == t_POLMOD) c = Q_remove_denom(c, &dc);
    4707         875 :     v = RgC_Rg_mul(v, c);
    4708             :   }
    4709         882 :   if (dc) v = RgC_Rg_div(v, dc);
    4710         882 :   return v;
    4711             : }
    4712             : static void
    4713         427 : pol_red(GEN NF, GEN *pP, GEN *pa, long flag)
    4714             : {
    4715         427 :   GEN dP, a, P = *pP;
    4716         427 :   long d = degpol(P);
    4717             : 
    4718         427 :   *pa = a = pol_x(varn(P));
    4719         427 :   if (d * (NF ? nf_get_degree(NF): 1) > 30) return;
    4720             : 
    4721         420 :   dP = RgX_disc(P);
    4722         420 :   if (typ(dP) != t_INT)
    4723          98 :   { dP = gnorm(dP); if (typ(dP) != t_INT) pari_err_BUG("mfnewsplit"); }
    4724         420 :   if (d == 2 || expi(dP) < 62)
    4725             :   {
    4726         385 :     if (expi(dP) < 31)
    4727         385 :       P = NF? rnfpolredabs(NF, P,flag): polredabs0(P,flag);
    4728             :     else
    4729           0 :       P = NF? rnfpolredbest(NF,P,flag): polredbest(P,flag);
    4730         385 :     if (flag)
    4731             :     {
    4732         357 :       a = gel(P,2); if (typ(a) == t_POLMOD) a = gel(a,2);
    4733         357 :       P = gel(P,1);
    4734             :     }
    4735             :   }
    4736         420 :   *pP = P;
    4737         420 :   *pa = a;
    4738             : }
    4739             : 
    4740             : /* Diagonalize and normalize. See mfsplit for meaning of flag. */
    4741             : static GEN
    4742        1071 : mfspclean(GEN mf, GEN mf0, GEN NF, long ord, GEN simplesp, long flag)
    4743             : {
    4744        1071 :   const long vz = 1;
    4745        1071 :   long i, l = lg(simplesp), dim = MF_get_dim(mf);
    4746        1071 :   GEN res = cgetg(l, t_MAT), pols = cgetg(l, t_VEC);
    4747        1071 :   GEN zeros = (mf == mf0)? NULL: zerocol(dim - MF_get_dim(mf0));
    4748        2576 :   for (i = 1; i < l; i++)
    4749             :   {
    4750        1505 :     GEN ATP = gel(simplesp, i), A = gel(ATP,1), P = gel(ATP,3);
    4751        1505 :     long d = degpol(P);
    4752        1505 :     GEN a, v = (flag && d > flag)? NULL: gel(A,1);
    4753        1505 :     if (d == 1) P = pol_x(vz);
    4754             :     else
    4755             :     {
    4756         427 :       pol_red(NF, &P, &a, !!v);
    4757         427 :       if (v)
    4758             :       { /* Mod(a,P) root of charpoly(T), K*gpowers(a) = eigenvector of T */
    4759         399 :         GEN K, den, M = cgetg(d+1, t_MAT), T = gel(ATP,2);
    4760             :         long j;
    4761         399 :         T = shallowtrans(T);
    4762         399 :         gel(M,1) = vec_ei(d,1); /* basis of cyclic vectors */
    4763        1302 :         for (j = 2; j <= d; j++) gel(M,j) = RgM_RgC_mul(T, gel(M,j-1));
    4764         399 :         M = Q_primpart(M);
    4765         133 :         K = NF? ZabM_inv(liftpol_shallow(M), nf_get_pol(NF), ord, &den)
    4766         399 :               : ZM_inv(M,&den);
    4767         399 :         K = shallowtrans(K);
    4768         399 :         v = gequalX(a)? pol_x_powers(d, vz): RgXQ_powers(a, d-1, P);
    4769         399 :         v = gmodulo(RgM_RgC_mul(A, RgM_RgC_mul(K,v)), P);
    4770             :       }
    4771             :     }
    4772        1505 :     if (v)
    4773             :     {
    4774        1477 :       v = mf_normalize(mf0, v); if (zeros) v = shallowconcat(zeros,v);
    4775        1477 :       gel(res,i) = v; if (flag) setlg(res,i+1);
    4776             :     }
    4777             :     else
    4778          28 :       gel(res,i) = zerocol(dim);
    4779        1505 :     gel(pols,i) = P;
    4780             :   }
    4781        1071 :   return mkvec2(res, pols);
    4782             : }
    4783             : 
    4784             : /* return v = v_{X-r}(P), and set Z = P / (X-r)^v */
    4785             : static long
    4786          70 : RgX_valrem_root(GEN P, GEN r, GEN *Z)
    4787             : {
    4788             :   long v;
    4789         140 :   for (v = 0; degpol(P); v++)
    4790             :   {
    4791         140 :     GEN t, Q = RgX_div_by_X_x(P, r, &t);
    4792         140 :     if (!gequal0(t)) break;
    4793          70 :     P = Q;
    4794             :   }
    4795          70 :   *Z = P; return v;
    4796             : }
    4797             : static GEN
    4798        1498 : mynffactor(GEN NF, GEN P, long dimlim)
    4799             : {
    4800             :   long i, l, v;
    4801             :   GEN R, E;
    4802        1498 :   if (dimlim != 1)
    4803             :   {
    4804         931 :     R = NF? nffactor(NF, P): QX_factor(P);
    4805         931 :     if (!dimlim) return R;
    4806          21 :     E = gel(R,2);
    4807          21 :     R = gel(R,1); l = lg(R);
    4808          98 :     for (i = 1; i < l; i++)
    4809          91 :       if (degpol(gel(R,i)) > dimlim) break;
    4810          21 :     if (i == 1) return NULL;
    4811          21 :     setlg(E,i);
    4812          21 :     setlg(R,i); return mkmat2(R, E);
    4813             :   }
    4814             :   /* dimlim = 1 */
    4815         567 :   R = nfroots(NF, P); l = lg(R);
    4816         567 :   if (l == 1) return NULL;
    4817         504 :   v = varn(P);
    4818         504 :   settyp(R, t_COL);
    4819         504 :   if (degpol(P) == l-1)
    4820         448 :     E = const_col(l-1, gen_1);
    4821             :   else
    4822             :   {
    4823          56 :     E = cgetg(l, t_COL);
    4824         126 :     for (i = 1; i < l; i++) gel(E,i) = utoi(RgX_valrem_root(P, gel(R,i), &P));
    4825             :   }
    4826         504 :   R = deg1_from_roots(R, v);
    4827         504 :   return mkmat2(R, E);
    4828             : }
    4829             : 
    4830             : /* Let K be a number field attached to NF (Q if NF = NULL). A K-vector
    4831             :  * space of dimension d > 0 is given by a t_MAT A (n x d, full column rank)
    4832             :  * giving a K-basis, X a section (d x n: left pseudo-inverse of A). Return a
    4833             :  * pair (T, fa), where T is an element of the Hecke algebra (a sum of Tp taken
    4834             :  * from vector vTp) acting on A (a d x d t_MAT) and fa is the factorization of
    4835             :  * its characteristic polynomial, limited to factors of degree <= dimlim if
    4836             :  * dimlim != 0 (return NULL if there are no factors of degree <= dimlim) */
    4837             : static GEN
    4838        1323 : findbestsplit(GEN NF, GEN vTp, GEN A, GEN X, long dimlim, long vz)
    4839             : {
    4840        1323 :   GEN T = NULL, Tkeep = NULL, fakeep = NULL;
    4841        1323 :   long lmax = 0, i, lT = lg(vTp);
    4842        1743 :   for (i = 1; i < lT; i++)
    4843             :   {
    4844        1743 :     GEN D, P, E, fa, TpA = gel(vTp,i);
    4845             :     long l;
    4846        2758 :     if (typ(TpA) == t_INT) break;
    4847        1498 :     if (lg(TpA) > lg(A)) TpA = RgM_mul(X, RgM_mul(TpA, A)); /* Tp | A */
    4848        1498 :     T = T ? RgM_add(T, TpA) : TpA;
    4849        1498 :     if (!NF) { P = QM_charpoly_ZX(T); setvarn(P, vz); }
    4850             :     else
    4851             :     {
    4852         273 :       P = charpoly(Q_remove_denom(T, &D), vz);
    4853         273 :       if (D) P = gdiv(RgX_unscale(P, D), powiu(D, degpol(P)));
    4854             :     }
    4855        1498 :     fa = mynffactor(NF, P, dimlim);
    4856        1498 :     if (!fa) return NULL;
    4857        1435 :     E = gel(fa, 2);
    4858             :     /* characteristic polynomial is separable ? */
    4859        1435 :     if (isint1(vecmax(E))) { Tkeep = T; fakeep = fa; break; }
    4860         420 :     l = lg(E);
    4861             :     /* characteristic polynomial has more factors than before ? */
    4862         420 :     if (l > lmax) { lmax = l; Tkeep = T; fakeep = fa; }
    4863             :   }
    4864        1260 :   return mkvec2(Tkeep, fakeep);
    4865             : }
    4866             : 
    4867             : static GEN
    4868         210 : nfcontent(GEN nf, GEN v)
    4869             : {
    4870         210 :   long i, l = lg(v);
    4871         210 :   GEN c = gel(v,1);
    4872        1134 :   for (i = 2; i < l; i++) c = idealadd(nf, c, gel(v,i));
    4873         210 :   if (typ(c) == t_MAT && gequal1(gcoeff(c,1,1))) c = gen_1;
    4874         210 :   return c;
    4875             : }
    4876             : static GEN
    4877         329 : nf_primpart(GEN nf, GEN x)
    4878             : {
    4879         329 :   switch(typ(x))
    4880             :   {
    4881         210 :     case t_COL:
    4882             :     {
    4883         210 :       GEN A = matalgtobasis(nf, x), c = nfcontent(nf, A);
    4884         210 :       if (typ(c) == t_INT) return x;
    4885          21 :       c = idealred_elt(nf,c);
    4886          21 :       A = Q_primpart( nfC_nf_mul(nf, A, Q_primpart(nfinv(nf,c))) );
    4887          21 :       A = liftpol_shallow( matbasistoalg(nf, A) );
    4888          21 :       if (gexpo(A) > gexpo(x)) A = x;
    4889          21 :       return A;
    4890             :     }
    4891         329 :     case t_MAT: pari_APPLY_same(nf_primpart(nf, gel(x,i)));
    4892           0 :     default:
    4893           0 :       pari_err_TYPE("nf_primpart", x);
    4894             :       return NULL; /*LCOV_EXCL_LINE*/
    4895             :   }
    4896             : }
    4897             : 
    4898             : /* rotate entries of v to accomodate new entry 'x' (push out oldest entry) */
    4899             : static void
    4900        1211 : vecpush(GEN v, GEN x)
    4901             : {
    4902             :   long i;
    4903        6055 :   for (i = lg(v)-1; i > 1; i--) gel(v,i) = gel(v,i-1);
    4904        1211 :   gel(v,1) = x;
    4905        1211 : }
    4906             : 
    4907             : /* sort t_VEC of vector spaces by increasing dimension */
    4908             : static GEN
    4909        1071 : sort_by_dim(GEN v)
    4910             : {
    4911        1071 :   long i, l = lg(v);
    4912        1071 :   GEN D = cgetg(l, t_VECSMALL);
    4913        2576 :   for (i = 1; i < l; i++) D[i] = lg(gmael(v,i,2));
    4914        1071 :   return vecpermute(v , vecsmall_indexsort(D));
    4915             : }
    4916             : static GEN
    4917        1071 : split_starting_space(GEN mf)
    4918             : {
    4919        1071 :   long d = MF_get_dim(mf), d2;
    4920        1071 :   GEN id = matid(d);
    4921        1071 :   switch(MF_get_space(mf))
    4922             :   {
    4923        1064 :     case mf_NEW:
    4924        1064 :     case mf_CUSP: return mkvec2(id, id);
    4925             :   }
    4926           7 :   d2 = lg(MF_get_S(mf))-1;
    4927           7 :   return mkvec2(vecslice(id, d-d2+1,d),
    4928             :                 shallowconcat(zeromat(d2,d-d2),matid(d2)));
    4929             : }
    4930             : /* If dimlim > 0, keep only the dimension <= dimlim eigenspaces.
    4931             :  * See mfsplit for the meaning of flag. */
    4932             : static GEN
    4933        1470 : split_ii(GEN mf, long dimlim, long flag, GEN vSP, long *pnewd)
    4934             : {
    4935             :   forprime_t iter;
    4936        1470 :   GEN CHI = MF_get_CHI(mf), empty = cgetg(1, t_VEC), mf0 = mf;
    4937             :   GEN NF, POLCYC, todosp, Tpbigvec, simplesp;
    4938        1470 :   long N = MF_get_N(mf), k = MF_get_k(mf);
    4939        1470 :   long ord, FC, NEWT, dimsimple = 0, newd = -1;
    4940        1470 :   const long NBH = 5, vz = 1;
    4941             :   ulong p;
    4942             : 
    4943        1470 :   switch(MF_get_space(mf))
    4944             :   {
    4945        1183 :     case mf_NEW: break;
    4946         280 :     case mf_CUSP:
    4947             :     case mf_FULL:
    4948             :     {
    4949             :       GEN CHIP;
    4950         280 :       if (k > 1) { mf0 = mfinittonew(mf); break; }
    4951         259 :       CHIP = mfchartoprimitive(CHI, NULL);
    4952         259 :       newd = lg(MF_get_S(mf))-1 - mfolddim_i(N, k, CHIP, vSP);
    4953         259 :       break;
    4954             :     }
    4955           7 :     default: pari_err_TYPE("mfsplit [space does not contain newspace]", mf);
    4956             :       return NULL; /*LCOV_EXCL_LINE*/
    4957             :   }
    4958        1463 :   if (newd < 0) newd = mf0? MF_get_dim(mf0): 0;
    4959        1463 :   *pnewd = newd;
    4960        1463 :   if (!newd) return mkvec2(cgetg(1, t_MAT), empty);
    4961             : 
    4962        1071 :   NEWT = (k > 1 && MF_get_space(mf0) == mf_NEW);
    4963        1071 :   todosp = mkvec( split_starting_space(mf0) );
    4964        1071 :   simplesp = empty;
    4965        1071 :   FC = mfcharconductor(CHI);
    4966        1071 :   ord = mfcharorder(CHI);
    4967        1071 :   if (ord <= 2) NF = POLCYC = NULL;
    4968             :   else
    4969             :   {
    4970         203 :     POLCYC = mfcharpol(CHI);
    4971         203 :     NF = nfinit(POLCYC,DEFAULTPREC);
    4972             :   }
    4973        1071 :   Tpbigvec = zerovec(NBH);
    4974        1071 :   u_forprime_init(&iter, 2, ULONG_MAX);
    4975        1498 :   while (dimsimple < newd && (p = u_forprime_next(&iter)))
    4976             :   {
    4977             :     GEN nextsp;
    4978             :     long ind;
    4979        1498 :     if (N % (p*p) == 0 && N/p % FC == 0) continue; /* T_p = 0 in this case */
    4980        1211 :     vecpush(Tpbigvec, NEWT? mfnewmathecke_p(mf0,p): mfheckemat_p(mf0,p));
    4981        1211 :     nextsp = empty;
    4982        1596 :     for (ind = 1; ind < lg(todosp); ind++)
    4983             :     {
    4984        1323 :       GEN tmp = gel(todosp, ind), fa, P, E, D, Tp, DTp;
    4985        1323 :       GEN A = gel(tmp, 1);
    4986        1323 :       GEN X = gel(tmp, 2);
    4987             :       long lP, i;
    4988        1323 :       tmp = findbestsplit(NF, Tpbigvec, A, X, dimlim, vz);
    4989        1442 :       if (!tmp) continue; /* nothing there */
    4990        1260 :       Tp = gel(tmp, 1);
    4991        1260 :       fa = gel(tmp, 2);
    4992        1260 :       P = gel(fa, 1);
    4993        1260 :       E = gel(fa, 2); lP = lg(P);
    4994             :       /* lP > 1 */
    4995        1260 :       if (DEBUGLEVEL) err_printf("Exponents = %Ps\n", E);
    4996        1260 :       if (lP == 2)
    4997             :       {
    4998         868 :         GEN P1 = gel(P,1);
    4999         868 :         long e1 = itos(gel(E,1)), d1 = degpol(P1);
    5000         868 :         if (e1 * d1 == lg(Tp)-1)
    5001             :         {
    5002         819 :           if (e1 > 1) nextsp = vec_append(nextsp, mkvec2(A,X));
    5003             :           else
    5004             :           { /* simple module */
    5005         721 :             simplesp = vec_append(simplesp, mkvec3(A,Tp,P1));
    5006         959 :             if ((dimsimple += d1) == newd) goto END;
    5007             :           }
    5008         119 :           continue;
    5009             :         }
    5010             :       }
    5011             :       /* Found splitting */
    5012         441 :       DTp = Q_remove_denom(Tp, &D);
    5013        1204 :       for (i = 1; i < lP; i++)
    5014             :       {
    5015        1001 :         GEN Ai, Xi, dXi, AAi, v, y, Pi = gel(P,i);
    5016        1001 :         Ai = RgX_RgM_eval(D? RgX_rescale(Pi,D): Pi, DTp);
    5017        1001 :         Ai = QabM_ker(Ai, POLCYC, ord);
    5018        1001 :         if (NF) Ai = nf_primpart(NF, Ai);
    5019             : 
    5020        1001 :         AAi = RgM_mul(A, Ai);
    5021             :         /* gives section, works on nonsquare matrices */
    5022        1001 :         Xi = QabM_pseudoinv(Ai, POLCYC, ord, &v, &dXi);
    5023        1001 :         Xi = RgM_Rg_div(Xi, dXi);
    5024        1001 :         y = gel(v,1);
    5025        1001 :         if (isint1(gel(E,i)))
    5026             :         {
    5027         784 :           GEN Tpi = RgM_mul(Xi, RgM_mul(rowpermute(Tp,y), Ai));
    5028         784 :           simplesp = vec_append(simplesp, mkvec3(AAi, Tpi, Pi));
    5029         784 :           if ((dimsimple += degpol(Pi)) == newd) goto END;
    5030             :         }
    5031             :         else
    5032             :         {
    5033         217 :           Xi = RgM_mul(Xi, rowpermute(X,y));
    5034         217 :           nextsp = vec_append(nextsp, mkvec2(AAi, Xi));
    5035             :         }
    5036             :       }
    5037             :     }
    5038         273 :     todosp = nextsp; if (lg(todosp) == 1) break;
    5039             :   }
    5040           0 : END:
    5041        1071 :   if (DEBUGLEVEL) err_printf("end split, need to clean\n");
    5042        1071 :   return mfspclean(mf, mf0, NF, ord, sort_by_dim(simplesp), flag);
    5043             : }
    5044             : static GEN
    5045          28 : dim_filter(GEN v, long dim)
    5046             : {
    5047          28 :   GEN P = gel(v,2);
    5048          28 :   long j, l = lg(P);
    5049         140 :   for (j = 1; j < l; j++)
    5050         126 :     if (degpol(gel(P,j)) > dim)
    5051             :     {
    5052          14 :       v = mkvec2(vecslice(gel(v,1),1,j-1), vecslice(P,1,j-1));
    5053          14 :       break;
    5054             :     }
    5055          28 :   return v;
    5056             : }
    5057             : static long
    5058         287 : dim_sum(GEN v)
    5059             : {
    5060         287 :   GEN P = gel(v,2);
    5061         287 :   long j, l = lg(P), d = 0;
    5062         707 :   for (j = 1; j < l; j++) d += degpol(gel(P,j));
    5063         287 :   return d;
    5064             : }
    5065             : static GEN
    5066        1148 : split_i(GEN mf, long dimlim, long flag)
    5067        1148 : { long junk; return split_ii(mf, dimlim, flag, NULL, &junk); }
    5068             : /* mf is either already split or output by mfinit. Splitting is done only for
    5069             :  * newspace except in weight 1. If flag = 0 (default) split completely.
    5070             :  * If flag = d > 0, only give the Galois polynomials in degree > d
    5071             :  * Flag is ignored if dimlim = 1. */
    5072             : GEN
    5073          98 : mfsplit(GEN mf0, long dimlim, long flag)
    5074             : {
    5075          98 :   pari_sp av = avma;
    5076          98 :   GEN v, mf = checkMF_i(mf0);
    5077          98 :   if (!mf) pari_err_TYPE("mfsplit", mf0);
    5078          98 :   if ((v = obj_check(mf, MF_SPLIT)))
    5079          28 :   { if (dimlim) v = dim_filter(v, dimlim); }
    5080          70 :   else if (dimlim && (v = obj_check(mf, MF_SPLITN)))
    5081          21 :   { v = (itos(gel(v,1)) >= dimlim)? dim_filter(gel(v,2), dimlim): NULL; }
    5082          98 :   if (!v)
    5083             :   {
    5084             :     long newd;
    5085          70 :     v = split_ii(mf, dimlim, flag, NULL, &newd);
    5086          70 :     if (lg(v) == 1) obj_insert(mf, MF_SPLITN, mkvec2(utoi(dimlim), v));
    5087          70 :     else if (!flag)
    5088             :     {
    5089          49 :       if (dim_sum(v) == newd) obj_insert(mf, MF_SPLIT,v);
    5090          21 :       else obj_insert(mf, MF_SPLITN, mkvec2(utoi(dimlim), v));
    5091             :     }
    5092             :   }
    5093          98 :   return gerepilecopy(av, v);
    5094             : }
    5095             : static GEN
    5096         231 : split(GEN mf) { return split_i(mf,0,0); }
    5097             : GEN
    5098         798 : MF_get_newforms(GEN mf) { return gel(obj_checkbuild(mf,MF_SPLIT,&split),1); }
    5099             : GEN
    5100         595 : MF_get_fields(GEN mf) { return gel(obj_checkbuild(mf,MF_SPLIT,&split),2); }
    5101             : 
    5102             : /*************************************************************************/
    5103             : /*                     Modular forms of Weight 1                         */
    5104             : /*************************************************************************/
    5105             : /* S_1(G_0(N)), small N. Return 1 if definitely empty; return 0 if maybe
    5106             :  * nonempty  */
    5107             : static int
    5108       16632 : wt1empty(long N)
    5109             : {
    5110       16632 :   if (N <= 100) switch (N)
    5111             :   { /* nonempty [32/100] */
    5112        5453 :     case 23: case 31: case 39: case 44: case 46:
    5113             :     case 47: case 52: case 55: case 56: case 57:
    5114             :     case 59: case 62: case 63: case 68: case 69:
    5115             :     case 71: case 72: case 76: case 77: case 78:
    5116             :     case 79: case 80: case 83: case 84: case 87:
    5117             :     case 88: case 92: case 93: case 94: case 95:
    5118        5453 :     case 99: case 100: return 0;
    5119        3549 :     default: return 1;
    5120             :   }
    5121        7630 :   if (N <= 600) switch(N)
    5122             :   { /* empty [111/500] */
    5123         336 :     case 101: case 102: case 105: case 106: case 109:
    5124             :     case 113: case 121: case 122: case 123: case 125:
    5125             :     case 130: case 134: case 137: case 146: case 149:
    5126             :     case 150: case 153: case 157: case 162: case 163:
    5127             :     case 169: case 170: case 173: case 178: case 181:
    5128             :     case 182: case 185: case 187: case 193: case 194:
    5129             :     case 197: case 202: case 205: case 210: case 218:
    5130             :     case 221: case 226: case 233: case 241: case 242:
    5131             :     case 245: case 246: case 250: case 257: case 265:
    5132             :     case 267: case 269: case 274: case 277: case 281:
    5133             :     case 289: case 293: case 298: case 305: case 306:
    5134             :     case 313: case 314: case 317: case 326: case 337:
    5135             :     case 338: case 346: case 349: case 353: case 361:
    5136             :     case 362: case 365: case 369: case 370: case 373:
    5137             :     case 374: case 377: case 386: case 389: case 394:
    5138             :     case 397: case 401: case 409: case 410: case 421:
    5139             :     case 425: case 427: case 433: case 442: case 449:
    5140             :     case 457: case 461: case 466: case 481: case 482:
    5141             :     case 485: case 490: case 493: case 509: case 514:
    5142             :     case 521: case 530: case 533: case 534: case 538:
    5143             :     case 541: case 545: case 554: case 557: case 562:
    5144             :     case 565: case 569: case 577: case 578: case 586:
    5145         336 :     case 593: return 1;
    5146        6979 :     default: return 0;
    5147             :   }
    5148         315 :   return 0;
    5149             : }
    5150             : 
    5151             : static GEN
    5152          28 : initwt1trace(GEN mf)
    5153             : {
    5154          28 :   GEN S = MF_get_S(mf), v, H;
    5155             :   long l, i;
    5156          28 :   if (lg(S) == 1) return mftrivial();
    5157          28 :   H = mfheckemat(mf, Mindex_as_coef(mf));
    5158          28 :   l = lg(H); v = cgetg(l, t_VEC);
    5159          63 :   for (i = 1; i < l; i++) gel(v,i) = gtrace(gel(H,i));
    5160          28 :   v = Minv_RgC_mul(MF_get_Minv(mf), v);
    5161          28 :   return mflineardiv_linear(S, v, 1);
    5162             : }
    5163             : static GEN
    5164          21 : initwt1newtrace(GEN mf)
    5165             : {
    5166          21 :   GEN v, D, S, Mindex, CHI = MF_get_CHI(mf);
    5167          21 :   long FC, lD, i, sb, N1, N2, lM, N = MF_get_N(mf);
    5168          21 :   CHI = mfchartoprimitive(CHI, &FC);
    5169          21 :   if (N % FC || mfcharparity(CHI) == 1) return mftrivial();
    5170          21 :   D = mydivisorsu(N/FC); lD = lg(D);
    5171          21 :   S = MF_get_S(mf);
    5172          21 :   if (lg(S) == 1) return mftrivial();
    5173          21 :   N2 = newd_params2(N);
    5174          21 :   N1 = N / N2;
    5175          21 :   Mindex = MF_get_Mindex(mf);
    5176          21 :   lM = lg(Mindex);
    5177          21 :   sb = Mindex[lM-1];
    5178          21 :   v = zerovec(sb+1);
    5179          42 :   for (i = 1; i < lD; i++)
    5180             :   {
    5181          21 :     long M = FC*D[i], j;
    5182          21 :     GEN tf = initwt1trace(M == N? mf: mfinit_Nkchi(M, 1, CHI, mf_CUSP, 0));
    5183             :     GEN listd, w;
    5184          21 :     if (mf_get_type(tf) == t_MF_CONST) continue;
    5185          21 :     w = mfcoefs_i(tf, sb, 1);
    5186          21 :     if (M == N) { v = gadd(v, w); continue; }
    5187           0 :     listd = mydivisorsu(u_ppo(ugcd(N/M, N1), FC));
    5188           0 :     for (j = 1; j < lg(listd); j++)
    5189             :     {
    5190           0 :       long d = listd[j], d2 = d*d; /* coprime to FC */
    5191           0 :       GEN dk = mfchareval(CHI, d);
    5192           0 :       long NMd = N/(M*d), m;
    5193           0 :       for (m = 1; m <= sb/d2; m++)
    5194             :       {
    5195           0 :         long be = mubeta2(NMd, m);
    5196           0 :         if (be)
    5197             :         {
    5198           0 :           GEN c = gmul(dk, gmulsg(be, gel(w, m+1)));
    5199           0 :           long n = m*d2;
    5200           0 :           gel(v, n+1) = gadd(gel(v, n+1), c);
    5201             :         }
    5202             :       }
    5203             :     }
    5204             :   }
    5205          21 :   if (gequal0(gel(v,2))) return mftrivial();
    5206          21 :   v = vecpermute(v,Mindex);
    5207          21 :   v = Minv_RgC_mul(MF_get_Minv(mf), v);
    5208          21 :   return mflineardiv_linear(S, v, 1);
    5209             : }
    5210             : 
    5211             : /* i*p + 1, i*p < lim corresponding to a_p(f_j), a_{2p}(f_j)...  */
    5212             : static GEN
    5213        1834 : pindices(long p, long lim)
    5214             : {
    5215        1834 :   GEN v = cgetg(lim, t_VECSMALL);
    5216             :   long i, ip;
    5217       22190 :   for (i = 1, ip = p + 1; ip < lim; i++, ip += p) v[i] = ip;
    5218        1834 :   setlg(v, i); return v;
    5219             : }
    5220             : 
    5221             : /* assume !wt1empty(N), in particular N>25 */
    5222             : /* Returns [[lim,p], mf (weight 2), p*lim x dim matrix] */
    5223             : static GEN
    5224        1834 : mf1_pre(long N)
    5225             : {
    5226             :   pari_timer tt;
    5227             :   GEN mf, v, L, I, M, Minv, den;
    5228             :   long B, lim, LIM, p;
    5229             : 
    5230        1834 :   if (DEBUGLEVEL) timer_start(&tt);
    5231        1834 :   mf = mfinit_Nkchi(N, 2, mfchartrivial(), mf_CUSP, 0);
    5232        1834 :   if (DEBUGLEVEL)
    5233           0 :     timer_printf(&tt, "mf1basis [pre]: S_2(%ld), dim = %ld",
    5234             :                  N, MF_get_dim(mf));
    5235        1834 :   M = MF_get_M(mf); Minv = MF_get_Minv(mf); den = gel(Minv,2);
    5236        1834 :   B = mfsturm_mf(mf);
    5237        1834 :   if (uisprime(N))
    5238             :   {
    5239         392 :     lim = 2 * MF_get_dim(mf); /* ensure mfstabiter's first kernel ~ square */
    5240         392 :     p = 2;
    5241             :   }
    5242             :   else
    5243             :   {
    5244             :     forprime_t S;
    5245        1442 :     u_forprime_init(&S, 2, N);
    5246        2576 :     while ((p = u_forprime_next(&S)))
    5247        2576 :       if (N % p) break;
    5248        1442 :     lim = B + 1;
    5249             :   }
    5250        1834 :   LIM = (N & (N - 1))? 2 * lim: 3 * lim; /* N power of 2 ? */
    5251        1834 :   L = mkvecsmall4(lim, LIM, mfsturmNk(N,1), p);
    5252        1834 :   M = bhnmat_extend_nocache(M, N, LIM-1, 1, MF_get_S(mf));
    5253        1834 :   if (DEBUGLEVEL) timer_printf(&tt, "mf1basis [pre]: bnfmat_extend");
    5254        1834 :   v = pindices(p, LIM);
    5255        1834 :   if (!LIM) return mkvec4(L, mf, M, v);
    5256        1834 :   I = RgM_Rg_div(ZM_mul(rowslice(M, B+2, LIM), gel(Minv,1)), den);
    5257        1834 :   I = Q_remove_denom(I, &den);
    5258        1834 :   if (DEBUGLEVEL) timer_printf(&tt, "mf1basis [prec]: Iden");
    5259        1834 :   return mkvec5(L, mf, M, v, mkvec2(I, den));
    5260             : }
    5261             : 
    5262             : /* lg(A) > 1, E a t_POL */
    5263             : static GEN
    5264         686 : mfmatsermul(GEN A, GEN E)
    5265             : {
    5266         686 :   long j, l = lg(A), r = nbrows(A);
    5267         686 :   GEN M = cgetg(l, t_MAT);
    5268         686 :   E = RgXn_red_shallow(E, r+1);
    5269        5866 :   for (j = 1; j < l; j++)
    5270             :   {
    5271        5180 :     GEN c = RgV_to_RgX(gel(A,j), 0);
    5272        5180 :     gel(M, j) = RgX_to_RgC(RgXn_mul(c, E, r+1), r);
    5273             :   }
    5274         686 :   return M;
    5275             : }
    5276             : /* lg(Ap) > 1, Ep an Flxn */
    5277             : static GEN
    5278        1141 : mfmatsermul_Fl(GEN Ap, GEN Ep, ulong p)
    5279             : {
    5280        1141 :   long j, l = lg(Ap), r = nbrows(Ap);
    5281        1141 :   GEN M = cgetg(l, t_MAT);
    5282       42630 :   for (j = 1; j < l; j++)
    5283             :   {
    5284       41489 :     GEN c = Flv_to_Flx(gel(Ap,j), 0);
    5285       41489 :     gel(M,j) = Flx_to_Flv(Flxn_mul(c, Ep, r+1, p), r);
    5286             :   }
    5287        1141 :   return M;
    5288             : }
    5289             : 
    5290             : /* CHI mod F | N, return mfchar of modulus N.
    5291             :  * FIXME: wasteful, G should be precomputed  */
    5292             : static GEN
    5293       13048 : mfcharinduce(GEN CHI, long N)
    5294             : {
    5295             :   GEN G, chi;
    5296       13048 :   if (mfcharmodulus(CHI) == N) return CHI;
    5297        1463 :   G = znstar0(utoipos(N), 1);
    5298        1463 :   chi = zncharinduce(gel(CHI,1), gel(CHI,2), G);
    5299        1463 :   CHI = leafcopy(CHI);
    5300        1463 :   gel(CHI,1) = G;
    5301        1463 :   gel(CHI,2) = chi; return CHI;
    5302             : }
    5303             : 
    5304             : static GEN
    5305        3983 : gmfcharno(GEN CHI)
    5306             : {
    5307        3983 :   GEN G = gel(CHI,1), chi = gel(CHI,2);
    5308        3983 :   return mkintmod(znconreyexp(G, chi), znstar_get_N(G));
    5309             : }
    5310             : static long
    5311       13678 : mfcharno(GEN CHI)
    5312             : {
    5313       13678 :   GEN n = znconreyexp(gel(CHI,1), gel(CHI,2));
    5314       13678 :   return itou(n);
    5315             : }
    5316             : 
    5317             : /* return k such that minimal mfcharacter in Galois orbit of CHI is CHI^k */
    5318             : static long
    5319       12138 : mfconreyminimize(GEN CHI)
    5320             : {
    5321       12138 :   GEN G = gel(CHI,1), cyc, chi;
    5322       12138 :   cyc = ZV_to_zv(znstar_get_cyc(G));
    5323       12138 :   chi = ZV_to_zv(znconreychar(G, gel(CHI,2)));
    5324       12138 :   return zv_cyc_minimize(cyc, chi, coprimes_zv(mfcharorder(CHI)));
    5325             : }
    5326             : 
    5327             : /* find scalar c such that first nonzero entry of c*v is 1; return c*v */
    5328             : static GEN
    5329        2065 : RgV_normalize(GEN v, GEN *pc)
    5330             : {
    5331        2065 :   long i, l = lg(v);
    5332        5313 :   for (i = 1; i < l; i++)
    5333             :   {
    5334        5313 :     GEN c = gel(v,i);
    5335        5313 :     if (!gequal0(c))
    5336             :     {
    5337        2065 :       if (gequal1(c)) break;
    5338         679 :       *pc = ginv(c); return RgV_Rg_mul(v, *pc);
    5339             :     }
    5340             :   }
    5341        1386 :   *pc = gen_1; return v;
    5342             : }
    5343             : /* pS != NULL; dim > 0 */
    5344             : static GEN
    5345         784 : mftreatdihedral(long N, GEN DIH, GEN POLCYC, long ordchi, GEN *pS)
    5346             : {
    5347         784 :   long l = lg(DIH), lim = mfsturmNk(N, 1), i;
    5348         784 :   GEN Minv, C = cgetg(l, t_VEC), M = cgetg(l, t_MAT);
    5349        2436 :   for (i = 1; i < l; i++)
    5350             :   {
    5351        1652 :     GEN c, v = mfcoefs_i(gel(DIH,i), lim, 1);
    5352        1652 :     gel(M,i) = RgV_normalize(v, &c);
    5353        1652 :     gel(C,i) = Rg_col_ei(c, l-1, i);
    5354             :   }
    5355         784 :   Minv = gel(mfclean(M,POLCYC,ordchi,0),2);
    5356         784 :   M = RgM_Minv_mul(M, Minv);
    5357         784 :   C = RgM_Minv_mul(C, Minv);
    5358         784 :   *pS = vecmflinear(DIH, C); return M;
    5359             : }
    5360             : 
    5361             : /* same mode a maximal ideal above q */
    5362             : static GEN
    5363        2408 : Tpmod(GEN Ap, GEN A, ulong chip, long p, ulong q)
    5364             : {
    5365        2408 :   GEN B = leafcopy(Ap);
    5366        2408 :   long i, ip, l = lg(B);
    5367       86345 :   for (i = 1, ip = p; ip < l; i++, ip += p)
    5368       83937 :     B[ip] = Fl_add(B[ip], Fl_mul(A[i], chip, q), q);
    5369        2408 :   return B;
    5370             : }
    5371             : /* Tp(f_1), ..., Tp(f_d) mod q */
    5372             : static GEN
    5373         301 : matTpmod(GEN xp, GEN x, ulong chip, long p, ulong q)
    5374        2709 : { pari_APPLY_same(Tpmod(gel(xp,i), gel(x,i), chip, p, q)); }
    5375             : 
    5376             : /* Ap[i] = a_{p*i}(F), A[i] = a_i(F), i = 1..lim
    5377             :  * Tp(f)[n] = a_{p*n}(f) + chi(p) a_{n/p}(f) * 1_{p | n} */
    5378             : static GEN
    5379         469 : Tp(GEN Ap, GEN A, GEN chip, long p)
    5380             : {
    5381         469 :   GEN B = leafcopy(Ap);
    5382         469 :   long i, ip, l = lg(B);
    5383       12915 :   for (i = 1, ip = p; ip < l; i++, ip += p)
    5384       12446 :     gel(B,ip) = gadd(gel(B,ip), gmul(gel(A,i), chip));
    5385         469 :   return B;
    5386             : }
    5387             : /* Tp(f_1), ..., Tp(f_d) */
    5388             : static GEN
    5389          56 : matTp(GEN xp, GEN x, GEN chip, long p)
    5390         525 : { pari_APPLY_same(Tp(gel(xp,i), gel(x,i), chip, p)); }
    5391             : 
    5392             : static GEN
    5393         378 : _RgXQM_mul(GEN x, GEN y, GEN T)
    5394         378 : { return T? RgXQM_mul(x, y, T): RgM_mul(x, y); }
    5395             : /* largest T-stable Q(CHI)-subspace of Q(CHI)-vector space spanned by columns
    5396             :  * of A */
    5397             : static GEN
    5398          28 : mfstabiter(GEN *pC, GEN A0, GEN chip, GEN TMP, GEN P, long ordchi)
    5399             : {
    5400          28 :   GEN A, Ap, vp = gel(TMP,4), C = NULL;
    5401          28 :   long i, lA, lim1 = gel(TMP,1)[3], p = gel(TMP,1)[4];
    5402             :   pari_timer tt;
    5403             : 
    5404          28 :   Ap = rowpermute(A0, vp);
    5405          28 :   A = rowslice(A0, 2, nbrows(Ap)+1); /* remove a0 */
    5406             :   for(;;)
    5407          28 :   {
    5408          56 :     GEN R = shallowconcat(matTp(Ap, A, chip, p), A);
    5409          56 :     GEN B = QabM_ker(R, P, ordchi);
    5410          56 :     long lB = lg(B);
    5411          56 :     if (DEBUGLEVEL)
    5412           0 :       timer_printf(&tt, "mf1basis: Hecke intersection (dim %ld)", lB-1);
    5413          56 :     if (lB == 1) return NULL;
    5414          56 :     lA = lg(A); if (lB == lA) break;
    5415          28 :     B = rowslice(B, 1, lA-1);
    5416          28 :     Ap = _RgXQM_mul(Ap, B, P);
    5417          28 :     A = _RgXQM_mul(A, B, P);
    5418          28 :     C = C? _RgXQM_mul(C, B, P): B;
    5419             :   }
    5420          28 :   if (nbrows(A) < lim1)
    5421             :   {
    5422          14 :     A0 = rowslice(A0, 2, lim1);
    5423          14 :     A = C? _RgXQM_mul(A0, C, P): A0;
    5424             :   }
    5425             :   else /* all needed coefs computed */
    5426          14 :     A = rowslice(A, 1, lim1-1);
    5427          28 :   if (*pC) C = C? _RgXQM_mul(*pC, C, P): *pC;
    5428             :   /* put back a0 */
    5429         119 :   for (i = 1; i < lA; i++) gel(A,i) = vec_prepend(gel(A,i), gen_0);
    5430          28 :   *pC = C; return A;
    5431             : }
    5432             : 
    5433             : static long
    5434         252 : mfstabitermod(GEN A, GEN vp, ulong chip, long p, ulong q)
    5435             : {
    5436         252 :   GEN Ap, C = NULL;
    5437         252 :   Ap = rowpermute(A, vp);
    5438         252 :   A = rowslice(A, 2, nbrows(Ap)+1);
    5439             :   while (1)
    5440          49 :   {
    5441         301 :     GEN Rp = shallowconcat(matTpmod(Ap, A, chip, p, q), A);
    5442         301 :     GEN B = Flm_ker(Rp, q);
    5443         301 :     long lA = lg(A), lB = lg(B);
    5444         301 :     if (lB == 1) return 0;
    5445         266 :     if (lB == lA) return lA-1;
    5446          49 :     B = rowslice(B, 1, lA-1);
    5447          49 :     Ap = Flm_mul(Ap, B, q);
    5448          49 :     A = Flm_mul(A, B, q);
    5449          49 :     C = C? Flm_mul(C, B, q): B;
    5450             :   }
    5451             : }
    5452             : 
    5453             : static GEN
    5454         595 : mfcharinv_i(GEN CHI)
    5455             : {
    5456         595 :   GEN G = gel(CHI,1);
    5457         595 :   CHI = leafcopy(CHI); gel(CHI,2) =  zncharconj(G, gel(CHI,2)); return CHI;
    5458             : }
    5459             : 
    5460             : /* upper bound dim S_1(Gamma_0(N),chi) performing the linear algebra mod p */
    5461             : static long
    5462         595 : mf1dimmod(GEN E1, GEN E, GEN chip, long ordchi, long dih, GEN TMP)
    5463             : {
    5464         595 :   GEN E1i, A, vp, mf, C = NULL;
    5465         595 :   ulong q, r = QabM_init(ordchi, &q);
    5466             :   long lim, LIM, p;
    5467             : 
    5468         595 :   LIM = gel(TMP,1)[2]; lim = gel(TMP,1)[1];
    5469         595 :   mf= gel(TMP,2);
    5470         595 :   A = gel(TMP,3);
    5471         595 :   A = QabM_to_Flm(A, r, q);
    5472         595 :   E1 = QabX_to_Flx(E1, r, q);
    5473         595 :   E1i = Flxn_inv(E1, nbrows(A), q);
    5474         595 :   if (E)
    5475             :   {
    5476         574 :     GEN Iden = gel(TMP,5), I = gel(Iden,1), den = gel(Iden,2);
    5477         574 :     GEN Mindex = MF_get_Mindex(mf), F = rowslice(A, 1, LIM);
    5478         574 :     GEN E1ip = Flxn_red(E1i, LIM);
    5479         574 :     ulong d = den? umodiu(den, q): 1;
    5480         574 :     long i, nE = lg(E) - 1;
    5481             :     pari_sp av;
    5482             : 
    5483         574 :     I = ZM_to_Flm(I, q);
    5484         574 :     if (d != 1) I = Flm_Fl_mul(I, Fl_inv(d, q), q);
    5485         574 :     av = avma;
    5486        1120 :     for (i = 1; i <= nE; i++)
    5487             :     {
    5488         889 :       GEN e = Flxn_mul(E1ip, QabX_to_Flx(gel(E,i), r, q), LIM, q);
    5489         889 :       GEN B = mfmatsermul_Fl(F, e, q), z;
    5490         889 :       GEN B2 = Flm_mul(I, rowpermute(B, Mindex), q);
    5491         889 :       B = rowslice(B, lim+1,LIM);
    5492         889 :       z = Flm_ker(Flm_sub(B2, B, q), q);
    5493         889 :       if (lg(z)-1 == dih) return dih;
    5494         546 :       C = C? Flm_mul(C, z, q): z;
    5495         546 :       F = Flm_mul(F, z, q);
    5496         546 :       gerepileall(av, 2, &F,&C);
    5497             :     }
    5498         231 :     A = F;
    5499             :   }
    5500             :   /* use Schaeffer */
    5501         252 :   p = gel(TMP,1)[4]; vp = gel(TMP,4);
    5502         252 :   A = mfmatsermul_Fl(A, E1i, q);
    5503         252 :   return mfstabitermod(A, vp, Qab_to_Fl(chip, r, q), p, q);
    5504             : }
    5505             : 
    5506             : static GEN
    5507         224 : mf1intermat(GEN A, GEN Mindex, GEN e, GEN Iden, long lim, GEN POLCYC)
    5508             : {
    5509         224 :   long j, l = lg(A), LIM = nbrows(A);
    5510         224 :   GEN I = gel(Iden,1), den = gel(Iden,2), B = cgetg(l, t_MAT);
    5511             : 
    5512        5257 :   for (j = 1; j < l; j++)
    5513             :   {
    5514        5033 :     pari_sp av = avma;
    5515        5033 :     GEN c = RgV_to_RgX(gel(A,j), 0), c1, c2;
    5516        5033 :     c = RgX_to_RgC(RgXn_mul(c, e, LIM), LIM);
    5517        5033 :     if (POLCYC) c = liftpol_shallow(c);
    5518        5033 :     c1 = vecslice(c, lim+1, LIM);
    5519        5033 :     if (den) c1 = RgC_Rg_mul(c1, den);
    5520        5033 :     c2 = RgM_RgC_mul(I, vecpermute(c, Mindex));
    5521        5033 :     gel(B, j) = gerepileupto(av, RgC_sub(c2, c1));
    5522             :   }
    5523         224 :   return B;
    5524             : }
    5525             : /* Compute the full S_1(\G_0(N),\chi); return NULL if space is empty; else
    5526             :  * if pS is NULL, return stoi(dim), where dim is the dimension; else *pS is
    5527             :  * set to a vector of forms making up a basis, and return the matrix of their
    5528             :  * Fourier expansions. pdih gives the dimension of the subspace generated by
    5529             :  * dihedral forms; TMP is from mf1_pre or NULL. */
    5530             : static GEN
    5531       11284 : mf1basis(long N, GEN CHI, GEN TMP, GEN vSP, GEN *pS, long *pdih)
    5532             : {
    5533       11284 :   GEN E = NULL, EB, E1, E1i, dE1i, mf, A, C, POLCYC, DIH, Minv, chip;
    5534       11284 :   long nE = 0, p, LIM, lim, lim1, i, lA, dimp, ordchi, dih;
    5535             :   pari_timer tt;
    5536             :   pari_sp av;
    5537             : 
    5538       11284 :   if (pdih) *pdih = 0;
    5539       11284 :   if (pS) *pS = NULL;
    5540       11284 :   if (wt1empty(N) || mfcharparity(CHI) != -1) return NULL;
    5541       10990 :   ordchi = mfcharorder(CHI);
    5542       10990 :   if (uisprime(N) && ordchi > 4) return NULL;
    5543       10962 :   if (pS)
    5544             :   {
    5545        3857 :     DIH = mfdihedralcusp(N, CHI, vSP);
    5546        3857 :     dih = lg(DIH) - 1;
    5547             :   }
    5548             :   else
    5549             :   {
    5550        7105 :     DIH = NULL;
    5551        7105 :     dih = mfdihedralcuspdim(N, CHI, vSP);
    5552             :   }
    5553       10962 :   POLCYC = (ordchi <= 2)? NULL: mfcharpol(CHI);
    5554       10962 :   if (pdih) *pdih = dih;
    5555       10962 :   if (N <= 600) switch(N)
    5556             :   {
    5557             :     long m;
    5558         126 :     case 219: case 273: case 283: case 331: case 333: case 344: case 416:
    5559             :     case 438: case 468: case 491: case 504: case 546: case 553: case 563:
    5560             :     case 566: case 581: case 592:
    5561         126 :       break; /* one chi with both exotic and dihedral forms */
    5562        9499 :     default: /* only dihedral forms */
    5563        9499 :       if (!dih) return NULL;
    5564             :       /* fall through */
    5565             :     case 124: case 133: case 148: case 171: case 201: case 209: case 224:
    5566             :     case 229: case 248: case 261: case 266: case 288: case 296: case 301:
    5567             :     case 309: case 325: case 342: case 371: case 372: case 380: case 399:
    5568             :     case 402: case 403: case 404: case 408: case 418: case 432: case 444:
    5569             :     case 448: case 451: case 453: case 458: case 496: case 497: case 513:
    5570             :     case 522: case 527: case 532: case 576: case 579:
    5571             :       /* no chi with both exotic and dihedral; one chi with exotic forms */
    5572        3248 :       if (dih)
    5573             :       {
    5574        2338 :         if (!pS) return utoipos(dih);
    5575         728 :         return mftreatdihedral(N, DIH, POLCYC, ordchi, pS) ;
    5576             :       }
    5577         910 :       m = mfcharno(mfcharinduce(CHI,N));
    5578         910 :       if (N == 124 && (m != 67 && m != 87)) return NULL;
    5579         784 :       if (N == 133 && (m != 83 && m !=125)) return NULL;
    5580         490 :       if (N == 148 && (m !=105 && m !=117)) return NULL;
    5581         364 :       if (N == 171 && (m != 94 && m !=151)) return NULL;
    5582         364 :       if (N == 201 && (m != 29 && m !=104)) return NULL;
    5583         364 :       if (N == 209 && (m != 87 && m !=197)) return NULL;
    5584         364 :       if (N == 224 && (m != 95 && m !=191)) return NULL;
    5585         364 :       if (N == 229 && (m !=107 && m !=122)) return NULL;
    5586         364 :       if (N == 248 && (m != 87 && m !=191)) return NULL;
    5587         273 :       if (N == 261 && (m != 46 && m !=244)) return NULL;
    5588         273 :       if (N == 266 && (m != 83 && m !=125)) return NULL;
    5589         273 :       if (N == 288 && (m != 31 && m !=223)) return NULL;
    5590         273 :       if (N == 296 && (m !=105 && m !=265)) return NULL;
    5591             :   }
    5592         595 :   if (DEBUGLEVEL)
    5593           0 :     err_printf("mf1basis: start character %Ps, conductor = %ld, order = %ld\n",
    5594             :                gmfcharno(CHI), mfcharconductor(CHI), ordchi);
    5595         595 :   if (!TMP) TMP = mf1_pre(N);
    5596         595 :   lim = gel(TMP,1)[1]; LIM = gel(TMP,1)[2]; lim1 = gel(TMP,1)[3];
    5597         595 :   p = gel(TMP,1)[4];
    5598         595 :   mf  = gel(TMP,2);
    5599         595 :   A   = gel(TMP,3);
    5600         595 :   EB = mfeisensteinbasis(N, 1, mfcharinv_i(CHI));
    5601         595 :   nE = lg(EB) - 1;
    5602         595 :   E1 = RgV_to_RgX(mftocol(gel(EB,1), LIM-1, 1), 0); /* + O(x^LIM) */
    5603         595 :   if (--nE)
    5604         574 :     E = RgM_to_RgXV(mfvectomat(vecslice(EB, 2, nE+1), LIM-1, 1), 0);
    5605         595 :   chip = mfchareval(CHI, p); /* != 0 */
    5606         595 :   if (DEBUGLEVEL) timer_start(&tt);
    5607         595 :   av = avma; dimp = mf1dimmod(E1, E, chip, ordchi, dih, TMP);
    5608         595 :   set_avma(av);
    5609         595 :   if (DEBUGLEVEL) timer_printf(&tt, "mf1basis: dim mod p is %ld", dimp);
    5610         595 :   if (!dimp) return NULL;
    5611         280 :   if (!pS) return utoi(dimp);
    5612         224 :   if (dimp == dih) return mftreatdihedral(N, DIH, POLCYC, ordchi, pS);
    5613         168 :   E1i = RgXn_inv(E1, LIM); /* E[1] does not vanish at oo */
    5614         168 :   if (POLCYC) E1i = liftpol_shallow(E1i);
    5615         168 :   E1i = Q_remove_denom(E1i, &dE1i);
    5616         168 :   if (DEBUGLEVEL)
    5617             :   {
    5618           0 :     GEN a0 = gel(E1,2);
    5619           0 :     if (typ(a0) == t_POLMOD) a0 = gnorm(a0);
    5620           0 :     a0 = Q_abs_shallow(a0);
    5621           0 :     timer_printf(&tt, "mf1basis: invert E; norm(a0(E)) = %Ps", a0);
    5622             :   }
    5623         168 :   C = NULL;
    5624         168 :   if (nE)
    5625             :   { /* mf attached to S2(N), fi = mfbasis(mf)
    5626             :      * M = coefs(f1,...,fd) up to LIM
    5627             :      * F = coefs(F1,...,FD) = M * C, for some matrix C over Q(chi),
    5628             :      * initially 1, eventually giving \cap_E S2 / E; D <= d.
    5629             :      * B = coefs(E/E1 F1, .., E/E1 FD); we want X in Q(CHI)^d and
    5630             :      * Y in Q(CHI)^D such that
    5631             :      *   B * X = M * Y, i.e. Minv * rowpermute(B, Mindex * X) = Y
    5632             :      *(B  - I * rowpermute(B, Mindex)) * X = 0.
    5633             :      * where I = M * Minv. Rows of (B - I * ...) are 0 up to lim so
    5634             :      * are not included */
    5635         154 :     GEN Mindex = MF_get_Mindex(mf), Iden  = gel(TMP,5);
    5636             :     pari_timer TT;
    5637         154 :     pari_sp av = avma;
    5638         154 :     if (DEBUGLEVEL) timer_start(&TT);
    5639         238 :     for (i = 1; i <= nE; i++)
    5640             :     {
    5641         224 :       pari_sp av2 = avma;
    5642             :       GEN e, z, B;
    5643             : 
    5644         224 :       e = Q_primpart(RgXn_mul(E1i, gel(E,i), LIM));
    5645         224 :       if (DEBUGLEVEL) timer_printf(&TT, "mf1basis: E[%ld] / E[1]", i+1);
    5646             :       /* the first time A is over Z and it is more efficient to lift than
    5647             :          * to let RgXn_mul use Kronecker's trick */
    5648         224 :       if (POLCYC && i == 1) e = liftpol_shallow(e);
    5649         224 :       B = mf1intermat(A, Mindex, e, Iden, lim, i == 1? NULL: POLCYC);
    5650         224 :       if (DEBUGLEVEL) timer_printf(&TT, "mf1basis: ... intermat");
    5651         224 :       z = gerepileupto(av2, QabM_ker(B, POLCYC, ordchi));
    5652         224 :       if (DEBUGLEVEL)
    5653           0 :         timer_printf(&TT, "mf1basis: ... kernel (dim %ld)",lg(z)-1);
    5654         224 :       if (lg(z) == 1) return NULL;
    5655         224 :       if (lg(z) == lg(A)) { set_avma(av2); continue; } /* no progress */
    5656         224 :       C = C? _RgXQM_mul(C, z, POLCYC): z;
    5657         224 :       A = _RgXQM_mul(A, z, POLCYC);
    5658         224 :       if (DEBUGLEVEL) timer_printf(&TT, "mf1basis: ... updates");
    5659         224 :       if (lg(z)-1 == dimp) break;
    5660          84 :       if (gc_needed(av, 1))
    5661             :       {
    5662           0 :         if (DEBUGMEM > 1) pari_warn(warnmem,"mf1basis i = %ld", i);
    5663           0 :         gerepileall(av, 2, &A, &C);
    5664             :       }
    5665             :     }
    5666         154 :     if (DEBUGLEVEL) timer_printf(&tt, "mf1basis: intersection [total]");
    5667             :   }
    5668         168 :   lA = lg(A);
    5669         168 :   if (lA-1 == dimp)
    5670             :   {
    5671         140 :     A = mfmatsermul(rowslice(A, 1, lim1), E1i);
    5672         140 :     if (POLCYC) A = RgXQM_red(A, POLCYC);
    5673         140 :     if (DEBUGLEVEL) timer_printf(&tt, "mf1basis: matsermul [1]");
    5674             :   }
    5675             :   else
    5676             :   {
    5677          28 :     A = mfmatsermul(A, E1i);
    5678          28 :     if (POLCYC) A = RgXQM_red(A, POLCYC);
    5679          28 :     if (DEBUGLEVEL) timer_printf(&tt, "mf1basis: matsermul [2]");
    5680          28 :     A = mfstabiter(&C, A, chip, TMP, POLCYC, ordchi);
    5681          28 :     if (DEBUGLEVEL) timer_printf(&tt, "mf1basis: Hecke stability");
    5682          28 :     if (!A) return NULL;
    5683             :   }
    5684         168 :   if (dE1i) C = RgM_Rg_mul(C, dE1i);
    5685         168 :   if (POLCYC)
    5686             :   {
    5687         147 :     A = QXQM_to_mod_shallow(A, POLCYC);
    5688         147 :     C = QXQM_to_mod_shallow(C, POLCYC);
    5689             :   }
    5690         168 :   lA = lg(A);
    5691         581 :   for (i = 1; i < lA; i++)
    5692             :   {
    5693         413 :     GEN c, v = gel(A,i);
    5694         413 :     gel(A,i) = RgV_normalize(v, &c);
    5695         413 :     gel(C,i) = RgC_Rg_mul(gel(C,i), c);
    5696             :   }
    5697         168 :   Minv = gel(mfclean(A, POLCYC, ordchi, 0), 2);
    5698         168 :   A = RgM_Minv_mul(A, Minv);
    5699         168 :   C = RgM_Minv_mul(C, Minv);
    5700         168 :   *pS = vecmflineardiv0(MF_get_S(mf), C, gel(EB,1));
    5701         168 :   return A;
    5702             : }
    5703             : 
    5704             : static void
    5705         413 : MF_set_space(GEN mf, long x) { gmael(mf,1,4) = utoi(x); }
    5706             : static GEN
    5707         252 : mf1_cusptonew(GEN mf, GEN vSP)
    5708             : {
    5709         252 :   const long vy = 1;
    5710             :   long i, lP, dSnew, ct;
    5711         252 :   GEN vP, F, S, Snew, vF, v = split_ii(mf, 0, 0, vSP, &i);
    5712             : 
    5713         252 :   F = gel(v,1);
    5714         252 :   vP= gel(v,2); lP = lg(vP);
    5715         252 :   if (lP == 1) { obj_insert(mf, MF_SPLIT, v); return NULL; }
    5716         238 :   MF_set_space(mf, mf_NEW);
    5717         238 :   S = MF_get_S(mf);
    5718         238 :   dSnew = dim_sum(v);
    5719         238 :   Snew = cgetg(dSnew + 1, t_VEC); ct = 0;
    5720         238 :   vF = cgetg(lP, t_MAT);
    5721         546 :   for (i = 1; i < lP; i++)
    5722             :   {
    5723         308 :     GEN V, P = gel(vP,i), f = liftpol_shallow(gel(F,i));
    5724         308 :     long j, d = degpol(P);
    5725         308 :     gel(vF,i) = V = zerocol(dSnew);
    5726         308 :     if (d == 1)
    5727             :     {
    5728         140 :       gel(Snew, ct+1) = mflineardiv_linear(S, f, 0);
    5729         140 :       gel(V, ct+1) = gen_1;
    5730             :     }
    5731             :     else
    5732             :     {
    5733         168 :       f = RgXV_to_RgM(f,d);
    5734         511 :       for (j = 1; j <= d; j++)
    5735             :       {
    5736         343 :         gel(Snew, ct+j) = mflineardiv_linear(S, row(f,j), 0);
    5737         343 :         gel(V, ct+j) = mkpolmod(pol_xn(j-1,vy), P);
    5738             :       }
    5739             :     }
    5740         308 :     ct += d;
    5741             :   }
    5742         238 :   obj_insert(mf, MF_SPLIT, mkvec2(vF, vP));
    5743         238 :   gel(mf,3) = Snew; return mf;
    5744             : }
    5745             : static GEN
    5746        3969 : mf1init(long N, GEN CHI, GEN TMP, GEN vSP, long space, long flraw)
    5747             : {
    5748        3969 :   GEN mf, mf1, S, M = mf1basis(N, CHI, TMP, vSP, &S, NULL);
    5749        3969 :   if (!M) return NULL;
    5750         952 :   mf1 = mkvec4(stoi(N), gen_1, CHI, utoi(mf_CUSP));
    5751         952 :   mf = mkmf(mf1, cgetg(1,t_VEC), S, gen_0, NULL);
    5752         952 :   if (space == mf_NEW)
    5753             :   {
    5754         252 :     gel(mf,5) = mfcleanCHI(M,CHI, 0);
    5755         252 :     mf = mf1_cusptonew(mf, vSP); if (!mf) return NULL;
    5756         238 :     if (!flraw) M = mfcoefs_mf(mf, mfsturmNk(N,1)+1, 1);
    5757             :   }
    5758         938 :   gel(mf,5) = flraw? zerovec(3): mfcleanCHI(M, CHI, 0);
    5759         938 :   return mf;
    5760             : }
    5761             : 
    5762             : static GEN
    5763        1022 : mfEMPTY(GEN mf1)
    5764             : {
    5765        1022 :   GEN Minv = mkMinv(cgetg(1,t_MAT), NULL,NULL,NULL);
    5766        1022 :   GEN M = mkvec3(cgetg(1,t_VECSMALL), Minv, cgetg(1,t_MAT));
    5767        1022 :   return mkmf(mf1, cgetg(1,t_VEC), cgetg(1,t_VEC), cgetg(1,t_VEC), M);
    5768             : }
    5769             : static GEN
    5770         616 : mfEMPTYall(long N, GEN gk, GEN vCHI, long space)
    5771             : {
    5772             :   long i, l;
    5773             :   GEN v, gN, gs;
    5774         616 :   if (!vCHI) return cgetg(1, t_VEC);
    5775          14 :   gN = utoipos(N); gs = utoi(space);
    5776          14 :   l = lg(vCHI); v = cgetg(l, t_VEC);
    5777          42 :   for (i = 1; i < l; i++) gel(v,i) = mfEMPTY(mkvec4(gN,gk,gel(vCHI,i),gs));
    5778          14 :   return v;
    5779             : }
    5780             : 
    5781             : static GEN
    5782        3983 : fmt_dim(GEN CHI, long d, long dih)
    5783        3983 : { return mkvec4(gmfcharorder(CHI), gmfcharno(CHI), utoi(d), stoi(dih)); }
    5784             : /* merge two vector of fmt_dim's for the same vector of characters. If CHI
    5785             :  * is not NULL, remove dim-0 spaces and add character from CHI */
    5786             : static GEN
    5787           7 : merge_dims(GEN V, GEN W, GEN CHI)
    5788             : {
    5789           7 :   long i, j, id, l = lg(V);
    5790           7 :   GEN A = cgetg(l, t_VEC);
    5791           7 :   if (l == 1) return A;
    5792           7 :   id = CHI? 1: 3;
    5793          21 :   for (i = j = 1; i < l; i++)
    5794             :   {
    5795          14 :     GEN v = gel(V,i), w = gel(W,i);
    5796          14 :     long dv = itou(gel(v,id)), dvh = itou(gel(v,id+1)), d;
    5797          14 :     long dw = itou(gel(w,id)), dwh = itou(gel(w,id+1));
    5798          14 :     d = dv + dw;
    5799          14 :     if (d || CHI)
    5800          14 :       gel(A,j++) = CHI? fmt_dim(gel(CHI,i),d, dvh+dwh)
    5801          14 :                       : mkvec2s(d,dvh+dwh);
    5802             :   }
    5803           7 :   setlg(A, j); return A;
    5804             : }
    5805             : static GEN
    5806        3010 : mfdim0all(GEN w)
    5807             : {
    5808        3038 :   if (w) retconst_vec(lg(w)-1, zerovec(2));
    5809        3003 :   return cgetg(1,t_VEC);
    5810             : }
    5811             : static long
    5812        7315 : mf1cuspdim_i(long N, GEN CHI, GEN TMP, GEN vSP, long *dih)
    5813             : {
    5814        7315 :   pari_sp av = avma;
    5815        7315 :   GEN b = mf1basis(N, CHI, TMP, vSP, NULL, dih);
    5816        7315 :   return gc_long(av, b? itou(b): 0);
    5817             : }
    5818             : 
    5819             : static long
    5820         476 : mf1cuspdim(long N, GEN CHI, GEN vSP)
    5821             : {
    5822         476 :   if (!vSP) vSP = get_vDIH(N, divisorsNF(N, mfcharconductor(CHI)));
    5823         476 :   return mf1cuspdim_i(N, CHI, NULL, vSP, NULL);
    5824             : }
    5825             : static GEN
    5826        4144 : mf1cuspdimall(long N, GEN vCHI)
    5827             : {
    5828             :   GEN z, TMP, w, vSP;
    5829             :   long i, j, l;
    5830        4144 :   if (wt1empty(N)) return mfdim0all(vCHI);
    5831        1141 :   w = mf1chars(N,vCHI);
    5832        1141 :   l = lg(w); if (l == 1) return cgetg(1,t_VEC);
    5833        1141 :   z = cgetg(l, t_VEC);
    5834        1141 :   TMP = mf1_pre(N); vSP = get_vDIH(N, NULL);
    5835        7861 :   for (i = j = 1; i < l; i++)
    5836             :   {
    5837        6720 :     GEN CHI = gel(w,i);
    5838        6720 :     long dih, d = mf1cuspdim_i(N, CHI, TMP, vSP, &dih);
    5839        6720 :     if (vCHI)
    5840          42 :       gel(z,j++) = mkvec2s(d, dih);
    5841        6678 :     else if (d)
    5842        1428 :       gel(z,j++) = fmt_dim(CHI, d, dih);
    5843             :   }
    5844        1141 :   setlg(z,j); return z;
    5845             : }
    5846             : 
    5847             : /* dimension of S_1(Gamma_1(N)) */
    5848             : static long
    5849        4123 : mf1cuspdimsum(long N)
    5850             : {
    5851        4123 :   pari_sp av = avma;
    5852        4123 :   GEN v = mf1cuspdimall(N, NULL);
    5853        4123 :   long i, ct = 0, l = lg(v);
    5854        5544 :   for (i = 1; i < l; i++)
    5855             :   {
    5856        1421 :     GEN w = gel(v,i); /* [ord(CHI),*,dim,*] */
    5857        1421 :     ct += itou(gel(w,3))*myeulerphiu(itou(gel(w,1)));
    5858             :   }
    5859        4123 :   return gc_long(av,ct);
    5860             : }
    5861             : 
    5862             : static GEN
    5863          56 : mf1newdimall(long N, GEN vCHI)
    5864             : {
    5865             :   GEN z, w, vTMP, vSP, fa, P, E;
    5866             :   long i, c, l, lw, P1;
    5867          56 :   if (wt1empty(N)) return mfdim0all(vCHI);
    5868          56 :   w = mf1chars(N,vCHI);
    5869          56 :   lw = lg(w); if (lw == 1) return cgetg(1,t_VEC);
    5870          56 :   vTMP = const_vec(N, NULL);
    5871          56 :   vSP = get_vDIH(N, NULL);
    5872          56 :   gel(vTMP,N) = mf1_pre(N);
    5873             :   /* if p || N and p \nmid F(CHI), S_1^new(G0(N),chi) = 0 */
    5874          56 :   fa = znstar_get_faN(gmael(w,1,1));
    5875          56 :   P = gel(fa,1); l = lg(P);
    5876          56 :   E = gel(fa,2);
    5877         154 :   for (i = P1 = 1; i < l; i++)
    5878          98 :     if (E[i] == 1) P1 *= itou(gel(P,i));
    5879             :   /* P1 = \prod_{v_p(N) = 1} p */
    5880          56 :   z = cgetg(lw, t_VEC);
    5881         182 :   for (i = c = 1; i < lw; i++)
    5882             :   {
    5883             :     long S, j, l, F, dihnew;
    5884         126 :     GEN D, CHI = gel(w,i), CHIP = mfchartoprimitive(CHI,&F);
    5885             : 
    5886         126 :     S = F % P1? 0: mf1cuspdim_i(N, CHI, gel(vTMP,N), vSP, &dihnew);
    5887         126 :     if (!S)
    5888             :     {
    5889          56 :       if (vCHI) gel(z, c++) = zerovec(2);
    5890          56 :       continue;
    5891             :     }
    5892          70 :     D = mydivisorsu(N/F); l = lg(D);
    5893          77 :     for (j = l-2; j > 0; j--) /* skip last M = N */
    5894             :     {
    5895           7 :       long M = D[j]*F, m, s, dih;
    5896           7 :       GEN TMP = gel(vTMP,M);
    5897           7 :       if (wt1empty(M) || !(m = mubeta(D[l-j]))) continue; /*m = mubeta(N/M)*/
    5898           7 :       if (!TMP) gel(vTMP,M) = TMP = mf1_pre(M);
    5899           7 :       s = mf1cuspdim_i(M, CHIP, TMP, vSP, &dih);
    5900           7 :       if (s) { S += m * s; dihnew += m * dih; }
    5901             :     }
    5902          70 :     if (vCHI)
    5903          63 :       gel(z,c++) = mkvec2s(S, dihnew);
    5904           7 :     else if (S)
    5905           7 :       gel(z, c++) = fmt_dim(CHI, S, dihnew);
    5906             :   }
    5907          56 :   setlg(z,c); return z;
    5908             : }
    5909             : 
    5910             : static GEN
    5911          28 : mf1olddimall(long N, GEN vCHI)
    5912             : {
    5913             :   long i, j, l;
    5914             :   GEN z, w;
    5915          28 :   if (wt1empty(N)) return mfdim0all(vCHI);
    5916          28 :   w = mf1chars(N,vCHI);
    5917          28 :   l = lg(w); z = cgetg(l, t_VEC);
    5918          84 :   for (i = j = 1; i < l; i++)
    5919             :   {
    5920          56 :     GEN CHI = gel(w,i);
    5921          56 :     long d = mfolddim(N, 1, CHI);
    5922          56 :     if (vCHI)
    5923          28 :       gel(z,j++) = mkvec2s(d,d?-1:0);
    5924          28 :     else if (d)
    5925           7 :       gel(z, j++) = fmt_dim(CHI, d, -1);
    5926             :   }
    5927          28 :   setlg(z,j); return z;
    5928             : }
    5929             : 
    5930             : static long
    5931         469 : mf1olddimsum(long N)
    5932             : {
    5933             :   GEN D;
    5934         469 :   long N2, i, l, S = 0;
    5935         469 :   newd_params(N, &N2); /* will ensure mubeta != 0 */
    5936         469 :   D = mydivisorsu(N/N2); l = lg(D);
    5937        2485 :   for (i = 2; i < l; i++)
    5938             :   {
    5939        2016 :     long M = D[l-i]*N2, d = mf1cuspdimsum(M);
    5940        2016 :     if (d) S -= mubeta(D[i]) * d;
    5941             :   }
    5942         469 :   return S;
    5943             : }
    5944             : static long
    5945        1050 : mf1newdimsum(long N)
    5946             : {
    5947        1050 :   long S = mf1cuspdimsum(N);
    5948        1050 :   return S? S - mf1olddimsum(N): 0;
    5949             : }
    5950             : 
    5951             : /* return the automorphism of a degree-2 nf */
    5952             : static GEN
    5953        5768 : nf2_get_conj(GEN nf)
    5954             : {
    5955        5768 :   GEN pol = nf_get_pol(nf);
    5956        5768 :   return deg1pol_shallow(gen_m1, negi(gel(pol,3)), varn(pol));
    5957             : }
    5958             : static int
    5959          42 : foo_stable(GEN foo)
    5960          42 : { return lg(foo) != 3 || equalii(gel(foo,1), gel(foo,2)); }
    5961             : 
    5962             : static long
    5963         224 : mfisdihedral(GEN vF, GEN DIH)
    5964             : {
    5965         224 :   GEN vG = gel(DIH,1), M = gel(DIH,2), v, G, bnr, w, gen, D, f, nf, tau;
    5966         224 :   GEN bnr0 = NULL, f0, f0b, xin, foo;
    5967             :   long i, l, e, j, L, n;
    5968         224 :   if (lg(M) == 1) return 0;
    5969          42 :   v = RgM_RgC_invimage(M, vF);
    5970          42 :   if (!v) return 0;
    5971          42 :   l = lg(v);
    5972          42 :   for (i = 1; i < l; i++)
    5973          42 :     if (!gequal0(gel(v,i))) break;
    5974          42 :   if (i == l) return 0;
    5975          42 :   G = gel(vG,i);
    5976          42 :   bnr = gel(G,2); D = cyc_get_expo(bnr_get_cyc(bnr));
    5977          42 :   w = gel(G,3);
    5978          42 :   f = bnr_get_mod(bnr);
    5979          42 :   nf = bnr_get_nf(bnr);
    5980          42 :   tau = nf2_get_conj(nf);
    5981          42 :   f0 = gel(f,1); foo = gel(f,2);
    5982          42 :   f0b = galoisapply(nf, tau, f0);
    5983          42 :   xin = zv_to_ZV(gel(w,2)); /* xi(bnr.gen[i]) = e(xin[i] / D) */
    5984          42 :   if (!foo_stable(foo)) { foo = mkvec2(gen_1, gen_1); bnr0 = bnr; }
    5985          42 :   if (!gequal(f0, f0b))
    5986             :   {
    5987          21 :     f0 = idealmul(nf, f0, idealdivexact(nf, f0b, idealadd(nf, f0, f0b)));
    5988          21 :     bnr0 = bnr;
    5989             :   }
    5990          42 :   if (bnr0)
    5991             :   { /* conductor not ambiguous */
    5992             :     GEN S;
    5993          28 :     bnr = Buchray(bnr_get_bnf(bnr), mkvec2(f0, foo), nf_INIT | nf_GEN);
    5994          28 :     S = bnrsurjection(bnr, bnr0);
    5995          28 :     xin = FpV_red(RgV_RgM_mul(xin, gel(S,1)), D);
    5996             :     /* still xi(gen[i]) = e(xin[i] / D), for the new generators; D stays
    5997             :      * the same, not exponent(bnr.cyc) ! */
    5998             :   }
    5999          42 :   gen = bnr_get_gen(bnr); L = lg(gen);
    6000          77 :   for (j = 1, e = itou(D); j < L; j++)
    6001             :   {
    6002          63 :     GEN Ng = idealnorm(nf, gel(gen,j));
    6003          63 :     GEN a = shifti(gel(xin,j), 1); /* xi(g_j^2) = e(a/D) */
    6004          63 :     GEN b = FpV_dotproduct(xin, isprincipalray(bnr,Ng), D);
    6005          63 :     GEN m = Fp_sub(a, b, D); /* xi(g_j/g_j^\tau) = e(m/D) */
    6006          63 :     e = ugcd(e, itou(m)); if (e == 1) break;
    6007             :   }
    6008          42 :   n = itou(D) / e;
    6009          42 :   return n == 1? 4: 2*n;
    6010             : }
    6011             : 
    6012             : static ulong
    6013         119 : myradicalu(ulong n) { return zv_prod(gel(myfactoru(n),1)); }
    6014             : 
    6015             : /* list of fundamental discriminants unramified outside N, with sign s
    6016             :  * [s = 0 => no sign condition] */
    6017             : static GEN
    6018         119 : mfunram(long N, long s)
    6019             : {
    6020         119 :   long cN = myradicalu(N >> vals(N)), p = 1, m = 1, l, c, i;
    6021         119 :   GEN D = mydivisorsu(cN), res;
    6022         119 :   l = lg(D);
    6023         119 :   if (s == 1) m = 0; else if (s == -1) p = 0;
    6024         119 :   res = cgetg(6*l - 5, t_VECSMALL);
    6025         119 :   c = 1;
    6026         119 :   if (!odd(N))
    6027             :   { /* d = 1 */
    6028          56 :     if (p) res[c++] = 8;
    6029          56 :     if (m) { res[c++] =-8; res[c++] =-4; }
    6030             :   }
    6031         364 :   for (i = 2; i < l; i++)
    6032             :   { /* skip d = 1, done above */
    6033         245 :     long d = D[i], d4 = d & 3L; /* d odd, squarefree, d4 = 1 or 3 */
    6034         245 :     if (d4 == 1) { if (p) res[c++] = d; }
    6035         182 :     else         { if (m) res[c++] =-d; }
    6036         245 :     if (!odd(N))
    6037             :     {
    6038          56 :       if (p) { res[c++] = 8*d; if (d4 == 3) res[c++] = 4*d; }
    6039          56 :       if (m) { res[c++] =-8*d; if (d4 == 1) res[c++] =-4*d; }
    6040             :     }
    6041             :   }
    6042         119 :   setlg(res, c); return res;
    6043             : }
    6044             : 
    6045             : /* Return 1 if F is definitely not S4 type; return 0 on failure. */
    6046             : static long
    6047         105 : mfisnotS4(long N, GEN w)
    6048             : {
    6049         105 :   GEN D = mfunram(N, 0);
    6050         105 :   long i, lD = lg(D), lw = lg(w);
    6051         616 :   for (i = 1; i < lD; i++)
    6052             :   {
    6053         511 :     long p, d = D[i], ok = 0;
    6054        1442 :     for (p = 2; p < lw; p++)
    6055        1442 :       if (w[p] && kross(d,p) == -1) { ok = 1; break; }
    6056         511 :     if (!ok) return 0;
    6057             :   }
    6058         105 :   return 1;
    6059             : }
    6060             : 
    6061             : /* Return 1 if Q(sqrt(5)) \not\subset Q(F), i.e. F is definitely not A5 type;
    6062             :  * return 0 on failure. */
    6063             : static long
    6064         105 : mfisnotA5(GEN F)
    6065             : {
    6066         105 :   GEN CHI = mf_get_CHI(F), P = mfcharpol(CHI), T, Q;
    6067             : 
    6068         105 :   if (mfcharorder(CHI) % 5 == 0) return 0;
    6069         105 :   T = mf_get_field(F); if (degpol(T) == 1) return 1;
    6070         105 :   if (degpol(P) > 1) T = rnfequation(P,T);
    6071         105 :   Q = gsubgs(pol_xn(2,varn(T)), 5);
    6072         105 :   return (typ(nfisincl(Q, T)) == t_INT);
    6073             : }
    6074             : 
    6075             : /* v[p+1]^2 / chi(p) - 2 = z + 1/z with z primitive root of unity of order n,
    6076             :  * return n */
    6077             : static long
    6078        6741 : mffindrootof1(GEN v, long p, GEN CHI)
    6079             : {
    6080        6741 :   GEN ap = gel(v,p+1), u0, u1, u1k, u2;
    6081        6741 :   long c = 1;
    6082        6741 :   if (gequal0(ap)) return 2;
    6083        5033 :   u0 = gen_2; u1k = u1 = gsubgs(gdiv(gsqr(ap), mfchareval(CHI, p)), 2);
    6084       14812 :   while (!gequalsg(2, liftpol_shallow(u1))) /* u1 = z^c + z^-c */
    6085             :   {
    6086        9779 :     u2 = gsub(gmul(u1k, u1), u0);
    6087        9779 :     u0 = u1; u1 = u2; c++;
    6088             :   }
    6089        5033 :   return c;
    6090             : }
    6091             : 
    6092             : /* we known that F is not dihedral */
    6093             : static long
    6094         182 : mfgaloistype_i(long N, GEN CHI, GEN F, GEN v)
    6095             : {
    6096             :   forprime_t iter;
    6097         182 :   long lim = lg(v)-2;
    6098         182 :   GEN w = zero_zv(lim);
    6099             :   pari_sp av;
    6100             :   ulong p;
    6101         182 :   u_forprime_init(&iter, 2, lim);
    6102         182 :   av = avma;
    6103        5292 :   while((p = u_forprime_next(&iter))) if (N%p) switch(mffindrootof1(v, p, CHI))
    6104             :   {
    6105        1400 :     case 1: case 2: continue;
    6106        3451 :     case 3: w[p] = 1; break;
    6107          70 :     case 4: return -24; /* S4 */
    6108           0 :     case 5: return -60; /* A5 */
    6109           7 :     default: pari_err_DOMAIN("mfgaloistype", "form", "not a",
    6110             :                              strtoGENstr("cuspidal eigenform"), F);
    6111           0 :     set_avma(av);
    6112             :   }
    6113         105 :   if (mfisnotS4(N,w) && mfisnotA5(F)) return -12; /* A4 */
    6114           0 :   return 0; /* FAILURE */
    6115             : }
    6116             : 
    6117             : static GEN
    6118         224 : mfgaloistype0(long N, GEN CHI, GEN F, GEN DIH, long lim)
    6119             : {
    6120         224 :   pari_sp av = avma;
    6121         224 :   GEN vF = mftocol(F, lim, 1);
    6122         224 :   long t = mfisdihedral(vF, DIH), bound;
    6123         224 :   if (t) return gc_stoi(av,t);
    6124         182 :   bound = maxss(200, 5*expu(N)*expu(N));
    6125             :   for(;;)
    6126             :   {
    6127         182 :     t = mfgaloistype_i(N, CHI, F, vF);
    6128         175 :     set_avma(av); if (t) return stoi(t);
    6129           0 :     if (lim > bound) return gen_0;
    6130           0 :     lim += lim >> 1;
    6131           0 :     vF = mfcoefs_i(F,lim,1);
    6132             :   }
    6133             : }
    6134             : 
    6135             : /* If f is NULL, give all the galoistypes, otherwise just for f */
    6136             : /* Return 0 to indicate failure; in this case the type is either -12 or -60,
    6137             :  * most likely -12. FIXME using the Galois representation. */
    6138             : GEN
    6139         231 : mfgaloistype(GEN NK, GEN f)
    6140             : {
    6141         231 :   pari_sp av = avma;
    6142         231 :   GEN CHI, T, F, DIH, SP, mf = checkMF_i(NK);
    6143             :   long N, k, lL, i, lim, SB;
    6144             : 
    6145         231 :   if (f && !checkmf_i(f)) pari_err_TYPE("mfgaloistype", f);
    6146         224 :   if (mf)
    6147             :   {
    6148         189 :     N = MF_get_N(mf);
    6149         189 :     k = MF_get_k(mf);
    6150         189 :     CHI = MF_get_CHI(mf);
    6151             :   }
    6152             :   else
    6153             :   {
    6154          35 :     checkNK(NK, &N, &k, &CHI, 0);
    6155          35 :     mf = f? NULL: mfinit_i(NK, mf_NEW);
    6156             :   }
    6157         224 :   if (k != 1) pari_err_DOMAIN("mfgaloistype", "k", "!=", gen_1, stoi(k));
    6158         224 :   SB = mf? mfsturm_mf(mf): mfsturmNk(N,1);
    6159         224 :   SP = get_DIH(N);
    6160         224 :   DIH = mfdihedralnew(N, CHI, SP);
    6161         224 :   lim = lg(DIH) == 1? 200: SB;
    6162         224 :   DIH = mkvec2(DIH, mfvectomat(DIH,SB,1));
    6163         224 :   if (f) return gerepileuptoint(av, mfgaloistype0(N,CHI, f, DIH, lim));
    6164         126 :   F = mfeigenbasis(mf); lL = lg(F);
    6165         126 :   T = cgetg(lL, t_VEC);
    6166         252 :   for (i=1; i < lL; i++) gel(T,i) = mfgaloistype0(N, CHI, gel(F,i), DIH, lim);
    6167         126 :   return gerepileupto(av, T);
    6168             : }
    6169             : 
    6170             : /******************************************************************/
    6171             : /*                   Find all dihedral forms.                     */
    6172             : /******************************************************************/
    6173             : /* lim >= 2 */
    6174             : static void
    6175          14 : consttabdihedral(long lim) { cache_set(cache_DIH, mfdihedralall(lim)); }
    6176             : 
    6177             : /* a ideal coprime to bnr modulus */
    6178             : static long
    6179      107611 : mfdiheval(GEN bnr, GEN w, GEN a)
    6180             : {
    6181      107611 :   GEN L, cycn = gel(w,1), chin = gel(w,2);
    6182      107611 :   long ordmax = cycn[1];
    6183      107611 :   L = ZV_to_Flv(isprincipalray(bnr,a), ordmax);
    6184      107611 :   return Flv_dotproduct(chin, L, ordmax);
    6185             : }
    6186             : 
    6187             : /* x(t^k) mod T = polcyclo(m), 0 <= k < m */
    6188             : static GEN
    6189       30331 : Galois(GEN x, long k, GEN T, long m)
    6190             : {
    6191             :   GEN B;
    6192             :   long i, ik, d;
    6193       30331 :   if (typ(x) != t_POL) return x;
    6194        7455 :   if (varn(x) != varn(T)) pari_APPLY_pol_normalized(Galois(gel(x,i), k, T, m));
    6195        7420 :   if ((d = degpol(x)) <= 0) return x;
    6196        7063 :   B = cgetg(m + 2, t_POL); B[1] = x[1]; gel(B,2) = gel(x,2);
    6197       61565 :   for (i = 1; i < m; i++) gel(B, i+2) = gen_0;
    6198       23940 :   for (i = 1, ik = k; i <= d; i++, ik = Fl_add(ik, k, m))
    6199       16877 :     gel(B, ik + 2) = gel(x, i+2);
    6200        7063 :   return QX_ZX_rem(normalizepol(B), T);
    6201             : }
    6202             : static GEN
    6203        1022 : vecGalois(GEN x, long k, GEN T, long m)
    6204       31332 : { pari_APPLY_same(Galois(gel(x,i), k, T, m)); }
    6205             : 
    6206             : static GEN
    6207      234178 : fix_pol(GEN S, GEN Pn, int *trace)
    6208             : {
    6209      234178 :   if (typ(S) != t_POL) return S;
    6210      118069 :   S = RgX_rem(S, Pn);
    6211      118069 :   if (typ(S) == t_POL)
    6212             :   {
    6213      118069 :     switch(lg(S))
    6214             :     {
    6215       45108 :       case 2: return gen_0;
    6216       20517 :       case 3: return gel(S,2);
    6217             :     }
    6218       52444 :     *trace = 1;
    6219             :   }
    6220       52444 :   return S;
    6221             : }
    6222             : 
    6223             : static GEN
    6224       13573 : dihan(GEN bnr, GEN w, GEN k0j, long m, ulong lim)
    6225             : {
    6226       13573 :   GEN nf = bnr_get_nf(bnr), f = bid_get_ideal(bnr_get_bid(bnr));
    6227       13573 :   GEN v = zerovec(lim+1), cycn = gel(w,1), Tinit = gel(w,3);
    6228       13573 :   GEN Pn = gel(Tinit,lg(Tinit)==4? 2: 1);
    6229       13573 :   long j, ordmax = cycn[1];
    6230       13573 :   long D = itos(nf_get_disc(nf)), vt = varn(Pn);
    6231       13573 :   int trace = 0;
    6232             :   ulong p, n;
    6233             :   forprime_t T;
    6234             : 
    6235       13573 :   if (!lim) return v;
    6236       13363 :   gel(v,2) = gen_1;
    6237       13363 :   u_forprime_init(&T, 2, lim);
    6238             :   /* fill in prime powers first */
    6239      116207 :   while ((p = u_forprime_next(&T)))
    6240             :   {
    6241             :     GEN vP, vchiP, S;
    6242             :     long k, lP;
    6243             :     ulong q, qk;
    6244      102844 :     if (kross(D,p) >= 0) q = p;
    6245       45192 :     else if (!(q = umuluu_le(p,p,lim))) continue;
    6246             :     /* q = Norm P */
    6247       65856 :     vP = idealprimedec(nf, utoipos(p));
    6248       65856 :     lP = lg(vP);
    6249       65856 :     vchiP = cgetg(lP, t_VECSMALL);
    6250      179081 :     for (j = k = 1; j < lP; j++)
    6251             :     {
    6252      113225 :       GEN P = gel(vP,j);
    6253      113225 :       if (!idealval(nf, f, P)) vchiP[k++] = mfdiheval(bnr,w,P);
    6254             :     }
    6255       65856 :     if (k == 1) continue;
    6256       62188 :     setlg(vchiP, k); lP = k;
    6257       62188 :     if (lP == 2)
    6258             :     { /* one prime above p not dividing f */
    6259       16765 :       long s, s0 = vchiP[1];
    6260       27069 :       for (qk=q, s = s0;; s = Fl_add(s,s0,ordmax))
    6261             :       {
    6262       27069 :         S = Qab_zeta(s, ordmax, vt);
    6263       27069 :         gel(v, qk+1) = fix_pol(S, Pn, &trace);
    6264       27069 :         if (!(qk = umuluu_le(qk,q,lim))) break;
    6265             :       }
    6266             :     }
    6267             :     else /* two primes above p not dividing f */
    6268             :     {
    6269       45423 :       long s, s0 = vchiP[1], s1 = vchiP[2];
    6270       45423 :       for (qk=q, k = 1;; k++)
    6271       18424 :       { /* sum over a,b s.t. Norm( P1^a P2^b ) = q^k, i.e. a+b = k */
    6272             :         long a;
    6273       63847 :         GEN S = gen_0;
    6274      220752 :         for (a = 0; a <= k; a++)
    6275             :         {
    6276      156905 :           s = Fl_add(Fl_mul(a, s0, ordmax), Fl_mul(k-a, s1, ordmax), ordmax);
    6277      156905 :           S = gadd(S, Qab_zeta(s, ordmax, vt));
    6278             :         }
    6279       63847 :         gel(v, qk+1) = fix_pol(S, Pn, &trace);
    6280       63847 :         if (!(qk = umuluu_le(qk,q,lim))) break;
    6281             :       }
    6282             :     }
    6283             :   }
    6284             :   /* complete with nonprime powers */
    6285      308098 :   for (n = 2; n <= lim; n++)
    6286             :   {
    6287      294735 :     GEN S, fa = myfactoru(n), P = gel(fa, 1), E = gel(fa, 2);
    6288             :     long q;
    6289      294735 :     if (lg(P) == 2) continue;
    6290             :     /* not a prime power */
    6291      143262 :     q = upowuu(P[1],E[1]);
    6292      143262 :     S = gmul(gel(v, q + 1), gel(v, n/q + 1));
    6293      143262 :     gel(v, n+1) = fix_pol(S, Pn, &trace);
    6294             :   }
    6295       13363 :   if (trace)
    6296             :   {
    6297        7154 :     long k0 = k0j[1], jdeg = k0j[2];
    6298        7154 :     v = QabV_tracerel(Tinit, jdeg, v); /* Apply Galois Mod(k0, ordw) */
    6299        7154 :     if (k0 > 1) v = vecGalois(v, k0, gel(Tinit,1), m);
    6300             :   }
    6301       13363 :   return v;
    6302             : }
    6303             : 
    6304             : /* as cyc_normalize for t_VECSMALL cyc */
    6305             : static GEN
    6306       26810 : cyc_normalize_zv(GEN cyc)
    6307             : {
    6308       26810 :   long i, o = cyc[1], l = lg(cyc); /* > 1 */
    6309       26810 :   GEN D = cgetg(l, t_VECSMALL);
    6310       31185 :   D[1] = o; for (i = 2; i < l; i++) D[i] = o / cyc[i];
    6311       26810 :   return D;
    6312             : }
    6313             : /* as char_normalize for t_VECSMALLs */
    6314             : static GEN
    6315      118517 : char_normalize_zv(GEN chi, GEN ncyc)
    6316             : {
    6317      118517 :   long i, l = lg(chi);
    6318      118517 :   GEN c = cgetg(l, t_VECSMALL);
    6319      118517 :   if (l > 1) {
    6320      118517 :     c[1] = chi[1];
    6321      160454 :     for (i = 2; i < l; i++) c[i] = chi[i] * ncyc[i];
    6322             :   }
    6323      118517 :   return c;
    6324             : }
    6325             : 
    6326             : static GEN
    6327        9331 : dihan_bnf(long D)
    6328             : {
    6329        9331 :   GEN c = getrand(), bnf;
    6330        9331 :   setrand(gen_1);
    6331        9331 :   bnf = Buchall(quadpoly_i(stoi(D)), nf_FORCE, LOWDEFAULTPREC);
    6332        9331 :   setrand(c);
    6333        9331 :   return bnf;
    6334             : }
    6335             : static GEN
    6336       37758 : dihan_bnr(GEN bnf, GEN A)
    6337             : {
    6338       37758 :   GEN c = getrand(), bnr;
    6339       37758 :   setrand(gen_1);
    6340       37758 :   bnr = Buchray(bnf, A, nf_INIT|nf_GEN);
    6341       37758 :   setrand(c);
    6342       37758 :   return bnr;
    6343             : }
    6344             : /* Hecke xi * (D/.) = Dirichlet chi, return v in Q^r st chi(g_i) = e(v[i]).
    6345             :  * cycn = cyc_normalize_zv(bnr.cyc), chin = char_normalize_zv(chi,cyc) */
    6346             : static GEN
    6347       34489 : bnrchartwist2conrey(GEN chin, GEN cycn, GEN bnrconreyN, GEN kroconreyN)
    6348             : {
    6349       34489 :   long l = lg(bnrconreyN), c1 = cycn[1], i;
    6350       34489 :   GEN v = cgetg(l, t_COL);
    6351      125363 :   for (i = 1; i < l; i++)
    6352             :   {
    6353       90874 :     GEN d = sstoQ(zv_dotproduct(chin, gel(bnrconreyN,i)), c1);
    6354       90874 :     if (kroconreyN[i] < 0) d = gadd(d, ghalf);
    6355       90874 :     gel(v,i) = d;
    6356             :   }
    6357       34489 :   return v;
    6358             : }
    6359             : 
    6360             : /* chi(g_i) = e(v[i]) denormalize wrt Conrey generators orders */
    6361             : static GEN
    6362       34489 : conreydenormalize(GEN znN, GEN v)
    6363             : {
    6364       34489 :   GEN gcyc = znstar_get_conreycyc(znN), w;
    6365       34489 :   long l = lg(v), i;
    6366       34489 :   w = cgetg(l, t_COL);
    6367      125363 :   for (i = 1; i < l; i++)
    6368       90874 :     gel(w,i) = modii(gmul(gel(v,i), gel(gcyc,i)), gel(gcyc,i));
    6369       34489 :   return w;
    6370             : }
    6371             : 
    6372             : static long
    6373       84028 : Miyake(GEN vchi, GEN gb, GEN cycn)
    6374             : {
    6375       84028 :   long i, e = cycn[1], lb = lg(gb);
    6376       84028 :   GEN v = char_normalize_zv(vchi, cycn);
    6377      124992 :   for (i = 1; i < lb; i++)
    6378      100268 :     if ((zv_dotproduct(v, gel(gb,i)) -  v[i]) % e) return 1;
    6379       24724 :   return 0;
    6380             : }
    6381             : 
    6382             : /* list of Hecke characters not induced by a Dirichlet character up to Galois
    6383             :  * conjugation, whose conductor is bnr.cond; cycn = cyc_normalize(bnr.cyc)*/
    6384             : static GEN
    6385       26810 : mklvchi(GEN bnr, GEN cycn, GEN gb)
    6386             : {
    6387       26810 :   GEN cyc = bnr_get_cyc(bnr), cycsmall = ZV_to_zv(cyc);
    6388       26810 :   GEN vchi = cyc2elts(cycsmall);
    6389       26810 :   long ordmax = cycsmall[1], c, i, l;
    6390       26810 :   l = lg(vchi);
    6391      304024 :   for (i = c = 1; i < l; i++)
    6392             :   {
    6393      277214 :     GEN chi = gel(vchi,i);
    6394      277214 :     if (!gb || Miyake(chi, gb, cycn)) gel(vchi, c++) = Flv_to_ZV(chi);
    6395             :   }
    6396       26810 :   setlg(vchi, c); l = c;
    6397      279300 :   for (i = 1; i < l; i++)
    6398             :   {
    6399      252490 :     GEN chi = gel(vchi,i);
    6400             :     long n;
    6401      252490 :     if (!chi) continue;
    6402     1055754 :     for (n = 2; n < ordmax; n++)
    6403      966476 :       if (ugcd(n, ordmax) == 1)
    6404             :       {
    6405      397670 :         GEN tmp = ZV_ZV_mod(gmulsg(n, chi), cyc);
    6406             :         long j;
    6407     7623539 :         for (j = i+1; j < l; j++)
    6408     7225869 :           if (gel(vchi,j) && gequal(gel(vchi,j), tmp)) gel(vchi,j) = NULL;
    6409             :       }
    6410             :   }
    6411      279300 :   for (i = c = 1; i < l; i++)
    6412             :   {
    6413      252490 :     GEN chi = gel(vchi,i);
    6414      252490 :     if (chi && bnrisconductor(bnr, chi)) gel(vchi, c++) = chi;
    6415             :   }
    6416       26810 :   setlg(vchi, c); return vchi;
    6417             : }
    6418             : 
    6419             : static GEN
    6420        7805 : get_gb(GEN bnr, GEN con)
    6421             : {
    6422        7805 :   GEN gb, g = bnr_get_gen(bnr), nf = bnr_get_nf(bnr);
    6423        7805 :   long i, l = lg(g);
    6424        7805 :   gb = cgetg(l, t_VEC);
    6425       18326 :   for (i = 1; i < l; i++)
    6426       10521 :     gel(gb,i) = ZV_to_zv(isprincipalray(bnr, galoisapply(nf, con, gel(g,i))));
    6427        7805 :   return gb;
    6428             : }
    6429             : static GEN
    6430       15862 : get_bnrconreyN(GEN bnr, GEN znN)
    6431             : {
    6432       15862 :   GEN z, g = znstar_get_conreygen(znN);
    6433       15862 :   long i, l = lg(g);
    6434       15862 :   z = cgetg(l, t_VEC);
    6435       57134 :   for (i = 1; i < l; i++) gel(z,i) = ZV_to_zv(isprincipalray(bnr,gel(g,i)));
    6436       15862 :   return z;
    6437             : }
    6438             : /* con = NULL if D > 0 or if D < 0 and id != idcon. */
    6439             : static GEN
    6440       33698 : mfdihedralcommon(GEN bnf, GEN id, GEN znN, GEN kroconreyN, long vt,
    6441             :                  long N, long D, GEN con)
    6442             : {
    6443       33698 :   GEN bnr = dihan_bnr(bnf, id), cyc = ZV_to_zv( bnr_get_cyc(bnr) );
    6444             :   GEN bnrconreyN, cycn, cycN, Lvchi, res, P, vT;
    6445             :   long j, ordmax, l, lc, deghecke;
    6446             : 
    6447       33698 :   lc = lg(cyc); if (lc == 1) return NULL;
    6448       26810 :   cycn = cyc_normalize_zv(cyc);
    6449       26810 :   Lvchi = mklvchi(bnr, cycn, con? get_gb(bnr, con): NULL);
    6450       26810 :   l = lg(Lvchi);
    6451       26810 :   if (l == 1) return NULL;
    6452             : 
    6453       15862 :   bnrconreyN = get_bnrconreyN(bnr, znN);
    6454       15862 :   cycN = ZV_to_zv(znstar_get_cyc(znN));
    6455       15862 :   ordmax = cyc[1];
    6456       15862 :   vT = const_vec(odd(ordmax)? ordmax << 1: ordmax, NULL);
    6457       15862 :   P = polcyclo(ordmax, vt);
    6458       15862 :   gel(vT,ordmax) = Qab_trace_init(ordmax, ordmax, P, P);
    6459       15862 :   deghecke = myeulerphiu(ordmax);
    6460       15862 :   res = cgetg(l, t_VEC);
    6461       50351 :   for (j = 1; j < l; j++)
    6462             :   {
    6463       34489 :     GEN T, v, vchi = ZV_to_zv(gel(Lvchi,j));
    6464       34489 :     GEN chi, chin = char_normalize_zv(vchi, cycn);
    6465             :     long o, vnum, k0, degrel;
    6466       34489 :     v = bnrchartwist2conrey(chin, cycn, bnrconreyN, kroconreyN);
    6467       34489 :     o = itou(Q_denom(v));
    6468       34489 :     T = gel(vT, o);
    6469       34489 :     if (!T) gel(vT,o) = T = Qab_trace_init(ordmax, o, P, polcyclo(o,vt));
    6470       34489 :     chi = conreydenormalize(znN, v);
    6471       34489 :     vnum = itou(znconreyexp(znN, chi));
    6472       34489 :     chi = ZV_to_zv(znconreychar(znN,chi));
    6473       34489 :     degrel = deghecke / degpol(gel(T,1));
    6474       34489 :     k0 = zv_cyc_minimize(cycN, chi, coprimes_zv(o));
    6475       34489 :     vnum = Fl_powu(vnum, k0, N);
    6476             :     /* encodes degrel forms: jdeg = 0..degrel-1 */
    6477       34489 :     gel(res,j) = mkvec3(mkvecsmalln(5, N, k0 % o, vnum, D, degrel),
    6478             :                         id, mkvec3(cycn,chin,T));
    6479             :   }
    6480       15862 :   return res;
    6481             : }
    6482             : 
    6483             : static long
    6484       49364 : is_cond(long D, long n)
    6485             : {
    6486       49364 :   if (D > 0) return n != 4 || (D&7L) == 1;
    6487       30114 :   return n != 2 && n != 3 && (n != 4 || (D&7L)!=1);
    6488             : }
    6489             : /* Append to v all dihedral weight 1 forms coming from D, if fundamental.
    6490             :  * level in [l1, l2] */
    6491             : static void
    6492       18718 : append_dihedral(GEN v, long D, long l1, long l2, long vt)
    6493             : {
    6494       18718 :   long Da = labs(D), no, i, numi, ct, min, max;
    6495             :   GEN bnf, con, vI, resall, arch1, arch2;
    6496             :   pari_sp av;
    6497             : 
    6498             :   /* min <= Nf <= max */
    6499       18718 :   max = l2 / Da;
    6500       18718 :   if (l1 == l2)
    6501             :   { /* assume Da | l2 */
    6502         140 :     min = max;
    6503         140 :     if (D > 0 && min < 3) return;
    6504             :   }
    6505             :   else /* assume l1 < l2 */
    6506       18578 :     min = (l1 + Da-1)/Da;
    6507       18718 :   if (!sisfundamental(D)) return;
    6508             : 
    6509        5726 :   av = avma;
    6510        5726 :   bnf = dihan_bnf(D);
    6511        5726 :   con = nf2_get_conj(bnf_get_nf(bnf));
    6512        5726 :   vI = ideallist(bnf, max);
    6513       55090 :   numi = 0; for (i = min; i <= max; i++) numi += lg(gel(vI, i)) - 1;
    6514        5726 :   if (D > 0)
    6515             :   {
    6516        1428 :     numi <<= 1;
    6517        1428 :     arch1 = mkvec2(gen_1,gen_0);
    6518        1428 :     arch2 = mkvec2(gen_0,gen_1);
    6519             :   }
    6520             :   else
    6521        4298 :     arch1 = arch2 = NULL;
    6522        5726 :   resall = cgetg(numi+1, t_VEC); ct = 1;
    6523       55090 :   for (no = min; no <= max; no++) if (is_cond(D, no))
    6524             :   {
    6525       44646 :     long N = Da*no, lc, lI;
    6526       44646 :     GEN I = gel(vI, no), znN = znstar0(utoipos(N), 1), conreyN, kroconreyN;
    6527             : 
    6528       44646 :     conreyN = znstar_get_conreygen(znN); lc = lg(conreyN);
    6529       44646 :     kroconreyN = cgetg(lc, t_VECSMALL);
    6530      166054 :     for (i = 1; i < lc; i++) kroconreyN[i] = krosi(D, gel(conreyN, i));
    6531       44646 :     lI = lg(I);
    6532       87822 :     for (i = 1; i < lI; i++)
    6533             :     {
    6534       43176 :       GEN id = gel(I, i), idcon, z;
    6535             :       long j;
    6536       43176 :       if (typ(id) == t_INT) continue;
    6537       28182 :       idcon = galoisapply(bnf, con, id);
    6538       51408 :       for (j = i; j < lI; j++)
    6539       51408 :         if (gequal(idcon, gel(I, j))) { gel(I, j) = gen_0; break; }
    6540       28182 :       if (D < 0)
    6541             :       {
    6542       17479 :         GEN conk = i == j ? con : NULL;
    6543       17479 :         z = mfdihedralcommon(bnf, id, znN, kroconreyN, vt, N, D, conk);
    6544       17479 :         if (z) gel(resall, ct++) = z;
    6545             :       }
    6546             :       else
    6547             :       {
    6548             :         GEN ide;
    6549       10703 :         ide = mkvec2(id, arch1);
    6550       10703 :         z = mfdihedralcommon(bnf, ide, znN, kroconreyN, vt, N, D, NULL);
    6551       10703 :         if (z) gel(resall, ct++) = z;
    6552       10703 :         if (gequal(idcon,id)) continue;
    6553        5516 :         ide = mkvec2(id, arch2);
    6554        5516 :         z = mfdihedralcommon(bnf, ide, znN, kroconreyN, vt, N, D, NULL);
    6555        5516 :         if (z) gel(resall, ct++) = z;
    6556             :       }
    6557             :     }
    6558             :   }
    6559        5726 :   if (ct == 1) set_avma(av);
    6560             :   else
    6561             :   {
    6562        4816 :     setlg(resall, ct);
    6563        4816 :     vectrunc_append(v, gerepilecopy(av, shallowconcat1(resall)));
    6564             :   }
    6565             : }
    6566             : 
    6567             : static long
    6568       42042 : di_N(GEN a) { return gel(a,1)[1]; }
    6569             : static GEN
    6570          14 : mfdihedral(long N)
    6571             : {
    6572          14 :   GEN D = mydivisorsu(N), res = vectrunc_init(2*N);
    6573          14 :   long j, l = lg(D), vt = fetch_user_var("t");
    6574         105 :   for (j = 2; j < l; j++)
    6575             :   { /* skip d = 1 */
    6576          91 :     long d = D[j];
    6577          91 :     if (d == 2) continue;
    6578          84 :     append_dihedral(res, -d, N,N, vt);
    6579          84 :     if (d >= 5 && D[l-j] >= 3) append_dihedral(res, d, N,N, vt);/* Nf >= 3 */
    6580             :   }
    6581          14 :   if (lg(res) > 1) res = shallowconcat1(res);
    6582          14 :   return res;
    6583             : }
    6584             : /* All primitive dihedral weight 1 forms of leven in [1, N], N > 1 */
    6585             : static GEN
    6586          14 : mfdihedralall(long N)
    6587             : {
    6588          14 :   GEN res = vectrunc_init(2*N), z;
    6589          14 :   long D, ct, i, vt = fetch_user_var("t");
    6590             : 
    6591       13986 :   for (D = -3; D >= -N; D--) append_dihedral(res, D, 1,N, vt);
    6592             :   /* Nf >= 3 (GTM 193, prop 3.3.18) */
    6593        4620 :   for (D = N / 3; D >= 5; D--) append_dihedral(res, D, 1,N, vt);
    6594          14 :   ct = lg(res);
    6595          14 :   if (ct > 1)
    6596             :   { /* sort wrt N */
    6597          14 :     res = shallowconcat1(res);
    6598          14 :     res = vecpermute(res, indexvecsort(res, mkvecsmall(1)));
    6599          14 :     ct = lg(res);
    6600             :   }
    6601          14 :   z = const_vec(N, cgetg(1,t_VEC));
    6602        7658 :   for (i = 1; i < ct;)
    6603             :   { /* regroup result sharing the same N */
    6604        7644 :     long n = di_N(gel(res,i)), j = i+1, k;
    6605             :     GEN v;
    6606       34412 :     while (j < ct && di_N(gel(res,j)) == n) j++;
    6607        7644 :     gel(z, n) = v = cgetg(j-i+1, t_VEC);
    6608       42056 :     for (k = 1; i < j; k++,i++) gel(v,k) = gel(res,i);
    6609             :   }
    6610          14 :   return z;
    6611             : }
    6612             : 
    6613             : /* return [vF, index], where vecpermute(vF,index) generates dihedral forms
    6614             :  * for character CHI */
    6615             : static GEN
    6616       24969 : mfdihedralnew_i(long N, GEN CHI, GEN SP)
    6617             : {
    6618             :   GEN bnf, Tinit, Pm, vf, M, V, NK;
    6619             :   long Dold, d, ordw, i, SB, c, l, k0, k1, chino, chinoorig, lv;
    6620             : 
    6621       24969 :   lv = lg(SP); if (lv == 1) return NULL;
    6622       12138 :   CHI = mfcharinduce(CHI,N);
    6623       12138 :   ordw = mfcharorder(CHI);
    6624       12138 :   chinoorig = mfcharno(CHI);
    6625       12138 :   k0 = mfconreyminimize(CHI);
    6626       12138 :   chino = Fl_powu(chinoorig, k0, N);
    6627       12138 :   k1 = Fl_inv(k0 % ordw, ordw);
    6628       12138 :   V = cgetg(lv, t_VEC);
    6629       12138 :   d = 0;
    6630       39039 :   for (i = l = 1; i < lv; i++)
    6631             :   {
    6632       26901 :     GEN sp = gel(SP,i), T = gel(sp,1);
    6633       26901 :     if (T[3] != chino) continue;
    6634        4060 :     d += T[5];
    6635        4060 :     if (k1 != 1)
    6636             :     {
    6637          77 :       GEN t = leafcopy(T);
    6638          77 :       t[3] = chinoorig;
    6639          77 :       t[2] = (t[2]*k1) % ordw;
    6640          77 :       sp = mkvec4(t, gel(sp,2), gel(sp,3), gel(sp,4));
    6641             :     }
    6642        4060 :     gel(V, l++) = sp;
    6643             :   }
    6644       12138 :   setlg(V, l); /* dihedral forms of level N and character CHI */
    6645       12138 :   if (l == 1) return NULL;
    6646             : 
    6647        2555 :   SB = mfsturmNk(N,1) + 1;
    6648        2555 :   M = cgetg(d+1, t_MAT);
    6649        2555 :   vf = cgetg(d+1, t_VEC);
    6650        2555 :   NK = mkNK(N, 1, CHI);
    6651        2555 :   bnf = NULL; Dold = 0;
    6652        6615 :   for (i = c = 1; i < l; i++)
    6653             :   { /* T = [N, k0, conreyno, D, degrel] */
    6654        4060 :     GEN bnr, Vi = gel(V,i), T = gel(Vi,1), id = gel(Vi,2), w = gel(Vi,3);
    6655        4060 :     long jdeg, k0i = T[2], D = T[4], degrel = T[5];
    6656             : 
    6657        4060 :     if (D != Dold) { Dold = D; bnf = dihan_bnf(D); }
    6658        4060 :     bnr = dihan_bnr(bnf, id);
    6659       12054 :     for (jdeg = 0; jdeg < degrel; jdeg++,c++)
    6660             :     {
    6661        7994 :       GEN k0j = mkvecsmall2(k0i, jdeg), an = dihan(bnr, w, k0j, ordw, SB);
    6662        7994 :       settyp(an, t_COL); gel(M,c) = an;
    6663        7994 :       gel(vf,c) = tag3(t_MF_DIHEDRAL, NK, bnr, w, k0j);
    6664             :     }
    6665             :   }
    6666        2555 :   Tinit = gmael3(V,1,3,3); Pm = gel(Tinit,1);
    6667        2555 :   V = QabM_indexrank(M, degpol(Pm)==1? NULL: Pm, ordw);
    6668        2555 :   return mkvec2(vf,gel(V,2));
    6669             : }
    6670             : static long
    6671       16149 : mfdihedralnewdim(long N, GEN CHI, GEN SP)
    6672             : {
    6673       16149 :   pari_sp av = avma;
    6674       16149 :   GEN S = mfdihedralnew_i(N, CHI, SP);
    6675       16149 :   return gc_long(av, S? lg(gel(S,2))-1: 0);
    6676             : }
    6677             : static GEN
    6678        8820 : mfdihedralnew(long N, GEN CHI, GEN SP)
    6679             : {
    6680        8820 :   pari_sp av = avma;
    6681        8820 :   GEN S = mfdihedralnew_i(N, CHI, SP);
    6682        8820 :   if (!S) { set_avma(av); return cgetg(1, t_VEC); }
    6683         917 :   return vecpermute(gel(S,1), gel(S,2));
    6684             : }
    6685             : 
    6686             : static long
    6687        7105 : mfdihedralcuspdim(long N, GEN CHI, GEN vSP)
    6688             : {
    6689        7105 :   pari_sp av = avma;
    6690             :   GEN D, CHIP;
    6691             :   long F, i, lD, dim;
    6692             : 
    6693        7105 :   CHIP = mfchartoprimitive(CHI, &F);
    6694        7105 :   D = mydivisorsu(N/F); lD = lg(D);
    6695        7105 :   dim = mfdihedralnewdim(N, CHI, gel(vSP,N)); /* d = 1 */
    6696       16149 :   for (i = 2; i < lD; i++)
    6697             :   {
    6698        9044 :     long d = D[i], a = mfdihedralnewdim(N/d, CHIP, gel(vSP, N/d));
    6699        9044 :     if (a) dim += a * mynumdivu(d);
    6700             :   }
    6701        7105 :   return gc_long(av,dim);
    6702             : }
    6703             : 
    6704             : static GEN
    6705        7343 : mfbdall(GEN E, long N)
    6706             : {
    6707        7343 :   GEN v, D = mydivisorsu(N);
    6708        7343 :   long i, j, nD = lg(D) - 1, nE = lg(E) - 1;
    6709        7343 :   v = cgetg(nD*nE + 1, t_VEC);
    6710       10416 :   for (j = 1; j <= nE; j++)
    6711             :   {
    6712        3073 :     GEN Ej = gel(E, j);
    6713        9415 :     for (i = 0; i < nD; i++) gel(v, i*nE + j) = mfbd_i(Ej, D[i+1]);
    6714             :   }
    6715        7343 :   return v;
    6716             : }
    6717             : static GEN
    6718        3857 : mfdihedralcusp(long N, GEN CHI, GEN vSP)
    6719             : {
    6720        3857 :   pari_sp av = avma;
    6721             :   GEN D, CHIP, z;
    6722             :   long F, i, lD;
    6723             : 
    6724        3857 :   CHIP = mfchartoprimitive(CHI, &F);
    6725        3857 :   D = mydivisorsu(N/F); lD = lg(D);
    6726        3857 :   z = cgetg(lD, t_VEC);
    6727        3857 :   gel(z,1) = mfdihedralnew(N, CHI, gel(vSP,N));
    6728        8596 :   for (i = 2; i < lD; i++) /* skip 1 */
    6729             :   {
    6730        4739 :     GEN LF = mfdihedralnew(N / D[i], CHIP, gel(vSP, N / D[i]));
    6731        4739 :     gel(z,i) = mfbdall(LF, D[i]);
    6732             :   }
    6733        3857 :   return gerepilecopy(av, shallowconcat1(z));
    6734             : }
    6735             : 
    6736             : /* used to decide between ratlift and comatrix for ZM_inv; ratlift is better
    6737             :  * when N has many divisors */
    6738             : static int
    6739        2548 : abundant(ulong N) { return mynumdivu(N) >= 8; }
    6740             : 
    6741             : /* CHI an mfchar */
    6742             : static int
    6743         371 : cmp_ord(void *E, GEN a, GEN b)
    6744             : {
    6745         371 :   GEN chia = MF_get_CHI(a), chib = MF_get_CHI(b);
    6746         371 :   (void)E; return cmpii(gmfcharorder(chia), gmfcharorder(chib));
    6747             : }
    6748             : /* mfinit structure.
    6749             : -- mf[1] contains [N,k,CHI,space],
    6750             : -- mf[2] contains vector of closures of Eisenstein series, empty if not
    6751             :    full space.
    6752             : -- mf[3] contains vector of closures, so #mf[3] = dimension of cusp/new space.
    6753             : -- mf[4] contains the corresponding indices: either j for T(j)tf if newspace,
    6754             :    or [M,j,d] for B(d)T(j)tf_M if cuspspace or oldspace.
    6755             : -- mf[5] contains the matrix M of first coefficients of basis, never cleaned.
    6756             :  * NK is either [N,k] or [N,k,CHI].
    6757             :  * mfinit does not do the splitting, only the basis generation. */
    6758             : 
    6759             : /* Set flraw to 1 if do not need mf[5]: no mftobasis etc..., only the
    6760             :    expansions of the basis elements are needed. */
    6761             : 
    6762             : static GEN
    6763        4991 : mfinit_Nkchi(long N, long k, GEN CHI, long space, long flraw)
    6764             : {
    6765        4991 :   GEN M = NULL, mf = NULL, mf1 = mkvec4(utoi(N), stoi(k), CHI, utoi(space));
    6766        4991 :   long sb = mfsturmNk(N, k);
    6767        4991 :   if (k < 0 || badchar(N, k, CHI)) return mfEMPTY(mf1);
    6768        4956 :   if (k == 0 || space == mf_EISEN) /*nothing*/;
    6769        4795 :   else if (k == 1)
    6770             :   {
    6771         364 :     switch (space)
    6772             :     {
    6773         350 :       case mf_NEW:
    6774             :       case mf_FULL:
    6775         350 :       case mf_CUSP: mf = mf1init(N, CHI, NULL, get_vDIH(N,NULL), space, flraw);
    6776         350 :                     break;
    6777           7 :       case mf_OLD: pari_err_IMPL("mfinit in weight 1 for old space");
    6778           7 :       default: pari_err_FLAG("mfinit");
    6779             :     }
    6780             :   }
    6781             :   else /* k >= 2 */
    6782             :   {
    6783        4431 :     long ord = mfcharorder(CHI);
    6784        4431 :     GEN z = NULL, P = (ord <= 2)? NULL: mfcharpol(CHI);
    6785             :     cachenew_t cache;
    6786        4431 :     switch(space)
    6787             :     {
    6788        1225 :       case mf_NEW:
    6789        1225 :         mf = mfnewinit(N, k, CHI, &cache, 1);
    6790        1225 :         if (mf && !flraw) { M = MF_get_M(mf); z = MF_get_Mindex(mf); }
    6791        1225 :         break;
    6792        3199 :       case mf_OLD:
    6793             :       case mf_CUSP:
    6794             :       case mf_FULL:
    6795        3199 :         if (!(mf = mfinitcusp(N, k, CHI, &cache, space))) break;
    6796        2898 :         if (!flraw)
    6797             :         {
    6798        2247 :           M = bhnmat_extend(M, sb+1, 1, MF_get_S(mf), &cache);
    6799        2247 :           if (space != mf_FULL) gel(mf,5) = mfcleanCHI(M, CHI, abundant(N));
    6800             :         }
    6801        2898 :         dbg_cachenew(&cache); break;
    6802           7 :       default: pari_err_FLAG("mfinit");
    6803             :     }
    6804        4424 :     if (z) gel(mf,5) = mfclean2(M, z, P, ord);
    6805             :   }
    6806        4935 :   if (!mf) mf = mfEMPTY(mf1);
    6807             :   else
    6808             :   {
    6809        3976 :     gel(mf,1) = mf1;
    6810        3976 :     if (flraw) gel(mf,5) = zerovec(3);
    6811             :   }
    6812        4935 :   if (!space_is_cusp(space))
    6813             :   {
    6814         819 :     GEN E = mfeisensteinbasis(N, k, CHI);
    6815         819 :     gel(mf,2) = E;
    6816         819 :     if (!flraw)
    6817             :     {
    6818         497 :       if (M)
    6819         196 :         M = shallowconcat(mfvectomat(E, sb+1, 1), M);
    6820             :       else
    6821         301 :         M = mfcoefs_mf(mf, sb+1, 1);
    6822         497 :       gel(mf,5) = mfcleanCHI(M, CHI, abundant(N));
    6823             :     }
    6824             :   }
    6825        4935 :   return mf;
    6826             : }
    6827             : 
    6828             : /* mfinit for k = nk/dk */
    6829             : static GEN
    6830        2695 : mfinit_Nndkchi(long N, long nk, long dk, GEN CHI, long space, long flraw)
    6831         266 : { return (dk == 2)? mf2init_Nkchi(N, nk >> 1, CHI, space, flraw)
    6832        2961 :                   : mfinit_Nkchi(N, nk, CHI, space, flraw); }
    6833             : static GEN
    6834        3360 : mfinit_i(GEN NK, long space)
    6835             : {
    6836             :   GEN CHI, mf;
    6837             :   long N, k, dk, joker;
    6838        3360 :   if (checkmf_i(NK))
    6839             :   {
    6840         147 :     N = mf_get_N(NK);
    6841         147 :     Qtoss(mf_get_gk(NK), &k, &dk);
    6842         147 :     CHI = mf_get_CHI(NK);
    6843             :   }
    6844        3213 :   else if ((mf = checkMF_i(NK)))
    6845             :   {
    6846          21 :     long s = MF_get_space(mf);
    6847          21 :     if (s == space) return mf;
    6848          21 :     Qtoss(MF_get_gk(mf), &k, &dk);
    6849          21 :     if (dk == 1 && k > 1 && space == mf_NEW && (s == mf_CUSP || s == mf_FULL))
    6850          21 :       return mfinittonew(mf);
    6851           0 :     N = MF_get_N(mf);
    6852           0 :     CHI = MF_get_CHI(mf);
    6853             :   }
    6854             :   else
    6855        3192 :     checkNK2(NK, &N, &k, &dk, &CHI, 1);
    6856        3318 :   joker = !CHI || typ(CHI) == t_COL;
    6857        3318 :   if (joker)
    6858             :   {
    6859        1162 :     GEN mf, vCHI = CHI;
    6860             :     long i, j, l;
    6861        1162 :     if (CHI && lg(CHI) == 1) return cgetg(1,t_VEC);
    6862        1155 :     if (k < 0) return mfEMPTYall(N, uutoQ(k,dk), CHI, space);
    6863        1141 :     if (k == 1 && dk == 1 && space != mf_EISEN)
    6864         504 :     {
    6865             :       GEN TMP, vSP, gN, gs;
    6866             :       pari_timer tt;
    6867        1106 :       if (space != mf_CUSP && space != mf_NEW)
    6868           0 :         pari_err_IMPL("mfinit([N,1,wildcard], space != cusp or new space)");
    6869        1106 :       if (wt1empty(N)) return mfEMPTYall(N, gen_1, CHI, space);
    6870         504 :       vCHI = mf1chars(N,vCHI);
    6871         504 :       l = lg(vCHI); mf = cgetg(l, t_VEC); if (l == 1) return mf;
    6872         504 :       TMP = mf1_pre(N); vSP = get_vDIH(N, NULL);
    6873         504 :       gN = utoipos(N); gs = utoi(space);
    6874         504 :       if (DEBUGLEVEL) timer_start(&tt);
    6875        4123 :       for (i = j = 1; i < l; i++)
    6876             :       {
    6877        3619 :         pari_sp av = avma;
    6878        3619 :         GEN c = gel(vCHI,i), z = mf1init(N, c, TMP, vSP, space, 0);
    6879        3619 :         if (z) z = gerepilecopy(av, z);
    6880             :         else
    6881             :         {
    6882        2905 :           set_avma(av);
    6883        2905 :           if (CHI) z = mfEMPTY(mkvec4(gN,gen_1,c,gs));
    6884             :         }
    6885        3619 :         if (z) gel(mf, j++) = z;
    6886        3619 :         if (DEBUGLEVEL)
    6887           0 :           timer_printf(&tt, "mf1basis: character %ld / %ld (order = %ld)",
    6888             :                        i, l-1, mfcharorder(c));
    6889             :       }
    6890             :     }
    6891             :     else
    6892             :     {
    6893          35 :       vCHI = mfchars(N,k,dk,vCHI);
    6894          35 :       l = lg(vCHI); mf = cgetg(l, t_VEC);
    6895         119 :       for (i = j = 1; i < l; i++)
    6896             :       {
    6897          84 :         pari_sp av = avma;
    6898          84 :         GEN v = mfinit_Nndkchi(N, k, dk, gel(vCHI,i), space, 0);
    6899          84 :         if (MF_get_dim(v) || CHI) gel(mf, j++) = v; else set_avma(av);
    6900             :       }
    6901             :     }
    6902         539 :     setlg(mf,j);
    6903         539 :     if (!CHI) gen_sort_inplace(mf, NULL, &cmp_ord, NULL);
    6904         539 :     return mf;
    6905             :   }
    6906        2156 :   return mfinit_Nndkchi(N, k, dk, CHI, space, 0);
    6907             : }
    6908             : GEN
    6909        2394 : mfinit(GEN NK, long space)
    6910             : {
    6911        2394 :   pari_sp av = avma;
    6912        2394 :   return gerepilecopy(av, mfinit_i(NK, space));
    6913             : }
    6914             : 
    6915             : /* UTILITY FUNCTIONS */
    6916             : static void
    6917         364 : cusp_canon(GEN cusp, long N, long *pA, long *pC)
    6918             : {
    6919         364 :   pari_sp av = avma;
    6920             :   long A, C, tc, cg;
    6921         364 :   if (N <= 0) pari_err_DOMAIN("mfcuspwidth","N","<=",gen_0,stoi(N));
    6922         357 :   if (!cusp || (tc = typ(cusp)) == t_INFINITY) { *pA = 1; *pC = N; return; }
    6923         350 :   if (tc != t_INT && tc != t_FRAC) pari_err_TYPE("checkcusp", cusp);
    6924         350 :   Qtoss(cusp, &A,&C);
    6925         350 :   if (N % C)
    6926             :   {
    6927             :     ulong uC;
    6928          14 :     long u = Fl_invgen((C-1)%N + 1, N, &uC);
    6929          14 :     A = Fl_mul(A, u, N);
    6930          14 :     C = (long)uC;
    6931             :   }
    6932         350 :   cg = ugcd(C, N/C);
    6933         420 :   while (ugcd(A, N) > 1) A += cg;
    6934         350 :   *pA = A % N; *pC = C; set_avma(av);
    6935             : }
    6936             : static long
    6937         952 : mfcuspcanon_width(long N, long C)
    6938         952 : { return (!C || C == N)? 1 : N / ugcd(N, Fl_sqr(umodsu(C,N),N)); }
    6939             : /* v = [a,c] a ZC, width of cusp (a:c) */
    6940             : static long
    6941        9667 : mfZC_width(long N, GEN v)
    6942             : {
    6943        9667 :   ulong C = umodiu(gel(v,2), N);
    6944        9667 :   return (C == 0)? 1: N / ugcd(N, Fl_sqr(C,N));
    6945             : }
    6946             : long
    6947         161 : mfcuspwidth(GEN gN, GEN cusp)
    6948             : {
    6949         161 :   long N = 0, A, C;
    6950             :   GEN mf;
    6951         161 :   if (typ(gN) == t_INT) N = itos(gN);
    6952          42 :   else if ((mf = checkMF_i(gN))) N = MF_get_N(mf);
    6953           0 :   else pari_err_TYPE("mfcuspwidth", gN);
    6954         161 :   cusp_canon(cusp, N, &A, &C);
    6955         154 :   return mfcuspcanon_width(N, C);
    6956             : }
    6957             : 
    6958             : /* Q a t_INT */
    6959             : static GEN
    6960          14 : findq(GEN al, GEN Q)
    6961             : {
    6962             :   long n;
    6963          14 :   if (typ(al) == t_FRAC && cmpii(gel(al,2), Q) <= 0)
    6964           0 :     return mkvec(mkvec2(gel(al,1), gel(al,2)));
    6965          14 :   n = 1 + (long)ceil(2.0781*gtodouble(glog(Q, LOWDEFAULTPREC)));
    6966          14 :   return contfracpnqn(gboundcf(al,n), n);
    6967             : }
    6968             : static GEN
    6969          91 : findqga(long N, GEN z)
    6970             : {
    6971          91 :   GEN Q, LDC, CK = NULL, DK = NULL, ma, x, y = imag_i(z);
    6972             :   long j, l;
    6973          91 :   if (gcmpgs(gmulsg(2*N, y), 1) >= 0) return NULL;
    6974          14 :   x = real_i(z);
    6975          14 :   Q = ground(ginv(gsqrt(gmulsg(N, y), LOWDEFAULTPREC)));
    6976          14 :   LDC = findq(gmulsg(-N,x), Q);
    6977          14 :   ma = gen_1; l = lg(LDC);
    6978          35 :   for (j = 1; j < l; j++)
    6979             :   {
    6980          21 :     GEN D, DC = gel(LDC,j), C1 = gel(DC,2);
    6981          21 :     if (cmpii(C1,Q) > 0) break;
    6982          21 :     D = gel(DC,1);
    6983          21 :     if (ugcdiu(D,N) == 1)
    6984             :     {
    6985           7 :       GEN C = mului(N, C1), den;
    6986           7 :       den = gadd(gsqr(gmul(C,y)), gsqr(gadd(D, gmul(C,x))));
    6987           7 :       if (gcmp(den, ma) < 0) { ma = den; CK = C; DK = D; }
    6988             :     }
    6989             :   }
    6990          14 :   return DK? mkvec2(CK, DK): NULL;
    6991             : }
    6992             : 
    6993             : static long
    6994         168 : valNC2(GEN P, GEN E, long e)
    6995             : {
    6996         168 :   long i, d = 1, l = lg(P);
    6997         504 :   for (i = 1; i < l; i++)
    6998             :   {
    6999         336 :     long v = u_lval(e, P[i]) << 1;
    7000         336 :     if (v == E[i] + 1) v--;
    7001         336 :     d *= upowuu(P[i], v);
    7002             :   }
    7003         168 :   return d;
    7004             : }
    7005             : 
    7006             : static GEN
    7007          49 : findqganew(long N, GEN z)
    7008             : {
    7009          49 :   GEN MI, DI, x = real_i(z), y = imag_i(z), Ck = gen_0, Dk = gen_1, fa, P, E;
    7010             :   long i;
    7011          49 :   MI = uutoQ(1,N);
    7012          49 :   DI = mydivisorsu(mysqrtu(N));
    7013          49 :   fa = myfactoru(N); P = gel(fa,1); E = gel(fa,2);
    7014         217 :   for (i = 1; i < lg(DI); i++)
    7015             :   {
    7016         168 :     long e = DI[i], g;
    7017             :     GEN U, C, D, m;
    7018         168 :     (void)cxredsl2(gmulsg(e, z), &U);
    7019         168 :     C = gcoeff(U,2,1); if (!signe(C)) continue;
    7020         168 :     D = gcoeff(U,2,2);
    7021         168 :     g = ugcdiu(D,e);
    7022         168 :     if (g > 1) { C = muliu(C,e/g); D = diviuexact(D,g); } else C = muliu(C,e);
    7023         168 :     m = gadd(gsqr(gadd(gmul(C, x), D)), gsqr(gmul(C, y)));
    7024         168 :     m = gdivgu(m, valNC2(P, E, e));
    7025         168 :     if (gcmp(m, MI) < 0) { MI = m; Ck = C; Dk = D; }
    7026             :   }
    7027          49 :   return signe(Ck)? mkvec2(Ck, Dk): NULL;
    7028             : }
    7029             : 
    7030             : /* Return z' and U = [a,b;c,d] \in SL_2(Z), z' = U*z,
    7031             :  * Im(z')/width(U.oo) > sqrt(3)/(2N). Set *pczd = c*z+d */
    7032             : static GEN
    7033         175 : cxredga0N(long N, GEN z, GEN *pU, GEN *pczd, long flag)
    7034             : {
    7035         175 :   GEN v = NULL, A, B, C, D;
    7036             :   long e;
    7037         175 :   if (N == 1) return cxredsl2_i(z, pU, pczd);
    7038         140 :   e = gexpo(gel(z,2));
    7039         140 :   if (e < 0) z = gprec_wensure(z, precision(z) + nbits2extraprec(-e));
    7040         140 :   v = flag? findqganew(N,z): findqga(N,z);
    7041         140 :   if (!v) { *pU = matid(2); *pczd = gen_1; return z; }
    7042          56 :   C = gel(v,1);
    7043          56 :   D = gel(v,2);
    7044          56 :   if (!is_pm1(bezout(C,D, &B,&A))) pari_err_BUG("cxredga0N [gcd > 1]");
    7045          56 :   B = negi(B);
    7046          56 :   *pU = mkmat2(mkcol2(A,C), mkcol2(B,D));
    7047          56 :   *pczd = gadd(gmul(C,z), D);
    7048          56 :   return gdiv(gadd(gmul(A,z), B), *pczd);
    7049             : }
    7050             : 
    7051             : static GEN
    7052         154 : lfunthetaall(GEN b, GEN vL, GEN t, long bitprec)
    7053             : {
    7054         154 :   long i, l = lg(vL);
    7055         154 :   GEN v = cgetg(l, t_VEC);
    7056         336 :   for (i = 1; i < l; i++)
    7057             :   {
    7058         182 :     GEN T, L = gel(vL,i), a0 = gel(L,1), ldata = gel(L,2);
    7059         182 :     GEN van = gel(ldata_get_an(ldata),2);
    7060         182 :     if (lg(van) == 1)
    7061             :     {
    7062           0 :       T = gmul(b, a0);
    7063           0 :       if (isexactzero(T)) { GEN z = real_0_bit(-bitprec); T = mkcomplex(z,z); }
    7064             :     }
    7065             :     else
    7066             :     {
    7067         182 :       T = gmul2n(lfuntheta(ldata, t, 0, bitprec), -1);
    7068         182 :       T = gmul(b, gadd(a0, T));
    7069             :     }
    7070         182 :     gel(v,i) = T;
    7071             :   }
    7072         154 :   return l == 2? gel(v,1): v;
    7073             : }
    7074             : 
    7075             : /* P in ZX, irreducible */
    7076             : static GEN
    7077         182 : ZX_roots(GEN P, long prec)
    7078             : {
    7079         182 :   long d = degpol(P);
    7080         182 :   if (d == 1) return mkvec(gen_0);
    7081         182 :   if (d == 2 && isint1(gel(P,2)) && isintzero(gel(P,3)) && isint1(gel(P,4)))
    7082           7 :     return mkvec2(powIs(3), gen_I()); /* order as polroots */
    7083         294 :   return (ZX_sturm_irred(P) == d)? ZX_realroots_irred(P, prec)
    7084         294 :                                  : QX_complex_roots(P, prec);
    7085             : }
    7086             : /* initializations for RgX_RgV_eval / RgC_embed */
    7087             : static GEN
    7088         217 : rootspowers(GEN v)
    7089             : {
    7090         217 :   long i, l = lg(v);
    7091         217 :   GEN w = cgetg(l, t_VEC);
    7092         868 :   for (i = 1; i < l; i++) gel(w,i) = gpowers(gel(v,i), l-2);
    7093         217 :   return w;
    7094             : }
    7095             : /* mf embeddings attached to Q(chi)/(T), chi attached to cyclotomic P */
    7096             : static GEN
    7097         903 : getembed(GEN P, GEN T, GEN zcyclo, long prec)
    7098             : {
    7099             :   long i, l;
    7100             :   GEN v;
    7101         903 :   if (degpol(P) == 1) P = NULL; /* mfcharpol for quadratic char */
    7102         903 :   if (degpol(T) == 1) T = NULL; /* dim 1 orbit */
    7103         903 :   if (T && P)
    7104          35 :   { /* K(y) / (T(y)), K = Q(t)/(P) cyclotomic */
    7105          35 :     GEN vr = RgX_is_ZX(T)? ZX_roots(T,prec): roots(RgX_embed1(T,zcyclo), prec);
    7106          35 :     v = rootspowers(vr); l = lg(v);
    7107         105 :     for (i = 1; i < l; i++) gel(v,i) = mkcol3(P,zcyclo,gel(v,i));
    7108             :   }
    7109         868 :   else if (T)
    7110             :   { /* Q(y) / (T(y)), T noncyclotomic */
    7111         182 :     GEN vr = ZX_roots(T, prec);
    7112         182 :     v = rootspowers(vr); l = lg(v);
    7113         763 :     for (i = 1; i < l; i++) gel(v,i) = mkcol2(T, gel(v,i));
    7114             :   }
    7115             :   else /* cyclotomic or rational */
    7116         686 :     v = mkvec(P? mkvec2(P, zcyclo): cgetg(1,t_VEC));
    7117         903 :   return v;
    7118             : }
    7119             : static GEN
    7120         756 : grootsof1_CHI(GEN CHI, long prec)
    7121         756 : { return grootsof1(mfcharorder(CHI), prec); }
    7122             : /* return the [Q(F):Q(chi)] embeddings of F */
    7123             : static GEN
    7124         588 : mfgetembed(GEN F, long prec)
    7125             : {
    7126         588 :   GEN T = mf_get_field(F), CHI = mf_get_CHI(F), P = mfcharpol(CHI);
    7127         588 :   return getembed(P, T, grootsof1_CHI(CHI, prec), prec);
    7128             : }
    7129             : static GEN
    7130           7 : mfchiembed(GEN mf, long prec)
    7131             : {
    7132           7 :   GEN CHI = MF_get_CHI(mf), P = mfcharpol(CHI);
    7133           7 :   return getembed(P, pol_x(0), grootsof1_CHI(CHI, prec), prec);
    7134             : }
    7135             : /* mfgetembed for the successive eigenforms in MF_get_newforms */
    7136             : static GEN
    7137         161 : mfeigenembed(GEN mf, long prec)
    7138             : {
    7139         161 :   GEN vP = MF_get_fields(mf), vF = MF_get_newforms(mf);
    7140         161 :   GEN zcyclo, vE, CHI = MF_get_CHI(mf), P = mfcharpol(CHI);
    7141         161 :   long i, l = lg(vP);
    7142         161 :   vF = Q_remove_denom(liftpol_shallow(vF), NULL);
    7143         161 :   prec += nbits2extraprec(gexpo(vF));
    7144         161 :   zcyclo = grootsof1_CHI(CHI, prec);
    7145         161 :   vE = cgetg(l, t_VEC);
    7146         469 :   for (i = 1; i < l; i++) gel(vE,i) = getembed(P, gel(vP,i), zcyclo, prec);
    7147         161 :   return vE;
    7148             : }
    7149             : 
    7150             : static int
    7151          28 : checkPv(GEN P, GEN v)
    7152          28 : { return typ(P) == t_POL && is_vec_t(typ(v)) && lg(v)-1 >= degpol(P); }
    7153             : static int
    7154          28 : checkemb_i(GEN E)
    7155             : {
    7156          28 :   long t = typ(E), l = lg(E);
    7157          28 :   if (t == t_VEC) return l == 1 || (l == 3 && checkPv(gel(E,1), gel(E,2)));
    7158          21 :   if (t != t_COL) return 0;
    7159          21 :   if (l == 3) return checkPv(gel(E,1), gel(E,2));
    7160          21 :   return l == 4 && is_vec_t(typ(gel(E,2))) && checkPv(gel(E,1), gel(E,3));
    7161             : }
    7162             : static GEN
    7163          28 : anyembed(GEN v, GEN E)
    7164             : {
    7165          28 :   switch(typ(v))
    7166             :   {
    7167          21 :     case t_VEC: case t_COL: return mfvecembed(E, v);
    7168           7 :     case t_MAT: return mfmatembed(E, v);
    7169             :   }
    7170           0 :   return mfembed(E, v);
    7171             : }
    7172             : GEN
    7173          49 : mfembed0(GEN E, GEN v, long prec)
    7174             : {
    7175          49 :   pari_sp av = avma;
    7176          49 :   GEN mf, vE = NULL;
    7177          49 :   if (checkmf_i(E)) vE = mfgetembed(E, prec);
    7178          35 :   else if ((mf = checkMF_i(E))) vE = mfchiembed(mf, prec);
    7179          49 :   if (vE)
    7180             :   {
    7181          21 :     long i, l = lg(vE);
    7182             :     GEN w;
    7183          21 :     if (!v) return gerepilecopy(av, l == 2? gel(vE,1): vE);
    7184           0 :     w = cgetg(l, t_VEC);
    7185           0 :     for (i = 1; i < l; i++) gel(w,i) = anyembed(v, gel(vE,i));
    7186           0 :     return gerepilecopy(av, l == 2? gel(w,1): w);
    7187             :   }
    7188          28 :   if (!checkemb_i(E) || !v) pari_err_TYPE("mfembed", E);
    7189          28 :   return gerepilecopy(av, anyembed(v,E));
    7190             : }
    7191             : 
    7192             : /* dummy lfun create for theta evaluation */
    7193             : static GEN
    7194         938 : mfthetaancreate(GEN van, GEN N, GEN k)
    7195             : {
    7196         938 :   GEN L = zerovec(6);
    7197         938 :   gel(L,1) = lfuntag(t_LFUN_GENERIC, van);
    7198         938 :   gel(L,3) = mkvec2(gen_0, gen_1);
    7199         938 :   gel(L,4) = k;
    7200         938 :   gel(L,5) = N; return L;
    7201             : }
    7202             : /* destroy van and prepare to evaluate theta(sigma(van)), for all sigma in
    7203             :  * embeddings vector vE */
    7204             : static GEN
    7205         336 : van_embedall(GEN van, GEN vE, GEN gN, GEN gk)
    7206             : {
    7207         336 :   GEN a0 = gel(van,1), vL;
    7208         336 :   long i, lE = lg(vE), l = lg(van);
    7209         336 :   van++; van[0] = evaltyp(t_VEC) | _evallg(l-1); /* remove a0 */
    7210         336 :   vL = cgetg(lE, t_VEC);
    7211         903 :   for (i = 1; i < lE; i++)
    7212             :   {
    7213         567 :     GEN E = gel(vE,i), v = mfvecembed(E, van);
    7214         567 :     gel(vL,i) = mkvec2(mfembed(E,a0), mfthetaancreate(v, gN, gk));
    7215             :   }
    7216         336 :   return vL;
    7217             : }
    7218             : 
    7219             : static int
    7220        1120 : cusp_AC(GEN cusp, long *A, long *C)
    7221             : {
    7222        1120 :   switch(typ(cusp))
    7223             :   {
    7224         140 :     case t_INFINITY: *A = 1; *C = 0; break;
    7225         294 :     case t_INT:  *A = itos(cusp); *C = 1; break;
    7226         448 :     case t_FRAC: *A = itos(gel(cusp, 1)); *C = itos(gel(cusp, 2)); break;
    7227         238 :     case t_REAL: case t_COMPLEX:
    7228         238 :       *A = 0; *C = 0;
    7229         238 :       if (gsigne(imag_i(cusp)) <= 0)
    7230           7 :         pari_err_DOMAIN("mfeval","imag(tau)","<=",gen_0,cusp);
    7231         231 :       return 0;
    7232           0 :     default: pari_err_TYPE("cusp_AC", cusp);
    7233             :   }
    7234         882 :   return 1;
    7235             : }
    7236             : static GEN
    7237         518 : cusp2mat(long A, long C)
    7238             : { long B, D;
    7239         518 :   cbezout(A, C, &D, &B);
    7240         518 :   return mkmat22s(A, -B, C, D);
    7241             : }
    7242             : static GEN
    7243          21 : mkS(void) { return mkmat22s(0,-1,1,0); }
    7244             : 
    7245             : /* if t is a cusp, return F(t), else NULL */
    7246             : static GEN
    7247         350 : evalcusp(GEN mf, GEN F, GEN t, long prec)
    7248             : {
    7249             :   long A, C;
    7250             :   GEN R;
    7251         350 :   if (!cusp_AC(t, &A,&C)) return NULL;
    7252         189 :   if (C % mf_get_N(F) == 0) return gel(mfcoefs_i(F, 0, 1), 1);
    7253         175 :   R = mfgaexpansion(mf, F, cusp2mat(A,C), 0, prec);
    7254         175 :   return gequal0(gel(R,1))? gmael(R,3,1): gen_0;
    7255             : }
    7256             : /* Evaluate an mf closure numerically, i.e., in the usual sense, either for a
    7257             :  * single tau or a vector of tau; for each, return a vector of results
    7258             :  * corresponding to all complex embeddings of F. If flag is nonzero, allow
    7259             :  * replacing F by F | gamma to increase imag(gamma^(-1).tau) [ expensive if
    7260             :  * MF_EISENSPACE not present ] */
    7261             : static GEN
    7262         161 : mfeval_i(GEN mf, GEN F, GEN vtau, long flag, long bitprec)
    7263             : {
    7264             :   GEN L0, vL, vb, sqN, vczd, vTAU, vs, van, vE;
    7265         161 :   long N = MF_get_N(mf), N0, ta, lv, i, prec = nbits2prec(bitprec);
    7266         161 :   GEN gN = utoipos(N), gk = mf_get_gk(F), gk1 = gsubgs(gk,1), vgk;
    7267         161 :   long flscal = 0;
    7268             : 
    7269             :   /* gen_0 is ignored, second component assumes Ramanujan-Petersson in
    7270             :    * 1/2-integer weight */
    7271         161 :   vgk = mkvec2(gen_0, mfiscuspidal(mf,F)? gmul2n(gk1,-1): gk1);
    7272         161 :   ta = typ(vtau);
    7273         161 :   if (!is_vec_t(ta)) { flscal = 1; vtau = mkvec(vtau); ta = t_VEC; }
    7274         161 :   lv = lg(vtau);
    7275         161 :   sqN = sqrtr_abs(utor(N, prec));
    7276         161 :   vs = const_vec(lv-1, NULL);
    7277         161 :   vb = const_vec(lv-1, NULL);
    7278         161 :   vL = cgetg(lv, t_VEC);
    7279         161 :   vTAU = cgetg(lv, t_VEC);
    7280         161 :   vczd = cgetg(lv, t_VEC);
    7281         161 :   L0 = mfthetaancreate(NULL, gN, vgk); /* only for thetacost */
    7282         161 :   vE = mfgetembed(F, prec);
    7283         161 :   N0 = 0;
    7284         343 :   for (i = 1; i < lv; i++)
    7285             :   {
    7286         189 :     GEN z = gel(vtau,i), tau, U;
    7287             :     long w, n;
    7288             : 
    7289         189 :     gel(vs,i) = evalcusp(mf, F, z, prec);
    7290         182 :     if (gel(vs,i)) continue;
    7291         154 :     tau = cxredga0N(N, z, &U, &gel(vczd,i), flag);
    7292         154 :     if (!flag) w = 0; else { w = mfZC_width(N, gel(U,1)); tau = gdivgu(tau,w); }
    7293         154 :     gel(vTAU,i) = mulcxmI(gmul(tau, sqN));
    7294         154 :     n = lfunthetacost(L0, real_i(gel(vTAU,i)), 0, bitprec);
    7295         154 :     if (N0 < n) N0 = n;
    7296         154 :     if (flag)
    7297             :     {
    7298          42 :       GEN A, al, v = mfslashexpansion(mf, F, ZM_inv(U,NULL), n, 0, &A, prec);
    7299          42 :       gel(vL,i) = van_embedall(v, vE, gN, vgk);
    7300          42 :       al = gel(A,1);
    7301          42 :       if (!gequal0(al))
    7302           7 :         gel(vb,i) = gexp(gmul(gmul(gmulsg(w,al),PiI2(prec)), tau), prec);
    7303             :     }
    7304             :   }
    7305         154 :   if (!flag)
    7306             :   {
    7307         112 :     van = mfcoefs_i(F, N0, 1);
    7308         112 :     vL = const_vec(lv-1, van_embedall(van, vE, gN, vgk));
    7309             :   }
    7310         336 :   for (i = 1; i < lv; i++)
    7311             :   {
    7312             :     GEN T;
    7313         182 :     if (gel(vs,i)) continue;
    7314         154 :     T = gpow(gel(vczd,i), gneg(gk), prec);
    7315         154 :     if (flag && gel(vb,i)) T = gmul(T, gel(vb,i));
    7316         154 :     gel(vs,i) = lfunthetaall(T, gel(vL,i), gel(vTAU,i), bitprec);
    7317             :   }
    7318         154 :   return flscal? gel(vs,1): vs;
    7319             : }
    7320             : 
    7321             : static long
    7322        1323 : mfistrivial(GEN F)
    7323             : {
    7324        1323 :   switch(mf_get_type(F))
    7325             :   {
    7326           7 :     case t_MF_CONST: return lg(gel(F,2)) == 1;
    7327         266 :     case t_MF_LINEAR: case t_MF_LINEAR_BHN: return gequal0(gel(F,3));
    7328        1050 :     default: return 0;
    7329             :   }
    7330             : }
    7331             : 
    7332             : static long
    7333        1141 : mf_same_k(GEN mf, GEN f) { return gequal(MF_get_gk(mf), mf_get_gk(f)); }
    7334             : static long
    7335        1099 : mf_same_CHI(GEN mf, GEN f)
    7336             : {
    7337        1099 :   GEN F1, F2, chi1, chi2, CHI1 = MF_get_CHI(mf), CHI2 = mf_get_CHI(f);
    7338             :   /* are the primitive chars attached to CHI1 and CHI2 equal ? */
    7339        1099 :   F1 = znconreyconductor(gel(CHI1,1), gel(CHI1,2), &chi1);
    7340        1099 :   if (typ(F1) == t_VEC) F1 = gel(F1,1);
    7341        1099 :   F2 = znconreyconductor(gel(CHI2,1), gel(CHI2,2), &chi2);
    7342        1099 :   if (typ(F2) == t_VEC) F2 = gel(F2,1);
    7343        1099 :   return equalii(F1,F2) && ZV_equal(chi1,chi2);
    7344             : }
    7345             : /* check k and CHI rigorously, but not coefficients nor N */
    7346             : static long
    7347         238 : mfisinspace_i(GEN mf, GEN F)
    7348             : {
    7349         238 :   return mfistrivial(F) || (mf_same_k(mf,F) && mf_same_CHI(mf,F));
    7350             : }
    7351             : static void
    7352           7 : err_space(GEN F)
    7353           7 : { pari_err_DOMAIN("mftobasis", "form", "does not belong to",
    7354           0 :                   strtoGENstr("space"), F); }
    7355             : 
    7356             : static long
    7357         147 : mfcheapeisen(GEN mf)
    7358             : {
    7359         147 :   long k, L, N = MF_get_N(mf);
    7360             :   GEN P;
    7361         147 :   if (N <= 70) return 1;
    7362          84 :   k = itos(gceil(MF_get_gk(mf)));
    7363          84 :   if (odd(k)) k--;
    7364          84 :   switch (k)
    7365             :   {
    7366           0 :     case 2:  L = 190; break;
    7367          14 :     case 4:  L = 162; break;
    7368          70 :     case 6:
    7369          70 :     case 8:  L = 88; break;
    7370           0 :     case 10: L = 78; break;
    7371           0 :     default: L = 66; break;
    7372             :   }
    7373          84 :   P = gel(myfactoru(N), 1);
    7374          84 :   return P[lg(P)-1] <= L;
    7375             : }
    7376             : 
    7377             : static GEN
    7378         182 : myimag_i(GEN x)
    7379             : {
    7380         182 :   long tc = typ(x);
    7381         182 :   if (tc == t_INFINITY || tc == t_INT || tc == t_FRAC) return gen_1;
    7382         189 :   if (tc == t_VEC) pari_APPLY_same(myimag_i(gel(x,i)));
    7383         147 :   return imag_i(x);
    7384             : }
    7385             : 
    7386             : static GEN
    7387         147 : mintau(GEN vtau)
    7388             : {
    7389         147 :   if (!is_vec_t(typ(vtau))) return myimag_i(vtau);
    7390           7 :   return (lg(vtau) == 1)? gen_1: vecmin(myimag_i(vtau));
    7391             : }
    7392             : 
    7393             : /* initialization for mfgaexpansion: what does not depend on cusp */
    7394             : static GEN
    7395        1001 : mf_eisendec(GEN mf, GEN F, long prec)
    7396             : {
    7397        1001 :   GEN B = liftpol_shallow(mfeisensteindec(mf, F)), v = variables_vecsmall(B);
    7398        1001 :   GEN Mvecj = obj_check(mf, MF_EISENSPACE);
    7399        1001 :   long l = lg(v), i, ord;
    7400        1001 :   if (lg(Mvecj) < 5) Mvecj = gel(Mvecj,1);
    7401        1001 :   ord = itou(gel(Mvecj,4));
    7402        1057 :   for (i = 1; i < l; i++)
    7403         721 :     if (v[i] != 1)
    7404             :     {
    7405             :       GEN d;
    7406             :       long e;
    7407         665 :       B = Q_remove_denom(B, &d);
    7408         665 :       e = gexpo(B);
    7409         665 :       if (e > 0) prec += nbits2prec(e);
    7410         665 :       B = gsubst(B, v[i], rootsof1u_cx(ord, prec));
    7411         665 :       if (d) B = gdiv(B, d);
    7412         665 :       break;
    7413             :     }
    7414        1001 :   return B;
    7415             : }
    7416             : 
    7417             : GEN
    7418         161 : mfeval(GEN mf0, GEN F, GEN vtau, long bitprec)
    7419             : {
    7420         161 :   pari_sp av = avma;
    7421         161 :   long flnew = 1;
    7422         161 :   GEN mf = checkMF_i(mf0);
    7423         161 :   if (!mf) pari_err_TYPE("mfeval", mf0);
    7424         161 :   if (!checkmf_i(F)) pari_err_TYPE("mfeval", F);
    7425         161 :   if (!mfisinspace_i(mf, F)) err_space(F);
    7426         161 :   if (!obj_check(mf, MF_EISENSPACE)) flnew = mfcheapeisen(mf);
    7427         161 :   if (flnew && gcmpgs(gmulsg(2*MF_get_N(mf), mintau(vtau)), 1) >= 0) flnew = 0;
    7428         161 :   return gerepilecopy(av, mfeval_i(mf, F, vtau, flnew, bitprec));
    7429             : }
    7430             : 
    7431             : static long
    7432         189 : val(GEN v, long bit)
    7433             : {
    7434         189 :   long c, l = lg(v);
    7435         392 :   for (c = 1; c < l; c++)
    7436         378 :     if (gexpo(gel(v,c)) > -bit) return c-1;
    7437          14 :   return -1;
    7438             : }
    7439             : GEN
    7440         203 : mfcuspval(GEN mf, GEN F, GEN cusp, long bitprec)
    7441             : {
    7442         203 :   pari_sp av = avma;
    7443         203 :   long lvE, w, N, sb, n, A, C, prec = nbits2prec(bitprec);
    7444             :   GEN ga, gk, vE;
    7445         203 :   mf = checkMF(mf);
    7446         203 :   if (!checkmf_i(F)) pari_err_TYPE("mfcuspval",F);
    7447         203 :   N = MF_get_N(mf);
    7448         203 :   cusp_canon(cusp, N, &A, &C);
    7449         203 :   gk = mf_get_gk(F);
    7450         203 :   if (typ(gk) != t_INT)
    7451             :   {
    7452          42 :     GEN FT = mfmultheta(F), mf2 = obj_checkbuild(mf, MF_MF2INIT, &mf2init);
    7453          42 :     GEN r = mfcuspval(mf2, FT, cusp, bitprec);
    7454          42 :     if ((C & 3L) == 2)
    7455             :     {
    7456          14 :       GEN z = uutoQ(1,4);
    7457          14 :       r = gsub(r, typ(r) == t_VEC? const_vec(lg(r)-1, z): z);
    7458             :     }
    7459          42 :     return gerepileupto(av, r);
    7460             :   }
    7461         161 :   vE = mfgetembed(F, prec);
    7462         161 :   lvE = lg(vE);
    7463         161 :   w = mfcuspcanon_width(N, C);
    7464         161 :   sb = w * mfsturmNk(N, itos(gk));
    7465         161 :   ga = cusp2mat(A,C);
    7466         168 :   for (n = 8;; n = minss(sb, n << 1))
    7467           7 :   {
    7468         168 :     GEN R = mfgaexpansion(mf, F, ga, n, prec), res = liftpol_shallow(gel(R,3));
    7469         168 :     GEN v = cgetg(lvE-1, t_VECSMALL);
    7470         168 :     long j, ok = 1;
    7471         168 :     res = RgC_embedall(res, vE);
    7472         357 :     for (j = 1; j < lvE; j++)
    7473             :     {
    7474         189 :       v[j] = val(gel(res,j), bitprec/2);
    7475         189 :       if (v[j] < 0) ok = 0;
    7476             :     }
    7477         168 :     if (ok)
    7478             :     {
    7479         154 :       res = cgetg(lvE, t_VEC);
    7480         329 :       for (j = 1; j < lvE; j++) gel(res,j) = gadd(gel(R,1), uutoQ(v[j], w));
    7481         154 :       return gerepilecopy(av, lvE==2? gel(res,1): res);
    7482             :     }
    7483          14 :     if (n == sb) return lvE==2? mkoo(): const_vec(lvE-1, mkoo()); /* 0 */
    7484             :   }
    7485             : }
    7486             : 
    7487             : long
    7488         224 : mfiscuspidal(GEN mf, GEN F)
    7489             : {
    7490         224 :   pari_sp av = avma;
    7491             :   GEN mf2;
    7492         224 :   if (space_is_cusp(MF_get_space(mf))) return 1;
    7493          98 :   if (typ(mf_get_gk(F)) == t_INT)
    7494             :   {
    7495          56 :     GEN v = mftobasis(mf,F,0), vE = vecslice(v, 1, lg(MF_get_E(mf))-1);
    7496          56 :     return gc_long(av, gequal0(vE));
    7497             :   }
    7498          42 :   if (!gequal0(mfak_i(F, 0))) return 0;
    7499          21 :   mf2 = obj_checkbuild(mf, MF_MF2INIT, &mf2init);
    7500          21 :   return mfiscuspidal(mf2, mfmultheta(F));
    7501             : }
    7502             : 
    7503             : /* F = vector of newforms in mftobasis format */
    7504             : static GEN
    7505         105 : mffrickeeigen_i(GEN mf, GEN F, GEN vE, long prec)
    7506             : {
    7507         105 :   GEN M, Z, L0, gN = MF_get_gN(mf), gk = MF_get_gk(mf);
    7508         105 :   long N0, i, lM, bit = prec2nbits(prec), k = itou(gk);
    7509         105 :   long LIM = 5; /* Sturm bound is enough */
    7510             : 
    7511         105 :   L0 = mfthetaancreate(NULL, gN, gk); /* only for thetacost */
    7512         105 : START:
    7513         105 :   N0 = lfunthetacost(L0, gen_1, LIM, bit);
    7514         105 :   M = mfcoefs_mf(mf, N0, 1);
    7515         105 :   lM = lg(F);
    7516         105 :   Z = cgetg(lM, t_VEC);
    7517         287 :   for (i = 1; i < lM; i++)
    7518             :   { /* expansion of D * F[i] */
    7519         182 :     GEN D, z, van = RgM_RgC_mul(M, Q_remove_denom(gel(F,i), &D));
    7520         182 :     GEN L = van_embedall(van, gel(vE,i), gN, gk);
    7521         182 :     long l = lg(L), j, bit_add = D? expi(D): 0;
    7522         182 :     gel(Z,i) = z = cgetg(l, t_VEC);
    7523         567 :     for (j = 1; j < l; j++)
    7524             :     {
    7525             :       GEN v, C, C0;
    7526             :       long m, e;
    7527         518 :       for (m = 0; m <= LIM; m++)
    7528             :       {
    7529         518 :         v = lfuntheta(gmael(L,j,2), gen_1, m, bit);
    7530         518 :         if (gexpo(v) > bit_add - bit/2) break;
    7531             :       }
    7532         385 :       if (m > LIM) { LIM <<= 1; goto START; }
    7533         385 :       C = mulcxpowIs(gdiv(v,conj_i(v)), 2*m - k);
    7534         385 :       C0 = grndtoi(C, &e); if (e < 5-prec2nbits(precision(C))) C = C0;
    7535         385 :       gel(z,j) = C;
    7536             :     }
    7537             :   }
    7538         105 :   return Z;
    7539             : }
    7540             : static GEN
    7541          84 : mffrickeeigen(GEN mf, GEN vE, long prec)
    7542             : {
    7543          84 :   GEN D = obj_check(mf, MF_FRICKE);
    7544          84 :   if (D) { long p = gprecision(D); if (!p || p >= prec) return D; }
    7545          77 :   D = mffrickeeigen_i(mf, MF_get_newforms(mf), vE, prec);
    7546          77 :   return obj_insert(mf, MF_FRICKE, D);
    7547             : }
    7548             : 
    7549             : /* integral weight, new space for primitive quadratic character CHIP;
    7550             :  * MF = vector of embedded eigenforms coefs on mfbasis, by orbit.
    7551             :  * Assume N > Q > 1 and (Q,f(CHIP)) = 1 */
    7552             : static GEN
    7553          56 : mfatkineigenquad(GEN mf, GEN CHIP, long Q, GEN MF, long bitprec)
    7554             : {
    7555             :   GEN L0, la2, S, F, vP, tau, wtau, Z, va, vb, den, coe, sqrtQ, sqrtN;
    7556          56 :   GEN M, gN, gk = MF_get_gk(mf);
    7557          56 :   long N0, x, yq, i, j, lF, dim, muQ, prec = nbits2prec(bitprec);
    7558          56 :   long N = MF_get_N(mf), k = itos(gk), NQ = N / Q;
    7559             : 
    7560             :   /* Q coprime to FC */
    7561          56 :   F = MF_get_newforms(mf);
    7562          56 :   vP = MF_get_fields(mf);
    7563          56 :   lF = lg(F);
    7564          56 :   Z = cgetg(lF, t_VEC);
    7565          56 :   S = MF_get_S(mf); dim = lg(S) - 1;
    7566          56 :   muQ = mymoebiusu(Q);
    7567          56 :   if (muQ)
    7568             :   {
    7569          42 :     GEN SQ = cgetg(dim+1,t_VEC), Qk = gpow(stoi(Q), sstoQ(k-2, 2), prec);
    7570          42 :     long i, bit2 = bitprec >> 1;
    7571         154 :     for (j = 1; j <= dim; j++) gel(SQ,j) = mfak_i(gel(S,j), Q);
    7572          84 :     for (i = 1; i < lF; i++)
    7573             :     {
    7574          42 :       GEN S = RgV_dotproduct(gel(F,i), SQ), T = gel(vP,i);
    7575             :       long e;
    7576          42 :       if (degpol(T) > 1 && typ(S) != t_POLMOD) S = gmodulo(S, T);
    7577          42 :       S = grndtoi(gdiv(conjvec(S, prec), Qk), &e);
    7578          42 :       if (e > -bit2) pari_err_PREC("mfatkineigenquad");
    7579          42 :       if (muQ == -1) S = gneg(S);
    7580          42 :       gel(Z,i) = S;
    7581             :     }
    7582          42 :     return Z;
    7583             :   }
    7584          14 :   la2 = mfchareval(CHIP, Q); /* 1 or -1 */
    7585          14 :   (void)cbezout(Q, NQ, &x, &yq);
    7586          14 :   sqrtQ = sqrtr_abs(utor(Q,prec));
    7587          14 :   tau = mkcomplex(gadd(sstoQ(-1, NQ), uutoQ(1, 1000)),
    7588             :                   divru(sqrtQ, N));
    7589          14 :   den = gaddgs(gmulsg(NQ, tau), 1);
    7590          14 :   wtau = gdiv(gsub(gmulsg(x, tau), sstoQ(yq, Q)), den);
    7591          14 :   coe = gpowgs(gmul(sqrtQ, den), k);
    7592             : 
    7593          14 :   sqrtN = sqrtr_abs(utor(N,prec));
    7594          14 :   tau  = mulcxmI(gmul(tau,  sqrtN));
    7595          14 :   wtau = mulcxmI(gmul(wtau, sqrtN));
    7596          14 :   gN = utoipos(N);
    7597          14 :   L0 = mfthetaancreate(NULL, gN, gk); /* only for thetacost */
    7598          14 :   N0 = maxss(lfunthetacost(L0,real_i(tau), 0,bitprec),
    7599             :              lfunthetacost(L0,real_i(wtau),0,bitprec));
    7600          14 :   M = mfcoefs_mf(mf, N0, 1);
    7601          14 :   va = cgetg(dim+1, t_VEC);
    7602          14 :   vb = cgetg(dim+1, t_VEC);
    7603         105 :   for (j = 1; j <= dim; j++)
    7604             :   {
    7605          91 :     GEN L, v = vecslice(gel(M,j), 2, N0+1); /* remove a0 */
    7606          91 :     settyp(v, t_VEC); L = mfthetaancreate(v, gN, gk);
    7607          91 :     gel(va,j) = lfuntheta(L, tau,0,bitprec);
    7608          91 :     gel(vb,j) = lfuntheta(L,wtau,0,bitprec);
    7609             :   }
    7610          84 :   for (i = 1; i < lF; i++)
    7611             :   {
    7612          70 :     GEN z, FE = gel(MF,i);
    7613          70 :     long l = lg(FE);
    7614          70 :     z = cgetg(l, t_VEC);
    7615          70 :     for (j = 1; j < l; j++)
    7616             :     {
    7617          70 :       GEN f = gel(FE,j), a = RgV_dotproduct(va,f), b = RgV_dotproduct(vb,f);
    7618          70 :       GEN la = ground( gdiv(b, gmul(a,coe)) );
    7619          70 :       if (!gequal(gsqr(la), la2)) pari_err_PREC("mfatkineigenquad");
    7620          70 :       if (typ(la) == t_INT)
    7621             :       {
    7622          70 :         if (j != 1) pari_err_BUG("mfatkineigenquad");
    7623          70 :         z = const_vec(l-1, la); break;
    7624             :       }
    7625           0 :       gel(z,j) = la;
    7626             :     }
    7627          70 :     gel(Z,i) = z;
    7628             :   }
    7629          14 :   return Z;
    7630             : }
    7631             : 
    7632             : static GEN
    7633          84 : myusqrt(ulong a, long prec)
    7634             : {
    7635          84 :   if (a == 1UL) return gen_1;
    7636          70 :   if (uissquareall(a, &a)) return utoipos(a);
    7637          49 :   return sqrtr_abs(utor(a, prec));
    7638             : }
    7639             : /* Assume mf is a nontrivial new space, rational primitive character CHIP
    7640             :  * and (Q,FC) = 1 */
    7641             : static GEN
    7642         112 : mfatkinmatnewquad(GEN mf, GEN CHIP, long Q, long flag, long PREC)
    7643             : {
    7644         112 :   GEN cM, M, D, MF, den, vE, F = MF_get_newforms(mf);
    7645         112 :   long i, c, e, prec, bitprec, lF = lg(F), N = MF_get_N(mf), k = MF_get_k(mf);
    7646             : 
    7647         112 :   if (Q == 1) return mkvec4(gen_0, matid(MF_get_dim(mf)), gen_1, mf);
    7648         112 :   den = gel(MF_get_Minv(mf), 2);
    7649         112 :   bitprec = expi(den) + 64;
    7650         112 :   if (!flag) bitprec = maxss(bitprec, prec2nbits(PREC));
    7651             : 
    7652          35 : START:
    7653         112 :   prec = nbits2prec(bitprec);
    7654         112 :   vE = mfeigenembed(mf, prec);
    7655         112 :   M = cgetg(lF, t_VEC);
    7656         294 :   for (i = 1; i < lF; i++) gel(M,i) = RgC_embedall(gel(F,i), gel(vE,i));
    7657         112 :   if (Q != N)
    7658             :   {
    7659          56 :     D = mfatkineigenquad(mf, CHIP, Q, M, bitprec);
    7660          56 :     c = odd(k)? Q: 1;
    7661             :   }
    7662             :   else
    7663             :   {
    7664          56 :     D = mffrickeeigen(mf, vE, prec);
    7665          56 :     c = mfcharmodulus(CHIP); if (odd(k)) c = -Q/c;
    7666             :   }
    7667         112 :   D = shallowconcat1(D);
    7668         112 :   if (vec_isconst(D)) { MF = diagonal_shallow(D); flag = 0; }
    7669             :   else
    7670             :   {
    7671          63 :     M = shallowconcat1(M);
    7672          63 :     MF = RgM_mul(matmuldiagonal(M,D), ginv(M));
    7673             :   }
    7674         112 :   if (!flag) return mkvec4(gen_0, MF, gen_1, mf);
    7675             : 
    7676          21 :   if (c > 0)
    7677          21 :     cM = myusqrt(c, PREC);
    7678             :   else
    7679             :   {
    7680           0 :     MF = imag_i(MF); c = -c;
    7681           0 :     cM = mkcomplex(gen_0, myusqrt(c,PREC));
    7682             :   }
    7683          21 :   if (c != 1) MF = RgM_Rg_mul(MF, myusqrt(c,prec));
    7684          21 :   MF = grndtoi(RgM_Rg_mul(MF,den), &e);
    7685          21 :   if (e > -32) { bitprec <<= 1; goto START; }
    7686          21 :   MF = RgM_Rg_div(MF, den);
    7687          21 :   if (is_rational_t(typ(cM)) && !isint1(cM))
    7688           0 :   { MF = RgM_Rg_div(MF, cM); cM = gen_1; }
    7689          21 :   return mkvec4(gen_0, MF, cM, mf);
    7690             : }
    7691             : 
    7692             : /* let CHI mod N, Q || N, return \bar{CHI_Q} * CHI_{N/Q} */
    7693             : static GEN
    7694         105 : mfcharAL(GEN CHI, long Q)
    7695             : {
    7696         105 :   GEN G = gel(CHI,1), c = gel(CHI,2), cycc, d, P, E, F;
    7697         105 :   long l = lg(c), N = mfcharmodulus(CHI), i;
    7698         105 :   if (N == Q) return mfcharconj(CHI);
    7699          49 :   if (N == 1) return CHI;
    7700          42 :   CHI = leafcopy(CHI);
    7701          42 :   gel(CHI,2) = d = leafcopy(c);
    7702          42 :   F = znstar_get_faN(G);
    7703          42 :   P = gel(F,1);
    7704          42 :   E = gel(F,2);
    7705          42 :   cycc = znstar_get_conreycyc(G);
    7706          42 :   if (!odd(Q) && equaliu(gel(P,1), 2) && E[1] >= 3)
    7707          14 :     gel(d,2) = Fp_neg(gel(d,2), gel(cycc,2));
    7708          56 :   else for (i = 1; i < l; i++)
    7709          28 :     if (!umodui(Q, gel(P,i))) gel(d,i) = Fp_neg(gel(d,i), gel(cycc,i));
    7710          42 :   return CHI;
    7711             : }
    7712             : static long
    7713         238 : atkin_get_NQ(long N, long Q, const char *f)
    7714             : {
    7715         238 :   long NQ = N / Q;
    7716         238 :   if (N % Q) pari_err_DOMAIN(f,"N % Q","!=",gen_0,utoi(Q));
    7717         238 :   if (ugcd(NQ, Q) > 1) pari_err_DOMAIN(f,"gcd(Q,N/Q)","!=",gen_1,utoi(Q));
    7718         238 :   return NQ;
    7719             : }
    7720             : 
    7721             : /* transform mf to new_NEW if possible */
    7722             : static GEN
    7723        1379 : MF_set_new(GEN mf)
    7724             : {
    7725        1379 :   GEN vMjd, vj, gk = MF_get_gk(mf);
    7726             :   long l, j;
    7727        1379 :   if (MF_get_space(mf) != mf_CUSP
    7728        1379 :       || typ(gk) != t_INT || itou(gk) == 1) return mf;
    7729         182 :   vMjd = MFcusp_get_vMjd(mf); l = lg(vMjd);
    7730         182 :   if (l > 1 && gel(vMjd,1)[1] != MF_get_N(mf)) return mf; /* oldspace != 0 */
    7731         175 :   mf = shallowcopy(mf);
    7732         175 :   gel(mf,1) = shallowcopy(gel(mf,1));
    7733         175 :   MF_set_space(mf, mf_NEW);
    7734         175 :   vj = cgetg(l, t_VECSMALL);
    7735         938 :   for (j = 1; j < l; j++) vj[j] = gel(vMjd, j)[2];
    7736         175 :   gel(mf,4) = vj; return mf;
    7737             : }
    7738             : 
    7739             : /* if flag = 1, rationalize, else don't */
    7740             : static GEN
    7741         217 : mfatkininit_i(GEN mf, long Q, long flag, long prec)
    7742             : {
    7743             :   GEN M, B, C, CHI, CHIAL, G, chi, P, z, g, mfB, s, Mindex, Minv;
    7744         217 :   long j, l, lim, ord, FC, NQ, cQ, nk, dk, N = MF_get_N(mf);
    7745             : 
    7746         217 :   B = MF_get_basis(mf); l = lg(B);
    7747         217 :   M = cgetg(l, t_MAT); if (l == 1) return mkvec4(gen_0,M,gen_1,mf);
    7748         217 :   Qtoss(MF_get_gk(mf), &nk,&dk);
    7749         217 :   Q = labs(Q);
    7750         217 :   NQ = atkin_get_NQ(N, Q, "mfatkininit");
    7751         217 :   CHI = MF_get_CHI(mf);
    7752         217 :   CHI = mfchartoprimitive(CHI, &FC);
    7753         217 :   ord = mfcharorder(CHI);
    7754         217 :   mf = MF_set_new(mf);
    7755         217 :   if (MF_get_space(mf) == mf_NEW && ord <= 2 && NQ % FC == 0 && dk == 1)
    7756         112 :     return mfatkinmatnewquad(mf, CHI, Q, flag, prec);
    7757             :   /* now flag != 0 */
    7758         105 :   G   = gel(CHI,1);
    7759         105 :   chi = gel(CHI,2);
    7760         105 :   if (Q == N) { g = mkmat22s(0, -1, N, 0); cQ = NQ; } /* Fricke */
    7761             :   else
    7762             :   {
    7763          28 :     GEN F, gQP = utoi(ugcd(Q, FC));
    7764             :     long t, v;
    7765          28 :     chi = znchardecompose(G, chi, gQP);
    7766          28 :     F = znconreyconductor(G, chi, &chi);
    7767          28 :     G = znstar0(F,1);
    7768          28 :     (void)cbezout(Q, NQ, &t, &v);
    7769          28 :     g = mkmat22s(Q*t, 1, -N*v, Q);
    7770          28 :     cQ = -NQ*v;
    7771             :   }
    7772         105 :   C = s = gen_1;
    7773             :   /* N.B. G,chi are G_Q,chi_Q [primitive] at this point */
    7774         105 :   if (lg(chi) != 1) C = ginv( znchargauss(G, chi, gen_1, prec2nbits(prec)) );
    7775         105 :   if (dk == 1)
    7776          84 :   { if (odd(nk)) s = myusqrt(Q,prec); }
    7777             :   else
    7778             :   {
    7779          21 :     long r = nk >> 1; /* k-1/2 */
    7780          21 :     s = gpow(utoipos(Q), mkfracss(odd(r)? 1: 3, 4), prec);
    7781          21 :     if (odd(cQ))
    7782             :     {
    7783          21 :       long t = r + ((cQ-1) >> 1);
    7784          21 :       s = mkcomplex(s, odd(t)? gneg(s): s);
    7785             :     }
    7786             :   }
    7787         105 :   if (!isint1(s)) C = gmul(C, s);
    7788         105 :   CHIAL = mfcharAL(CHI, Q);
    7789         105 :   if (dk == 2)
    7790             :   {
    7791          21 :     ulong q = odd(Q)? Q << 2: Q, Nq = ulcm(q, mfcharmodulus(CHIAL));
    7792          21 :     CHIAL = induceN(Nq, CHIAL);
    7793          21 :     CHIAL = mfcharmul(CHIAL, induce(gel(CHIAL,1), utoipos(q)));
    7794             :   }
    7795         105 :   CHIAL = mfchartoprimitive(CHIAL,NULL);
    7796         105 :   mfB = gequal(CHIAL,CHI)? mf: mfinit_Nndkchi(N,nk,dk,CHIAL,MF_get_space(mf),0);
    7797         105 :   Mindex = MF_get_Mindex(mfB);
    7798         105 :   Minv = MF_get_Minv(mfB);
    7799         105 :   P = z = NULL;
    7800         105 :   if (ord > 2) { P = mfcharpol(CHI); z = rootsof1u_cx(ord, prec); }
    7801         105 :   lim = maxss(mfsturm(mfB), mfsturm(mf)) + 1;
    7802         343 :   for (j = 1; j < l; j++)
    7803             :   {
    7804         238 :     GEN v = mfslashexpansion(mf, gel(B,j), g, lim, 0, NULL, prec+EXTRAPREC64);
    7805             :     long junk;
    7806         238 :     if (!isint1(C)) v = RgV_Rg_mul(v, C);
    7807         238 :     v = bestapprnf(v, P, z, prec);
    7808         238 :     v = vecpermute_partial(v, Mindex, &junk);
    7809         238 :     v = Minv_RgC_mul(Minv, v); /* cf mftobasis_i */
    7810         238 :     gel(M, j) = v;
    7811             :   }
    7812         105 :   if (is_rational_t(typ(C)) && !gequal1(C)) { M = gdiv(M, C); C = gen_1; }
    7813         105 :   if (mfB == mf) mfB = gen_0;
    7814         105 :   return mkvec4(mfB, M, C, mf);
    7815             : }
    7816             : GEN
    7817          91 : mfatkininit(GEN mf, long Q, long prec)
    7818             : {
    7819          91 :   pari_sp av = avma;
    7820          91 :   mf = checkMF(mf); return gerepilecopy(av, mfatkininit_i(mf, Q, 1, prec));
    7821             : }
    7822             : static void
    7823          56 : checkmfa(GEN z)
    7824             : {
    7825          56 :   if (typ(z) != t_VEC || lg(z) != 5 || typ(gel(z,2)) != t_MAT
    7826          56 :       || !checkMF_i(gel(z,4))
    7827          56 :       || (!isintzero(gel(z,1)) && !checkMF_i(gel(z,1))))
    7828           0 :     pari_err_TYPE("mfatkin [please apply mfatkininit()]",z);
    7829          56 : }
    7830             : 
    7831             : /* Apply atkin Q to closure F */
    7832             : GEN
    7833          56 : mfatkin(GEN mfa, GEN F)
    7834             : {
    7835          56 :   pari_sp av = avma;
    7836             :   GEN z, mfB, MQ, mf;
    7837          56 :   checkmfa(mfa);
    7838          56 :   mfB= gel(mfa,1);
    7839          56 :   MQ = gel(mfa,2);
    7840          56 :   mf = gel(mfa,4);
    7841          56 :   if (typ(mfB) == t_INT) mfB = mf;
    7842          56 :   z = RgM_RgC_mul(MQ, mftobasis_i(mf,F));
    7843          56 :   return gerepileupto(av, mflinear(mfB, z));
    7844             : }
    7845             : 
    7846             : GEN
    7847          49 : mfatkineigenvalues(GEN mf, long Q, long prec)
    7848             : {
    7849          49 :   pari_sp av = avma;
    7850             :   GEN vF, L, CHI, M, mfatk, C, MQ, vE, mfB;
    7851             :   long N, NQ, l, i;
    7852             : 
    7853          49 :   mf = checkMF(mf); N = MF_get_N(mf);
    7854          49 :   vF = MF_get_newforms(mf); l = lg(vF);
    7855             :   /* N.B. k is integral */
    7856          49 :   if (l == 1) { set_avma(av); return cgetg(1, t_VEC); }
    7857          49 :   L = cgetg(l, t_VEC);
    7858          49 :   if (Q == 1)
    7859             :   {
    7860           7 :     GEN vP = MF_get_fields(mf);
    7861          21 :     for (i = 1; i < l; i++) gel(L,i) = const_vec(degpol(gel(vP,i)), gen_1);
    7862           7 :     return L;
    7863             :   }
    7864          42 :   vE = mfeigenembed(mf,prec);
    7865          42 :   if (Q == N) return gerepileupto(av, mffrickeeigen(mf, vE, prec));
    7866          21 :   Q = labs(Q);
    7867          21 :   NQ = atkin_get_NQ(N, Q, "mfatkineigenvalues"); /* != 1 */
    7868          21 :   mfatk = mfatkininit(mf, Q, prec);
    7869          21 :   mfB= gel(mfatk,1); if (typ(mfB) != t_VEC) mfB = mf;
    7870          21 :   MQ = gel(mfatk,2);
    7871          21 :   C  = gel(mfatk,3);
    7872          21 :   M = row(mfcoefs_mf(mfB,1,1), 2); /* vec of a_1(b_i) for mfbasis functions */
    7873          56 :   for (i = 1; i < l; i++)
    7874             :   {
    7875          35 :     GEN c = RgV_dotproduct(RgM_RgC_mul(MQ,gel(vF,i)), M); /* C * eigen_i */
    7876          35 :     gel(L,i) = Rg_embedall_i(c, gel(vE,i));
    7877             :   }
    7878          21 :   if (!gequal1(C)) L = gdiv(L, C);
    7879          21 :   CHI = MF_get_CHI(mf);
    7880          21 :   if (mfcharorder(CHI) <= 2 && NQ % mfcharconductor(CHI) == 0) L = ground(L);
    7881          21 :   return gerepilecopy(av, L);
    7882             : }
    7883             : 
    7884             : /* expand B_d V, keeping same length */
    7885             : static GEN
    7886        6370 : bdexpand(GEN V, long d)
    7887             : {
    7888             :   GEN W;
    7889             :   long N, n;
    7890        6370 :   if (d == 1) return V;
    7891        2352 :   N = lg(V)-1; W = zerovec(N);
    7892       46634 :   for (n = 0; n <= (N-1)/d; n++) gel(W, n*d+1) = gel(V, n+1);
    7893        2352 :   return W;
    7894             : }
    7895             : /* expand B_d V, increasing length up to lim */
    7896             : static GEN
    7897         294 : bdexpandall(GEN V, long d, long lim)
    7898             : {
    7899             :   GEN W;
    7900             :   long N, n;
    7901         294 :   if (d == 1) return V;
    7902          35 :   N = lg(V)-1; W = zerovec(lim);
    7903         259 :   for (n = 0; n <= N-1 && n*d <= lim; n++) gel(W, n*d+1) = gel(V, n+1);
    7904          35 :   return W;
    7905             : }
    7906             : 
    7907             : static void
    7908        9366 : parse_vecj(GEN T, GEN *E1, GEN *E2)
    7909             : {
    7910        9366 :   if (lg(T)==3) { *E1 = gel(T,1); *E2 = gel(T,2); }
    7911        5180 :   else { *E1 = T; *E2 = NULL; }
    7912        9366 : }
    7913             : 
    7914             : /* g in M_2(Z) ? */
    7915             : static int
    7916        2982 : check_M2Z(GEN g)
    7917        2982 : {  return typ(g) == t_MAT && lg(g) == 3 && lgcols(g) == 3 && RgM_is_ZM(g); }
    7918             : /* g in SL_2(Z) ? */
    7919             : static int
    7920        1806 : check_SL2Z(GEN g) { return check_M2Z(g) && equali1(ZM_det(g)); }
    7921             : 
    7922             : static GEN
    7923        9436 : mfcharcxeval(GEN CHI, long n, long prec)
    7924             : {
    7925        9436 :   ulong ord, N = mfcharmodulus(CHI);
    7926             :   GEN ordg;
    7927        9436 :   if (N == 1) return gen_1;
    7928        3696 :   if (ugcd(N, labs(n)) > 1) return gen_0;
    7929        3696 :   ordg = gmfcharorder(CHI);
    7930        3696 :   ord = itou(ordg);
    7931        3696 :   return rootsof1q_cx(znchareval_i(CHI,n,ordg), ord, prec);
    7932             : }
    7933             : 
    7934             : static GEN
    7935        5159 : RgV_shift(GEN V, GEN gn)
    7936             : {
    7937             :   long i, n, l;
    7938             :   GEN W;
    7939        5159 :   if (typ(gn) != t_INT) pari_err_BUG("RgV_shift [n not integral]");
    7940        5159 :   n = itos(gn);
    7941        5159 :   if (n < 0) pari_err_BUG("RgV_shift [n negative]");
    7942        5159 :   if (!n) return V;
    7943         112 :   W = cgetg_copy(V, &l); if (n > l-1) n = l-1;
    7944         308 :   for (i=1; i <= n; i++) gel(W,i) = gen_0;
    7945        4900 :   for (    ; i < l; i++) gel(W,i) = gel(V, i-n);
    7946         112 :   return W;
    7947             : }
    7948             : static GEN
    7949        7868 : hash_eisengacx(hashtable *H, void *E, long w, GEN ga, long n, long prec)
    7950             : {
    7951        7868 :   ulong h = H->hash(E);
    7952        7868 :   hashentry *e = hash_search2(H, E, h);
    7953             :   GEN v;
    7954        7868 :   if (e) v = (GEN)e->val;
    7955             :   else
    7956             :   {
    7957        5390 :     v = mfeisensteingacx((GEN)E, w, ga, n, prec);
    7958        5390 :     hash_insert2(H, E, (void*)v, h);
    7959             :   }
    7960        7868 :   return v;
    7961             : }
    7962             : static GEN
    7963        5159 : vecj_expand(GEN B, hashtable *H, long w, GEN ga, long n, long prec)
    7964             : {
    7965             :   GEN E1, E2, v;
    7966        5159 :   parse_vecj(B, &E1, &E2);
    7967        5159 :   v = hash_eisengacx(H, (void*)E1, w, ga, n, prec);
    7968        5159 :   if (E2)
    7969             :   {
    7970        2653 :     GEN u = hash_eisengacx(H, (void*)E2, w, ga, n, prec);
    7971        2653 :     GEN a = gadd(gel(v,1), gel(u,1));
    7972        2653 :     GEN b = RgV_mul_RgXn(gel(v,2), gel(u,2));
    7973        2653 :     v = mkvec2(a,b);
    7974             :   }
    7975        5159 :   return v;
    7976             : }
    7977             : static GEN
    7978        1085 : shift_M(GEN M, GEN Valpha, long w)
    7979             : {
    7980        1085 :   long i, l = lg(Valpha);
    7981        1085 :   GEN almin = vecmin(Valpha);
    7982        6244 :   for (i = 1; i < l; i++)
    7983             :   {
    7984        5159 :     GEN alpha = gel(Valpha, i), gsh = gmulsg(w, gsub(alpha,almin));
    7985        5159 :     gel(M,i) = RgV_shift(gel(M,i), gsh);
    7986             :   }
    7987        1085 :   return almin;
    7988             : }
    7989             : static GEN mfeisensteinspaceinit(GEN NK);
    7990             : #if 0
    7991             : /* ga in M_2^+(Z)), n >= 0 */
    7992             : static GEN
    7993             : mfgaexpansion_init(GEN mf, GEN ga, long n, long prec)
    7994             : {
    7995             :   GEN M, Mvecj, vecj, almin, Valpha;
    7996             :   long i, w, l, N = MF_get_N(mf), c = itos(gcoeff(ga,2,1));
    7997             :   hashtable *H;
    7998             : 
    7999             :   if (c % N == 0)
    8000             :   { /* ga in G_0(N), trivial case; w = 1 */
    8001             :     GEN chid = mfcharcxeval(MF_get_CHI(mf), itos(gcoeff(ga,2,2)), prec);
    8002             :     return mkvec2(chid, utoi(n));
    8003             :   }
    8004             : 
    8005             :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    8006             :   if (lg(Mvecj) < 5) pari_err_IMPL("mfgaexpansion_init in this case");
    8007             :   w = mfcuspcanon_width(N, c);
    8008             :   vecj = gel(Mvecj, 3);
    8009             :   l = lg(vecj);
    8010             :   M = cgetg(l, t_VEC);
    8011             :   Valpha = cgetg(l, t_VEC);
    8012             :   H = hash_create(l, (ulong(*)(void*))&hash_GEN,
    8013             :                      (int(*)(void*,void*))&gidentical, 1);
    8014             :   for (i = 1; i < l; i++)
    8015             :   {
    8016             :     GEN v = vecj_expand(gel(vecj,i), H, w, ga, n, prec);
    8017             :     gel(Valpha,i) = gel(v,1);
    8018             :     gel(M,i) = gel(v,2);
    8019             :   }
    8020             :   almin = shift_M(M, Valpha, w);
    8021             :   return mkvec3(almin, utoi(w), M);
    8022             : }
    8023             : /* half-integer weight not supported; vF = [F,eisendec(F)].
    8024             :  * Minit = mfgaexpansion_init(mf, ga, n, prec) */
    8025             : static GEN
    8026             : mfgaexpansion_with_init(GEN Minit, GEN vF)
    8027             : {
    8028             :   GEN v;
    8029             :   if (lg(Minit) == 3)
    8030             :   { /* ga in G_0(N) */
    8031             :     GEN chid = gel(Minit,1), gn = gel(Minit,2);
    8032             :     v = mfcoefs_i(gel(vF,1), itou(gn), 1);
    8033             :     v = mkvec3(gen_0, gen_1, RgV_Rg_mul(v,chid));
    8034             :   }
    8035             :   else
    8036             :   {
    8037             :     GEN V = RgM_RgC_mul(gel(Minit,3), gel(vF,2));
    8038             :     v = mkvec3(gel(Minit,1), gel(Minit,2), V);
    8039             :   }
    8040             :   return v;
    8041             : }
    8042             : #endif
    8043             : 
    8044             : /* B = mfeisensteindec(F) already embedded, ga in M_2^+(Z)), n >= 0 */
    8045             : static GEN
    8046        1085 : mfgaexpansion_i(GEN mf, GEN B0, GEN ga, long n, long prec)
    8047             : {
    8048        1085 :   GEN M, Mvecj, vecj, almin, Valpha, B, E = NULL;
    8049        1085 :   long i, j, w, nw, l, N = MF_get_N(mf), bit = prec2nbits(prec) / 2;
    8050             :   hashtable *H;
    8051             : 
    8052        1085 :   Mvecj = obj_check(mf, MF_EISENSPACE);
    8053        1085 :   if (lg(Mvecj) < 5) { E = gel(Mvecj, 2); Mvecj = gel(Mvecj, 1); }
    8054        1085 :   vecj = gel(Mvecj, 3);
    8055        1085 :   l = lg(vecj);
    8056        1085 :   B = cgetg(l, t_COL);
    8057        1085 :   M = cgetg(l, t_VEC);
    8058        1085 :   Valpha = cgetg(l, t_VEC);
    8059        1085 :   w = mfZC_width(N, gel(ga,1));
    8060        1085 :   nw = E ? n + w : n;
    8061        1085 :   H = hash_create(l, (ulong(*)(void*))&hash_GEN,
    8062             :                      (int(*)(void*,void*))&gidentical, 1);
    8063        9177 :   for (i = j = 1; i < l; i++)
    8064             :   {
    8065             :     GEN v;
    8066        8092 :     if (gequal0(gel(B0,i))) continue;
    8067        5159 :     v = vecj_expand(gel(vecj,i), H, w, ga, nw, prec);
    8068        5159 :     gel(B,j) = gel(B0,i);
    8069        5159 :     gel(Valpha,j) = gel(v,1);
    8070        5159 :     gel(M,j) = gel(v,2); j++;
    8071             :   }
    8072        1085 :   setlg(Valpha, j);
    8073        1085 :   setlg(B, j);
    8074        1085 :   setlg(M, j); l = j;
    8075        1085 :   if (l == 1) return mkvec3(gen_0, utoi(w), zerovec(n+1));
    8076        1085 :   almin = shift_M(M, Valpha, w);
    8077        1085 :   B = RgM_RgC_mul(M, B); l = lg(B);
    8078      151445 :   for (i = 1; i < l; i++)
    8079      150360 :     if (gexpo(gel(B,i)) < -bit) gel(B,i) = gen_0;
    8080        1085 :   settyp(B, t_VEC);
    8081        1085 :   if (E)
    8082             :   {
    8083             :     GEN v, e;
    8084          56 :     long ell = 0, vB, ve;
    8085         126 :     for (i = 1; i < l; i++)
    8086         126 :       if (!gequal0(gel(B,i))) break;
    8087          56 :     vB = i-1;
    8088          56 :     v = hash_eisengacx(H, (void*)E, w, ga, n + vB, prec);
    8089          56 :     e = gel(v,2); l = lg(e);
    8090          56 :     for (i = 1; i < l; i++)
    8091          56 :       if (!gequal0(gel(e,i))) break;
    8092          56 :     ve = i-1;
    8093          56 :     almin = gsub(almin, gel(v,1));
    8094          56 :     if (gsigne(almin) < 0)
    8095             :     {
    8096           0 :       GEN gell = gceil(gmulsg(-w, almin));
    8097           0 :       ell = itos(gell);
    8098           0 :       almin = gadd(almin, gdivgu(gell, w));
    8099           0 :       if (nw < ell) pari_err_IMPL("alpha < 0 in mfgaexpansion");
    8100             :     }
    8101          56 :     if (ve) { ell += ve; e = vecslice(e, ve+1, l-1); }
    8102          56 :     B = vecslice(B, ell + 1, minss(n + ell + 1, lg(B)-1));
    8103          56 :     B = RgV_div_RgXn(B, e);
    8104             :   }
    8105        1085 :   return mkvec3(almin, utoi(w), B);
    8106             : }
    8107             : 
    8108             : /* Theta multiplier: assume 4 | C, (C,D)=1 */
    8109             : static GEN
    8110         343 : mfthetamultiplier(GEN C, GEN D)
    8111             : {
    8112         343 :   long s = kronecker(C, D);
    8113         343 :   if (Mod4(D) == 1) return s > 0 ? gen_1: gen_m1;
    8114          84 :   return s > 0? powIs(3): gen_I();
    8115             : }
    8116             : /* theta | [*,*;C,D] defined over Q(i) [else over Q] */
    8117             : static int
    8118          56 : mfthetaI(long C, long D) { return odd(C) || (D & 3) == 3; }
    8119             : /* (theta | M) [0..n], assume (C,D) = 1 */
    8120             : static GEN
    8121         343 : mfthetaexpansion(GEN M, long n)
    8122             : {
    8123         343 :   GEN w, s, al, sla, E, V = zerovec(n+1), C = gcoeff(M,2,1), D = gcoeff(M,2,2);
    8124         343 :   long lim, la, f, C4 = Mod4(C);
    8125         343 :   switch (C4)
    8126             :   {
    8127          70 :     case 0: al = gen_0; w = gen_1;
    8128          70 :       s = mfthetamultiplier(C,D);
    8129          70 :       lim = usqrt(n); gel(V, 1) = s;
    8130          70 :       s = gmul2n(s, 1);
    8131         756 :       for (f = 1; f <= lim; f++) gel(V, f*f + 1) = s;
    8132          70 :       break;
    8133         105 :     case 2: al = uutoQ(1,4); w = gen_1;
    8134         105 :       E = subii(C, shifti(D,1)); /* (E, D) = 1 */
    8135         105 :       s = gmul2n(mfthetamultiplier(E, D), 1);
    8136         105 :       if ((!signe(E) && equalim1(D)) || (signe(E) > 0 && signe(C) < 0))
    8137          14 :         s = gneg(s);
    8138         105 :       lim = (usqrt(n << 2) - 1) >> 1;
    8139         966 :       for (f = 0; f <= lim; f++) gel(V, f*(f+1) + 1) = s;
    8140         105 :       break;
    8141         168 :     default: al = gen_0; w = utoipos(4);
    8142         168 :       la = (-Mod4(D)*C4) & 3L;
    8143         168 :       E = negi(addii(D, mului(la, C)));
    8144         168 :       s = mfthetamultiplier(E, C); /* (E,C) = 1 */
    8145         168 :       if (signe(C) < 0 && signe(E) >= 0) s = gneg(s);
    8146         168 :       s = gsub(s, mulcxI(s));
    8147         168 :       sla = gmul(s, powIs(-la));
    8148         168 :       lim = usqrt(n); gel(V, 1) = gmul2n(s, -1);
    8149        1708 :       for (f = 1; f <= lim; f++) gel(V, f*f + 1) = odd(f) ? sla : s;
    8150         168 :       break;
    8151             :   }
    8152         343 :   return mkvec3(al, w, V);
    8153             : }
    8154             : 
    8155             : /* F 1/2 integral weight */
    8156             : static GEN
    8157         343 : mf2gaexpansion(GEN mf2, GEN F, GEN ga, long n, long prec)
    8158             : {
    8159         343 :   GEN FT = mfmultheta(F), mf = obj_checkbuild(mf2, MF_MF2INIT, &mf2init);
    8160         343 :   GEN res, V1, Tres, V2, al, V, gsh, C = gcoeff(ga,2,1);
    8161         343 :   long w2, N = MF_get_N(mf), w = mfcuspcanon_width(N, umodiu(C,N));
    8162         343 :   long ext = (Mod4(C) != 2)? 0: (w+3) >> 2;
    8163         343 :   long prec2 = prec + nbits2extraprec((long)M_PI/(2*M_LN2)*sqrt(n + ext));
    8164         343 :   res = mfgaexpansion(mf, FT, ga, n + ext, prec2);
    8165         343 :   Tres = mfthetaexpansion(ga, n + ext);
    8166         343 :   V1 = gel(res,3);
    8167         343 :   V2 = gel(Tres,3);
    8168         343 :   al = gsub(gel(res,1), gel(Tres,1));
    8169         343 :   w2 = itos(gel(Tres,2));
    8170         343 :   if (w != itos(gel(res,2)) || w % w2)
    8171           0 :     pari_err_BUG("mf2gaexpansion [incorrect w2 or w]");
    8172         343 :   if (w2 != w) V2 = bdexpand(V2, w/w2);
    8173         343 :   V = RgV_div_RgXn(V1, V2);
    8174         343 :   gsh = gfloor(gmulsg(w, al));
    8175         343 :   if (!gequal0(gsh))
    8176             :   {
    8177          35 :     al = gsub(al, gdivgu(gsh, w));
    8178          35 :     if (gsigne(gsh) > 0)
    8179             :     {
    8180           0 :       V = RgV_shift(V, gsh);
    8181           0 :       V = vecslice(V, 1, n + 1);
    8182             :     }
    8183             :     else
    8184             :     {
    8185          35 :       long sh = -itos(gsh), i;
    8186          35 :       if (sh > ext) pari_err_BUG("mf2gaexpansion [incorrect sh]");
    8187         154 :       for (i = 1; i <= sh; i++)
    8188         119 :         if (!gequal0(gel(V,i))) pari_err_BUG("mf2gaexpansion [sh too large]");
    8189          35 :       V = vecslice(V, sh+1, n + sh+1);
    8190             :     }
    8191             :   }
    8192         343 :   obj_free(mf); return mkvec3(al, stoi(w), gprec_wtrunc(V, prec));
    8193             : }
    8194             : 
    8195             : static GEN
    8196          77 : mfgaexpansionatkin(GEN mf, GEN F, GEN C, GEN D, long Q, long n, long prec)
    8197             : {
    8198          77 :   GEN mfa = mfatkininit_i(mf, Q, 0, prec), MQ = gel(mfa,2);
    8199          77 :   long i, FC, k = MF_get_k(mf);
    8200          77 :   GEN x, v, V, z, s, CHI = mfchartoprimitive(MF_get_CHI(mf), &FC);
    8201             : 
    8202             :   /* V = mfcoefs(F | w_Q, n), can't use mfatkin because MQ nonrational */
    8203          77 :   V = RgM_RgC_mul(mfcoefs_mf(mf,n,1), RgM_RgC_mul(MQ, mftobasis_i(mf,F)));
    8204          77 :   (void)bezout(utoipos(Q), C, &x, &v);
    8205          77 :   s = mfchareval(CHI, (umodiu(x, FC) * umodiu(D, FC)) % FC);
    8206          77 :   s = gdiv(s, gpow(utoipos(Q), uutoQ(k,2), prec));
    8207          77 :   V = RgV_Rg_mul(V, s);
    8208          77 :   z = rootsof1powinit(umodiu(D,Q)*umodiu(v,Q) % Q, Q, prec);
    8209       11613 :   for (i = 1; i <= n+1; i++) gel(V,i) = gmul(gel(V,i), rootsof1pow(z, i-1));
    8210          77 :   return mkvec3(gen_0, utoipos(Q), V);
    8211             : }
    8212             : 
    8213             : static long
    8214          70 : inveis_extraprec(long N, GEN ga, GEN Mvecj, long n)
    8215             : {
    8216          70 :   long e, w = mfZC_width(N, gel(ga,1));
    8217          70 :   GEN f, E = gel(Mvecj,2), v = mfeisensteingacx(E, w, ga, n, DEFAULTPREC);
    8218          70 :   v = gel(v,2);
    8219          70 :   f = RgV_to_RgX(v,0); n -= RgX_valrem(f, &f);
    8220          70 :   e = gexpo(RgXn_inv(f, n+1));
    8221          70 :   return (e > 0)? nbits2extraprec(e): 0;
    8222             : }
    8223             : /* allow F of the form [F, mf_eisendec(F)]~ */
    8224             : static GEN
    8225        1799 : mfgaexpansion(GEN mf, GEN F, GEN ga, long n, long prec)
    8226             : {
    8227        1799 :   GEN v, EF = NULL, res, Mvecj, c, d;
    8228             :   long precnew, N;
    8229             : 
    8230        1799 :   if (n < 0) pari_err_DOMAIN("mfgaexpansion", "n", "<", gen_0, stoi(n));
    8231        1799 :   if (typ(F) == t_COL && lg(F) == 3) { EF = gel(F,2); F = gel(F,1); }
    8232        1799 :   if (!checkmf_i(F)) pari_err_TYPE("mfgaexpansion", F);
    8233        1799 :   if (!check_SL2Z(ga)) pari_err_TYPE("mfgaexpansion",ga);
    8234        1799 :   if (typ(mf_get_gk(F)) != t_INT) return mf2gaexpansion(mf, F, ga, n, prec);
    8235        1456 :   c = gcoeff(ga,2,1);
    8236        1456 :   d = gcoeff(ga,2,2);
    8237        1456 :   N = MF_get_N(mf);
    8238        1456 :   if (!umodiu(c, mf_get_N(F)))
    8239             :   { /* trivial case: ga in Gamma_0(N) */
    8240         294 :     long w = mfcuspcanon_width(N, umodiu(c,N));
    8241         294 :     GEN CHI = mf_get_CHI(F);
    8242         294 :     GEN chid = mfcharcxeval(CHI, umodiu(d,mfcharmodulus(CHI)), prec);
    8243         294 :     v = mfcoefs_i(F, n/w, 1); if (!isint1(chid)) v = RgV_Rg_mul(v,chid);
    8244         294 :     return mkvec3(gen_0, stoi(w), bdexpandall(v,w,n+1));
    8245             :   }
    8246        1162 :   mf = MF_set_new(mf);
    8247        1162 :   if (MF_get_space(mf) == mf_NEW)
    8248             :   {
    8249         483 :     long cN = umodiu(c,N), g = ugcd(cN,N), Q = N/g;
    8250         483 :     GEN CHI = MF_get_CHI(mf);
    8251         483 :     if (ugcd(cN, Q)==1 && mfcharorder(CHI) <= 2
    8252         231 :                        && g % mfcharconductor(CHI) == 0
    8253         119 :                        && degpol(mf_get_field(F)) == 1)
    8254          77 :       return mfgaexpansionatkin(mf, F, c, d, Q, n, prec);
    8255             :   }
    8256        1085 :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    8257        1085 :   precnew = prec;
    8258        1085 :   if (lg(Mvecj) < 5) precnew += inveis_extraprec(N, ga, Mvecj, n);
    8259        1085 :   if (!EF) EF = mf_eisendec(mf, F, precnew);
    8260        1085 :   res = mfgaexpansion_i(mf, EF, ga, n, precnew);
    8261        1085 :   return precnew == prec ? res : gprec_wtrunc(res, prec);
    8262             : }
    8263             : 
    8264             : /* parity = -1 or +1 */
    8265             : static GEN
    8266         217 : findd(long N, long parity)
    8267             : {
    8268         217 :   GEN L, D = mydivisorsu(N);
    8269         217 :   long i, j, l = lg(D);
    8270         217 :   L = cgetg(l, t_VEC);
    8271        1218 :   for (i = j = 1; i < l; i++)
    8272             :   {
    8273        1001 :     long d = D[i];
    8274        1001 :     if (parity == -1) d = -d;
    8275        1001 :     if (sisfundamental(d)) gel(L,j++) = stoi(d);
    8276             :   }
    8277         217 :   setlg(L,j); return L;
    8278             : }
    8279             : /* does ND contain a divisor of N ? */
    8280             : static int
    8281         413 : seenD(long N, GEN ND)
    8282             : {
    8283         413 :   long j, l = lg(ND);
    8284         427 :   for (j = 1; j < l; j++)
    8285          14 :     if (N % ND[j] == 0) return 1;
    8286         413 :   return 0;
    8287             : }
    8288             : static GEN
    8289          63 : search_levels(GEN vN, const char *f)
    8290             : {
    8291          63 :   switch(typ(vN))
    8292             :   {
    8293          28 :     case t_INT: vN = mkvecsmall(itos(vN)); break;
    8294          35 :     case t_VEC: case t_COL: vN = ZV_to_zv(vN); break;
    8295           0 :     case t_VECSMALL: vN = leafcopy(vN); break;
    8296           0 :     default: pari_err_TYPE(f, vN);
    8297             :   }
    8298          63 :   vecsmall_sort(vN); return vN;
    8299             : }
    8300             : GEN
    8301          28 : mfsearch(GEN NK, GEN V, long space)
    8302             : {
    8303          28 :   pari_sp av = avma;
    8304             :   GEN F, gk, NbyD, vN;
    8305             :   long n, nk, dk, parity, nV, i, lvN;
    8306             : 
    8307          28 :   if (typ(NK) != t_VEC || lg(NK) != 3) pari_err_TYPE("mfsearch", NK);
    8308          28 :   gk = gel(NK,2);
    8309          28 :   if (typ(gmul2n(gk, 1)) != t_INT) pari_err_TYPE("mfsearch [k]", gk);
    8310          28 :   switch(typ(V))
    8311             :   {
    8312          28 :     case t_VEC: V = shallowtrans(V);
    8313          28 :     case t_COL: break;
    8314           0 :     default: pari_err_TYPE("mfsearch [V]", V);
    8315             :   }
    8316          28 :   vN = search_levels(gel(NK,1), "mfsearch [N]");
    8317          28 :   if (gequal0(V)) { set_avma(av); retmkvec(mftrivial()); }
    8318          14 :   lvN = lg(vN);
    8319             : 
    8320          14 :   Qtoss(gk, &nk,&dk);
    8321          14 :   parity = (dk == 1 && odd(nk)) ? -1 : 1;
    8322          14 :   nV = lg(V)-2;
    8323          14 :   F = cgetg(1, t_VEC);
    8324          14 :   NbyD = const_vec(vN[lvN-1], cgetg(1,t_VECSMALL));
    8325         231 :   for (n = 1; n < lvN; n++)
    8326             :   {
    8327         217 :     long N = vN[n];
    8328             :     GEN L;
    8329         217 :     if (N <= 0 || (dk == 2 && (N & 3))) continue;
    8330         217 :     L = findd(N, parity);
    8331         630 :     for (i = 1; i < lg(L); i++)
    8332             :     {
    8333         413 :       GEN mf, M, CO, gD = gel(L,i);
    8334         413 :       GEN *ND = (GEN*)NbyD + itou(gD); /* points to NbyD[|D|] */
    8335             : 
    8336         413 :       if (seenD(N, *ND)) continue;
    8337         413 :       mf = mfinit_Nndkchi(N, nk, dk, get_mfchar(gD), space, 1);
    8338         413 :       M = mfcoefs_mf(mf, nV, 1);
    8339         413 :       CO = inverseimage(M, V); if (lg(CO) == 1) continue;
    8340             : 
    8341          42 :       F = vec_append(F, mflinear(mf,CO));
    8342          42 :       *ND = vecsmall_append(*ND, N); /* add to NbyD[|D|] */
    8343             :     }
    8344             :   }
    8345          14 :   return gerepilecopy(av, F);
    8346             : }
    8347             : 
    8348             : static GEN
    8349         889 : search_from_split(GEN mf, GEN vap, GEN vlp)
    8350             : {
    8351         889 :   pari_sp av = avma;
    8352         889 :   long lvlp = lg(vlp), j, jv, l1;
    8353         889 :   GEN v, NK, S1, S, M = NULL;
    8354             : 
    8355         889 :   S1 = gel(split_i(mf, 1, 0), 1); /* rational newforms */
    8356         889 :   l1 = lg(S1);
    8357         889 :   if (l1 == 1) return gc_NULL(av);
    8358         455 :   v = cgetg(l1, t_VEC);
    8359         455 :   S = MF_get_S(mf);
    8360         455 :   NK = mf_get_NK(gel(S,1));
    8361         455 :   if (lvlp > 1) M = rowpermute(mfcoefs_mf(mf, vlp[lvlp-1], 1), vlp);
    8362         980 :   for (j = jv = 1; j < l1; j++)
    8363             :   {
    8364         525 :     GEN vF = gel(S1,j);
    8365             :     long t;
    8366         658 :     for (t = lvlp-1; t > 0; t--)
    8367             :     { /* lhs = vlp[j]-th coefficient of eigenform */
    8368         595 :       GEN rhs = gel(vap,t), lhs = RgMrow_RgC_mul(M, vF, t);
    8369         595 :       if (!gequal(lhs, rhs)) break;
    8370             :     }
    8371         525 :     if (!t) gel(v,jv++) = mflinear_i(NK,S,vF);
    8372             :   }
    8373         455 :   if (jv == 1) return gc_NULL(av);
    8374          63 :   setlg(v,jv); return v;
    8375             : }
    8376             : GEN
    8377          35 : mfeigensearch(GEN NK, GEN AP)
    8378             : {
    8379          35 :   pari_sp av = avma;
    8380          35 :   GEN k, vN, vap, vlp, vres = cgetg(1, t_VEC), D;
    8381             :   long n, lvN, i, l, even;
    8382             : 
    8383          35 :   if (!AP) l = 1;
    8384             :   else
    8385             :   {
    8386          28 :     l = lg(AP);
    8387          28 :     if (typ(AP) != t_VEC) pari_err_TYPE("mfeigensearch",AP);
    8388             :   }
    8389          35 :   vap = cgetg(l, t_VEC);
    8390          35 :   vlp = cgetg(l, t_VECSMALL);
    8391          35 :   if (l > 1)
    8392             :   {
    8393          28 :     GEN perm = indexvecsort(AP, mkvecsmall(1));
    8394          77 :     for (i = 1; i < l; i++)
    8395             :     {
    8396          49 :       GEN v = gel(AP,perm[i]), gp, ap;
    8397          49 :       if (typ(v) != t_VEC || lg(v) != 3) pari_err_TYPE("mfeigensearch", AP);
    8398          49 :       gp = gel(v,1);
    8399          49 :       ap = gel(v,2);
    8400          49 :       if (typ(gp) != t_INT || (typ(ap) != t_INT && typ(ap) != t_INTMOD))
    8401           0 :         pari_err_TYPE("mfeigensearch", AP);
    8402          49 :       gel(vap,i) = ap;
    8403          49 :       vlp[i] = itos(gp)+1; if (vlp[i] < 0) pari_err_TYPE("mfeigensearch", AP);
    8404             :     }
    8405             :   }
    8406          35 :   l = lg(NK);
    8407          35 :   if (typ(NK) != t_VEC || l != 3) pari_err_TYPE("mfeigensearch",NK);
    8408          35 :   k = gel(NK,2);
    8409          35 :   vN = search_levels(gel(NK,1), "mfeigensearch [N]");
    8410          35 :   lvN = lg(vN);
    8411          35 :   vecsmall_sort(vlp);
    8412          35 :   even = !mpodd(k);
    8413         980 :   for (n = 1; n < lvN; n++)
    8414             :   {
    8415         945 :     pari_sp av2 = avma;
    8416             :     GEN mf, L;
    8417         945 :     long N = vN[n];
    8418         945 :     if (even) D = gen_1;
    8419             :     else
    8420             :     {
    8421         112 :       long r = (N&3L);
    8422         112 :       if (r == 1 || r == 2) continue;
    8423          56 :       D = stoi( corediscs(-N, NULL) ); /* < 0 */
    8424             :     }
    8425         889 :     mf = mfinit_i(mkvec3(utoipos(N), k, D), mf_NEW);
    8426         889 :     L = search_from_split(mf, vap, vlp);
    8427         889 :     if (L) vres = shallowconcat(vres, L); else set_avma(av2);
    8428             :   }
    8429          35 :   return gerepilecopy(av, vres);
    8430             : }
    8431             : 
    8432             : /* tf_{N,k}(n) */
    8433             : static GEN
    8434     4501119 : mfnewtracecache(long N, long k, long n, cachenew_t *cache)
    8435             : {
    8436     4501119 :   GEN C = NULL, S;
    8437             :   long lcache;
    8438     4501119 :   if (!n) return gen_0;
    8439     4363093 :   S = gel(cache->vnew,N);
    8440     4363093 :   lcache = lg(S);
    8441     4363093 :   if (n < lcache) C = gel(S, n);
    8442     4363093 :   if (C) cache->newHIT++;
    8443     2599588 :   else C = mfnewtrace_i(N,k,n,cache);
    8444     4363093 :   cache->newTOTAL++;
    8445     4363093 :   if (n < lcache) gel(S,n) = C;
    8446     4363093 :   return C;
    8447             : }
    8448             : 
    8449             : static long
    8450        1393 : mfdim_Nkchi(long N, long k, GEN CHI, long space)
    8451             : {
    8452        1393 :   if (k < 0 || badchar(N,k,CHI)) return 0;
    8453        1092 :   if (k == 0)
    8454          35 :     return mfcharistrivial(CHI) && !space_is_cusp(space)? 1: 0;
    8455        1057 :   switch(space)
    8456             :   {
    8457         245 :     case mf_NEW: return mfnewdim(N,k,CHI);
    8458         196 :     case mf_CUSP:return mfcuspdim(N,k,CHI);
    8459         168 :     case mf_OLD: return mfolddim(N,k,CHI);
    8460         217 :     case mf_FULL:return mffulldim(N,k,CHI);
    8461         231 :     case mf_EISEN: return mfeisensteindim(N,k,CHI);
    8462           0 :     default: pari_err_FLAG("mfdim");
    8463             :   }
    8464             :   return 0;/*LCOV_EXCL_LINE*/
    8465             : }
    8466             : static long
    8467        2114 : mf1dimsum(long N, long space)
    8468             : {
    8469        2114 :   switch(space)
    8470             :   {
    8471        1050 :     case mf_NEW:  return mf1newdimsum(N);
    8472        1057 :     case mf_CUSP: return mf1cuspdimsum(N);
    8473           7 :     case mf_OLD:  return mf1olddimsum(N);
    8474             :   }
    8475           0 :   pari_err_FLAG("mfdim");
    8476             :   return 0; /*LCOV_EXCL_LINE*/
    8477             : }
    8478             : /* mfdim for k = nk/dk */
    8479             : static long
    8480       44744 : mfdim_Nndkchi(long N, long nk, long dk, GEN CHI, long space)
    8481       43463 : { return (dk == 2)? mf2dim_Nkchi(N, nk >> 1, CHI, space)
    8482       88186 :                   : mfdim_Nkchi(N, nk, CHI, space); }
    8483             : /* FIXME: use direct dim Gamma1(N) formula, don't compute individual spaces */
    8484             : static long
    8485         252 : mfkdimsum(long N, long k, long dk, long space)
    8486             : {
    8487         252 :   GEN w = mfchars(N, k, dk, NULL);
    8488         252 :   long i, j, D = 0, l = lg(w);
    8489        1239 :   for (i = j = 1; i < l; i++)
    8490             :   {
    8491         987 :     GEN CHI = gel(w,i);
    8492         987 :     long d = mfdim_Nndkchi(N,k,dk,CHI,space);
    8493         987 :     if (d) D += d * myeulerphiu(mfcharorder(CHI));
    8494             :   }
    8495         252 :   return D;
    8496             : }
    8497             : static GEN
    8498         105 : mf1dims(long N, GEN vCHI, long space)
    8499             : {
    8500         105 :   GEN D = NULL;
    8501         105 :   switch(space)
    8502             :   {
    8503          56 :     case mf_NEW: D = mf1newdimall(N, vCHI); break;
    8504          21 :     case mf_CUSP:D = mf1cuspdimall(N, vCHI); break;
    8505          28 :     case mf_OLD: D = mf1olddimall(N, vCHI); break;
    8506           0 :     default: pari_err_FLAG("mfdim");
    8507             :   }
    8508         105 :   return D;
    8509             : }
    8510             : static GEN
    8511        2961 : mfkdims(long N, long k, long dk, GEN vCHI, long space)
    8512             : {
    8513        2961 :   GEN D, w = mfchars(N, k, dk, vCHI);
    8514        2961 :   long i, j, l = lg(w);
    8515        2961 :   D = cgetg(l, t_VEC);
    8516       46592 :   for (i = j = 1; i < l; i++)
    8517             :   {
    8518       43631 :     GEN CHI = gel(w,i);
    8519       43631 :     long d = mfdim_Nndkchi(N,k,dk,CHI,space);
    8520       43631 :     if (vCHI)
    8521         574 :       gel(D, j++) = mkvec2s(d, 0);
    8522       43057 :     else if (d)
    8523        2520 :       gel(D, j++) = fmt_dim(CHI, d, 0);
    8524             :   }
    8525        2961 :   setlg(D,j); return D;
    8526             : }
    8527             : GEN
    8528        5719 : mfdim(GEN NK, long space)
    8529             : {
    8530        5719 :   pari_sp av = avma;
    8531             :   long N, k, dk, joker;
    8532             :   GEN CHI, mf;
    8533        5719 :   if ((mf = checkMF_i(NK))) return utoi(MF_get_dim(mf));
    8534        5586 :   checkNK2(NK, &N, &k, &dk, &CHI, 2);
    8535        5586 :   if (!CHI) joker = 1;
    8536             :   else
    8537        2611 :     switch(typ(CHI))
    8538             :     {
    8539        2373 :       case t_INT: joker = 2; break;
    8540         112 :       case t_COL: joker = 3; break;
    8541         126 :       default: joker = 0; break;
    8542             :     }
    8543        5586 :   if (joker)
    8544             :   {
    8545             :     long d;
    8546             :     GEN D;
    8547        5460 :     if (k < 0) switch(joker)
    8548             :     {
    8549           0 :       case 1: return cgetg(1,t_VEC);
    8550           7 :       case 2: return gen_0;
    8551           0 :       case 3: return mfdim0all(CHI);
    8552             :     }
    8553        5453 :     if (k == 0)
    8554             :     {
    8555          28 :       if (space_is_cusp(space)) switch(joker)
    8556             :       {
    8557           7 :         case 1: return cgetg(1,t_VEC);
    8558           0 :         case 2: return gen_0;
    8559           7 :         case 3: return mfdim0all(CHI);
    8560             :       }
    8561          14 :       switch(joker)
    8562             :       {
    8563             :         long i, l;
    8564           7 :         case 1: retmkvec(fmt_dim(mfchartrivial(),0,0));
    8565           0 :         case 2: return gen_1;
    8566           7 :         case 3: l = lg(CHI); D = cgetg(l,t_VEC);
    8567          35 :                 for (i = 1; i < l; i++)
    8568             :                 {
    8569          28 :                   long t = mfcharistrivial(gel(CHI,i));
    8570          28 :                   gel(D,i) = mkvec2(t? gen_1: gen_0, gen_0);
    8571             :                 }
    8572           7 :                 return D;
    8573             :       }
    8574             :     }
    8575        5425 :     if (dk == 1 && k == 1 && space != mf_EISEN)
    8576         105 :     {
    8577        2219 :       long fix = 0, space0 = space;
    8578        2219 :       if (space == mf_FULL) space = mf_CUSP; /* remove Eisenstein part */
    8579        2219 :       if (joker == 2)
    8580             :       {
    8581        2114 :         d = mf1dimsum(N, space);
    8582        2114 :         if (space0 == mf_FULL) d += mfkdimsum(N,k,dk,mf_EISEN);/*add it back*/
    8583        2114 :         return gc_utoi(av, d);
    8584             :       }
    8585             :       /* must initialize explicitly: trivial spaces for E_k/S_k differ */
    8586         105 :       if (space0 == mf_FULL)
    8587             :       {
    8588           7 :         if (!CHI) fix = 1; /* must remove 0 spaces */
    8589           7 :         CHI = mfchars(N, k, dk, CHI);
    8590             :       }
    8591         105 :       D = mf1dims(N, CHI, space);
    8592         105 :       if (space0 == mf_FULL)
    8593             :       {
    8594           7 :         GEN D2 = mfkdims(N, k, dk, CHI, mf_EISEN);
    8595           7 :         D = merge_dims(D, D2, fix? CHI: NULL);
    8596             :       }
    8597             :     }
    8598             :     else
    8599             :     {
    8600        3206 :       if (joker==2) { d = mfkdimsum(N,k,dk,space); return gc_utoi(av,d); }
    8601        2954 :       D = mfkdims(N, k, dk, CHI, space);
    8602             :     }
    8603        3059 :     if (!CHI) return gerepileupto(av, vecsort(D, mkvecsmall(1)));
    8604         105 :     return gerepilecopy(av, D);
    8605             :   }
    8606         126 :   return utoi( mfdim_Nndkchi(N, k, dk, CHI, space) );
    8607             : }
    8608             : 
    8609             : GEN
    8610         357 : mfbasis(GEN NK, long space)
    8611             : {
    8612         357 :   pari_sp av = avma;
    8613             :   long N, k, dk;
    8614             :   GEN mf, CHI;
    8615         357 :   if ((mf = checkMF_i(NK))) return gconcat(gel(mf,2), gel(mf,3));
    8616          14 :   checkNK2(NK, &N, &k, &dk, &CHI, 0);
    8617          14 :   if (dk == 2) return gerepilecopy(av, mf2basis(N, k>>1, CHI, NULL, space));
    8618          14 :   mf = mfinit_Nkchi(N, k, CHI, space, 1);
    8619          14 :   return gerepilecopy(av, MF_get_basis(mf));
    8620             : }
    8621             : 
    8622             : static GEN
    8623          49 : deg1ser_shallow(GEN a1, GEN a0, long v, long e)
    8624          49 : { return RgX_to_ser(deg1pol_shallow(a1, a0, v), e+2); }
    8625             : /* r / x + O(1) */
    8626             : static GEN
    8627          49 : simple_pole(GEN r)
    8628             : {
    8629          49 :   GEN S = deg1ser_shallow(gen_0, r, 0, 1);
    8630          49 :   setvalser(S, -1); return S;
    8631             : }
    8632             : 
    8633             : /* F form, E embedding; mfa = mfatkininit or root number (eigenform case) */
    8634             : static GEN
    8635         161 : mflfuncreate(GEN mfa, GEN F, GEN E, GEN N, GEN gk)
    8636             : {
    8637         161 :   GEN LF = cgetg(8,t_VEC), polar = cgetg(1,t_COL), eps;
    8638         161 :   long k = itou(gk);
    8639         161 :   gel(LF,1) = lfuntag(t_LFUN_MFCLOS, mkvec3(F,E,gen_1));
    8640         161 :   if (typ(mfa) != t_VEC)
    8641          98 :     eps = mfa; /* cuspidal eigenform: root number; no poles */
    8642             :   else
    8643             :   { /* mfatkininit */
    8644          63 :     GEN a0, b0, vF, vG, G = NULL;
    8645          63 :     GEN M = gel(mfa,2), C = gel(mfa,3), mf = gel(mfa,4);
    8646          63 :     M = gdiv(mfmatembed(E, M), C);
    8647          63 :     vF = mfvecembed(E, mftobasis_i(mf, F));
    8648          63 :     vG = RgM_RgC_mul(M, vF);
    8649          63 :     if (gequal(vF,vG)) eps = gen_1;
    8650          49 :     else if (gequal(vF,gneg(vG))) eps = gen_m1;
    8651             :     else
    8652             :     { /* not self-dual */
    8653          42 :       eps = NULL;
    8654          42 :       G = mfatkin(mfa, F);
    8655          42 :       gel(LF,2) = lfuntag(t_LFUN_MFCLOS, mkvec3(G,E,ginv(C)));
    8656          42 :       gel(LF,6) = powIs(k);
    8657             :     }
    8658             :     /* polar part */
    8659          63 :     a0 = mfembed(E, mfcoef(F,0));
    8660          63 :     b0 = eps? gmul(eps,a0): gdiv(mfembed(E, mfcoef(G,0)), C);
    8661          63 :     if (!gequal0(b0))
    8662             :     {
    8663          28 :       b0 = mulcxpowIs(gmul2n(b0,1), k);
    8664          28 :       polar = vec_append(polar, mkvec2(gk, simple_pole(b0)));
    8665             :     }
    8666          63 :     if (!gequal0(a0))
    8667             :     {
    8668          21 :       a0 = gneg(gmul2n(a0,1));
    8669          21 :       polar = vec_append(polar, mkvec2(gen_0, simple_pole(a0)));
    8670             :     }
    8671             :   }
    8672         161 :   if (eps) /* self-dual */
    8673             :   {
    8674         119 :     gel(LF,2) = mfcharorder(mf_get_CHI(F)) <= 2? gen_0: gen_1;
    8675         119 :     gel(LF,6) = mulcxpowIs(eps,k);
    8676             :   }
    8677         161 :   gel(LF,3) = mkvec2(gen_0, gen_1);
    8678         161 :   gel(LF,4) = gk;
    8679         161 :   gel(LF,5) = N;
    8680         161 :   if (lg(polar) == 1) setlg(LF,7); else gel(LF,7) = polar;
    8681         161 :   return LF;
    8682             : }
    8683             : static GEN
    8684         133 : mflfuncreateall(long sd, GEN mfa, GEN F, GEN vE, GEN gN, GEN gk)
    8685             : {
    8686         133 :   long i, l = lg(vE);
    8687         133 :   GEN L = cgetg(l, t_VEC);
    8688         294 :   for (i = 1; i < l; i++)
    8689         161 :     gel(L,i) = mflfuncreate(sd? gel(mfa,i): mfa, F, gel(vE,i), gN, gk);
    8690         133 :   return L;
    8691             : }
    8692             : GEN
    8693          84 : lfunmf(GEN mf, GEN F, long bitprec)
    8694             : {
    8695          84 :   pari_sp av = avma;
    8696          84 :   long i, l, prec = nbits2prec(bitprec);
    8697             :   GEN L, gk, gN;
    8698          84 :   mf = checkMF(mf);
    8699          84 :   gk = MF_get_gk(mf);
    8700          84 :   gN = MF_get_gN(mf);
    8701          84 :   if (typ(gk)!=t_INT) pari_err_IMPL("half-integral weight");
    8702          84 :   if (F)
    8703             :   {
    8704             :     GEN v;
    8705          77 :     long s = MF_get_space(mf);
    8706          77 :     if (!checkmf_i(F)) pari_err_TYPE("lfunmf", F);
    8707          77 :     if (!mfisinspace_i(mf, F)) err_space(F);
    8708          77 :     L = NULL;
    8709          77 :     if ((s == mf_NEW || s == mf_CUSP || s == mf_FULL)
    8710          63 :         && gequal(mfcoefs_i(F,1,1), mkvec2(gen_0,gen_1)))
    8711             :     { /* check if eigenform */
    8712          35 :       GEN vP, vF, b = mftobasis_i(mf, F);
    8713          35 :       long lF, d = degpol(mf_get_field(F));
    8714          35 :       v = mfsplit(mf, d, 0);
    8715          35 :       vF = gel(v,1);
    8716          35 :       vP = gel(v,2); lF = lg(vF);
    8717          35 :       for (i = 1; i < lF; i++)
    8718          28 :         if (degpol(gel(vP,i)) == d && gequal(gel(vF,i), b))
    8719             :         {
    8720          28 :           GEN vE = mfgetembed(F, prec);
    8721          28 :           GEN Z = mffrickeeigen_i(mf, mkvec(b), mkvec(vE), prec);
    8722          28 :           L = mflfuncreateall(1, gel(Z,1), F, vE, gN, gk);
    8723          28 :           break;
    8724             :         }
    8725             :     }
    8726          77 :     if (!L)
    8727             :     { /* not an eigenform: costly general case */
    8728          49 :       GEN mfa = mfatkininit_i(mf, itou(gN), 1, prec);
    8729          49 :       L = mflfuncreateall(0,mfa, F, mfgetembed(F,prec), gN, gk);
    8730             :     }
    8731          77 :     if (lg(L) == 2) L = gel(L,1);
    8732             :   }
    8733             :   else
    8734             :   {
    8735           7 :     GEN M = mfeigenbasis(mf), vE = mfeigenembed(mf, prec);
    8736           7 :     GEN v = mffrickeeigen(mf, vE, prec);
    8737           7 :     l = lg(vE); L = cgetg(l, t_VEC);
    8738          63 :     for (i = 1; i < l; i++)
    8739          56 :       gel(L,i) = mflfuncreateall(1,gel(v,i), gel(M,i), gel(vE,i), gN, gk);
    8740             :   }
    8741          84 :   return gerepilecopy(av, L);
    8742             : }
    8743             : 
    8744             : GEN
    8745          28 : mffromell(GEN E)
    8746             : {
    8747          28 :   pari_sp av = avma;
    8748             :   GEN mf, F, z, v, S;
    8749             :   long N, i, l;
    8750             : 
    8751          28 :   checkell(E);
    8752          28 :   if (ell_get_type(E) != t_ELL_Q) pari_err_TYPE("mfffromell [E not over Q]", E);
    8753          28 :   N = itos(ellQ_get_N(E));
    8754          28 :   mf = mfinit_i(mkvec2(utoi(N), gen_2), mf_NEW);
    8755          28 :   v = split_i(mf, 1, 0);
    8756          28 :   S = gel(v,1); l = lg(S); /* rational newforms */
    8757          28 :   F = tag(t_MF_ELL, mkNK(N,2,mfchartrivial()), E);
    8758          28 :   z = mftobasis_i(mf, F);
    8759          28 :   for(i = 1; i < l; i++)
    8760          28 :     if (gequal(z, gel(S,i))) break;
    8761          28 :   if (i == l) pari_err_BUG("mffromell [E is not modular]");
    8762          28 :   return gerepilecopy(av, mkvec3(mf, F, z));
    8763             : }
    8764             : 
    8765             : /* returns -1 if not, degree otherwise */
    8766             : long
    8767         140 : polishomogeneous(GEN P)
    8768             : {
    8769             :   long i, D, l;
    8770         140 :   if (typ(P) != t_POL) return 0;
    8771          77 :   D = -1; l = lg(P);
    8772         322 :   for (i = 2; i < l; i++)
    8773             :   {
    8774         245 :     GEN c = gel(P,i);
    8775             :     long d;
    8776         245 :     if (gequal0(c)) continue;
    8777         112 :     d = polishomogeneous(c);
    8778         112 :     if (d < 0) return -1;
    8779         112 :     if (D < 0) D = d + i-2; else if (D != d + i-2) return -1;
    8780             :   }
    8781          77 :   return D;
    8782             : }
    8783             : 
    8784             : /* M a pp((Gram q)^(-1)) ZM; P a homogeneous t_POL, is P spherical ? */
    8785             : static int
    8786          28 : RgX_isspherical(GEN M, GEN P)
    8787             : {
    8788          28 :   pari_sp av = avma;
    8789          28 :   GEN S, v = variables_vecsmall(P);
    8790          28 :   long i, j, l = lg(v);
    8791          28 :   if (l > lg(M)) pari_err(e_MISC, "too many variables in mffromqf");
    8792          21 :   S = gen_0;
    8793          63 :   for (j = 1; j < l; j++)
    8794             :   {
    8795          42 :     GEN Mj = gel(M, j), Pj = deriv(P, v[j]);
    8796         105 :     for (i = 1; i <= j; i++)
    8797             :     {
    8798          63 :       GEN c = gel(Mj, i);
    8799          63 :       if (!signe(c)) continue;
    8800          42 :       if (i != j) c = shifti(c, 1);
    8801          42 :       S = gadd(S, gmul(c, deriv(Pj, v[i])));
    8802             :     }
    8803             :   }
    8804          21 :   return gc_bool(av, gequal0(S));
    8805             : }
    8806             : 
    8807             : static GEN
    8808          49 : c_QFsimple_i(long n, GEN Q, GEN P)
    8809             : {
    8810          49 :   GEN V, v = qfrep0(Q, utoi(n), 1);
    8811          49 :   long i, l = lg(v);
    8812          49 :   V = cgetg(l+1, t_VEC);
    8813          49 :   if (!P || equali1(P))
    8814             :   {
    8815          42 :     gel(V,1) = gen_1;
    8816         420 :     for (i = 2; i <= l; i++) gel(V,i) = utoi(v[i-1] << 1);
    8817             :   }
    8818             :   else
    8819             :   {
    8820           7 :     gel(V,1) = gcopy(P);
    8821           7 :     for (i = 2; i <= l; i++) gel(V,i) = gmulgu(P, v[i-1] << 1);
    8822             :   }
    8823          49 :   return V;
    8824             : }
    8825             : 
    8826             : /* v a t_VECSMALL of variable numbers, lg(r) >= lg(v), r is a vector of
    8827             :  * scalars [not involving any variable in v] */
    8828             : static GEN
    8829          14 : gsubstvec_i(GEN e, GEN v, GEN r)
    8830             : {
    8831          14 :   long i, l = lg(v);
    8832          42 :   for(i = 1; i < l; i++) e = gsubst(e, v[i], gel(r,i));
    8833          14 :   return e;
    8834             : }
    8835             : static GEN
    8836          56 : c_QF_i(long n, GEN Q, GEN P)
    8837             : {
    8838          56 :   pari_sp av = avma;
    8839             :   GEN V, v, va;
    8840             :   long i, l;
    8841          56 :   if (!P || typ(P) != t_POL) return gerepileupto(av, c_QFsimple_i(n, Q, P));
    8842           7 :   v = gel(minim(Q, utoi(2*n), NULL), 3);
    8843           7 :   va = variables_vecsmall(P);
    8844           7 :   V = zerovec(n + 1); l = lg(v);
    8845          21 :   for (i = 1; i < l; i++)
    8846             :   {
    8847          14 :     pari_sp av = avma;
    8848          14 :     GEN X = gel(v,i);
    8849          14 :     long c = (itos(qfeval(Q, X)) >> 1) + 1;
    8850          14 :     gel(V, c) = gerepileupto(av, gadd(gel(V, c), gsubstvec_i(P, va, X)));
    8851             :   }
    8852           7 :   return gmul2n(V, 1);
    8853             : }
    8854             : 
    8855             : GEN
    8856          77 : mffromqf(GEN Q, GEN P)
    8857             : {
    8858          77 :   pari_sp av = avma;
    8859             :   GEN G, Qi, F, D, N, mf, v, gk, chi;
    8860             :   long m, d, space;
    8861          77 :   if (typ(Q) != t_MAT) pari_err_TYPE("mffromqf", Q);
    8862          77 :   if (!RgM_is_ZM(Q) || !qfiseven(Q))
    8863           0 :     pari_err_TYPE("mffromqf [not integral or even]", Q);
    8864          77 :   m = lg(Q)-1;
    8865          77 :   Qi = ZM_inv(Q, &N);
    8866          77 :   if (!qfiseven(Qi)) N = shifti(N, 1);
    8867          77 :   d = 0;
    8868          77 :   if (!P || gequal1(P)) P = NULL;
    8869             :   else
    8870             :   {
    8871          35 :     P = simplify_shallow(P);
    8872          35 :     if (typ(P) == t_POL)
    8873             :     {
    8874          28 :       d = polishomogeneous(P);
    8875          28 :       if (d < 0) pari_err_TYPE("mffromqf [not homogeneous t_POL]", P);
    8876          28 :       if (!RgX_isspherical(Qi, P))
    8877           7 :         pari_err_TYPE("mffromqf [not a spherical t_POL]", P);
    8878             :     }
    8879             :   }
    8880          63 :   gk = uutoQ(m + 2*d, 2);
    8881          63 :   D = ZM_det(Q);
    8882          63 :   if (!odd(m)) { if ((m & 3) == 2) D = negi(D); } else D = shifti(D, 1);
    8883          63 :   space = d > 0 ? mf_CUSP : mf_FULL;
    8884          63 :   G = znstar0(N,1);
    8885          63 :   chi = mkvec2(G, znchar_quad(G,D));
    8886          63 :   mf = mfinit(mkvec3(N, gk, chi), space);
    8887          63 :   if (odd(d))
    8888             :   {
    8889           7 :     F = mftrivial();
    8890           7 :     v = zerocol(MF_get_dim(mf));
    8891             :   }
    8892             :   else
    8893             :   {
    8894          56 :     F = c_QF_i(mfsturm(mf), Q, P);
    8895          56 :     v = mftobasis_i(mf, F);
    8896          56 :     F = mflinear(mf, v);
    8897             :   }
    8898          63 :   return gerepilecopy(av, mkvec3(mf, F, v));
    8899             : }
    8900             : 
    8901             : /***********************************************************************/
    8902             : /*                          Eisenstein Series                          */
    8903             : /***********************************************************************/
    8904             : /* \sigma_{k-1}(\chi,n) */
    8905             : static GEN
    8906       24192 : sigchi(long k, GEN CHI, long n)
    8907             : {
    8908       24192 :   pari_sp av = avma;
    8909       24192 :   GEN S = gen_1, D = mydivisorsu(u_ppo(n,mfcharmodulus(CHI)));
    8910       24192 :   long i, l = lg(D), ord = mfcharorder(CHI), vt = varn(mfcharpol(CHI));
    8911       83671 :   for (i = 2; i < l; i++) /* skip D[1] = 1 */
    8912             :   {
    8913       59479 :     long d = D[i], a = mfcharevalord(CHI, d, ord);
    8914       59479 :     S = gadd(S, Qab_Czeta(a, ord, powuu(d, k-1), vt));
    8915             :   }
    8916       24192 :   return gerepileupto(av,S);
    8917             : }
    8918             : 
    8919             : /* write n = n0*n1*n2, (n0,N1*N2) = 1, n1 | N1^oo, n2 | N2^oo;
    8920             :  * return NULL if (n,N1,N2) > 1, else return factoru(n0) */
    8921             : static GEN
    8922      649215 : sigchi2_dec(long n, long N1, long N2, long *pn1, long *pn2)
    8923             : {
    8924      649215 :   GEN P0, E0, P, E, fa = myfactoru(n);
    8925             :   long i, j, l;
    8926      649215 :   *pn1 = 1;
    8927      649215 :   *pn2 = 1;
    8928      649215 :   if (N1 == 1 && N2 == 1) return fa;
    8929      635593 :   P = gel(fa,1); l = lg(P);
    8930      635593 :   E = gel(fa,2);
    8931      635593 :   P0 = cgetg(l, t_VECSMALL);
    8932      635593 :   E0 = cgetg(l, t_VECSMALL);
    8933     1472373 :   for (i = j = 1; i < l; i++)
    8934             :   {
    8935      942088 :     long p = P[i], e = E[i];
    8936      942088 :     if (N1 % p == 0)
    8937             :     {
    8938      142520 :       if (N2 % p == 0) return NULL;
    8939       37212 :       *pn1 *= upowuu(p,e);
    8940             :     }
    8941      799568 :     else if (N2 % p == 0)
    8942      129059 :       *pn2 *= upowuu(p,e);
    8943      670509 :     else { P0[j] = p; E0[j] = e; j++; }
    8944             :   }
    8945      530285 :   setlg(P0, j);
    8946      530285 :   setlg(E0, j); return mkvec2(P0,E0);
    8947             : }
    8948             : 
    8949             : /* sigma_{k-1}(\chi_1,\chi_2,n), ord multiple of lcm(ord(CHI1),ord(CHI2)) */
    8950             : static GEN
    8951      593684 : sigchi2(long k, GEN CHI1, GEN CHI2, long n, long ord)
    8952             : {
    8953      593684 :   pari_sp av = avma;
    8954             :   GEN S, D;
    8955      593684 :   long i, l, n1, n2, vt, N1 = mfcharmodulus(CHI1), N2 = mfcharmodulus(CHI2);
    8956      593684 :   D = sigchi2_dec(n, N1, N2, &n1, &n2); if (!D) return gc_const(av, gen_0);
    8957      493059 :   D = divisorsu_fact(D); l = lg(D);
    8958      493059 :   vt = varn(mfcharpol(CHI1));
    8959     2122848 :   for (i = 1, S = gen_0; i < l; i++)
    8960             :   { /* S += d^(k-1)*chi1(d)*chi2(n/d) */
    8961     1629789 :     long a, d = n2*D[i], nd = n1*D[l-i]; /* (d,N1)=1; (n/d,N2) = 1 */
    8962     1629789 :     a = mfcharevalord(CHI1, d, ord) + mfcharevalord(CHI2, nd, ord);
    8963     1629789 :     if (a >= ord) a -= ord;
    8964     1629789 :     S = gadd(S, Qab_Czeta(a, ord, powuu(d, k-1), vt));
    8965             :   }
    8966      493059 :   return gerepileupto(av, S);
    8967             : }
    8968             : 
    8969             : /**************************************************************************/
    8970             : /**           Dirichlet characters with precomputed values               **/
    8971             : /**************************************************************************/
    8972             : /* CHI mfchar */
    8973             : static GEN
    8974       14266 : mfcharcxinit(GEN CHI, long prec)
    8975             : {
    8976       14266 :   GEN G = gel(CHI,1), chi = gel(CHI,2), z, V;
    8977       14266 :   GEN v = ncharvecexpo(G, znconrey_normalized(G,chi));
    8978       14266 :   long n, l = lg(v), o = mfcharorder(CHI);
    8979       14266 :   V = cgetg(l, t_VEC);
    8980       14266 :   z = grootsof1(o, prec); /* Mod(t, Phi_o(t)) -> e(1/o) */
    8981      123893 :   for (n = 1; n < l; n++) gel(V,n) = v[n] < 0? gen_0: gel(z, v[n]+1);
    8982       14266 :   return mkvecn(6, G, chi, gmfcharorder(CHI), v, V, mfcharpol(CHI));
    8983             : }
    8984             : /* v a "CHIvec" */
    8985             : static long
    8986    24694754 : CHIvec_N(GEN v) { return itou(znstar_get_N(gel(v,1))); }
    8987             : static GEN
    8988       15372 : CHIvec_CHI(GEN v)
    8989       15372 : { return mkvec4(gel(v,1), gel(v,2), gel(v,3), gel(v,6)); }
    8990             : /* character order */
    8991             : static long
    8992       35763 : CHIvec_ord(GEN v) { return itou(gel(v,3)); }
    8993             : /* character exponents, i.e. t such that chi(n) = e(t) */
    8994             : static GEN
    8995      422177 : CHIvec_expo(GEN v) { return gel(v,4); }
    8996             : /* character values chi(n) */
    8997             : static GEN
    8998    24048745 : CHIvec_val(GEN v) { return gel(v,5); }
    8999             : /* CHI(n) */
    9000             : static GEN
    9001    24038714 : mychareval(GEN v, long n)
    9002             : {
    9003    24038714 :   long N = CHIvec_N(v), ind = n%N;
    9004    24038714 :   if (ind <= 0) ind += N;
    9005    24038714 :   return gel(CHIvec_val(v), ind);
    9006             : }
    9007             : /* return c such that CHI(n) = e(c / ordz) or -1 if (n,N) > 1 */
    9008             : static long
    9009      422177 : mycharexpo(GEN v, long n)
    9010             : {
    9011      422177 :   long N = CHIvec_N(v), ind = n%N;
    9012      422177 :   if (ind <= 0) ind += N;
    9013      422177 :   return CHIvec_expo(v)[ind];
    9014             : }
    9015             : /* faster than mfcharparity */
    9016             : static long
    9017       54054 : CHIvec_parity(GEN v) { return mycharexpo(v,-1) ? -1: 1; }
    9018             : /**************************************************************************/
    9019             : 
    9020             : static ulong
    9021       55531 : sigchi2_Fl(long k, GEN CHI1vec, GEN CHI2vec, long n, GEN vz, ulong p)
    9022             : {
    9023       55531 :   pari_sp av = avma;
    9024       55531 :   long ordz = lg(vz)-2, i, l, n1, n2;
    9025       55531 :   ulong S = 0;
    9026       55531 :   GEN D = sigchi2_dec(n, CHIvec_N(CHI1vec), CHIvec_N(CHI2vec), &n1, &n2);
    9027       55531 :   if (!D) return gc_ulong(av,S);
    9028       50848 :   D = divisorsu_fact(D);
    9029       50848 :   l = lg(D);
    9030      172053 :   for (i = 1; i < l; i++)
    9031             :   { /* S += d^(k-1)*chi1(d)*chi2(n/d) */
    9032      121205 :     long a, d = n2*D[i], nd = n1*D[l-i]; /* (d,N1)=1, (n/d,N2)=1 */
    9033      121205 :     a = mycharexpo(CHI2vec, nd) + mycharexpo(CHI1vec, d);
    9034      121205 :     if (a >= ordz) a -= ordz;
    9035      121205 :     S = Fl_add(S, Qab_Czeta_Fl(a, vz, Fl_powu(d,k-1,p), p), p);
    9036             :   }
    9037       50848 :   return gc_ulong(av,S);
    9038             : }
    9039             : 
    9040             : /**********************************************************************/
    9041             : /* Fourier expansions of Eisenstein series                            */
    9042             : /**********************************************************************/
    9043             : /* L(CHI_t,0) / 2, CHI_t(n) = CHI(n)(t/n) as a character modulo N*t,
    9044             :  * order(CHI) | ord != 0 */
    9045             : static GEN
    9046        2618 : charLFwt1(long N, GEN CHI, long ord, long t)
    9047             : {
    9048             :   GEN S;
    9049             :   long r, vt;
    9050             : 
    9051        2618 :   if (N == 1 && t == 1) return mkfrac(gen_m1,stoi(4));
    9052        2618 :   S = gen_0; vt = varn(mfcharpol(CHI));
    9053      295435 :   for (r = 1; r < N; r++)
    9054             :   { /* S += r*chi(r) */
    9055             :     long a, c;
    9056      292817 :     if (ugcd(N,r) != 1) continue;
    9057      233310 :     a = mfcharevalord(CHI,r,ord);
    9058      233310 :     c = (t != 1 && kross(t, r) < 0)? -r: r;
    9059      233310 :     S = gadd(S, Qab_Czeta(a, ord, stoi(c), vt));
    9060             :   }
    9061        2618 :   return gdivgs(S, -2*N);
    9062             : }
    9063             : /* L(CHI,0) / 2, mod p */
    9064             : static ulong
    9065        2002 : charLFwt1_Fl(GEN CHIvec, GEN vz, ulong p)
    9066             : {
    9067        2002 :   long r, m = CHIvec_N(CHIvec);
    9068             :   ulong S;
    9069        2002 :   if (m == 1) return Rg_to_Fl(mkfrac(gen_m1,stoi(4)), p);
    9070        2002 :   S = 0;
    9071       95977 :   for (r = 1; r < m; r++)
    9072             :   { /* S += r*chi(r) */
    9073       93975 :     long a = mycharexpo(CHIvec,r);
    9074       93975 :     if (a < 0) continue;
    9075       91616 :     S = Fl_add(S, Qab_Czeta_Fl(a, vz, r, p), p);
    9076             :   }
    9077        2002 :   return Fl_div(Fl_neg(S,p), 2*m, p);
    9078             : }
    9079             : /* L(CHI_t,1-k) / 2, CHI_t(n) = CHI(n) * (t/n), order(CHI) | ord != 0;
    9080             :  * assume conductor of CHI_t divides N */
    9081             : static GEN
    9082        4298 : charLFwtk(long N, long k, GEN CHI, long ord, long t)
    9083             : {
    9084             :   GEN S, P, dS;
    9085             :   long r, vt;
    9086             : 
    9087        4298 :   if (k == 1) return charLFwt1(N, CHI, ord, t);
    9088        1680 :   if (N == 1 && t == 1) return gdivgs(bernfrac(k),-2*k);
    9089        1057 :   vt = varn(mfcharpol(CHI));
    9090        1057 :   P = bern_init(N, k, &dS);
    9091        1057 :   dS = mul_denom(dS, stoi(-2*N*k));
    9092       14126 :   for (r = 1, S = gen_0; r < N; r++)
    9093             :   { /* S += P(r)*chi(r) */
    9094             :     long a;
    9095             :     GEN C;
    9096       13069 :     if (ugcd(r,N) != 1) continue;
    9097       10472 :     a = mfcharevalord(CHI,r,ord);
    9098       10472 :     C = ZX_Z_eval(P, utoi(r));
    9099       10472 :     if (t != 1 && kross(t, r) < 0) C = gneg(C);
    9100       10472 :     S = gadd(S, Qab_Czeta(a, ord, C, vt));
    9101             :   }
    9102        1057 :   return gdiv(S, dS);
    9103             : }
    9104             : /* L(CHI,1-k) / 2, mod p */
    9105             : static ulong
    9106        2849 : charLFwtk_Fl(long k, GEN CHIvec, GEN vz, ulong p)
    9107             : {
    9108             :   GEN P, dS;
    9109             :   long r, m;
    9110             :   ulong S, d;
    9111        2849 :   if (k == 1) return charLFwt1_Fl(CHIvec, vz, p);
    9112         847 :   m = CHIvec_N(CHIvec);
    9113         847 :   if (m == 1) return Rg_to_Fl(gdivgs(bernfrac(k),-2*k), p);
    9114         504 :   P = ZX_to_Flx(bern_init(m, k, &dS), p);
    9115       10402 :   for (r = 1, S = 0; r < m; r++)
    9116             :   { /* S += P(r)*chi(r) */
    9117        9898 :     long a = mycharexpo(CHIvec,r);
    9118        9898 :     if (a < 0) continue;
    9119        8638 :     S = Fl_add(S, Qab_Czeta_Fl(a, vz, Flx_eval(P,r,p), p), p);
    9120             :   }
    9121         504 :   d = (2 * k * m) % p; if (dS) d = Fl_mul(d, umodiu(dS, p), p);
    9122         504 :   return Fl_div(Fl_neg(S,p), d, p);
    9123             : }
    9124             : 
    9125             : static GEN
    9126        7854 : mfeisenstein2_0(long k, GEN CHI1, GEN CHI2, long ord)
    9127             : {
    9128        7854 :   long N1 = mfcharmodulus(CHI1), N2 = mfcharmodulus(CHI2);
    9129        7854 :   if (k == 1 && N1 == 1) return charLFwtk(N2, 1, CHI2, ord, 1);
    9130        5243 :   if (N2 == 1) return charLFwtk(N1, k, CHI1, ord, 1);
    9131        3773 :   return gen_0;
    9132             : }
    9133             : static ulong
    9134        4340 : mfeisenstein2_0_Fl(long k, GEN CHI1vec, GEN CHI2vec, GEN vz, ulong p)
    9135             : {
    9136        4340 :   if (k == 1 && CHIvec_N(CHI1vec) == 1)
    9137        2002 :     return charLFwtk_Fl(k, CHI2vec, vz, p);
    9138        2338 :   else if (CHIvec_N(CHI2vec) == 1)
    9139         847 :     return charLFwtk_Fl(k, CHI1vec, vz, p);
    9140        1491 :   else return 0;
    9141             : }
    9142             : static GEN
    9143         133 : NK_eisen2(long k, GEN CHI1, GEN CHI2, long ord)
    9144             : {
    9145         133 :   long o, N = mfcharmodulus(CHI1)*mfcharmodulus(CHI2);
    9146         133 :   GEN CHI = mfcharmul(CHI1, CHI2);
    9147         133 :   o = mfcharorder(CHI);
    9148         133 :   if ((ord & 3) == 2) ord >>= 1;
    9149         133 :   if ((o & 3) == 2) o >>= 1;
    9150         133 :   if (ord != o) pari_err_IMPL("mfeisenstein for these characters");
    9151         126 :   return mkNK(N, k, CHI);
    9152             : }
    9153             : static GEN
    9154         371 : mfeisenstein_i(long k, GEN CHI1, GEN CHI2)
    9155             : {
    9156         371 :   long s = 1, ord, vt;
    9157             :   GEN E0, NK, vchi, T;
    9158         371 :   if (CHI2) { CHI2 = get_mfchar(CHI2); if (mfcharparity(CHI2) < 0) s = -s; }
    9159         371 :   if (CHI1) { CHI1 = get_mfchar(CHI1); if (mfcharparity(CHI1) < 0) s = -s; }
    9160         357 :   if (s != m1pk(k)) return mftrivial();
    9161         336 :   if (!CHI1) CHI1 = mfchartrivial();
    9162         336 :   if (!CHI2)
    9163             :   { /* E_k(chi1) */
    9164         203 :     vt = varn(mfcharpol(CHI1));
    9165         203 :     ord = mfcharorder(CHI1);
    9166         203 :     NK = mkNK(mfcharmodulus(CHI1), k, CHI1);
    9167         203 :     E0 = charLFwtk(mfcharmodulus(CHI1), k, CHI1, ord, 1);
    9168         203 :     vchi = mkvec3(E0, mkvec(mfcharpol(CHI1)), CHI1);
    9169         203 :     return tag(t_MF_EISEN, NK, vchi);
    9170             :   }
    9171             :   /* E_k(chi1,chi2) */
    9172         133 :   vt = varn(mfcharpol(CHI1));
    9173         133 :   ord = ulcm(mfcharorder(CHI1), mfcharorder(CHI2));
    9174         133 :   NK = NK_eisen2(k, CHI1, CHI2, ord);
    9175         126 :   E0 = mfeisenstein2_0(k, CHI1, CHI2, ord);
    9176         126 :   T = mkvec(polcyclo(ord, vt));
    9177         126 :   vchi = mkvec4(E0, T, CHI1, CHI2);
    9178         126 :   return tag2(t_MF_EISEN, NK, vchi, mkvecsmall2(ord,0));
    9179             : }
    9180             : GEN
    9181         371 : mfeisenstein(long k, GEN CHI1, GEN CHI2)
    9182             : {
    9183         371 :   pari_sp av = avma;
    9184         371 :   if (k < 1) pari_err_DOMAIN("mfeisenstein", "k", "<", gen_1, stoi(k));
    9185         371 :   return gerepilecopy(av, mfeisenstein_i(k, CHI1, CHI2));
    9186             : }
    9187             : 
    9188             : static GEN
    9189        2604 : mfeisenstein2all(long N0, GEN NK, long k, GEN CHI1, GEN CHI2, GEN T, long o)
    9190             : {
    9191        2604 :   GEN E, E0 = mfeisenstein2_0(k, CHI1,CHI2, o), vchi = mkvec4(E0, T, CHI1,CHI2);
    9192        2604 :   long j, d = (lg(T)==4)? itou(gmael(T,3,1)): 1;
    9193        2604 :   E = cgetg(d+1, t_VEC);
    9194        5327 :   for (j=1; j<=d; j++) gel(E,j) = tag2(t_MF_EISEN, NK,vchi,mkvecsmall2(o,j-1));
    9195        2604 :   return mfbdall(E, N0 / mf_get_N(gel(E,1)));
    9196             : }
    9197             : 
    9198             : /* list of characters on G = (Z/NZ)^*, v[i] = NULL if (i,N) > 1, else
    9199             :  * the conductor of Conrey label i, [conductor, primitive char].
    9200             :  * Trivial chi (label 1) comes first */
    9201             : static GEN
    9202        1169 : zncharsG(GEN G)
    9203             : {
    9204        1169 :   long i, l, N = itou(znstar_get_N(G));
    9205             :   GEN vCHI, V;
    9206        1169 :   if (N == 1) return mkvec2(gen_1,cgetg(1,t_COL));
    9207        1169 :   vCHI = const_vec(N,NULL);
    9208        1169 :   V = cyc2elts(znstar_get_conreycyc(G));
    9209        1169 :   l = lg(V);
    9210      207739 :   for (i = 1; i < l; i++)
    9211             :   {
    9212      206570 :     GEN chi0, chi = zc_to_ZC(gel(V,i)), n, F;
    9213      206570 :     F = znconreyconductor(G, chi, &chi0);
    9214      206570 :     if (typ(F) != t_INT) F = gel(F,1);
    9215      206570 :     n = znconreyexp(G, chi);
    9216      206570 :     gel(vCHI, itos(n)) = mkvec2(chi0, F);
    9217             :   }
    9218        1169 :   return vCHI;
    9219             : }
    9220             : 
    9221             : /* CHI primitive, f(CHI) | N. Return pairs (CHI1,CHI2) both primitive
    9222             :  * such that f(CHI1)*f(CHI2) | N and CHI1 * CHI2 = CHI;
    9223             :  * if k = 1, CHI1 is even; if k = 2, omit (1,1) if CHI = 1 */
    9224             : static GEN
    9225        1386 : mfeisensteinbasis_i(long N0, long k, GEN CHI)
    9226             : {
    9227        1386 :   GEN G = gel(CHI,1), chi = gel(CHI,2), vT = const_vec(myeulerphiu(N0), NULL);
    9228        1386 :   GEN CHI0, GN, chiN, Lchi, LG, V, RES, NK, T, C = mfcharpol(CHI);
    9229        1386 :   long i, j, l, n, n1, N, ord = mfcharorder(CHI);
    9230        1386 :   long F = mfcharmodulus(CHI), vt = varn(mfcharpol(CHI));
    9231             : 
    9232        1386 :   CHI0 = (F == 1)? CHI: mfchartrivial();
    9233        1386 :   j = 1; RES = cgetg(N0+1, t_VEC);
    9234        1386 :   T = gel(vT,ord) = Qab_trace_init(ord, ord, C, C);
    9235        1386 :   if (F != 1 || k != 2)
    9236             :   { /* N1 = 1 */
    9237        1232 :     NK = mkNK(F, k, CHI);
    9238        1232 :     gel(RES, j++) = mfeisenstein2all(N0, NK, k, CHI0, CHI, T, ord);
    9239        1232 :     if (F != 1 && k != 1)
    9240         329 :       gel(RES, j++) = mfeisenstein2all(N0, NK, k, CHI, CHI0, T, ord);
    9241             :   }
    9242        1386 :   if (N0 == 1) { setlg(RES,j); return RES; }
    9243        1316 :   GN = G; chiN = chi;
    9244        1316 :   if (F == N0) N = N0;
    9245             :   else
    9246             :   {
    9247         714 :     GEN faN = myfactoru(N0), P = gel(faN,1), E = gel(faN,2);
    9248         714 :     long lP = lg(P);
    9249        1848 :     for (i = N = 1; i < lP; i++)
    9250             :     {
    9251        1134 :       long p = P[i];
    9252        1134 :       N *= upowuu(p, maxuu(E[i]/2, z_lval(F,p)));
    9253             :     }
    9254         714 :     if ((N & 3) == 2) N >>= 1;
    9255         714 :     if (N == 1) { setlg(RES,j); return RES; }
    9256         567 :     if (F != N)
    9257             :     {
    9258         133 :       GN = znstar0(utoipos(N),1);
    9259         133 :       chiN = zncharinduce(G, chi, GN);
    9260             :     }
    9261             :   }
    9262        1169 :   LG = const_vec(N, NULL); /* LG[d] = znstar(d,1) or NULL */
    9263        1169 :   gel(LG,1) = gel(CHI0,1);
    9264        1169 :   gel(LG,F) = G;
    9265        1169 :   gel(LG,N) = GN;
    9266        1169 :   Lchi = coprimes_zv(N);
    9267        1169 :   n = itou(znconreyexp(GN,chiN));
    9268        1169 :   V = zncharsG(GN); l = lg(V);
    9269      263305 :   for (n1 = 2; n1 < l; n1++) /* skip 1 (trivial char) */
    9270             :   {
    9271      262136 :     GEN v = gel(V,n1), w, chi1, chi2, G1, G2, CHI1, CHI2;
    9272             :     long N12, N1, N2, no, o12, t, m;
    9273      262136 :     if (!Lchi[n1] || n1 == n) continue; /* skip trivial chi2 */
    9274      204197 :     chi1 = gel(v,1); N1 = itou(gel(v,2)); /* conductor of chi1 */
    9275      204197 :     w = gel(V, Fl_div(n,n1,N));
    9276      204197 :     chi2 = gel(w,1); N2 = itou(gel(w,2)); /* conductor of chi2 */
    9277      204197 :     N12 = N1 * N2;
    9278      204197 :     if (N0 % N12) continue;
    9279             : 
    9280        1771 :     G1 = gel(LG,N1); if (!G1) gel(LG,N1) = G1 = znstar0(utoipos(N1), 1);
    9281        1771 :     if (k == 1 && zncharisodd(G1,chi1)) continue;
    9282        1043 :     G2 = gel(LG,N2); if (!G2) gel(LG,N2) = G2 = znstar0(utoipos(N2), 1);
    9283        1043 :     CHI1 = mfcharGL(G1, chi1);
    9284        1043 :     CHI2 = mfcharGL(G2, chi2);
    9285        1043 :     o12 = ulcm(mfcharorder(CHI1), mfcharorder(CHI2));
    9286             :     /* remove Galois orbit: same trace */
    9287        1043 :     no = Fl_powu(n1, ord, N);
    9288        1414 :     for (t = 1+ord, m = n1; t <= o12; t += ord)
    9289             :     { /* m <-> CHI1^t, if t in Gal(Q(chi1,chi2)/Q), omit (CHI1^t,CHI2^t) */
    9290         371 :       m = Fl_mul(m, no, N); if (!m) break;
    9291         371 :       if (ugcd(t, o12) == 1) Lchi[m] = 0;
    9292             :     }
    9293        1043 :     T = gel(vT,o12);
    9294        1043 :     if (!T) T = gel(vT,o12) = Qab_trace_init(o12, ord, polcyclo(o12,vt), C);
    9295        1043 :     NK = mkNK(N12, k, CHI);
    9296        1043 :     gel(RES, j++) = mfeisenstein2all(N0, NK, k, CHI1, CHI2, T, o12);
    9297             :   }
    9298        1169 :   setlg(RES,j); return RES;
    9299             : }
    9300             : 
    9301             : static GEN
    9302         721 : mfbd_E2(GEN E2, long d, GEN CHI)
    9303             : {
    9304         721 :   GEN E2d = mfbd_i(E2, d);
    9305         721 :   GEN F = mkvec2(E2, E2d), L = mkvec2(gen_1, utoineg(d));
    9306             :   /* cannot use mflinear_i: E2 and E2d do not have the same level */
    9307         721 :   return tag3(t_MF_LINEAR, mkNK(d,2,CHI), F, L, gen_1);
    9308             : }
    9309             : /* C-basis of E_k(Gamma_0(N),chi). If k = 1, the first basis element must not
    9310             :  * vanish at oo [used in mf1basis]. Here E_1(CHI), whose q^0 coefficient
    9311             :  * does not vanish (since L(CHI,0) does not) *if* CHI is not trivial; which
    9312             :  * must be the case in weight 1.
    9313             :  *
    9314             :  * (k>=3): In weight k >= 3, basis is B(d) E(CHI1,(CHI/CHI1)_prim), where
    9315             :  * CHI1 is primitive modulo N1, and if N2 is the conductor of CHI/CHI1
    9316             :  * then d*N1*N2 | N.
    9317             :  * (k=2): In weight k=2, same if CHI is nontrivial. If CHI is trivial, must
    9318             :  * not take CHI1 trivial, and must add E_2(tau)-dE_2(d tau)), where
    9319             :  * d|N, d > 1.
    9320             :  * (k=1): In weight k=1, same as k >= 3 except that we restrict to CHI1 even */
    9321             : static GEN
    9322        1414 : mfeisensteinbasis(long N, long k, GEN CHI)
    9323             : {
    9324             :   long i, F;
    9325             :   GEN L;
    9326        1414 :   if (badchar(N, k, CHI)) return cgetg(1, t_VEC);
    9327        1414 :   if (k == 0) return mfcharistrivial(CHI)? mkvec(mf1()): cgetg(1, t_VEC);
    9328        1386 :   CHI = mfchartoprimitive(CHI, &F);
    9329        1386 :   L = mfeisensteinbasis_i(N, k, CHI);
    9330        1386 :   if (F == 1 && k == 2)
    9331             :   {
    9332         154 :     GEN v, E2 = mfeisenstein(2, NULL, NULL), D = mydivisorsu(N);
    9333         154 :     long nD = lg(D)-1;
    9334         154 :     v = cgetg(nD, t_VEC); L = vec_append(L,v);
    9335         868 :     for (i = 1; i < nD; i++) gel(v,i) = mfbd_E2(E2, D[i+1], CHI);
    9336             :   }
    9337        1386 :   return lg(L) == 1? L: shallowconcat1(L);
    9338             : }
    9339             : 
    9340             : static GEN
    9341          77 : not_in_space(GEN F, long flag)
    9342             : {
    9343          77 :   if (!flag) err_space(F);
    9344          70 :   return cgetg(1, t_COL);
    9345             : }
    9346             : /* when flag set, no error */
    9347             : GEN
    9348        1001 : mftobasis(GEN mf, GEN F, long flag)
    9349             : {
    9350        1001 :   pari_sp av2, av = avma;
    9351             :   GEN G, v, y, gk;
    9352        1001 :   long N, B, ismf = checkmf_i(F);
    9353             : 
    9354        1001 :   mf = checkMF(mf);
    9355        1001 :   if (ismf)
    9356             :   {
    9357         910 :     if (mfistrivial(F)) return zerocol(MF_get_dim(mf));
    9358         903 :     if (!mf_same_k(mf, F) || !mf_same_CHI(mf, F)) return not_in_space(F, flag);
    9359             :   }
    9360         952 :   N = MF_get_N(mf);
    9361         952 :   gk = MF_get_gk(mf);
    9362         952 :   if (ismf)
    9363             :   {
    9364         861 :     long NF = mf_get_N(F);
    9365         861 :     B = maxuu(mfsturmNgk(NF,gk), mfsturmNgk(N,gk)) + 1;
    9366         861 :     v = mfcoefs_i(F,B,1);
    9367             :   }
    9368             :   else
    9369             :   {
    9370          91 :     B = mfsturmNgk(N, gk) + 1;
    9371          91 :     switch(typ(F))
    9372             :     { /* F(0),...,F(lg(v)-2) */
    9373          63 :       case t_SER: v = sertocol(F); settyp(v,t_VEC); break;
    9374          14 :       case t_VEC: v = F; break;
    9375           7 :       case t_COL: v = shallowtrans(F); break;
    9376           7 :       default: pari_err_TYPE("mftobasis",F);
    9377             :                v = NULL;/*LCOV_EXCL_LINE*/
    9378             :     }
    9379          84 :     if (flag) B = minss(B, lg(v)-2);
    9380             :   }
    9381         945 :   y = mftobasis_i(mf, v);
    9382         945 :   if (typ(y) == t_VEC)
    9383             :   {
    9384          21 :     if (flag) return gerepilecopy(av, y);
    9385           0 :     pari_err(e_MISC, "not enough coefficients in mftobasis");
    9386             :   }
    9387         924 :   av2 = avma;
    9388         924 :   if (MF_get_space(mf) == mf_FULL || mfsturm(mf)+1 == B) return y;
    9389         469 :   G = mflinear(mf, y);
    9390         469 :   if (!gequal(v, mfcoefs_i(G, lg(v)-2,1))) y = NULL;
    9391         469 :   if (!y) { set_avma(av); return not_in_space(F, flag); }
    9392         434 :   set_avma(av2); return gerepileupto(av, y);
    9393             : }
    9394             : 
    9395             : /* assume N > 0; first cusp is always 0 */
    9396             : static GEN
    9397          49 : mfcusps_i(long N)
    9398             : {
    9399             :   long i, c, l;
    9400             :   GEN D, v;
    9401             : 
    9402          49 :   if (N == 1) return mkvec(gen_0);
    9403          49 :   D = mydivisorsu(N); l = lg(D); /* left on stack */
    9404          49 :   c = mfnumcuspsu_fact(myfactoru(N));
    9405          49 :   v = cgetg(c + 1, t_VEC);
    9406         350 :   for (i = c = 1; i < l; i++)
    9407             :   {
    9408         301 :     long C = D[i], NC = D[l-i], lima = ugcd(C, NC), A0, A;
    9409         889 :     for (A0 = 0; A0 < lima; A0++)
    9410         588 :       if (ugcd(A0, lima) == 1)
    9411             :       {
    9412         539 :         A = A0; while (ugcd(A,C) > 1) A += lima;
    9413         392 :         gel(v, c++) = uutoQ(A, C);
    9414             :       }
    9415             :   }
    9416          49 :   return v;
    9417             : }
    9418             : /* List of cusps of Gamma_0(N) */
    9419             : GEN
    9420          28 : mfcusps(GEN gN)
    9421             : {
    9422             :   long N;
    9423             :   GEN mf;
    9424          28 :   if (typ(gN) == t_INT) N = itos(gN);
    9425          14 :   else if ((mf = checkMF_i(gN))) N = MF_get_N(mf);
    9426           0 :   else { pari_err_TYPE("mfcusps", gN); N = 0; }
    9427          28 :   if (N <= 0) pari_err_DOMAIN("mfcusps", "N", "<=", gen_0, stoi(N));
    9428          28 :   return mfcusps_i(N);
    9429             : }
    9430             : 
    9431             : long
    9432         315 : mfcuspisregular(GEN NK, GEN cusp)
    9433             : {
    9434             :   long v, N, dk, nk, t, o;
    9435             :   GEN mf, CHI, go, A, C, g, c, d;
    9436         315 :   if ((mf = checkMF_i(NK)))
    9437             :   {
    9438          49 :     GEN gk = MF_get_gk(mf);
    9439          49 :     N = MF_get_N(mf);
    9440          49 :     CHI = MF_get_CHI(mf);
    9441          49 :     Qtoss(gk, &nk, &dk);
    9442             :   }
    9443             :   else
    9444         266 :     checkNK2(NK, &N, &nk, &dk, &CHI, 0);
    9445         315 :   if (typ(cusp) == t_INFINITY) return 1;
    9446         315 :   if (typ(cusp) == t_FRAC) { A = gel(cusp,1); C = gel(cusp,2); }
    9447          28 :   else { A = cusp; C = gen_1; }
    9448         315 :   g = diviuexact(mului(N,C), ugcd(N, Fl_sqr(umodiu(C,N), N)));
    9449         315 :   c = mulii(negi(C),g);
    9450         315 :   d = addiu(mulii(A,g), 1);
    9451         315 :   if (!CHI) return 1;
    9452         315 :   go = gmfcharorder(CHI);
    9453         315 :   v = vali(go); if (v < 2) go = shifti(go, 2-v);
    9454         315 :   t = itou( znchareval(gel(CHI,1), gel(CHI,2), d, go) );
    9455         315 :   if (dk == 1) return t == 0;
    9456         154 :   o = itou(go);
    9457         154 :   if (kronecker(c,d) < 0) t = Fl_add(t, o/2, o);
    9458         154 :   if (Mod4(d) == 1) return t == 0;
    9459          14 :   t = Fl_sub(t, Fl_mul(o/4, nk, o), o);
    9460          14 :   return t == 0;
    9461             : }
    9462             : 
    9463             : /* Some useful closures */
    9464             : 
    9465             : /* sum_{d|n} d^k */
    9466             : static GEN
    9467       47600 : mysumdivku(ulong n, ulong k)
    9468             : {
    9469       47600 :   GEN fa = myfactoru(n);
    9470       47600 :   return k == 1? usumdiv_fact(fa): usumdivk_fact(fa,k);
    9471             : }
    9472             : static GEN
    9473         840 : c_Ek(long n, long d, GEN F)
    9474             : {
    9475         840 :   GEN E = cgetg(n + 2, t_VEC), C = gel(F,2);
    9476         840 :   long i, k = mf_get_k(F);
    9477         840 :   gel (E, 1) = gen_1;
    9478       25802 :   for (i = 1; i <= n; i++)
    9479             :   {
    9480       24962 :     pari_sp av = avma;
    9481       24962 :     gel(E, i+1) = gerepileupto(av, gmul(C, mysumdivku(i*d, k-1)));
    9482             :   }
    9483         840 :   return E;
    9484             : }
    9485             : 
    9486             : GEN
    9487         392 : mfEk(long k)
    9488             : {
    9489         392 :   pari_sp av = avma;
    9490             :   GEN E0, NK;
    9491         392 :   if (k < 0 || odd(k)) pari_err_TYPE("mfEk [incorrect k]", stoi(k));
    9492         392 :   if (!k) return mf1();
    9493         385 :   E0 = gdivsg(-2*k, bernfrac(k));
    9494         385 :   NK = mkNK(1,k,mfchartrivial());
    9495         385 :   return gerepilecopy(av, tag(t_MF_Ek, NK, E0));
    9496             : }
    9497             : 
    9498             : GEN
    9499          56 : mfDelta(void)
    9500             : {
    9501          56 :   pari_sp av = avma;
    9502          56 :   return gerepilecopy(av, tag0(t_MF_DELTA, mkNK(1,12,mfchartrivial())));
    9503             : }
    9504             : 
    9505             : GEN
    9506         798 : mfTheta(GEN psi)
    9507             : {
    9508         798 :   pari_sp av = avma;
    9509             :   GEN N, gk, psi2;
    9510             :   long par;
    9511         798 :   if (!psi) { psi = mfchartrivial(); N = utoipos(4); par = 1; }
    9512             :   else
    9513             :   {
    9514             :     long FC;
    9515          21 :     psi = get_mfchar(psi);
    9516          21 :     FC = mfcharconductor(psi);
    9517          21 :     if (mfcharmodulus(psi) != FC)
    9518           0 :       pari_err_TYPE("mfTheta [nonprimitive character]", psi);
    9519          21 :     par = mfcharparity(psi);
    9520          21 :     N = shifti(sqru(FC),2);
    9521             :   }
    9522         798 :   if (par > 0) { gk = ghalf; psi2 = psi; }
    9523           7 :   else { gk = gsubsg(2, ghalf); psi2 = mfcharmul(psi, get_mfchar(stoi(-4))); }
    9524         798 :   return gerepilecopy(av, tag(t_MF_THETA, mkgNK(N, gk, psi2, pol_x(1)), psi));
    9525             : }
    9526             : 
    9527             : /* Output 0 if not desired eta product: if flag=0 (default) require
    9528             :  * holomorphic at cusps. If flag set, accept meromorphic, but sill in some
    9529             :  * modular function space */
    9530             : GEN
    9531         210 : mffrometaquo(GEN eta, long flag)
    9532             : {
    9533         210 :   pari_sp av = avma;
    9534             :   GEN NK, N, k, BR, P;
    9535         210 :   long v, cusp = 0;
    9536         210 :   if (!etaquotype(&eta, &N,&k,&P, &v, NULL, flag? NULL: &cusp) || cusp < 0)
    9537          14 :     return gc_const(av, gen_0);
    9538         196 :   if (lg(gel(eta,1)) == 1) { set_avma(av); return mf1(); }
    9539         189 :   BR = mkvec2(ZV_to_zv(gel(eta,1)), ZV_to_zv(gel(eta,2)));
    9540         189 :   if (v < 0) v = 0;
    9541         189 :   NK = mkgNK(N, k, get_mfchar(P), pol_x(1));
    9542         189 :   return gerepilecopy(av, tag2(t_MF_ETAQUO, NK, BR, utoi(v)));
    9543             : }
    9544             : 
    9545             : /* Q^(-r) */
    9546             : static GEN
    9547         375 : RgXn_negpow(GEN Q, long r, long L)
    9548             : {
    9549         375 :   if (r < 0) r = -r; else Q = RgXn_inv_i(Q, L);
    9550         375 :   if (r != 1) Q = RgXn_powu_i(Q, r, L);
    9551         375 :   return Q;
    9552             : }
    9553             : /* flag same as in mffrometaquo: if set, accept meromorphic. */
    9554             : static GEN
    9555          49 : mfisetaquo_i(GEN F, long flag)
    9556             : {
    9557             :   GEN gk, P, E, M, S, G, CHI, v, w;
    9558             :   long b, l, L, N, vS, m, j;
    9559          49 :   const long bextra = 10;
    9560             : 
    9561          49 :   if (!checkmf_i(F)) pari_err_TYPE("mfisetaquo",F);
    9562          49 :   CHI = mf_get_CHI(F); if (mfcharorder(CHI) > 2) return NULL;
    9563          49 :   N = mf_get_N(F);
    9564          49 :   gk = mf_get_gk(F);
    9565          49 :   b = mfsturmNgk(N, gk);
    9566          49 :   L = maxss(N, b) + bextra;
    9567          49 :   S = mfcoefs_i(F, L, 1);
    9568          49 :   if (!RgV_is_ZV(S)) return NULL;
    9569         889 :   for (vS = 1; vS <= L+1; vS++)
    9570         889 :     if (signe(gel(S,vS))) break;
    9571          49 :   vS--;
    9572          49 :   if (vS >= bextra - 1) { L += vS; S = mfcoefs_i(F, L, 1); }
    9573          49 :   if (vS) { S = vecslice(S, vS+1, L+1); L -= vS; }
    9574          49 :   S = RgV_to_RgX(S, 0); l = lg(S)-2;
    9575          49 :   P = cgetg(l, t_COL);
    9576          49 :   E = cgetg(l, t_COL); w = v = gen_0; /* w = weight, v = valuation */
    9577        1908 :   for (m = j = 1; m+2 < lg(S); m++)
    9578             :   {
    9579        1866 :     GEN c = gel(S,m+2);
    9580             :     long r;
    9581        1866 :     if (is_bigint(c)) return NULL;
    9582        1859 :     r = -itos(c);
    9583        1859 :     if (r)
    9584             :     {
    9585         375 :       S = ZXn_mul(S, RgXn_negpow(eta_ZXn(m, L), r, L), L);
    9586         375 :       gel(P,j) = utoipos(m);
    9587         375 :       gel(E,j) = stoi(r);
    9588         375 :       v = addmuliu(v, gel(E,j), m);
    9589         375 :       w = addis(w, r);
    9590         375 :       j++;
    9591             :     }
    9592             :   }
    9593          42 :   if (!equalii(w, gmul2n(gk, 1)) || (!flag && !equalii(v, muluu(24,vS))))
    9594           7 :     return NULL;
    9595          35 :   setlg(P, j);
    9596          35 :   setlg(E, j); M = mkmat2(P, E); G = mffrometaquo(M, flag);
    9597          35 :   return (typ(G) != t_INT
    9598          35 :           && (mfsturmmf(G) <= b + bextra || mfisequal(F, G, b)))? M: NULL;
    9599             : }
    9600             : GEN
    9601          49 : mfisetaquo(GEN F, long flag)
    9602             : {
    9603          49 :   pari_sp av = avma;
    9604          49 :   GEN M = mfisetaquo_i(F, flag);
    9605          49 :   return M? gerepilecopy(av, M): gc_const(av, gen_0);
    9606             : }
    9607             : 
    9608             : #if 0
    9609             : /* number of primitive characters modulo N */
    9610             : static ulong
    9611             : numprimchars(ulong N)
    9612             : {
    9613             :   GEN fa, P, E;
    9614             :   long i, l;
    9615             :   ulong n;
    9616             :   if ((N & 3) == 2) return 0;
    9617             :   fa = myfactoru(N);
    9618             :   P = gel(fa,1); l = lg(P);
    9619             :   E = gel(fa,2);
    9620             :   for (i = n = 1; i < l; i++)
    9621             :   {
    9622             :     ulong p = P[i], e = E[i];
    9623             :     if (e == 2) n *= p-2; else n *= (p-1)*(p-1)*upowuu(p,e-2);
    9624             :   }
    9625             :   return n;
    9626             : }
    9627             : #endif
    9628             : 
    9629             : /* Space generated by products of two Eisenstein series */
    9630             : 
    9631             : static int
    9632       73759 : cmp_small_priority(void *E, GEN a, GEN b)
    9633             : {
    9634       73759 :   GEN prio = (GEN)E;
    9635       73759 :   return cmpss(prio[(long)a], prio[(long)b]);
    9636             : }
    9637             : static long
    9638        1260 : znstar_get_expo(GEN G) { return itou(cyc_get_expo(znstar_get_cyc(G))); }
    9639             : 
    9640             : /* Return [vchi, bymod, vG]:
    9641             :  * vG[f] = znstar(f,1) for f a conductor of (at least) a char mod N; else NULL
    9642             :  * bymod[f] = vecsmall of conrey indexes of chars modulo f | N; else NULL
    9643             :  * vchi[n] = a list of CHIvec [G0,chi0,o,ncharvecexpo(G0,nchi0),...]:
    9644             :  *   chi0 = primitive char attached to Conrey Mod(n,N)
    9645             :  * (resp. NULL if (n,N) > 1) */
    9646             : static GEN
    9647         630 : charsmodN(long N)
    9648             : {
    9649         630 :   GEN D, G, prio, phio, dummy = cgetg(1,t_VEC);
    9650         630 :   GEN vP, vG = const_vec(N,NULL), vCHI  = const_vec(N,NULL);
    9651         630 :   GEN bymod = const_vec(N,NULL);
    9652         630 :   long pn, i, l, vt = fetch_user_var("t");
    9653         630 :   D = mydivisorsu(N); l = lg(D);
    9654        3892 :   for (i = 1; i < l; i++)
    9655        3262 :     gel(bymod, D[i]) = vecsmalltrunc_init(myeulerphiu(D[i])+1);
    9656         630 :   gel(vG,N) = G = znstar0(utoipos(N),1);
    9657         630 :   pn = znstar_get_expo(G);  /* exponent(Z/NZ)^* */
    9658         630 :   vP = const_vec(pn,NULL);
    9659       26817 :   for (i = 1; i <= N; i++)
    9660             :   {
    9661             :     GEN P, gF, G0, chi0, nchi0, chi, v, go;
    9662             :     long j, F, o;
    9663       26187 :     if (ugcd(i,N) != 1) continue;
    9664       13923 :     chi = znconreylog(G, utoipos(i));
    9665       13923 :     gF = znconreyconductor(G, chi, &chi0);
    9666       13923 :     F = (typ(gF) == t_INT)? itou(gF): itou(gel(gF,1));
    9667       13923 :     G0 = gel(vG, F); if (!G0) G0 = gel(vG,F) = znstar0(gF, 1);
    9668       13923 :     nchi0 = znconreylog_normalize(G0,chi0);
    9669       13923 :     go = gel(nchi0,1); o = itou(go); /* order(chi0) */
    9670       13923 :     v = ncharvecexpo(G0, nchi0);
    9671       13923 :     if (!equaliu(go, pn)) v = zv_z_mul(v, pn / o);
    9672       13923 :     P = gel(vP, o); if (!P) P = gel(vP,o) = polcyclo(o,vt);
    9673             :     /* mfcharcxinit with dummy complex powers */
    9674       13923 :     gel(vCHI,i) = mkvecn(6, G0, chi0, go, v, dummy, P);
    9675       13923 :     D = mydivisorsu(N / F); l = lg(D);
    9676       40110 :     for (j = 1; j < l; j++) vecsmalltrunc_append(gel(bymod, F*D[j]), i);
    9677             :   }
    9678         630 :   phio = zero_zv(pn); l = lg(vCHI); prio = cgetg(l, t_VEC);
    9679       26817 :   for (i = 1; i < l; i++)
    9680             :   {
    9681       26187 :     GEN CHI = gel(vCHI,i);
    9682             :     long o;
    9683       26187 :     if (!CHI) continue;
    9684       13923 :     o = CHIvec_ord(CHI);
    9685       13923 :     if (!phio[o]) phio[o] = myeulerphiu(o);
    9686       13923 :     prio[i] = phio[o];
    9687             :   }
    9688         630 :   l = lg(bymod);
    9689             :   /* sort characters by increasing value of phi(order) */
    9690       26817 :   for (i = 1; i < l; i++)
    9691             :   {
    9692       26187 :     GEN z = gel(bymod,i);
    9693       26187 :     if (z) gen_sort_inplace(z, (void*)prio, &cmp_small_priority, NULL);
    9694             :   }
    9695         630 :   return mkvec3(vCHI, bymod, vG);
    9696             : }
    9697             : 
    9698             : static GEN
    9699        5124 : mfeisenstein2pure(long k, GEN CHI1, GEN CHI2, long ord, GEN P, long lim)
    9700             : {
    9701        5124 :   GEN c, V = cgetg(lim+2, t_COL);
    9702             :   long n;
    9703        5124 :   c = mfeisenstein2_0(k, CHI1, CHI2, ord);
    9704        5124 :   if (P) c = grem(c, P);
    9705        5124 :   gel(V,1) = c;
    9706       98994 :   for (n=1; n <= lim; n++)
    9707             :   {
    9708       93870 :     c = sigchi2(k, CHI1, CHI2, n, ord);
    9709       93870 :     if (P) c = grem(c, P);
    9710       93870 :     gel(V,n+1) = c;
    9711             :   }
    9712        5124 :   return V;
    9713             : }
    9714             : static GEN
    9715        4340 : mfeisenstein2pure_Fl(long k, GEN CHI1vec, GEN CHI2vec, GEN vz, ulong p, long lim)
    9716             : {
    9717        4340 :   GEN V = cgetg(lim+2, t_VECSMALL);
    9718             :   long n;
    9719        4340 :   V[1] = mfeisenstein2_0_Fl(k, CHI1vec, CHI2vec, vz, p);
    9720       59871 :   for (n=1; n <= lim; n++) V[n+1] = sigchi2_Fl(k, CHI1vec, CHI2vec, n, vz, p);
    9721        4340 :   return V;
    9722             : }
    9723             : 
    9724             : static GEN
    9725         252 : getcolswt2(GEN M, GEN D, ulong p)
    9726             : {
    9727         252 :   GEN R, v = gel(M,1);
    9728         252 :   long i, l = lg(M) - 1;
    9729         252 :   R = cgetg(l, t_MAT); /* skip D[1] = 1 */
    9730        1008 :   for (i = 1; i < l; i++)
    9731             :   {
    9732         756 :     GEN w = Flv_Fl_mul(gel(M,i+1), D[i+1], p);
    9733         756 :     gel(R,i) = Flv_sub(v, w, p);
    9734             :   }
    9735         252 :   return R;
    9736             : }
    9737             : static GEN
    9738        5383 : expandbd(GEN V, long d)
    9739             : {
    9740             :   long L, n, nd;
    9741             :   GEN W;
    9742        5383 :   if (d == 1) return V;
    9743        2086 :   L = lg(V)-1; W = zerocol(L); /* nd = n/d */
    9744       17871 :   for (n = nd = 0; n < L; n += d, nd++) gel(W, n+1) = gel(V, nd+1);
    9745        2086 :   return W;
    9746             : }
    9747             : static GEN
    9748        6979 : expandbd_Fl(GEN V, long d)
    9749             : {
    9750             :   long L, n, nd;
    9751             :   GEN W;
    9752        6979 :   if (d == 1) return V;
    9753        2639 :   L = lg(V)-1; W = zero_Flv(L); /* nd = n/d */
    9754       16366 :   for (n = nd = 0; n < L; n += d, nd++) W[n+1] = V[nd+1];
    9755        2639 :   return W;
    9756             : }
    9757             : static void
    9758        4340 : getcols_i(GEN *pM, GEN *pvj, GEN gk, GEN CHI1vec, GEN CHI2vec, long NN1, GEN vz,
    9759             :           ulong p, long lim)
    9760             : {
    9761        4340 :   GEN CHI1 = CHIvec_CHI(CHI1vec), CHI2 = CHIvec_CHI(CHI2vec);
    9762        4340 :   long N2 = CHIvec_N(CHI2vec);
    9763        4340 :   GEN vj, M, D = mydivisorsu(NN1/N2);
    9764        4340 :   long i, l = lg(D), k = gk[2];
    9765        4340 :   GEN V = mfeisenstein2pure_Fl(k, CHI1vec, CHI2vec, vz, p, lim);
    9766        4340 :   M = cgetg(l, t_MAT);
    9767       11319 :   for (i = 1; i < l; i++) gel(M,i) = expandbd_Fl(V, D[i]);
    9768        4340 :   if (k == 2 && N2 == 1 && CHIvec_N(CHI1vec) == 1)
    9769             :   {
    9770         252 :     M = getcolswt2(M, D, p); l--;
    9771         252 :     D = vecslice(D, 2, l);
    9772             :   }
    9773        4340 :   *pM = M;
    9774        4340 :   *pvj = vj = cgetg(l, t_VEC);
    9775       11067 :   for (i = 1; i < l; i++) gel(vj,i) = mkvec4(gk, CHI1, CHI2, utoipos(D[i]));
    9776        4340 : }
    9777             : 
    9778             : /* find all CHI1, CHI2 mod N such that CHI1*CHI2 = CHI, f(CHI1)*f(CHI2) | N.
    9779             :  * set M = mfcoefs(B_e E(CHI1,CHI2), lim), vj = [e,i1,i2] */
    9780             : static void
    9781        1778 : getcols(GEN *pM, GEN *pv, long k, long nCHI, GEN allN, GEN vz, ulong p,
    9782             :         long lim)
    9783             : {
    9784        1778 :   GEN vCHI = gel(allN,1), gk = utoi(k);
    9785        1778 :   GEN M = cgetg(1,t_MAT), v = cgetg(1,t_VEC);
    9786        1778 :   long i1, N = lg(vCHI)-1;
    9787       86079 :   for (i1 = 1; i1 <= N; i1++)
    9788             :   {
    9789       84301 :     GEN CHI1vec = gel(vCHI, i1), CHI2vec, M1, v1;
    9790             :     long NN1, i2;
    9791      147301 :     if (!CHI1vec) continue;
    9792       66192 :     if (k == 1 && CHIvec_parity(CHI1vec) == -1) continue;
    9793       41433 :     NN1 = N/CHIvec_N(CHI1vec); /* N/f(chi1) */;
    9794       41433 :     i2 = Fl_div(nCHI,i1, N);
    9795       41433 :     if (!i2) i2 = 1;
    9796       41433 :     CHI2vec = gel(vCHI,i2);
    9797       41433 :     if (NN1 % CHIvec_N(CHI2vec)) continue; /* f(chi1)f(chi2) | N ? */
    9798        3192 :     getcols_i(&M1, &v1, gk, CHI1vec, CHI2vec, NN1, vz, p, lim);
    9799        3192 :     M = shallowconcat(M, M1);
    9800        3192 :     v = shallowconcat(v, v1);
    9801             :   }
    9802        1778 :   *pM = M;
    9803        1778 :   *pv = v;
    9804        1778 : }
    9805             : 
    9806             : static void
    9807        1176 : update_Mj(GEN *M, GEN *vecj, GEN *pz, ulong p)
    9808             : {
    9809             :   GEN perm;
    9810        1176 :   *pz = Flm_indexrank(*M, p); perm = gel(*pz,2);
    9811        1176 :   *M = vecpermute(*M, perm);
    9812        1176 :   *vecj = vecpermute(*vecj, perm);
    9813        1176 : }
    9814             : static int
    9815         413 : getcolsgen(long dim, GEN *pM, GEN *pvj, GEN *pz, long k, long ell, long nCHI,
    9816             :            GEN allN, GEN vz, ulong p, long lim)
    9817             : {
    9818         413 :   GEN vCHI = gel(allN,1), bymod = gel(allN,2), gell = utoi(ell);
    9819         413 :   long i1, N = lg(vCHI)-1;
    9820         413 :   long L = lim+1;
    9821         413 :   if (lg(*pvj)-1 >= dim) update_Mj(pM, pvj, pz, p);
    9822         413 :   if (lg(*pvj)-1 == dim) return 1;
    9823        1561 :   for (i1 = 1; i1 <= N; i1++)
    9824             :   {
    9825        1540 :     GEN CHI1vec = gel(vCHI, i1), T;
    9826             :     long par1, j, l, N1, NN1;
    9827             : 
    9828        1540 :     if (!CHI1vec) continue;
    9829        1519 :     par1 = CHIvec_parity(CHI1vec);
    9830        1519 :     if (ell == 1 && par1 == -1) continue;
    9831         938 :     if (odd(ell)) par1 = -par1;
    9832         938 :     N1 = CHIvec_N(CHI1vec);
    9833         938 :     NN1 = N/N1;
    9834         938 :     T = gel(bymod, NN1); l = lg(T);
    9835        3598 :     for (j = 1; j < l; j++)
    9836             :     {
    9837        3017 :       long i2 = T[j], l1, l2, j1, s, nC;
    9838        3017 :       GEN M, M1, M2, vj, vj1, vj2, CHI2vec = gel(vCHI, i2);
    9839        3017 :       if (CHIvec_parity(CHI2vec) != par1) continue;
    9840        1148 :       nC = Fl_div(nCHI, Fl_mul(i1,i2,N), N);
    9841        1148 :       getcols(&M2, &vj2, k-ell, nC, allN, vz, p, lim);
    9842        1148 :       l2 = lg(M2); if (l2 == 1) continue;
    9843        1148 :       getcols_i(&M1, &vj1, gell, CHI1vec, CHI2vec, NN1, vz, p, lim);
    9844        1148 :       l1 = lg(M1);
    9845        1148 :       M1 = Flm_to_FlxV(M1, 0);
    9846        1148 :       M2 = Flm_to_FlxV(M2, 0);
    9847        1148 :       M  = cgetg((l1-1)*(l2-1) + 1, t_MAT);
    9848        1148 :       vj = cgetg((l1-1)*(l2-1) + 1, t_VEC);
    9849        2835 :       for (j1 = s = 1; j1 < l1; j1++)
    9850             :       {
    9851        1687 :         GEN E = gel(M1,j1), v = gel(vj1,j1);
    9852             :         long j2;
    9853        7084 :         for (j2 = 1; j2 < l2; j2++, s++)
    9854             :         {
    9855        5397 :           GEN c = Flx_to_Flv(Flxn_mul(E, gel(M2,j2), L, p), L);
    9856        5397 :           gel(M,s) = c;
    9857        5397 :           gel(vj,s) = mkvec2(v, gel(vj2,j2));
    9858             :         }
    9859             :       }
    9860        1148 :       *pM = shallowconcat(*pM, M);
    9861        1148 :       *pvj = shallowconcat(*pvj, vj);
    9862        1148 :       if (lg(*pvj)-1 >= dim) update_Mj(pM, pvj, pz, p);
    9863        1148 :       if (lg(*pvj)-1 == dim) return 1;
    9864             :     }
    9865             :   }
    9866          21 :   if (ell == 1)
    9867             :   {
    9868          21 :     update_Mj(pM, pvj, pz, p);
    9869          21 :     return (lg(*pvj)-1 == dim);
    9870             :   }
    9871           0 :   return 0;
    9872             : }
    9873             : 
    9874             : static GEN
    9875        1645 : mkF2bd(long d, long lim)
    9876             : {
    9877        1645 :   GEN V = zerovec(lim + 1);
    9878             :   long n;
    9879        1645 :   gel(V, 1) = sstoQ(-1, 24);
    9880       24248 :   for (n = 1; n <= lim/d; n++) gel(V, n*d + 1) = mysumdivku(n, 1);
    9881        1645 :   return V;
    9882             : }
    9883             : 
    9884             : static GEN
    9885        5740 : mkeisen(GEN E, long ord, GEN P, long lim)
    9886             : {
    9887        5740 :   long k = itou(gel(E,1)), e = itou(gel(E,4));
    9888        5740 :   GEN CHI1 = gel(E,2), CHI2 = gel(E,3);
    9889        5740 :   if (k == 2 && mfcharistrivial(CHI1) && mfcharistrivial(CHI2))
    9890         616 :     return gsub(mkF2bd(1,lim), gmulgu(mkF2bd(e,lim), e));
    9891             :   else
    9892             :   {
    9893        5124 :     GEN V = mfeisenstein2pure(k, CHI1, CHI2, ord, P, lim);
    9894        5124 :     return expandbd(V, e);
    9895             :   }
    9896             : }
    9897             : static GEN
    9898         588 : mkM(GEN vj, long pn, GEN P, long lim)
    9899             : {
    9900         588 :   long j, l = lg(vj), L = lim+1;
    9901         588 :   GEN M = cgetg(l, t_MAT);
    9902        4795 :   for (j = 1; j < l; j++)
    9903             :   {
    9904             :     GEN E1, E2;
    9905        4207 :     parse_vecj(gel(vj,j), &E1,&E2);
    9906        4207 :     E1 = RgV_to_RgX(mkeisen(E1, pn, P, lim), 0);
    9907        4207 :     if (E2)
    9908             :     {
    9909        1533 :       E2 = RgV_to_RgX(mkeisen(E2, pn, P, lim), 0);
    9910        1533 :       E1 = RgXn_mul(E1, E2, L);
    9911             :     }
    9912        4207 :     E1 = RgX_to_RgC(E1, L);
    9913        4207 :     if (P && E2) E1 = RgXQV_red(E1, P);
    9914        4207 :     gel(M,j) = E1;
    9915             :   }
    9916         588 :   return M;
    9917             : }
    9918             : 
    9919             : /* assume N > 2 */
    9920             : static GEN
    9921          35 : mffindeisen1(long N)
    9922             : {
    9923          35 :   GEN G = znstar0(utoipos(N), 1), L = chargalois(G, NULL), chi0 = NULL;
    9924          35 :   long j, m = N, l = lg(L);
    9925         259 :   for (j = 1; j < l; j++)
    9926             :   {
    9927         245 :     GEN chi = gel(L,j);
    9928         245 :     long r = myeulerphiu(itou(zncharorder(G,chi)));
    9929         245 :     if (r >= m) continue;
    9930         182 :     chi = znconreyfromchar(G, chi);
    9931         182 :     if (zncharisodd(G,chi)) { m = r; chi0 = chi; if (r == 1) break; }
    9932             :   }
    9933          35 :   if (!chi0) pari_err_BUG("mffindeisen1 [no Eisenstein series found]");
    9934          35 :   chi0 = znchartoprimitive(G,chi0);
    9935          35 :   return mfcharGL(gel(chi0,1), gel(chi0,2));
    9936             : }
    9937             : 
    9938             : static GEN
    9939         630 : mfeisensteinspaceinit_i(long N, long k, GEN CHI)
    9940             : {
    9941         630 :   GEN M, Minv, vj, vG, GN, allN, P, vz, z = NULL;
    9942         630 :   long nCHI, lim, ell, ord, dim = mffulldim(N, k, CHI);
    9943             :   ulong r, p;
    9944             : 
    9945         630 :   if (!dim) retmkvec3(cgetg(1,t_VECSMALL),
    9946             :                       mkvec2(cgetg(1,t_MAT),gen_1),cgetg(1,t_VEC));
    9947         630 :   lim = mfsturmNk(N, k) + 1;
    9948         630 :   allN = charsmodN(N);
    9949         630 :   vG = gel(allN,3);
    9950         630 :   GN = gel(vG,N);
    9951         630 :   ord = znstar_get_expo(GN);
    9952         630 :   P = ord <= 2? NULL: polcyclo(ord, varn(mfcharpol(CHI)));
    9953         630 :   CHI = induce(GN, CHI); /* lift CHI mod N before mfcharno*/
    9954         630 :   nCHI = mfcharno(CHI);
    9955         630 :   r = QabM_init(ord, &p);
    9956         630 :   vz = Fl_powers(r, ord, p);
    9957         630 :   getcols(&M, &vj, k, nCHI, allN, vz, p, lim);
    9958         651 :   for (ell = k>>1; ell >= 1; ell--)
    9959         413 :     if (getcolsgen(dim, &M, &vj, &z, k, ell, nCHI, allN, vz, p, lim)) break;
    9960         630 :   if (!z) update_Mj(&M, &vj, &z, p);
    9961         630 :   if (lg(vj) - 1 < dim) return NULL;
    9962         588 :   M = mkM(vj, ord, P, lim);
    9963         588 :   Minv = QabM_Minv(rowpermute(M, gel(z,1)), P, ord);
    9964         588 :   return mkvec4(gel(z,1), Minv, vj, utoi(ord));
    9965             : }
    9966             : /* true mf */
    9967             : static GEN
    9968         588 : mfeisensteinspaceinit(GEN mf)
    9969             : {
    9970         588 :   pari_sp av = avma;
    9971         588 :   GEN z, CHI = MF_get_CHI(mf);
    9972         588 :   long N = MF_get_N(mf), k = MF_get_k(mf);
    9973         588 :   if (!CHI) CHI = mfchartrivial();
    9974         588 :   z = mfeisensteinspaceinit_i(N, k, CHI);
    9975         588 :   if (!z)
    9976             :   {
    9977          35 :     GEN E, CHIN = mffindeisen1(N), CHI0 = mfchartrivial();
    9978          35 :     z = mfeisensteinspaceinit_i(N, k+1, mfcharmul(CHI, CHIN));
    9979          35 :     if (z) E = mkvec4(gen_1, CHI0, CHIN, gen_1);
    9980             :     else
    9981             :     {
    9982           7 :       z = mfeisensteinspaceinit_i(N, k+2, CHI);
    9983           7 :       E = mkvec4(gen_2, CHI0, CHI0, utoipos(N));
    9984             :     }
    9985          35 :     z = mkvec2(z, E);
    9986             :   }
    9987         588 :   return gerepilecopy(av, z);
    9988             : }
    9989             : 
    9990             : /* decomposition of modular form on eisenspace */
    9991             : static GEN
    9992        1001 : mfeisensteindec(GEN mf, GEN F)
    9993             : {
    9994        1001 :   pari_sp av = avma;
    9995             :   GEN M, Mindex, Mvecj, V, B, CHI;
    9996             :   long o, ord;
    9997             : 
    9998        1001 :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    9999        1001 :   if (lg(Mvecj) < 5)
   10000             :   {
   10001          56 :     GEN E, e = gel(Mvecj,2), gkE = gel(e,1);
   10002          56 :     long dE = itou(gel(e,4));
   10003          56 :     Mvecj = gel(Mvecj,1);
   10004          56 :     E = mfeisenstein(itou(gkE), NULL, gel(e,3));
   10005          56 :     if (dE != 1) E = mfbd_E2(E, dE, gel(e,2)); /* here k = 2 */
   10006          56 :     F = mfmul(F, E);
   10007             :   }
   10008        1001 :   M = gel(Mvecj, 2);
   10009        1001 :   if (lg(M) == 1) return cgetg(1, t_VEC);
   10010        1001 :   Mindex = gel(Mvecj, 1);
   10011        1001 :   ord = itou(gel(Mvecj,4));
   10012        1001 :   V = mfcoefs(F, Mindex[lg(Mindex)-1]-1, 1); settyp(V, t_COL);
   10013        1001 :   CHI = mf_get_CHI(F);
   10014        1001 :   o = mfcharorder(CHI);
   10015        1001 :   if (o > 2 && o != ord)
   10016             :   { /* convert Mod(.,polcyclo(o)) to Mod(., polcyclo(N)) for o | N,
   10017             :      * o and N both != 2 (mod 4) */
   10018          84 :     GEN z, P = gel(M,4); /* polcyclo(ord) */
   10019          84 :     long vt = varn(P);
   10020          84 :     z = gmodulo(pol_xn(ord/o, vt), P);
   10021          84 :     if (ord % o) pari_err_TYPE("mfeisensteindec", V);
   10022          84 :     V = gsubst(liftpol_shallow(V), vt, z);
   10023             :   }
   10024        1001 :   B = Minv_RgC_mul(M, vecpermute(V, Mindex));
   10025        1001 :   return gerepileupto(av, B);
   10026             : }
   10027             : 
   10028             : /*********************************************************************/
   10029             : /*                        END EISENSPACE                             */
   10030             : /*********************************************************************/
   10031             : 
   10032             : static GEN
   10033          70 : sertocol2(GEN S, long l)
   10034             : {
   10035          70 :   GEN C = cgetg(l + 2, t_COL);
   10036             :   long i;
   10037         420 :   for (i = 0; i <= l; i++) gel(C, i+1) = polcoef_i(S, i, -1);
   10038          70 :   return C;
   10039             : }
   10040             : 
   10041             : /* Compute polynomial P0 such that F=E4^(k/4)P0(E6/E4^(3/2)). */
   10042             : static GEN
   10043          14 : mfcanfindp0(GEN F, long k)
   10044             : {
   10045          14 :   pari_sp ltop = avma;
   10046             :   GEN E4, E6, V, V1, Q, W, res, M, B;
   10047             :   long l, j;
   10048          14 :   l = k/6 + 2;
   10049          14 :   V = mfcoefsser(F,l);
   10050          14 :   E4 = mfcoefsser(mfEk(4),l);
   10051          14 :   E6 = mfcoefsser(mfEk(6),l);
   10052          14 :   V1 = gdiv(V, gpow(E4, uutoQ(k,4), 0));
   10053          14 :   Q = gdiv(E6, gpow(E4, uutoQ(3,2), 0));
   10054          14 :   W = gpowers(Q, l - 1);
   10055          14 :   M = cgetg(l + 1, t_MAT);
   10056          70 :   for (j = 1; j <= l; j++) gel(M,j) = sertocol2(gel(W,j), l);
   10057          14 :   B = sertocol2(V1, l);
   10058          14 :   res = inverseimage(M, B);
   10059          14 :   if (lg(res) == 1) err_space(F);
   10060          14 :   return gerepilecopy(ltop, gtopolyrev(res, 0));
   10061             : }
   10062             : 
   10063             : /* Compute the first n+1 Taylor coeffs at tau=I of a modular form
   10064             :  * on SL_2(Z). */
   10065             : GEN
   10066          14 : mftaylor(GEN F, long n, long flreal, long prec)
   10067             : {
   10068          14 :   pari_sp ltop = avma;
   10069          14 :   GEN P0, Pm1 = gen_0, v;
   10070          14 :   GEN X2 = mkpoln(3, ghalf,gen_0,gneg(ghalf)); /* (x^2-1) / 2 */
   10071             :   long k, m;
   10072          14 :   if (!checkmf_i(F)) pari_err_TYPE("mftaylor",F);
   10073          14 :   k = mf_get_k(F);
   10074          14 :   if (mf_get_N(F) != 1 || k < 0) pari_err_IMPL("mftaylor for this form");
   10075          14 :   P0 = mfcanfindp0(F, k);
   10076          14 :   v = cgetg(n+2, t_VEC); gel(v, 1) = RgX_coeff(P0,0);
   10077         154 :   for (m = 0; m < n; m++)
   10078             :   {
   10079         140 :     GEN P1 = gdivgu(gmulsg(-(k + 2*m), RgX_shift(P0,1)), 12);
   10080         140 :     P1 = gadd(P1, gmul(X2, RgX_deriv(P0)));
   10081         140 :     if (m) P1 = gsub(P1, gdivgu(gmulsg(m*(m+k-1), Pm1), 144));
   10082         140 :     Pm1 = P0; P0 = P1;
   10083         140 :     gel(v, m+2) = RgX_coeff(P0, 0);
   10084             :   }
   10085          14 :   if (flreal)
   10086             :   {
   10087           7 :     GEN pi2 = Pi2n(1, prec), pim4 = gmulsg(-2, pi2), VPC;
   10088           7 :     GEN C = gmulsg(3, gdiv(gpowgs(ggamma(uutoQ(1,4), prec), 8), gpowgs(pi2, 6)));
   10089             :     /* E_4(i): */
   10090           7 :     GEN facn = gen_1;
   10091           7 :     VPC = gpowers(gmul(pim4, gsqrt(C, prec)), n);
   10092           7 :     C = gpow(C, uutoQ(k,4), prec);
   10093          84 :     for (m = 0; m <= n; m++)
   10094             :     {
   10095          77 :       gel(v, m+1) = gdiv(gmul(C, gmul(gel(v, m+1), gel(VPC, m+1))), facn);
   10096          77 :       facn = gmulgu(facn, m+1);
   10097             :     }
   10098             :   }
   10099          14 :   return gerepilecopy(ltop, v);
   10100             : }
   10101             : 
   10102             : #if 0
   10103             : /* To be used in mfeigensearch() */
   10104             : GEN
   10105             : mfreadratfile()
   10106             : {
   10107             :   GEN eqn;
   10108             :   pariFILE *F = pari_fopengz("rateigen300.gp");
   10109             :   eqn = gp_readvec_stream(F->file);
   10110             :   pari_fclose(F);
   10111             :   return eqn;
   10112             : }
   10113             : #endif
   10114             :  /*****************************************************************/
   10115             : /*           EISENSTEIN CUSPS: COMPLEX DIRECTLY: one F_k         */
   10116             : /*****************************************************************/
   10117             : 
   10118             : /* CHIvec = charinit(CHI); data = [N1g/g1,N2g/g2,g1/g,g2/g,C/g1,C/g2,
   10119             :  * (N1g/g1)^{-1},(N2g/g2)^{-1}] */
   10120             : 
   10121             : /* nm = n/m;
   10122             :  * z1 = powers of \z_{C/g}^{(Ae/g)^{-1}},
   10123             :  * z2 = powers of \z_N^{A^{-1}(g1g2/C)}]
   10124             :  * N.B. : we compute value and conjugate at the end, so it is (Ae/g)^{-1}
   10125             :  * and not -(Ae/g)^{-1} */
   10126             : static GEN
   10127     7830900 : eiscnm(long nm, long m, GEN CHI1vec, GEN CHI2vec, GEN data, GEN z1)
   10128             : {
   10129     7830900 :   long Cg1 = data[5], s10 = (nm*data[7]) % Cg1, r10 = (nm - data[1]*s10) / Cg1;
   10130     7830900 :   long Cg2 = data[6], s20 = (m *data[8]) % Cg2, r20 = (m  - data[2]*s20) / Cg2;
   10131             :   long j1, r1, s1;
   10132     7830900 :   GEN T = gen_0;
   10133    18857104 :   for (j1 = 0, r1 = r10, s1 = s10; j1 < data[3]; j1++, r1 -= data[1], s1 += Cg1)
   10134             :   {
   10135    11026204 :     GEN c1 = mychareval(CHI1vec, r1);
   10136    11026204 :     if (!gequal0(c1))
   10137             :     {
   10138             :       long j2, r2, s2;
   10139     8127560 :       GEN S = gen_0;
   10140    21136570 :       for (j2 = 0, r2 = r20, s2 = s20; j2 < data[4]; j2++, r2 -= data[2], s2 += Cg2)
   10141             :       {
   10142    13009010 :         GEN c2 = mychareval(CHI2vec, r2);
   10143    13009010 :         if (!gequal0(c2)) S = gadd(S, gmul(c2, rootsof1pow(z1, s1*s2)));
   10144             :       }
   10145     8127560 :       T = gadd(T, gmul(c1, S));
   10146             :     }
   10147             :   }
   10148     7830900 :   return conj_i(T);
   10149             : }
   10150             : 
   10151             : static GEN
   10152      612675 : fg1g2n(long n, long k, GEN CHI1vec, GEN CHI2vec, GEN data, GEN z1, GEN z2)
   10153             : {
   10154      612675 :   pari_sp av = avma;
   10155      612675 :   GEN S = gen_0, D = mydivisorsu(n);
   10156      612675 :   long i, l = lg(D);
   10157     4528125 :   for (i = 1; i < l; i++)
   10158             :   {
   10159     3915450 :     long m = D[i], nm = D[l-i]; /* n/m */
   10160     3915450 :     GEN u = eiscnm( nm,  m, CHI1vec, CHI2vec, data, z1);
   10161     3915450 :     GEN v = eiscnm(-nm, -m, CHI1vec, CHI2vec, data, z1);
   10162     3915450 :     GEN w = odd(k) ? gsub(u, v) : gadd(u, v);
   10163     3915450 :     S = gadd(S, gmul(powuu(m, k-1), w));
   10164             :   }
   10165      612675 :   return gerepileupto(av, gmul(S, rootsof1pow(z2, n)));
   10166             : }
   10167             : 
   10168             : static GEN
   10169       14266 : gausssumcx(GEN CHIvec, long prec)
   10170             : {
   10171             :   GEN z, S, V;
   10172       14266 :   long m, N = CHIvec_N(CHIvec);
   10173       14266 :   if (N == 1) return gen_1;
   10174        7693 :   V = CHIvec_val(CHIvec);
   10175        7693 :   z = rootsof1u_cx(N, prec);
   10176        7693 :   S = gmul(z, gel(V, N));
   10177      103054 :   for (m = N-1; m >= 1; m--) S = gmul(z, gadd(gel(V, m), S));
   10178        7693 :   return S;
   10179             : }
   10180             : 
   10181             : /* Computation of Q_k(\z_N^s) as a polynomial in \z_N^s. FIXME: explicit
   10182             :  * formula ? */
   10183             : static GEN
   10184        2338 : mfqk(long k, long N)
   10185             : {
   10186             :   GEN X, P, ZI, Q, Xm1, invden;
   10187             :   long i;
   10188        2338 :   ZI = gdivgu(RgX_shift_shallow(RgV_to_RgX(identity_ZV(N-1), 0), 1), N);
   10189        2338 :   if (k == 1) return ZI;
   10190        1176 :   P = gsubgs(pol_xn(N,0), 1);
   10191        1176 :   invden = RgXQ_powu(ZI, k, P);
   10192        1176 :   X = pol_x(0); Q = gneg(X); Xm1 = gsubgs(X, 1);
   10193        2884 :   for (i = 2; i < k; i++)
   10194        1708 :     Q = RgX_shift_shallow(ZX_add(gmul(Xm1, ZX_deriv(Q)), gmulsg(-i, Q)), 1);
   10195        1176 :   return RgXQ_mul(Q, invden, P);
   10196             : }
   10197             : 
   10198             : /* CHI mfchar; M is a multiple of the conductor of CHI, but is NOT
   10199             :  * necessarily its modulus */
   10200             : static GEN
   10201        3346 : mfskcx(long k, GEN CHI, long M, long prec)
   10202             : {
   10203             :   GEN S, CHIvec, P;
   10204             :   long F, m, i, l;
   10205        3346 :   CHI = mfchartoprimitive(CHI, &F);
   10206        3346 :   CHIvec = mfcharcxinit(CHI, prec);
   10207        3346 :   if (F == 1) S = gdivgu(bernfrac(k), k);
   10208             :   else
   10209             :   {
   10210        2338 :     GEN Q = mfqk(k, F), V = CHIvec_val(CHIvec);
   10211        2338 :     S = gmul(gel(V, F), RgX_coeff(Q, 0));
   10212       39431 :     for (m = 1; m < F; m++) S = gadd(S, gmul(gel(V, m), RgX_coeff(Q, m)));
   10213        2338 :     S = conj_i(S);
   10214             :   }
   10215             :   /* prime divisors of M not dividing f(chi) */
   10216        3346 :   P = gel(myfactoru(u_ppo(M/F,F)), 1); l = lg(P);
   10217        3500 :   for (i = 1; i < l; i++)
   10218             :   {
   10219         154 :     long p = P[i];
   10220         154 :     S = gmul(S, gsubsg(1, gdiv(mychareval(CHIvec, p), powuu(p, k))));
   10221             :   }
   10222        3346 :   return gmul(gmul(gausssumcx(CHIvec, prec), S), powuu(M/F, k));
   10223             : }
   10224             : 
   10225             : static GEN
   10226        6146 : f00_i(long k, GEN CHI1vec, GEN CHI2vec, GEN G2, GEN S, long prec)
   10227             : {
   10228             :   GEN c, a;
   10229        6146 :   long N1 = CHIvec_N(CHI1vec), N2 = CHIvec_N(CHI2vec);
   10230        6146 :   if (S[2] != N1) return gen_0;
   10231        3346 :   c = mychareval(CHI1vec, S[3]);
   10232        3346 :   if (isintzero(c)) return gen_0;
   10233        3346 :   a = mfskcx(k, mfchardiv(CHIvec_CHI(CHI2vec), CHIvec_CHI(CHI1vec)), N1*N2, prec);
   10234        3346 :   a = gmul(a, conj_i(gmul(c,G2)));
   10235        3346 :   return gdiv(a, mulsi(-N2, powuu(S[1], k-1)));
   10236             : }
   10237             : 
   10238             : static GEN
   10239        4865 : f00(long k, GEN CHI1vec,GEN CHI2vec, GEN G1,GEN G2, GEN data, long prec)
   10240             : {
   10241             :   GEN T1, T2;
   10242        4865 :   T2 = f00_i(k, CHI1vec, CHI2vec, G2, data, prec);
   10243        4865 :   if (k > 1) return T2;
   10244        1281 :   T1 = f00_i(k, CHI2vec, CHI1vec, G1, data, prec);
   10245        1281 :   return gadd(T1, T2);
   10246             : }
   10247             : 
   10248             : /* ga in SL_2(Z), find beta [a,b;c,d] in Gamma_0(N) and mu in Z such that
   10249             :  * beta * ga * T^u = [A',B';C',D'] with C' | N and N | B', C' > 0 */
   10250             : static void
   10251        5460 : mfgatogap(GEN ga, long N, long *pA, long *pC, long *pD, long *pd, long *pmu)
   10252             : {
   10253        5460 :   GEN A = gcoeff(ga,1,1), B = gcoeff(ga,1,2);
   10254        5460 :   GEN C = gcoeff(ga,2,1), D = gcoeff(ga,2,2), a, b, c, d;
   10255             :   long t, Ap, Cp, B1, D1, mu;
   10256        5460 :   Cp = itou(bezout(muliu(A,N), C, &c, &d)); /* divides N */
   10257        5460 :   t = 0;
   10258        5460 :   if (Cp > 1)
   10259             :   { /* (d, N/Cp) = 1, find t such that (d - t*(A*N/Cp), N) = 1 */
   10260        2604 :     long dN = umodiu(d,Cp), Q = (N/Cp * umodiu(A,Cp)) % Cp;
   10261        2989 :     while (ugcd(dN, Cp) > 1) { t++; dN = Fl_sub(dN, Q, Cp); }
   10262             :   }
   10263        5460 :   if (t)
   10264             :   {
   10265         385 :     c = addii(c, mului(t, diviuexact(C,Cp)));
   10266         385 :     d = subii(d, mului(t, muliu(A, N/Cp))); /* (d,N) = 1 */
   10267             :   }
   10268        5460 :   D1 = umodiu(mulii(d,D), N);
   10269        5460 :   (void)bezout(d, mulis(c,-N), &a, &b); /* = 1 */
   10270        5460 :   t = 0; Ap = umodiu(addii(mulii(a,A), mulii(b,C)), N); /* (Ap,Cp) = 1 */
   10271        7483 :   while (ugcd(Ap, N) > 1) { t++; Ap = Fl_add(Ap, Cp, N); }
   10272        5460 :   B1 = umodiu(a,N)*umodiu(B,N) + umodiu(b,N)*umodiu(D,N) + t*D1;
   10273        5460 :   B1 %= N;
   10274        5460 :   *pmu = mu = Fl_neg(Fl_div(B1, Ap, N), N);
   10275             :   /* A', D' and d only needed modulo N */
   10276        5460 :   *pd = umodiu(d, N);
   10277        5460 :   *pA = Ap;
   10278        5460 :   *pC = Cp; *pD = (D1 + Cp*mu) % N;
   10279        5460 : }
   10280             : 
   10281             : #if 0
   10282             : /* CHI is a mfchar, return alpha(CHI) */
   10283             : static long
   10284             : mfalchi(GEN CHI, long AN, long cg)
   10285             : {
   10286             :   GEN G = gel(CHI,1), chi = gel(CHI,2), go = gmfcharorder(CHI);
   10287             :   long o = itou(go), a = itos( znchareval(G, chi, stoi(1 + AN/cg), go) );
   10288             :   if (a < 0 || (cg * a) % o) pari_err_BUG("mfalchi");
   10289             :   return (cg * a) / o;
   10290             : }
   10291             : #endif
   10292             : /* return A such that CHI1(t) * CHI2(t) = e(A) or NULL if (t,N1*N2) > 1 */
   10293             : static GEN
   10294       10920 : mfcharmuleval(GEN CHI1vec, GEN CHI2vec, long t)
   10295             : {
   10296       10920 :   long a1 = mycharexpo(CHI1vec, t), o1 = CHIvec_ord(CHI1vec);
   10297       10920 :   long a2 = mycharexpo(CHI2vec, t), o2 = CHIvec_ord(CHI2vec);;
   10298       10920 :   if (a1 < 0 || a2 < 0) return NULL;
   10299       10920 :   return sstoQ(a1*o2 + a2*o1, o1*o2);
   10300             : }
   10301             : static GEN
   10302        5460 : mfcharmulcxeval(GEN CHI1vec, GEN CHI2vec, long t, long prec)
   10303             : {
   10304        5460 :   GEN A = mfcharmuleval(CHI1vec, CHI2vec, t);
   10305             :   long n, d;
   10306        5460 :   if (!A) return gen_0;
   10307        5460 :   Qtoss(A, &n,&d); return rootsof1q_cx(n, d, prec);
   10308             : }
   10309             : /* alpha(CHI1 * CHI2) */
   10310             : static long
   10311        5460 : mfalchi2(GEN CHI1vec, GEN CHI2vec, long AN, long cg)
   10312             : {
   10313        5460 :   GEN A = mfcharmuleval(CHI1vec, CHI2vec, 1 + AN/cg);
   10314             :   long a;
   10315        5460 :   if (!A) pari_err_BUG("mfalchi2");
   10316        5460 :   A = gmulsg(cg, A);
   10317        5460 :   if (typ(A) != t_INT) pari_err_BUG("mfalchi2");
   10318        5460 :   a = itos(A) % cg; if (a < 0) a += cg;
   10319        5460 :   return a;
   10320             : }
   10321             : 
   10322             : /* return g = (a,b), set u >= 0 s.t. g = a * u (mod b) */
   10323             : static long
   10324       21840 : mybezout(long a, long b, long *pu)
   10325             : {
   10326       21840 :   long junk, g = cbezout(a, b, pu, &junk);
   10327       21840 :   if (*pu < 0) *pu += b/g;
   10328       21840 :   return g;
   10329             : }
   10330             : 
   10331             : /* E = [k, CHI1,CHI2, e], CHI1 and CHI2 primitive mfchars such that,
   10332             :  * CHI1(-1)*CHI2(-1) = (-1)^k; expansion of (B_e (E_k(CHI1,CHI2))) | ga.
   10333             :  * w is the width for the space of the calling function. */
   10334             : static GEN
   10335        5460 : mfeisensteingacx(GEN E, long w, GEN ga, long lim, long prec)
   10336             : {
   10337        5460 :   GEN CHI1vec, CHI2vec, CHI1 = gel(E,2), CHI2 = gel(E,3), v, S, ALPHA;
   10338             :   GEN G1, G2, z1, z2, data;
   10339        5460 :   long k = itou(gel(E,1)), e = itou(gel(E,4));
   10340        5460 :   long N1 = mfcharmodulus(CHI1);
   10341        5460 :   long N2 = mfcharmodulus(CHI2), N = e * N1 * N2;
   10342             :   long NsurC, cg, wN, A, C, Ai, d, mu, alchi, na, da;
   10343             :   long eg, g, gH, U, u0, u1, u2, Aig, H, m, n, t, Cg, NC1, NC2;
   10344             : 
   10345        5460 :   mfgatogap(ga, N, &A, &C, &Ai, &d, &mu);
   10346        5460 :   CHI1vec = mfcharcxinit(CHI1, prec);
   10347        5460 :   CHI2vec = mfcharcxinit(CHI2, prec);
   10348        5460 :   NsurC = N/C; cg  = ugcd(C, NsurC); wN = NsurC / cg;
   10349        5460 :   if (w%wN) pari_err_BUG("mfeisensteingacx [wN does not divide w]");
   10350        5460 :   alchi = mfalchi2(CHI1vec, CHI2vec, A*N, cg);
   10351        5460 :   ALPHA = sstoQ(alchi, NsurC);
   10352             : 
   10353        5460 :   g = mybezout(A*e, C, &u0); Cg = C/g; eg = e/g;
   10354        5460 :   NC1 = mybezout(N1, Cg, &u1);
   10355        5460 :   NC2 = mybezout(N2, Cg, &u2);
   10356        5460 :   H = (NC1*NC2*g)/Cg;
   10357        5460 :   Aig = (Ai*H)%N; if (Aig < 0) Aig += N;
   10358        5460 :   z1 = rootsof1powinit(u0, Cg, prec);
   10359        5460 :   z2 = rootsof1powinit(Aig, N, prec);
   10360        5460 :   data = mkvecsmalln(8, N1/NC1, N2/NC2, NC1, NC2, Cg/NC1, Cg/NC2, u1, u2);
   10361        5460 :   v = zerovec(lim + 1);
   10362             :   /* need n*H = alchi (mod cg) */
   10363        5460 :   gH = mybezout(H, cg, &U);
   10364        5460 :   if (gH > 1)
   10365             :   {
   10366         511 :     if (alchi % gH) return mkvec2(gen_0, v);
   10367         511 :     alchi /= gH; cg /= gH; H /= gH;
   10368             :   }
   10369        5460 :   G1 = gausssumcx(CHI1vec, prec);
   10370        5460 :   G2 = gausssumcx(CHI2vec, prec);
   10371        5460 :   if (!alchi)
   10372        4865 :     gel(v,1) = f00(k, CHI1vec,CHI2vec,G1,G2, mkvecsmall3(NC2,Cg,A*eg), prec);
   10373        5460 :   n = Fl_mul(alchi,U,cg); if (!n) n = cg;
   10374        5460 :   m = (n*H - alchi) / cg; /* positive, exact division */
   10375      618135 :   for (; m <= lim; n+=cg, m+=H)
   10376      612675 :     gel(v, m+1) = fg1g2n(n, k, CHI1vec, CHI2vec, data, z1,z2);
   10377        5460 :   t = (2*e)/g; if (odd(k)) t = -t;
   10378        5460 :   v = gdiv(v, gmul(conj_i(gmul(G1,G2)), mulsi(t, powuu(eg*N2/NC2, k-1))));
   10379        5460 :   if (k == 2 && N1 == 1 && N2 == 1) v = gsub(mkF2bd(wN,lim), gmulsg(e,v));
   10380             : 
   10381        5460 :   Qtoss(ALPHA, &na,&da);
   10382        5460 :   S = conj_i( mfcharmulcxeval(CHI1vec,CHI2vec,d,prec) ); /* CHI(1/d) */
   10383        5460 :   if (wN > 1)
   10384             :   {
   10385        4151 :     GEN z = rootsof1powinit(-mu, wN, prec);
   10386        4151 :     long i, l = lg(v);
   10387      578837 :     for (i = 1; i < l; i++) gel(v,i) = gmul(gel(v,i), rootsof1pow(z,i-1));
   10388             :   }
   10389        5460 :   v = RgV_Rg_mul(v, gmul(S, rootsof1q_cx(-mu*na, da, prec)));
   10390        5460 :   return mkvec2(ALPHA, bdexpand(v, w/wN));
   10391             : }
   10392             : 
   10393             : /*****************************************************************/
   10394             : /*                       END EISENSTEIN CUSPS                    */
   10395             : /*****************************************************************/
   10396             : 
   10397             : static GEN
   10398        1596 : mfchisimpl(GEN CHI)
   10399             : {
   10400             :   GEN G, chi;
   10401        1596 :   if (typ(CHI) == t_INT) return CHI;
   10402        1596 :   G = gel(CHI, 1); chi = gel(CHI, 2);
   10403        1596 :   switch(mfcharorder(CHI))
   10404             :   {
   10405        1148 :     case 1: chi = gen_1; break;
   10406         427 :     case 2: chi = znchartokronecker(G,chi,1); break;
   10407          21 :     default:chi = mkintmod(znconreyexp(G,chi), znstar_get_N(G)); break;
   10408             :   }
   10409        1596 :   return chi;
   10410             : }
   10411             : 
   10412             : GEN
   10413         700 : mfparams(GEN F)
   10414             : {
   10415         700 :   pari_sp av = avma;
   10416             :   GEN z, mf, CHI;
   10417         700 :   if ((mf = checkMF_i(F)))
   10418             :   {
   10419          14 :     long N = MF_get_N(mf);
   10420          14 :     GEN gk = MF_get_gk(mf);
   10421          14 :     CHI = MF_get_CHI(mf);
   10422          14 :     z = mkvec5(utoi(N), gk, CHI, utoi(MF_get_space(mf)), mfcharpol(CHI));
   10423             :   }
   10424             :   else
   10425             :   {
   10426         686 :     if (!checkmf_i(F)) pari_err_TYPE("mfparams", F);
   10427         686 :     z = vec_append(mf_get_NK(F), mfcharpol(mf_get_CHI(F)));
   10428             :   }
   10429         700 :   gel(z,3) = mfchisimpl(gel(z,3));
   10430         700 :   return gerepilecopy(av, z);
   10431             : }
   10432             : 
   10433             : GEN
   10434          14 : mfisCM(GEN F)
   10435             : {
   10436          14 :   pari_sp av = avma;
   10437             :   forprime_t S;
   10438             :   GEN D, v;
   10439             :   long N, k, lD, sb, p, i;
   10440          14 :   if (!checkmf_i(F)) pari_err_TYPE("mfisCM", F);
   10441          14 :   N = mf_get_N(F);
   10442          14 :   k = mf_get_k(F); if (N < 0 || k < 0) pari_err_IMPL("mfisCM for this F");
   10443          14 :   D = mfunram(N, -1);
   10444          14 :   lD = lg(D);
   10445          14 :   sb = maxss(mfsturmNk(N, k), 4*N);
   10446          14 :   v = mfcoefs_i(F, sb, 1);
   10447          14 :   u_forprime_init(&S, 2, sb);
   10448         504 :   while ((p = u_forprime_next(&S)))
   10449             :   {
   10450         490 :     GEN ap = gel(v, p+1);
   10451         490 :     if (!gequal0(ap))
   10452         406 :       for (i = 1; i < lD; i++)
   10453         245 :         if (kross(D[i], p) == -1) { D = vecsplice(D, i); lD--; }
   10454             :   }
   10455          14 :   if (lD == 1) return gc_const(av, gen_0);
   10456          14 :   if (lD == 2) return gc_stoi(av, D[1]);
   10457           7 :   if (k > 1) pari_err_BUG("mfisCM");
   10458           7 :   return gerepileupto(av, zv_to_ZV(D));
   10459             : }
   10460             : 
   10461             : static long
   10462         287 : mfspace_i(GEN mf, GEN F)
   10463             : {
   10464             :   GEN v, vF, gk;
   10465             :   long n, nE, i, l, s, N;
   10466             : 
   10467         287 :   mf = checkMF(mf); s = MF_get_space(mf);
   10468         287 :   if (!F) return s;
   10469         287 :   if (!checkmf_i(F)) pari_err_TYPE("mfspace",F);
   10470         287 :   v = mftobasis(mf, F, 1);
   10471         287 :   n = lg(v)-1; if (!n) return -1;
   10472         224 :   nE = lg(MF_get_E(mf))-1;
   10473         224 :   switch(s)
   10474             :   {
   10475          56 :     case mf_NEW: case mf_OLD: case mf_EISEN: return s;
   10476         140 :     case mf_FULL:
   10477         140 :       if (mf_get_type(F) == t_MF_THETA) return mf_EISEN;
   10478         133 :       if (!gequal0(vecslice(v,1,nE)))
   10479          63 :         return gequal0(vecslice(v,nE+1,n))? mf_EISEN: mf_FULL;
   10480             :   }
   10481             :   /* mf is mf_CUSP or mf_FULL, F a cusp form */
   10482          98 :   gk = mf_get_gk(F);
   10483          98 :   if (typ(gk) == t_FRAC || equali1(gk)) return mf_CUSP;
   10484          84 :   vF = mftonew_i(mf, vecslice(v, nE+1, n), &N);
   10485          84 :   if (N != MF_get_N(mf)) return mf_OLD;
   10486          56 :   l = lg(vF);
   10487          91 :   for (i = 1; i < l; i++)
   10488          56 :     if (itos(gmael(vF,i,1)) != N) return mf_CUSP;
   10489          35 :   return mf_NEW;
   10490             : }
   10491             : long
   10492         287 : mfspace(GEN mf, GEN F)
   10493         287 : { pari_sp av = avma; return gc_long(av, mfspace_i(mf,F)); }
   10494             : static GEN
   10495          21 : lfunfindchi(GEN ldata, GEN van, long prec)
   10496             : {
   10497          21 :   GEN gN = ldata_get_conductor(ldata), gk = ldata_get_k(ldata);
   10498          21 :   GEN G = znstar0(gN,1), cyc = znstar_get_conreycyc(G), L, go, vz;
   10499          21 :   long N = itou(gN), odd = typ(gk) == t_INT && mpodd(gk);
   10500          21 :   long i, j, o, l, B0 = 2, B = lg(van)-1, bit = 10 - prec2nbits(prec);
   10501             : 
   10502             :   /* if van is integral, chi must be trivial */
   10503          21 :   if (typ(van) == t_VECSMALL) return mfcharGL(G, zerocol(lg(cyc)-1));
   10504          14 :   L = cyc2elts(cyc); l = lg(L);
   10505          42 :   for (i = j = 1; i < l; i++)
   10506             :   {
   10507          28 :     GEN chi = zc_to_ZC(gel(L,i));
   10508          28 :     if (zncharisodd(G,chi) == odd) gel(L,j++) = mfcharGL(G,chi);
   10509             :   }
   10510          14 :   setlg(L,j); l = j;
   10511          14 :   if (l <= 2) return gel(L,1);
   10512           0 :   o = znstar_get_expo(G); go = utoi(o);
   10513           0 :   vz = grootsof1(o, prec);
   10514             :   for (;;)
   10515           0 :   {
   10516             :     long n;
   10517           0 :     for (n = B0; n <= B; n++)
   10518             :     {
   10519             :       GEN an, r;
   10520             :       long j;
   10521           0 :       if (ugcd(n, N) != 1) continue;
   10522           0 :       an = gel(van,n); if (gexpo(an) < bit) continue;
   10523           0 :       r = gdiv(an, conj_i(an));
   10524           0 :       for (i = 1; i < l; i++)
   10525             :       {
   10526           0 :         GEN CHI = gel(L,i);
   10527           0 :         if (gexpo(gsub(r, gel(vz, znchareval_i(CHI,n,go)+1))) > bit)
   10528           0 :           gel(L,i) = NULL;
   10529             :       }
   10530           0 :       for (i = j = 1; i < l; i++)
   10531           0 :         if (gel(L,i)) gel(L,j++) = gel(L,i);
   10532           0 :       l = j; setlg(L,l);
   10533           0 :       if (l == 2) return gel(L,1);
   10534             :     }
   10535           0 :     B0 = B+1; B <<= 1;
   10536           0 :     van = ldata_vecan(ldata_get_an(ldata), B, prec);
   10537             :   }
   10538             : }
   10539             : 
   10540             : GEN
   10541          21 : mffromlfun(GEN L, long prec)
   10542             : {
   10543          21 :   pari_sp av = avma;
   10544          21 :   GEN ldata = lfunmisc_to_ldata_shallow(L), Vga = ldata_get_gammavec(ldata);
   10545          21 :   GEN van, a0, CHI, NK, gk = ldata_get_k(ldata);
   10546             :   long N, space;
   10547          21 :   if (!gequal(Vga, mkvec2(gen_0, gen_1))) pari_err_TYPE("mffromlfun", L);
   10548          21 :   N = itou(ldata_get_conductor(ldata));
   10549          21 :   van = ldata_vecan(ldata_get_an(ldata), mfsturmNgk(N,gk) + 2, prec);
   10550          21 :   CHI = lfunfindchi(ldata, van, prec);
   10551          21 :   if (typ(van) != t_VEC) van = vecsmall_to_vec_inplace(van);
   10552          21 :   space = (lg(ldata) == 7)? mf_CUSP: mf_FULL;
   10553          21 :   a0 = (space == mf_CUSP)? gen_0: gneg(lfun(L, gen_0, prec2nbits(prec)));
   10554          21 :   NK = mkvec3(utoi(N), gk, mfchisimpl(CHI));
   10555          21 :   return gerepilecopy(av, mkvec3(NK, utoi(space), shallowconcat(a0, van)));
   10556             : }
   10557             : /*******************************************************************/
   10558             : /*                                                                 */
   10559             : /*                       HALF-INTEGRAL WEIGHT                      */
   10560             : /*                                                                 */
   10561             : /*******************************************************************/
   10562             : /* We use the prefix mf2; k represents the weight -1/2, so e.g.
   10563             :    k = 2 is weight 5/2. N is the level, so 4\mid N, and CHI is the
   10564             :    character, always even. */
   10565             : 
   10566             : static long
   10567        3360 : lamCO(long r, long s, long p)
   10568             : {
   10569        3360 :   if ((s << 1) <= r)
   10570             :   {
   10571        1232 :     long rp = r >> 1;
   10572        1232 :     if (odd(r)) return upowuu(p, rp) << 1;
   10573         336 :     else return (p + 1)*upowuu(p, rp - 1);
   10574             :   }
   10575        2128 :   else return upowuu(p, r - s) << 1;
   10576             : }
   10577             : 
   10578             : static int
   10579        1568 : condC(GEN faN, GEN valF)
   10580             : {
   10581        1568 :   GEN P = gel(faN, 1), E = gel(faN, 2);
   10582        1568 :   long l = lg(P), i;
   10583        3696 :   for (i = 1; i < l; i++)
   10584        3024 :     if ((P[i] & 3L) == 3)
   10585             :     {
   10586        1120 :       long r = E[i];
   10587        1120 :       if (odd(r) || r < (valF[i] << 1)) return 1;
   10588             :     }
   10589         672 :   return 0;
   10590             : }
   10591             : 
   10592             : /* returns 2*zetaCO; weight is k + 1/2 */
   10593             : static long
   10594        3696 : zeta2CO(GEN faN, GEN valF, long r2, long s2, long k)
   10595             : {
   10596        3696 :   if (r2 >= 4) return lamCO(r2, s2, 2) << 1;
   10597        2912 :   if (r2 == 3) return 6;
   10598        1568 :   if (condC(faN, valF)) return 4;
   10599         672 :   if (odd(k)) return s2 ? 3 : 5; else return s2 ? 5: 3;
   10600             : }
   10601             : 
   10602             : /* returns 4 times last term in formula */
   10603             : static long
   10604        3696 : dim22(long N, long F, long k)
   10605             : {
   10606        3696 :   pari_sp av = avma;
   10607        3696 :   GEN vF, faN = myfactoru(N), P = gel(faN, 1), E = gel(faN, 2);
   10608        3696 :   long i, D, l = lg(P);
   10609        3696 :   vF = cgetg(l, t_VECSMALL);
   10610        9968 :   for (i = 1; i < l; i++) vF[i] = u_lval(F, P[i]);
   10611        3696 :   D = zeta2CO(faN, vF, E[1], vF[1], k);
   10612        6272 :   for (i = 2; i < l; i++) D *= lamCO(E[i], vF[i], P[i]);
   10613        3696 :   return gc_long(av,D);
   10614             : }
   10615             : 
   10616             : /* PSI not necessarily primitive, of conductor F */
   10617             : static int
   10618       13846 : charistotallyeven(GEN PSI, long F)
   10619             : {
   10620       13846 :   pari_sp av = avma;
   10621       13846 :   GEN P = gel(myfactoru(F), 1);
   10622       13846 :   GEN G = gel(PSI,1), psi = gel(PSI,2);
   10623             :   long i;
   10624       14350 :   for (i = 1; i < lg(P); i++)
   10625             :   {
   10626         532 :     GEN psip = znchardecompose(G, psi, utoipos(P[i]));
   10627         532 :     if (zncharisodd(G, psip)) return gc_bool(av,0);
   10628             :   }
   10629       13818 :   return gc_bool(av,1);
   10630             : }
   10631             : 
   10632             : static GEN
   10633      299775 : get_PSI(GEN CHI, long t)
   10634             : {
   10635      299775 :   long r = t & 3L, t2 = (r == 2 || r == 3) ? t << 2 : t;
   10636      299775 :   return mfcharmul_i(CHI, induce(gel(CHI,1), utoipos(t2)));
   10637             : }
   10638             : /* space = mf_CUSP, mf_EISEN or mf_FULL, weight k + 1/2 */
   10639             : static long
   10640       41363 : mf2dimwt12(long N, GEN CHI, long space)
   10641             : {
   10642       41363 :   pari_sp av = avma;
   10643       41363 :   GEN D = mydivisorsu(N >> 2);
   10644       41363 :   long i, l = lg(D), dim3 = 0, dim4 = 0;
   10645             : 
   10646       41363 :   CHI = induceN(N, CHI);
   10647      341138 :   for (i = 1; i < l; i++)
   10648             :   {
   10649      299775 :     long rp, t = D[i], Mt = D[l-i];
   10650      299775 :     GEN PSI = get_PSI(CHI,t);
   10651      299775 :     rp = mfcharconductor(PSI);
   10652      299775 :     if (Mt % (rp*rp) == 0) { dim4++; if (charistotallyeven(PSI,rp)) dim3++; }
   10653             :   }
   10654       41363 :   set_avma(av);
   10655       41363 :   switch (space)
   10656             :   {
   10657       40439 :     case mf_CUSP: return dim4 - dim3;
   10658         462 :     case mf_EISEN:return dim3;
   10659         462 :     case mf_FULL: return dim4;
   10660             :   }
   10661             :   return 0; /*LCOV_EXCL_LINE*/
   10662             : }
   10663             : 
   10664             : static long
   10665         693 : mf2dimwt32(long N, GEN CHI, long F, long space)
   10666             : {
   10667             :   long D;
   10668         693 :   switch(space)
   10669             :   {
   10670         231 :     case mf_CUSP: D = mypsiu(N) - 6*dim22(N, F, 1);
   10671         231 :       if (D%24) pari_err_BUG("mfdim");
   10672         231 :       return D/24 + mf2dimwt12(N, CHI, 4);
   10673         231 :     case mf_FULL: D = mypsiu(N) + 6*dim22(N, F, 0);
   10674         231 :       if (D%24) pari_err_BUG("mfdim");
   10675         231 :       return D/24 + mf2dimwt12(N, CHI, 1);
   10676         231 :     case mf_EISEN: D = dim22(N, F, 0) + dim22(N, F, 1);
   10677         231 :       if (D & 3L) pari_err_BUG("mfdim");
   10678         231 :       return (D >> 2) - mf2dimwt12(N, CHI, 3);
   10679             :   }
   10680             :   return 0; /*LCOV_EXCL_LINE*/
   10681             : }
   10682             : 
   10683             : /* F = conductor(CHI), weight k = r+1/2 */
   10684             : static long
   10685       43729 : checkmf2(long N, long r, GEN CHI, long F, long space)
   10686             : {
   10687       43729 :   switch(space)
   10688             :   {
   10689       43708 :     case mf_FULL: case mf_CUSP: case mf_EISEN: break;
   10690          14 :     case mf_NEW: case mf_OLD:
   10691          14 :       pari_err_TYPE("half-integral weight [new/old spaces]", utoi(space));
   10692           7 :     default:
   10693           7 :       pari_err_TYPE("half-integral weight [incorrect space]",utoi(space));
   10694             :   }
   10695       43708 :   if (N & 3L)
   10696           0 :     pari_err_DOMAIN("half-integral weight", "N % 4", "!=", gen_0, stoi(N));
   10697       43708 :   return r >= 0 && mfcharparity(CHI) == 1 && N % F == 0;
   10698             : }
   10699             : 
   10700             : /* weight k = r + 1/2 */
   10701             : static long
   10702       43463 : mf2dim_Nkchi(long N, long r, GEN CHI, ulong space)
   10703             : {
   10704       43463 :   long D, D2, F = mfcharconductor(CHI);
   10705       43463 :   if (!checkmf2(N, r, CHI, F, space)) return 0;
   10706       43442 :   if (r == 0) return mf2dimwt12(N, CHI, space);
   10707        2772 :   if (r == 1) return mf2dimwt32(N, CHI, F, space);
   10708        2079 :   if (space == mf_EISEN)
   10709             :   {
   10710         693 :     D = dim22(N, F, r) + dim22(N, F, 1-r);
   10711         693 :     if (D & 3L) pari_err_BUG("mfdim");
   10712         693 :     return D >> 2;
   10713             :   }
   10714        1386 :   D2 = space == mf_FULL? dim22(N, F, 1-r): -dim22(N, F, r);
   10715        1386 :   D = (2*r-1)*mypsiu(N) + 6*D2;
   10716        1386 :   if (D%24) pari_err_BUG("mfdim");
   10717        1386 :   return D/24;
   10718             : }
   10719             : 
   10720             : /* weight k=r+1/2 */
   10721             : static GEN
   10722         266 : mf2init_Nkchi(long N, long r, GEN CHI, long space, long flraw)
   10723             : {
   10724         266 :   GEN CHI1, Minv, Minvmat, B, M, gk = gaddsg(r,ghalf);
   10725         266 :   GEN mf1 = mkvec4(utoi(N),gk,CHI,utoi(space));
   10726             :   long L;
   10727         266 :   if (!checkmf2(N, r, CHI, mfcharconductor(CHI), space)) return mfEMPTY(mf1);
   10728         266 :   if (space==mf_EISEN) pari_err_IMPL("half-integral weight Eisenstein space");
   10729         266 :   L = mfsturmNgk(N, gk) + 1;
   10730         266 :   B = mf2basis(N, r, CHI, &CHI1, space);
   10731         266 :   M = mflineardivtomat(N,B,L); /* defined modulo T = charpol(CHI) */
   10732         266 :   if (flraw) M = mkvec3(gen_0,gen_0,M);
   10733             :   else
   10734             :   {
   10735         266 :     long o1 = mfcharorder(CHI1), o = mfcharorder(CHI);
   10736         266 :     M = mfcleanCHI(M, CHI, 0);
   10737         266 :     Minv = gel(M,2);
   10738         266 :     Minvmat = RgM_Minv_mul(NULL, Minv); /* mod T */
   10739         266 :     if (o1 != o)
   10740             :     {
   10741         133 :       GEN tr = Qab_trace_init(o, o1, mfcharpol(CHI), mfcharpol(CHI1));
   10742         133 :       Minvmat = QabM_tracerel(tr, 0, Minvmat);
   10743             :     }
   10744             :     /* Minvmat mod T1 = charpol(CHI1) */
   10745         266 :     B = vecmflineardiv_linear(B, Minvmat);
   10746         266 :     gel(M,3) = RgM_Minv_mul(gel(M,3), Minv);
   10747         266 :     gel(M,2) = mkMinv(matid(lg(B)-1), NULL,NULL,NULL);
   10748             :   }
   10749         266 :   return mkmf(mf1, cgetg(1,t_VEC), B, gen_0, M);
   10750             : }
   10751             : 
   10752             : /**************************************************************************/
   10753             : /*                          Kohnen + space                                */
   10754             : /**************************************************************************/
   10755             : 
   10756             : static GEN
   10757          21 : mfkohnenbasis_i(GEN mf, GEN CHI, long eps, long sb)
   10758             : {
   10759          21 :   GEN M = mfcoefs_mf(mf, sb, 1), p, P;
   10760          21 :   long c, i, n = mfcharorder(CHI), l = sb + 2;
   10761          21 :   p = cgetg(l, t_VECSMALL);
   10762             :   /* keep the a_n, n = (2 or 2+eps) mod 4 */
   10763         210 :   for (i = 3, c = 1; i < l; i+=4) p[c++] = i;
   10764         203 :   for (i = 3+eps;    i < l; i+=4) p[c++] = i;
   10765          21 :   P = n <= 2? NULL: mfcharpol(CHI);
   10766          21 :   setlg(p, c);
   10767          21 :   return QabM_ker(rowpermute(M, p), P, n);
   10768             : }
   10769             : GEN
   10770          21 : mfkohnenbasis(GEN mf)
   10771             : {
   10772          21 :   pari_sp av = avma;
   10773             :   GEN gk, CHI, CHIP, K;
   10774             :   long N4, r, eps, sb;
   10775          21 :   mf = checkMF(mf);
   10776          21 :   if (MF_get_space(mf) != mf_CUSP)
   10777           0 :     pari_err_TYPE("mfkohnenbasis [not a cuspidal space", mf);
   10778          21 :   if (!MF_get_dim(mf)) return cgetg(1, t_MAT);
   10779          21 :   N4 = MF_get_N(mf) >> 2; gk = MF_get_gk(mf); CHI = MF_get_CHI(mf);
   10780          21 :   if (typ(gk) == t_INT) pari_err_TYPE("mfkohnenbasis", gk);
   10781          21 :   r = MF_get_r(mf);
   10782          21 :   CHIP = mfcharchiliftprim(CHI, N4);
   10783          21 :   eps = CHIP==CHI? 1: -1;
   10784          21 :   if (odd(r)) eps = -eps;
   10785          21 :   if (uissquarefree(N4))
   10786             :   {
   10787          14 :     long d = mfdim_Nkchi(N4, 2*r, mfcharpow(CHI, gen_2), mf_CUSP);
   10788          14 :     sb = mfsturmNgk(N4 << 2, gk) + 1;
   10789          14 :     K = mfkohnenbasis_i(mf, CHIP, eps, sb);
   10790          14 :     if (lg(K) - 1 == d) return gerepilecopy(av, K);
   10791             :   }
   10792           7 :   sb = mfsturmNgk(N4 << 4, gk) + 1;
   10793           7 :   K = mfkohnenbasis_i(mf, CHIP, eps, sb);
   10794           7 :   return gerepilecopy(av, K);
   10795             : }
   10796             : 
   10797             : static GEN
   10798          21 : get_Shimura(GEN mf, GEN CHI, GEN vB, long t)
   10799             : {
   10800          21 :   long N = MF_get_N(mf), r = MF_get_k(mf) >> 1;
   10801          21 :   long i, d = MF_get_dim(mf), sb = mfsturm_mf(mf);
   10802          21 :   GEN a = cgetg(d+1, t_MAT);
   10803          84 :   for (i = 1; i <= d; i++)
   10804             :   {
   10805          63 :     pari_sp av = avma;
   10806          63 :     GEN f = c_deflate(sb*sb, t, gel(vB,i));
   10807          63 :     f = mftobasis_i(mf, RgV_shimura(f, sb, t, N, r, CHI));
   10808          63 :     gel(a,i) = gerepileupto(av, f);
   10809             :   }
   10810          21 :   return a;
   10811             : }
   10812             : static long
   10813          35 : QabM_rank(GEN M, GEN P, long n)
   10814             : {
   10815          35 :   GEN z = QabM_indexrank(M, P, n);
   10816          35 :   return lg(gel(z,2))-1;
   10817             : }
   10818             : /* discard T[*i] */
   10819             : static void
   10820           0 : discard_Ti(GEN T, long *i, long *lt)
   10821             : {
   10822           0 :   long j, l = *lt-1;
   10823           0 :   for (j = *i; j < l; j++) T[j] = T[j+1];
   10824           0 :   (*i)--; *lt = l;
   10825           0 : }
   10826             : /* return [mf3, bijection, mfkohnenbasis, codeshi] */
   10827             : static GEN
   10828          14 : mfkohnenbijection_i(GEN mf)
   10829             : {
   10830          14 :   GEN CHI = MF_get_CHI(mf), K = mfkohnenbasis(mf);
   10831             :   GEN mres, dMi, Mi, M, C, vB, mf3, SHI, T, P;
   10832          14 :   long N4 = MF_get_N(mf)>>2, r = MF_get_r(mf), dK = lg(K) - 1;
   10833             :   long i, c, n, oldr, lt, ltold, sb3, t, limt;
   10834          14 :   const long MAXlt = 100;
   10835             : 
   10836          14 :   mf3 = mfinit_Nkchi(N4, r<<1, mfcharpow(CHI,gen_2), mf_CUSP, 0);
   10837          14 :   if (MF_get_dim(mf3) != dK)
   10838           0 :     pari_err_BUG("mfkohnenbijection [different dimensions]");
   10839          14 :   if (!dK) return mkvec4(mf3, cgetg(1, t_MAT), K, cgetg(1, t_VEC));
   10840          14 :   CHI = mfcharchiliftprim(CHI, N4);
   10841          14 :   n = mfcharorder(CHI);
   10842          14 :   P = n<=2? NULL: mfcharpol(CHI);
   10843          14 :   SHI = cgetg(MAXlt, t_COL);
   10844          14 :   T = cgetg(MAXlt, t_VECSMALL);
   10845          14 :   sb3 = mfsturm_mf(mf3);
   10846          14 :   limt = 6; oldr = 0; vB = C = M = NULL;
   10847          98 :   for (t = lt = ltold = 1; lt < MAXlt; t++)
   10848             :   {
   10849             :     pari_sp av;
   10850          98 :     if (!uissquarefree(t)) continue;
   10851          84 :     T[lt++] = t; if (t <= limt) continue;
   10852          14 :     av = avma;
   10853          14 :     if (vB) gunclone(vB);
   10854             :     /* could improve the rest but 99% of running time is spent here */
   10855          14 :     vB = gclone( RgM_mul(mfcoefs_mf(mf, t*sb3*sb3, 1), K) );
   10856          14 :     set_avma(av);
   10857          21 :     for (i = ltold; i < lt; i++)
   10858             :     {
   10859             :       pari_sp av;
   10860             :       long r;
   10861          21 :       M = get_Shimura(mf3, CHI, vB, T[i]);
   10862          21 :       r = QabM_rank(M, P, n); if (!r) { discard_Ti(T, &i, &lt); continue; }
   10863          21 :       gel(SHI, i) = M; setlg(SHI, i+1);
   10864          21 :       if (r >= dK) { C = vecsmall_ei(dK, i); goto DONE; }
   10865          14 :       if (i == 1) { oldr = r; continue; }
   10866           7 :       av = avma; M = shallowmatconcat(SHI);
   10867           7 :       r = QabM_rank(M, P, n); /* >= rank(sum C[j] SHI[j]), probably sharp */
   10868           7 :       if (r >= dK)
   10869             :       {
   10870           7 :         M = RgV_sum(SHI);
   10871           7 :         if (QabM_rank(M, P, n) >= dK) { C = const_vecsmall(dK, 1); goto DONE; }
   10872           0 :         C = random_Flv(dK, 16);
   10873           0 :         M = RgV_zc_mul(SHI, C);
   10874           0 :         if (QabM_rank(M, P, n) >= dK) goto DONE;
   10875             :       }
   10876           0 :       else if (r == oldr) discard_Ti(T, &i, &lt);
   10877           0 :       oldr = r; set_avma(av);
   10878             :     }
   10879           0 :     limt *= 2; ltold = lt;
   10880             :   }
   10881           0 :   pari_err_BUG("mfkohnenbijection");
   10882          14 : DONE:
   10883          14 :   gunclone(vB); lt = lg(SHI);
   10884          14 :   Mi = QabM_pseudoinv(M,P,n, NULL,&dMi); Mi = RgM_Rg_div(Mi,dMi);
   10885          14 :   mres = cgetg(lt, t_VEC);
   10886          35 :   for (i = c = 1; i < lt; i++)
   10887          21 :     if (C[i]) gel(mres,c++) = mkvec2s(T[i], C[i]);
   10888          14 :   setlg(mres,c); return mkvec4(mf3, Mi, K, mres);
   10889             : }
   10890             : GEN
   10891          14 : mfkohnenbijection(GEN mf)
   10892             : {
   10893          14 :   pari_sp av = avma;
   10894             :   long N;
   10895          14 :   mf = checkMF(mf); N = MF_get_N(mf);
   10896          14 :   if (!uissquarefree(N >> 2))
   10897           0 :     pari_err_TYPE("mfkohnenbijection [N/4 not squarefree]", utoi(N));
   10898          14 :   if (MF_get_space(mf) != mf_CUSP || MF_get_r(mf) == 0 || !mfshimura_space_cusp(mf))
   10899           0 :     pari_err_TYPE("mfkohnenbijection [incorrect mf for Kohnen]", mf);
   10900          14 :   return gerepilecopy(av, mfkohnenbijection_i(mf));
   10901             : }
   10902             : 
   10903             : static int
   10904           7 : checkbij_i(GEN b)
   10905             : {
   10906           7 :   return typ(b) == t_VEC && lg(b) == 5 && checkMF_i(gel(b,1))
   10907           7 :          && typ(gel(b,2)) == t_MAT
   10908           7 :          && typ(gel(b,3)) == t_MAT
   10909          14 :          && typ(gel(b,4)) == t_VEC;
   10910             : }
   10911             : 
   10912             : /* bij is the output of mfkohnenbijection */
   10913             : GEN
   10914           7 : mfkohneneigenbasis(GEN mf, GEN bij)
   10915             : {
   10916           7 :   pari_sp av = avma;
   10917             :   GEN mf3, mf30, B, KM, M, k;
   10918             :   long r, i, l, N4;
   10919           7 :   mf = checkMF(mf);
   10920           7 :   if (!checkbij_i(bij))
   10921           0 :     pari_err_TYPE("mfkohneneigenbasis [bijection]", bij);
   10922           7 :   if (MF_get_space(mf) != mf_CUSP)
   10923           0 :     pari_err_TYPE("mfkohneneigenbasis [not a cuspidal space]", mf);
   10924           7 :   if (!MF_get_dim(mf))
   10925           0 :     retmkvec3(cgetg(1, t_VEC), cgetg(1, t_VEC), cgetg(1, t_VEC));
   10926           7 :   N4 = MF_get_N(mf) >> 2; k = MF_get_gk(mf);
   10927           7 :   if (typ(k) == t_INT) pari_err_TYPE("mfkohneneigenbasis", k);
   10928           7 :   if (!uissquarefree(N4))
   10929           0 :     pari_err_TYPE("mfkohneneigenbasis [N not squarefree]", utoipos(N4));
   10930           7 :   r = MF_get_r(mf);
   10931           7 :   KM = RgM_mul(gel(bij,3), gel(bij,2));
   10932           7 :   mf3 = gel(bij,1);
   10933           7 :   mf30 = mfinit_Nkchi(N4, 2*r, MF_get_CHI(mf3), mf_NEW, 0);
   10934           7 :   B = mfcoefs_mf(mf30, mfsturm_mf(mf3), 1); l = lg(B);
   10935           7 :   M = cgetg(l, t_MAT);
   10936          21 :   for (i=1; i<l; i++) gel(M,i) = RgM_RgC_mul(KM, mftobasis_i(mf3, gel(B,i)));
   10937           7 :   return gerepilecopy(av, mkvec3(mf30, M, RgM_mul(M, MF_get_newforms(mf30))));
   10938             : }
   10939             : /*************************** End Kohnen ************************************/
   10940             : /***************************************************************************/
   10941             : 
   10942             : static GEN desc(GEN F);
   10943             : static GEN
   10944         504 : desc_mfeisen(GEN F)
   10945             : {
   10946         504 :   GEN R, gk = mf_get_gk(F);
   10947         504 :   if (typ(gk) == t_FRAC)
   10948           7 :     R = gsprintf("H_{%Ps}", gk);
   10949             :   else
   10950             :   {
   10951         497 :     GEN vchi = gel(F, 2), CHI = mfchisimpl(gel(vchi, 3));
   10952         497 :     long k = itou(gk);
   10953         497 :     if (lg(vchi) < 5) R = gsprintf("F_%ld(%Ps)", k, CHI);
   10954             :     else
   10955             :     {
   10956         294 :       GEN CHI2 = mfchisimpl(gel(vchi, 4));
   10957         294 :       R = gsprintf("F_%ld(%Ps, %Ps)", k, CHI, CHI2);
   10958             :     }
   10959             :   }
   10960         504 :   return R;
   10961             : }
   10962             : static GEN
   10963          35 : desc_hecke(GEN F)
   10964             : {
   10965             :   long n, N;
   10966          35 :   GEN D = gel(F,2);
   10967          35 :   if (typ(D) == t_VECSMALL) { N = D[3]; n = D[1]; }
   10968          14 :   else { GEN nN = gel(D,2); n = nN[1]; N = nN[2]; } /* half integer */
   10969          35 :   return gsprintf("T_%ld(%ld)(%Ps)", N, n, desc(gel(F,3)));
   10970             : }
   10971             : static GEN
   10972          98 : desc_linear(GEN FLD, GEN dL)
   10973             : {
   10974          98 :   GEN F = gel(FLD,2), L = gel(FLD,3), R = strtoGENstr("LIN([");
   10975          98 :   long n = lg(F) - 1, i;
   10976         168 :   for (i = 1; i <= n; i++)
   10977             :   {
   10978         168 :     R = shallowconcat(R, desc(gel(F,i))); if (i == n) break;
   10979          70 :     R = shallowconcat(R, strtoGENstr(", "));
   10980             :   }
   10981          98 :   return shallowconcat(R, gsprintf("], %Ps)", gdiv(L, dL)));
   10982             : }
   10983             : static GEN
   10984          21 : desc_dihedral(GEN F)
   10985             : {
   10986          21 :   GEN bnr = gel(F,2), D = nf_get_disc(bnr_get_nf(bnr)), f = bnr_get_mod(bnr);
   10987          21 :   GEN cyc = bnr_get_cyc(bnr);
   10988          21 :   GEN w = gel(F,3), chin = zv_to_ZV(gel(w,2)), o = utoi(gel(w,1)[1]);
   10989          21 :   GEN chi = char_denormalize(cyc, o, chin);
   10990          21 :   if (lg(gel(f,2)) == 1) f = gel(f,1);
   10991          21 :   return gsprintf("DIH(%Ps, %Ps, %Ps, %Ps)",D,f,cyc,chi);
   10992             : }
   10993             : 
   10994             : static void
   10995        1043 : unpack0(GEN *U)
   10996        1043 : { if (U) *U = mkvec2(cgetg(1, t_VEC), cgetg(1, t_VEC)); }
   10997             : static void
   10998          42 : unpack2(GEN F, GEN *U)
   10999          42 : { if (U) *U = mkvec2(mkvec2(gel(F,2), gel(F,3)), cgetg(1, t_VEC)); }
   11000             : static void
   11001         308 : unpack23(GEN F, GEN *U)
   11002         308 : { if (U) *U = mkvec2(mkvec(gel(F,2)), mkvec(gel(F,3))); }
   11003             : static GEN
   11004        1540 : desc_i(GEN F, GEN *U)
   11005             : {
   11006        1540 :   switch(mf_get_type(F))
   11007             :   {
   11008           7 :     case t_MF_CONST: unpack0(U); return gsprintf("CONST(%Ps)", gel(F,2));
   11009         504 :     case t_MF_EISEN: unpack0(U); return desc_mfeisen(F);
   11010         154 :     case t_MF_Ek: unpack0(U); return gsprintf("E_%ld", mf_get_k(F));
   11011          63 :     case t_MF_DELTA: unpack0(U); return gsprintf("DELTA");
   11012          35 :     case t_MF_THETA: unpack0(U);
   11013          35 :       return gsprintf("THETA(%Ps)", mfchisimpl(gel(F,2)));
   11014          56 :     case t_MF_ETAQUO: unpack0(U);
   11015          56 :       return gsprintf("ETAQUO(%Ps, %Ps)", gel(F,2), gel(F,3));
   11016          56 :     case t_MF_ELL: unpack0(U);
   11017          56 :       return gsprintf("ELL(%Ps)", vecslice(gel(F,2), 1, 5));
   11018           7 :     case t_MF_TRACE: unpack0(U); return gsprintf("TR(%Ps)", mfparams(F));
   11019         140 :     case t_MF_NEWTRACE: unpack0(U); return gsprintf("TR^new(%Ps)", mfparams(F));
   11020          21 :     case t_MF_DIHEDRAL: unpack0(U); return desc_dihedral(F);
   11021          28 :     case t_MF_MUL: unpack2(F, U);
   11022          28 :       return gsprintf("MUL(%Ps, %Ps)", desc(gel(F,2)), desc(gel(F,3)));
   11023          14 :     case t_MF_DIV: unpack2(F, U);
   11024          14 :       return gsprintf("DIV(%Ps, %Ps)", desc(gel(F,2)), desc(gel(F,3)));
   11025          14 :     case t_MF_POW: unpack23(F, U);
   11026          14 :       return gsprintf("POW(%Ps, %ld)", desc(gel(F,2)), itos(gel(F,3)));
   11027          14 :     case t_MF_SHIFT: unpack23(F, U);
   11028          14 :       return gsprintf("SHIFT(%Ps, %ld)", desc(gel(F,2)), itos(gel(F,3)));
   11029          14 :     case t_MF_DERIV: unpack23(F, U);
   11030          14 :       return gsprintf("DER^%ld(%Ps)", itos(gel(F,3)), desc(gel(F,2)));
   11031          21 :     case t_MF_DERIVE2: unpack23(F, U);
   11032          21 :       return gsprintf("DERE2^%ld(%Ps)", itos(gel(F,3)), desc(gel(F,2)));
   11033          14 :     case t_MF_TWIST: unpack23(F, U);
   11034          14 :       return gsprintf("TWIST(%Ps, %Ps)", desc(gel(F,2)), gel(F,3));
   11035         231 :     case t_MF_BD: unpack23(F, U);
   11036         231 :       return gsprintf("B(%ld)(%Ps)", itou(gel(F,3)), desc(gel(F,2)));
   11037          14 :     case t_MF_BRACKET:
   11038          14 :       if (U) *U = mkvec2(mkvec2(gel(F,2), gel(F,3)), mkvec(gel(F,4)));
   11039          14 :       return gsprintf("MULRC_%ld(%Ps, %Ps)", itos(gel(F,4)), desc(gel(F,2)), desc(gel(F,3)));
   11040          98 :     case t_MF_LINEAR_BHN:
   11041             :     case t_MF_LINEAR:
   11042          98 :       if (U) *U = mkvec2(gel(F,2), mkvec(gdiv(gel(F,3), gel(F,4))));
   11043          98 :       return desc_linear(F,gel(F,4));
   11044          35 :     case t_MF_HECKE:
   11045          35 :       if (U) *U = mkvec2(mkvec(gel(F,3)), mkvec(stoi(gel(F,2)[1])));
   11046          35 :       return desc_hecke(F);
   11047           0 :     default: pari_err_TYPE("mfdescribe",F);
   11048             :     return NULL;/*LCOV_EXCL_LINE*/
   11049             :   }
   11050             : }
   11051             : static GEN
   11052         623 : desc(GEN F) { return desc_i(F, NULL); }
   11053             : GEN
   11054         966 : mfdescribe(GEN F, GEN *U)
   11055             : {
   11056         966 :   pari_sp av = avma;
   11057             :   GEN mf;
   11058         966 :   if ((mf = checkMF_i(F)))
   11059             :   {
   11060          49 :     const char *f = NULL;
   11061          49 :     switch (MF_get_space(mf))
   11062             :     {
   11063           7 :       case mf_NEW:  f = "S_%Ps^new(G_0(%ld, %Ps))"; break;
   11064          14 :       case mf_CUSP: f = "S_%Ps(G_0(%ld, %Ps))"; break;
   11065           7 :       case mf_OLD:  f = "S_%Ps^old(G_0(%ld, %Ps))"; break;
   11066           7 :       case mf_EISEN:f = "E_%Ps(G_0(%ld, %Ps))"; break;
   11067          14 :       case mf_FULL: f = "M_%Ps(G_0(%ld, %Ps))"; break;
   11068             :     }
   11069          49 :     if (U) *U = cgetg(1, t_VEC);
   11070          49 :     return gsprintf(f, MF_get_gk(mf), MF_get_N(mf), mfchisimpl(MF_get_CHI(mf)));
   11071             :   }
   11072         917 :   if (!checkmf_i(F)) pari_err_TYPE("mfdescribe", F);
   11073         917 :   F = desc_i(F, U); return gc_all(av, U ? 2: 1, &F, U);
   11074             : }
   11075             : 
   11076             : /***********************************************************************/
   11077             : /*               Eisenstein series H_r of weight r+1/2                 */
   11078             : /***********************************************************************/
   11079             : /* radical(u_ppo(g,q)) */
   11080             : static long
   11081          28 : u_pporad(long g, long q)
   11082             : {
   11083          28 :   GEN F = myfactoru(g), P = gel(F,1);
   11084             :   long i, l, n;
   11085          28 :   if (q == 1) return zv_prod(P);
   11086          28 :   l = lg(P);
   11087          35 :   for (i = n = 1; i < l; i++)
   11088             :   {
   11089           7 :     long p = P[i];
   11090           7 :     if (q % p) n *= p;
   11091             :   }
   11092          28 :   return n;
   11093             : }
   11094             : static void
   11095         266 : c_F2TH4(long n, GEN *pF2, GEN *pTH4)
   11096             : {
   11097         266 :   GEN v = mfcoefs_i(mfEk(2), n, 1), v2 = bdexpand(v,2), v4 = bdexpand(v,4);
   11098         266 :   GEN F2 = gdivgs(ZC_add(ZC_sub(v, ZC_z_mul(v2,3)), ZC_z_mul(v4,2)), -24);
   11099         266 :   GEN TH4 = gdivgs(ZC_sub(v, ZC_z_mul(v4,4)), -3);
   11100         266 :   settyp(F2,t_VEC); *pF2 = F2;
   11101         266 :   settyp(TH4,t_VEC);*pTH4= TH4;
   11102         266 : }
   11103             : /* r > 0, N >= 0 */
   11104             : static GEN
   11105          77 : mfEHcoef(long r, long N)
   11106             : {
   11107             :   long D0, f, i, l, s;
   11108             :   GEN S, Df;
   11109             : 
   11110          77 :   if (r == 1) return hclassno(utoi(N));
   11111          77 :   if (N == 0) return gdivgs(bernfrac(2*r), -2*r);
   11112          56 :   s = N & 3L;
   11113          56 :   if (odd(r))
   11114             :   {
   11115          42 :     if (s == 2 || s == 1) return gen_0;
   11116          14 :     D0 = mycoredisc2neg(N,&f);
   11117             :   }
   11118             :   else
   11119             :   {
   11120          14 :     if (s == 2 || s == 3) return gen_0;
   11121          14 :     D0 = mycoredisc2pos(N,&f);
   11122             :   }
   11123          28 :   Df = mydivisorsu(u_pporad(f, D0)); l = lg(Df);
   11124          28 :   S = gen_0;
   11125          63 :   for (i = 1; i < l; i++)
   11126             :   {
   11127          35 :     long d = Df[i], s = mymoebiusu(d)*kross(D0, d); /* != 0 */
   11128          35 :     GEN c = gmul(powuu(d, r-1), mysumdivku(f/d, 2*r-1));
   11129          35 :     S = s > 0? addii(S, c): subii(S, c);
   11130             :   }
   11131          28 :   return gmul(lfunquadneg_naive(D0, r), S);
   11132             : }
   11133             : static GEN
   11134         266 : mfEHmat(long lim, long r)
   11135             : {
   11136         266 :   long j, l, d = r/2;
   11137             :   GEN f2, th4, th3, v, vth4, vf2;
   11138         266 :   c_F2TH4(lim, &f2, &th4);
   11139         266 :   f2 =  RgV_to_ser(f2, 0, lim+3);
   11140         266 :   th4 = RgV_to_ser(th4, 0, lim+3);
   11141         266 :   th3 = RgV_to_ser(c_theta(lim, 1, mfchartrivial()), 0, lim+3);
   11142         266 :   if (odd(r)) th3 = gpowgs(th3, 3);
   11143         266 :   vth4 = gpowers(th4, d);
   11144         266 :   vf2 = gpowers0(f2, d, th3); /* th3 f2^j */
   11145         266 :   l = d+2; v = cgetg(l, t_VEC);
   11146         924 :   for (j = 1; j < l; j++)
   11147         658 :     gel(v, j) = ser2rfrac_i(gmul(gel(vth4, l-j), gel(vf2, j)));
   11148         266 :   return RgXV_to_RgM(v, lim);
   11149             : }
   11150             : static GEN
   11151           7 : Hfind(long r, GEN *pden)
   11152             : {
   11153           7 :   long lim = (r/2)+3, i;
   11154             :   GEN res, M, B;
   11155             : 
   11156           7 :   if (r <= 0) pari_err_DOMAIN("mfEH", "r", "<=", gen_0, stoi(r));
   11157           7 :   M = mfEHmat(lim, r);
   11158           7 :   B = cgetg(lim+1, t_COL);
   11159          56 :   for (i = 1; i <= lim; i++) gel(B, i) = mfEHcoef(r, i-1);
   11160           7 :   res = QM_gauss(M, B);
   11161           7 :   if (lg(res) == 1) pari_err_BUG("mfEH");
   11162           7 :   return Q_remove_denom(res,pden);
   11163             : }
   11164             : GEN
   11165         266 : mfEH(GEN gk)
   11166             : {
   11167         266 :   pari_sp av = avma;
   11168         266 :   GEN v, d, NK, gr = gsub(gk, ghalf);
   11169             :   long r;
   11170         266 :   if (typ(gr) != t_INT) pari_err_TYPE("mfEH", gk);
   11171         266 :   r = itos(gr);
   11172         266 :   switch (r)
   11173             :   {
   11174           7 :     case 1: v=cgetg(1,t_VEC); d=gen_1; break;
   11175         133 :     case 2: v=mkvec2s(1,-20); d=utoipos(120); break;
   11176          56 :     case 3: v=mkvec2s(-1,14); d=utoipos(252); break;
   11177          35 :     case 4: v=mkvec3s(1,-16,16); d=utoipos(240); break;
   11178           7 :     case 5: v=mkvec3s(-1,22,-88); d=utoipos(132); break;
   11179          14 :     case 6: v=mkvec4s(691,-18096,110136,-4160); d=utoipos(32760); break;
   11180           7 :     case 7: v=mkvec4s(-1,30,-240,224); d=utoipos(12); break;
   11181           7 :     default: v = Hfind(r, &d); break;
   11182             :   }
   11183         266 :   NK = mkgNK(utoipos(4), gaddgs(ghalf,r), mfchartrivial(), pol_x(1));
   11184         266 :   return gerepilecopy(av, tag(t_MF_EISEN, NK, mkvec2(v,d)));
   11185             : }
   11186             : 
   11187             : /**********************************************************/
   11188             : /*             T(f^2) for half-integral weight            */
   11189             : /**********************************************************/
   11190             : 
   11191             : /* T_p^2 V, p2 = p^2, c1 = chi(p) (-1/p)^r p^(r-1), c2 = chi(p^2)*p^(2r-1) */
   11192             : static GEN
   11193          70 : tp2apply(GEN V, long p, long p2, GEN c1, GEN c2)
   11194             : {
   11195          70 :   long lw = (lg(V) - 2)/p2 + 1, m, n;
   11196          70 :   GEN a0 = gel(V,1), W = cgetg(lw + 1, t_VEC);
   11197             : 
   11198          70 :   gel(W,1) = gequal0(a0)? gen_0: gmul(a0, gaddsg(1, c2));
   11199       11109 :   for (n = 1; n < lw; n++)
   11200             :   {
   11201       11039 :     GEN c = gel(V, p2*n + 1);
   11202       11039 :     if (n%p) c = gadd(c, gmulsg(kross(n,p), gmul(gel(V,n+1), c1)));
   11203       11039 :     gel(W, n+1) = c; /* a(p^2*n) + c1 * (n/p) a(n) */
   11204             :   }
   11205        1253 :   for (m = 1, n = p2; n < lw; m++, n += p2)
   11206        1183 :     gel(W, n+1) = gadd(gel(W,n+1), gmul(gel(V,m+1), c2));
   11207          70 :   return W;
   11208             : }
   11209             : 
   11210             : /* T_{p^{2e}} V; can derecursify [Purkait, Hecke operators in half-integral
   11211             :  * weight, Prop 4.3], not worth it */
   11212             : static GEN
   11213          70 : tp2eapply(GEN V, long p, long p2, long e, GEN q, GEN c1, GEN c2)
   11214             : {
   11215          70 :   GEN V4 = NULL;
   11216          70 :   if (e > 1)
   11217             :   {
   11218          21 :     V4 = vecslice(V, 1, (lg(V) - 2)/(p2*p2) + 1);
   11219          21 :     V = tp2eapply(V, p, p2, e-1, q, c1, c2);
   11220             :   }
   11221          70 :   V = tp2apply(V, p, p2, c1, c2);
   11222          70 :   if (e > 1)
   11223          28 :     V = gsub(V, (e == 2)? gmul(q, V4)
   11224           7 :                         : gmul(c2, tp2eapply(V4, p, p2, e-2, q, c1, c2)));
   11225          70 :   return V;
   11226             : }
   11227             : /* weight k = r+1/2 */
   11228             : static GEN
   11229          98 : RgV_heckef2(long n, long d, GEN V, GEN F, GEN DATA)
   11230             : {
   11231          98 :   GEN CHI = mf_get_CHI(F), fa = gel(DATA,1), P = gel(fa,1), E = gel(fa,2);
   11232          98 :   long i, l = lg(P), r = mf_get_r(F), s4 = odd(r)? -4: 4, k2m2 = (r<<1)-1;
   11233          98 :   if (typ(V) == t_COL) V = shallowtrans(V);
   11234         140 :   for (i = 1; i < l; i++)
   11235             :   { /* p does not divide N */
   11236          42 :     long p = P[i], e = E[i], p2 = p*p;
   11237          42 :     GEN c1, c2, a, b, q = NULL, C = mfchareval(CHI,p), C2 = gsqr(C);
   11238          42 :     a = r? powuu(p,r-1): mkfrac(gen_1,utoipos(p)); /* p^(r-1) = p^(k-3/2) */
   11239          42 :     b = r? mulii(powuu(p,r), a): a; /* p^(2r-1) = p^(2k-2) */
   11240          42 :     c1 = gmul(C, gmulsg(kross(s4,p),a));
   11241          42 :     c2 = gmul(C2, b);
   11242          42 :     if (e > 1)
   11243             :     {
   11244          14 :       q = r? powuu(p,k2m2): a;
   11245          14 :       if (e == 2) q = gmul(q, uutoQ(p+1,p)); /* special case T_{p^4} */
   11246          14 :       q = gmul(C2, q); /* chi(p^2) [ p^(2k-2) or (p+1)p^(2k-3) ] */
   11247             :     }
   11248          42 :     V = tp2eapply(V, p, p2, e, q, c1, c2);
   11249             :   }
   11250          98 :   return c_deflate(n, d, V);
   11251             : }
   11252             : 
   11253             : static GEN
   11254        1176 : GL2toSL2(GEN g, GEN *abd)
   11255             : {
   11256             :   GEN A, B, C, D, u, v, a, b, d, q;
   11257        1176 :   g = Q_primpart(g);
   11258        1176 :   if (!check_M2Z(g)) pari_err_TYPE("GL2toSL2", g);
   11259        1176 :   A = gcoeff(g,1,1); B = gcoeff(g,1,2);
   11260        1176 :   C = gcoeff(g,2,1); D = gcoeff(g,2,2);
   11261        1176 :   a = bezout(A, C, &u, &v);
   11262        1176 :   if (!equali1(a)) { A = diviiexact(A,a); C = diviiexact(C,a); }
   11263        1176 :   d = subii(mulii(A,D), mulii(B,C));
   11264        1176 :   if (signe(d) <= 0) pari_err_TYPE("GL2toSL2",g);
   11265        1169 :   q = dvmdii(addii(mulii(u,B), mulii(v,D)), d, &b);
   11266        1169 :   *abd = (equali1(a) && equali1(d))? NULL: mkvec3(a, b, d);
   11267        1169 :   return mkmat22(A, subii(mulii(q,A), v), C, addii(mulii(q,C), u));
   11268             : }
   11269             : 
   11270             : static GEN
   11271        8414 : Rg_approx(GEN t, long bit)
   11272             : {
   11273        8414 :   GEN a = real_i(t), b = imag_i(t);
   11274        8414 :   long e1 = gexpo(a), e2 = gexpo(b);
   11275        8414 :   if (e2 < -bit) { t = e1 < -bit? gen_0: a; }
   11276        6426 :   else if (e1 < -bit) t = gmul(b, gen_I());
   11277        8414 :   return t;
   11278             : }
   11279             : static GEN
   11280         126 : RgV_approx(GEN x, long bit)
   11281         840 : { pari_APPLY_same(Rg_approx(gel(x,i), bit)); }
   11282             : /* m != 2 (mod 4), D t_INT; V has "denominator" D, recognize in Q(zeta_m) */
   11283             : static GEN
   11284         126 : bestapprnf2(GEN V, long m, GEN D, long prec)
   11285             : {
   11286         126 :   long i, j, f, vt = fetch_user_var("t"), bit = prec2nbits_mul(prec, 0.8);
   11287         126 :   GEN Tinit, Vl, H, Pf, P = polcyclo(m, vt);
   11288             : 
   11289         126 :   V = liftpol_shallow(V);
   11290         126 :   V = gmul(RgV_approx(V, bit), D);
   11291         126 :   V = bestapprnf(V, P, NULL, prec);
   11292         126 :   Vl = liftpol_shallow(V);
   11293         126 :   H = coprimes_zv(m);
   11294         672 :   for (i = 2; i < m; i++)
   11295             :   {
   11296         546 :     if (H[i] != 1) continue;
   11297         280 :     if (!gequal(Vl, vecGalois(Vl, i, P, m))) H[i] = 0;
   11298          14 :     else for (j = i; j < m; j *= i) H[i] = 3;
   11299             :   }
   11300         126 :   f = znstar_conductor_bits(Flv_to_F2v(H));
   11301         126 :   if (f == 1) return gdiv(V, D);
   11302          98 :   if (f == m) return gmodulo(gdiv(V, D), P);
   11303           7 :   Pf = polcyclo(f, vt);
   11304           7 :   Tinit = Qab_trace_init(m, f, P, Pf);
   11305           7 :   return gmodulo(gdiv(QabV_tracerel(Tinit, 0, Vl), D), Pf);
   11306             : }
   11307             : 
   11308             : /* f | ga expansion; [f, mf_eisendec(f)]~ allowed */
   11309             : GEN
   11310        1113 : mfslashexpansion(GEN mf, GEN f, GEN ga, long n, long flrat, GEN *params, long prec)
   11311             : {
   11312        1113 :   pari_sp av = avma;
   11313        1113 :   GEN a, b, d, res, al, V, M, ad, abd, gk, A, awd = NULL;
   11314             :   long i, w;
   11315             : 
   11316        1113 :   mf = checkMF(mf);
   11317        1113 :   gk = MF_get_gk(mf);
   11318        1113 :   M = GL2toSL2(ga, &abd);
   11319        1106 :   if (abd) { a = gel(abd,1); b = gel(abd,2); d = gel(abd,3); }
   11320         868 :   else { a = d = gen_1; b = gen_0; }
   11321        1106 :   ad = gdiv(a,d);
   11322        1106 :   res = mfgaexpansion(mf, f, M, n, prec);
   11323        1106 :   al = gel(res,1);
   11324        1106 :   w = itou(gel(res,2));
   11325        1106 :   V = gel(res,3);
   11326        1106 :   if (flrat)
   11327             :   {
   11328         126 :     GEN CHI = MF_get_CHI(mf);
   11329         126 :     long N = MF_get_N(mf), F = mfcharconductor(CHI);
   11330         126 :     long ord = mfcharorder(CHI), k, deg;
   11331         126 :     long B = umodiu(gcoeff(M,1,2), N);
   11332         126 :     long C = umodiu(gcoeff(M,2,1), N);
   11333         126 :     long D = umodiu(gcoeff(M,2,2), N);
   11334         126 :     long CD = (C * D) % N, BC = (B * C) % F;
   11335             :     GEN CV, t;
   11336             :     /* weight of f * Theta in 1/2-integral weight */
   11337         126 :     k = typ(gk) == t_INT? (long) itou(gk): MF_get_r(mf)+1;
   11338         126 :     CV = odd(k) ? powuu(N, k - 1) : powuu(N, k >> 1);
   11339         126 :     deg = ulcm(ulcm(ord, N/ugcd(N,CD)), F/ugcd(F,BC));
   11340         126 :     if ((deg & 3) == 2) deg >>= 1;
   11341         126 :     if (typ(gk) != t_INT && odd(deg) && mfthetaI(C,D)) deg <<= 2;
   11342         126 :     V = bestapprnf2(V, deg, CV, prec);
   11343         126 :     if (abd && !signe(b))
   11344             :     { /* can [a,0; 0,d] be simplified to id ? */
   11345           7 :       long nk, dk; Qtoss(gk, &nk, &dk);
   11346           7 :       if (ispower(ad, utoipos(2*dk), &t)) /* t^(2*dk) = a/d or t = NULL */
   11347             :       {
   11348           7 :         V = RgV_Rg_mul(V, powiu(t,nk));
   11349           7 :         awd = gdiv(a, muliu(d,w));
   11350             :       }
   11351             :     }
   11352             :   }
   11353         980 :   else if (abd)
   11354             :   { /* ga = M * [a,b;0,d] * rational, F := f | M = q^al * \sum V[j] q^(j/w) */
   11355         231 :     GEN u, t = NULL, wd = muliu(d,w);
   11356             :     /* a > 0, 0 <= b < d; f | ga = (a/d)^(k/2) * F(tau + b/d) */
   11357         231 :     if (signe(b))
   11358             :     {
   11359             :       long ns, ds;
   11360             :       GEN z;
   11361           0 :       Qtoss(gdiv(b, wd), &ns, &ds); z = rootsof1powinit(ns, ds, prec);
   11362           0 :       for (i = 1; i <= n+1; i++) gel(V,i) = gmul(gel(V,i), rootsof1pow(z, i-1));
   11363           0 :       if (!gequal0(al)) t = gexp(gmul(PiI2(prec), gmul(al, gdiv(b,d))), prec);
   11364             :     }
   11365         231 :     awd = gdiv(a, wd);
   11366         231 :     u = gpow(ad, gmul2n(gk,-1), prec);
   11367         231 :     t = t? gmul(t, u): u;
   11368         231 :     V = RgV_Rg_mul(V, t);
   11369             :   }
   11370        1106 :   if (!awd) A = mkmat22(a, b, gen_0, d);
   11371             :   else
   11372             :   { /* rescale and update w from [a,0; 0,d] */
   11373             :     long ns;
   11374         238 :     Qtoss(awd, &ns, &w); /* update w */
   11375         238 :     V = bdexpand(V, ns);
   11376         238 :     if (!gequal0(al))
   11377             :     {
   11378           0 :       GEN adal = gmul(ad, al), sh = gfloor(adal);
   11379           0 :       al = gsub(adal, sh);
   11380           0 :       V = RgV_shift(V, sh);
   11381             :     }
   11382         238 :     A = matid(2);
   11383             :   }
   11384        1106 :   if (params) *params = mkvec3(al, utoipos(w), A);
   11385        1106 :   return gc_all(av,params?2:1,&V,params);
   11386             : }
   11387             : 
   11388             : /**************************************************************/
   11389             : /*         Alternative method for 1/2-integral weight         */
   11390             : /**************************************************************/
   11391             : static GEN
   11392         266 : mf2basis(long N, long r, GEN CHI, GEN *pCHI1, long space)
   11393             : {
   11394             :   GEN CHI1, CHI2, mf1, mf2, B1, B2, BT, M1, M2, M, M2i, T, Th, v, den;
   11395         266 :   long sb, N2, o1, o2, k1 = r + 1;
   11396             : 
   11397         266 :   if (odd(k1))
   11398             :   {
   11399         154 :     CHI1 = mfcharmul(CHI, get_mfchar(stoi(-4)));
   11400         154 :     CHI2 = mfcharmul(CHI, get_mfchar(stoi(-8)));
   11401             :   }
   11402             :   else
   11403             :   {
   11404         112 :     CHI1 = CHI;
   11405         112 :     CHI2 = mfcharmul(CHI, get_mfchar(utoi(8)));
   11406             :   }
   11407         266 :   mf1 = mfinit_Nkchi(N, k1, CHI1, space, 1);
   11408         266 :   if (pCHI1) *pCHI1 = CHI1;
   11409         266 :   B1 = MF_get_basis(mf1); if (lg(B1) == 1) return cgetg(1,t_VEC);
   11410         259 :   N2 = ulcm(8, N);
   11411         259 :   mf2 = mfinit_Nkchi(N2, k1, CHI2, space, 1);
   11412         259 :   B2 = MF_get_basis(mf2); if (lg(B2) == 1) return cgetg(1,t_VEC);
   11413         259 :   sb = mfsturmNgk(N2, gaddsg(k1, ghalf));
   11414         259 :   M1 = mfcoefs_mf(mf1, sb, 1);
   11415         259 :   M2 = mfcoefs_mf(mf2, sb, 1);
   11416         259 :   Th = mfTheta(NULL);
   11417         259 :   BT = mfcoefs_i(Th, sb, 1);
   11418         259 :   M1 = mfmatsermul(M1, RgV_to_RgX(expandbd(BT,2),0));
   11419         259 :   M2 = mfmatsermul(M2, RgV_to_RgX(BT,0));
   11420         259 :   o1= mfcharorder(CHI1);
   11421         259 :   T = (o1 <= 2)? NULL: mfcharpol(CHI1);
   11422         259 :   if (o1 > 2) M1 = liftpol_shallow(M1);
   11423         259 :   o2= mfcharorder(CHI2);
   11424         259 :   if (T)
   11425             :   {
   11426          14 :     if (o2 == o1) M2 = liftpol_shallow(M2);
   11427             :     else
   11428             :     {
   11429           0 :       GEN tr = Qab_trace_init(o2, o1, mfcharpol(CHI2), mfcharpol(CHI1));
   11430           0 :       M2 = QabM_tracerel(tr, 0, M2);
   11431             :     }
   11432             :   }
   11433             :   /* now everything is defined mod T = mfcharpol(CHI1) */
   11434         259 :   M2i = QabM_pseudoinv_i(M2, T, o1, &v, &den);
   11435         259 :   M = RgM_mul(M2i, rowpermute(M1, gel(v,1)));
   11436         259 :   M = RgM_mul(M2, M);
   11437         259 :   M1 = RgM_Rg_mul(M1, den);
   11438         259 :   M = RgM_sub(M1, M); if (T) M = RgXQM_red(M, T);
   11439         259 :   return vecmflineardiv0(B1, QabM_ker(M, T, o1), Th);
   11440             : }
   11441             : 
   11442             : /*******************************************************************/
   11443             : /*                         Integration                             */
   11444             : /*******************************************************************/
   11445             : static GEN
   11446         462 : vanembed(GEN F, GEN v, long prec)
   11447             : {
   11448         462 :   GEN CHI = mf_get_CHI(F);
   11449         462 :   long o = mfcharorder(CHI);
   11450         462 :   if (o > 2 || degpol(mf_get_field(F)) > 1) v = liftpol_shallow(v);
   11451         462 :   if (o > 2) v = gsubst(v, varn(mfcharpol(CHI)), rootsof1u_cx(o, prec));
   11452         462 :   return v;
   11453             : }
   11454             : 
   11455             : static long
   11456        1204 : mfperiod_prelim_double(double t0, long k, long bitprec)
   11457             : {
   11458        1204 :   double nlim, c = 2*M_PI*t0;
   11459        1204 :   nlim = ceil(bitprec * M_LN2 / c);
   11460        1204 :   c -= (k - 1)/(2*nlim); if (c < 1) c = 1.;
   11461        1204 :   nlim += ceil((0.7 + (k-1)/2*log(nlim))/c);
   11462        1204 :   return (long)nlim;
   11463             : }
   11464             : static long
   11465         280 : mfperiod_prelim(GEN t0, long k, long bitprec)
   11466         280 : { return mfperiod_prelim_double(gtodouble(t0), k, bitprec); }
   11467             : 
   11468             : /* (-X)^(k-2) * P(-1/X) = (-1)^{k-2} P|_{k-2} S */
   11469             : static GEN
   11470        1281 : RgX_act_S(GEN P, long k)
   11471             : {
   11472        1281 :   P = RgX_unscale(RgX_recipspec_shallow(P+2, lgpol(P), k-1), gen_m1);
   11473        1281 :   setvarn(P,0); return P;
   11474             : }
   11475             : static int
   11476        2828 : RgX_act_typ(GEN P, long k)
   11477             : {
   11478        2828 :   switch(typ(P))
   11479             :   {
   11480          35 :     case t_RFRAC: return t_RFRAC;
   11481        2793 :     case t_POL:
   11482        2793 :       if (varn(P) == 0)
   11483             :       {
   11484        2793 :         long d = degpol(P);
   11485        2793 :         if (d > k-2) return t_RFRAC;
   11486        2646 :         if (d) return t_POL;
   11487             :       }
   11488             :   }
   11489        1211 :   return 0;
   11490             : }
   11491             : static GEN
   11492        2562 : act_S(GEN P, long k)
   11493             : {
   11494             :   GEN X;
   11495        2562 :   switch(RgX_act_typ(P, k))
   11496             :   {
   11497         133 :     case t_RFRAC:
   11498         133 :       X = gneg(pol_x(0));
   11499         133 :       return gmul(gsubst(P, 0, ginv(X)), gpowgs(X, k - 2));
   11500        1281 :     case t_POL: return RgX_act_S(P, k);
   11501             :   }
   11502        1148 :   return P;
   11503             : }
   11504             : 
   11505             : static GEN
   11506         203 : AX_B(GEN M)
   11507         203 : { GEN A = gcoeff(M,1,1), B = gcoeff(M,1,2); return deg1pol_shallow(A,B,0); }
   11508             : static GEN
   11509         203 : CX_D(GEN M)
   11510         203 : { GEN C = gcoeff(M,2,1), D = gcoeff(M,2,2); return deg1pol_shallow(C,D,0); }
   11511             : 
   11512             : /* P|_{2-k}M = (CX+D)^{k-2}P((AX+B)/(CX+D)) */
   11513             : static GEN
   11514         154 : RgX_act_gen(GEN P, GEN M, long k)
   11515             : {
   11516         154 :   GEN S = gen_0, PCD, PAB;
   11517             :   long i;
   11518         154 :   PCD = gpowers(CX_D(M), k-2);
   11519         154 :   PAB = gpowers(AX_B(M), k-2);
   11520         833 :   for (i = 0; i <= k-2; i++)
   11521             :   {
   11522         679 :     GEN t = RgX_coeff(P, i);
   11523         679 :     if (!gequal0(t)) S = gadd(S, gmul(t, gmul(gel(PCD, k-i-1), gel(PAB, i+1))));
   11524             :   }
   11525         154 :   return S;
   11526             : }
   11527             : static GEN
   11528         266 : act_GL2(GEN P, GEN M, long k)
   11529             : {
   11530         266 :   switch(RgX_act_typ(P, k))
   11531             :   {
   11532          49 :     case t_RFRAC:
   11533             :     {
   11534          49 :       GEN AB = AX_B(M), CD = CX_D(M);
   11535          49 :       return gmul(gsubst(P, 0, gdiv(AB, CD)), gpowgs(CD, k - 2));
   11536             :     }
   11537         154 :     case t_POL: return RgX_act_gen(P, M, k);
   11538             :   }
   11539          63 :   return P;
   11540             : }
   11541             : static GEN
   11542           7 : vecact_GL2(GEN x, GEN M, long k)
   11543          21 : { pari_APPLY_same(act_GL2(gel(x,i), M, k)); }
   11544             : 
   11545             : static GEN
   11546        2849 : RgX_approx(GEN x, long bit)
   11547       10549 : { pari_APPLY_pol_normalized(Rg_approx(gel(x,i),bit)); }
   11548             : 
   11549             : static GEN normalizeapprox(GEN x, long bit);
   11550             : static GEN
   11551        2884 : normalizeapprox_i(GEN x, long bit)
   11552             : {
   11553        2884 :   GEN D = gen_1;
   11554        2940 :   if (is_vec_t(typ(x))) pari_APPLY_same(normalizeapprox(gel(x,i), bit));
   11555        2856 :   if (typ(x) == t_RFRAC && varn(gel(x,2)) == 0) { D = gel(x,2); x = gel(x,1); }
   11556        2856 :   if (typ(x) != t_POL || varn(x) != 0) return gdiv(x, D);
   11557        2849 :   return gdiv(RgX_approx(x, bit), D);
   11558             : }
   11559             : static GEN
   11560          56 : normalizeapprox(GEN x, long bit)
   11561             : {
   11562          56 :   pari_sp av = avma;
   11563          56 :   return gerepileupto(av, normalizeapprox_i(x, bit));
   11564             : }
   11565             : 
   11566             : /* make sure T is a t_POL in variable 0 */
   11567             : static GEN
   11568        2828 : toRgX0(GEN T)
   11569        2828 : { return typ(T) == t_POL && varn(T) == 0? T: scalarpol_shallow(T,0); }
   11570             : 
   11571             : /* integrate by summing  nlim+1 terms of van [may be < lg(van)]
   11572             :  * van can be an expansion with vector coefficients
   11573             :  * \int_A^oo \sum_n van[n] * q^(n/w + al) * P(z-A) dz, q = e(z) */
   11574             : static GEN
   11575         910 : intAoo(GEN van, long nlim, GEN al, long w, GEN P, GEN A, long k, long prec)
   11576             : {
   11577             :   GEN alw, P1, piI2A, q, S, van0;
   11578         910 :   long n, vz = varn(gel(P,2));
   11579             : 
   11580         910 :   if (nlim < 1) nlim = 1;
   11581         910 :   alw = gmulsg(w, al);
   11582         910 :   P1 = RgX_translate(P, gneg(A));
   11583         910 :   piI2A = gmul(PiI2n(1, prec), A);
   11584         910 :   q = gexp(gdivgu(piI2A, w), prec);
   11585         910 :   S = gen_0;
   11586      120876 :   for (n = nlim; n >= 1; n--)
   11587             :   {
   11588      119966 :     GEN t = gsubst(P1, vz, gdivsg(w, gaddsg(n, alw)));
   11589      119966 :     S = gadd(gmul(gel(van, n+1), t), gmul(q, S));
   11590             :   }
   11591         910 :   S = gmul(q, S);
   11592         910 :   van0 = gel(van, 1);
   11593         910 :   if (!gequal0(al))
   11594             :   {
   11595          42 :     S = gadd(S, gmul(gsubst(P1, vz, ginv(al)), van0));
   11596          42 :     S = gmul(S, gexp(gmul(piI2A, al), prec));
   11597             :   }
   11598         868 :   else if (!gequal0(van0))
   11599         210 :     S = gsub(S, gdivgu(gmul(van0, gpowgs(gsub(pol_x(0), A), k-1)), k-1));
   11600         910 :   if (is_vec_t(typ(S)))
   11601             :   {
   11602         623 :     long j, l = lg(S);
   11603        3164 :     for (j = 1; j < l; j++) gel(S,j) = toRgX0(gel(S,j));
   11604             :   }
   11605             :   else
   11606         287 :     S = toRgX0(S);
   11607         910 :   return gneg(S);
   11608             : }
   11609             : 
   11610             : /* \sum_{j <= k} X^j * (Y / (2I\pi))^{k+1-j} k! / j! */
   11611             : static GEN
   11612         238 : get_P(long k, long v, long prec)
   11613             : {
   11614         238 :   GEN a, S = cgetg(k + 1, t_POL), u = invr(Pi2n(1, prec+EXTRAPREC64));
   11615         238 :   long j, K = k-2;
   11616         238 :   S[1] = evalsigne(1)|evalvarn(0); a = u;
   11617         238 :   gel(S,K+2) = monomial(mulcxpowIs(a,3), 1, v); /* j = K */
   11618         945 :   for(j = K-1; j >= 0; j--)
   11619             :   {
   11620         707 :     a = mulrr(mulru(a,j+1), u);
   11621         707 :     gel(S,j+2) = monomial(mulcxpowIs(a,3*(K+1-j)), K+1-j, v);
   11622             :   }
   11623         238 :   return S;
   11624             : }
   11625             : 
   11626             : static GEN
   11627        2541 : getw1w2(long N, GEN ga)
   11628        2541 : { return mkvecsmall2(mfZC_width(N, gel(ga,1)),
   11629        2541 :                      mfZC_width(N, gel(ga,2))); }
   11630             : 
   11631             : static GEN
   11632         133 : intAoowithvanall(GEN mf, GEN vanall, GEN P, GEN cosets, long bitprec)
   11633             : {
   11634         133 :   GEN vvan = gel(vanall,1), vaw = gel(vanall,2), W1W2, resall;
   11635         133 :   long prec = nbits2prec(bitprec), N, k, lco, j;
   11636             : 
   11637         133 :   N = MF_get_N(mf); k = MF_get_k(mf);
   11638         133 :   lco = lg(cosets);
   11639         133 :   W1W2 = cgetg(lco, t_VEC); resall = cgetg(lco, t_VEC);
   11640        2674 :   for (j = 1; j < lco; j++) gel(W1W2,j) = getw1w2(N, gel(cosets, j));
   11641        2674 :   for (j = 1; j < lco; j++)
   11642             :   {
   11643        2541 :     GEN w1w2j = gel(W1W2,j), alj, M, VAN, RES, AR, Q;
   11644             :     long jq, c, w1, w2, w;
   11645        2541 :     if (!w1w2j) continue;
   11646         623 :     alj = gel(vaw,j);
   11647         623 :     w1 = w1w2j[1]; Q = cgetg(lco, t_VECSMALL);
   11648         623 :     w2 = w1w2j[2]; M = cgetg(lco, t_COL);
   11649        8239 :     for (c = 1, jq = j; jq < lco; jq++)
   11650             :     {
   11651        7616 :       GEN W = gel(W1W2, jq);
   11652        7616 :       if (jq == j || (W && gequal(W, w1w2j) && gequal(gel(vaw, jq), alj)))
   11653             :       {
   11654        2541 :         Q[c] = jq; gel(W1W2, jq) = NULL;
   11655        2541 :         gel(M, c) = gel(vvan, jq); c++;
   11656             :       }
   11657             :     }
   11658         623 :     setlg(M,c); VAN = shallowmatconcat(M);
   11659         623 :     AR = mkcomplex(gen_0, sqrtr_abs(divru(utor(w1, prec+EXTRAPREC64), w2)));
   11660         623 :     w = itos(gel(alj,2));
   11661         623 :     RES = intAoo(VAN, lg(VAN)-2, gel(alj,1),w, P, AR, k, prec);
   11662        3164 :     for (jq = 1; jq < c; jq++) gel(resall, Q[jq]) = gel(RES, jq);
   11663             :   }
   11664         133 :   return resall;
   11665             : }
   11666             : 
   11667             : GEN
   11668         518 : mftobasisES(GEN mf, GEN F)
   11669             : {
   11670         518 :   GEN v = mftobasis(mf, F, 0);
   11671         511 :   long nE = lg(MF_get_E(mf))-1;
   11672         511 :   return mkvec2(vecslice(v,1,nE), vecslice(v,nE+1,lg(v)-1));
   11673             : }
   11674             : 
   11675             : static long
   11676           0 : wt1mulcond(GEN F, long D, long space)
   11677             : {
   11678           0 :   GEN E = mfeisenstein_i(1, mfchartrivial(), get_mfchar(stoi(D))), mf;
   11679           0 :   F = mfmul(F, E);
   11680           0 :   mf = mfinit_Nkchi(mf_get_N(F), mf_get_k(F), mf_get_CHI(F), space, 0);
   11681           0 :   return mfconductor(mf, F);
   11682             : }
   11683             : static int
   11684           7 : wt1newlevel(long N)
   11685             : {
   11686           7 :   GEN P = gel(myfactoru(N),1);
   11687           7 :   long l = lg(P), i;
   11688          14 :   for (i = 1; i < l; i++)
   11689           7 :     if (!wt1empty(N/P[i])) return 0;
   11690           7 :   return 1;
   11691             : }
   11692             : long
   11693         175 : mfconductor(GEN mf, GEN F)
   11694             : {
   11695         175 :   pari_sp av = avma;
   11696             :   GEN gk;
   11697             :   long space, N, M;
   11698             : 
   11699         175 :   mf = checkMF(mf);
   11700         175 :   if (!checkmf_i(F)) pari_err_TYPE("mfconductor",F);
   11701         175 :   if (mfistrivial(F)) return 1;
   11702         175 :   space = MF_get_space(mf);
   11703         175 :   if (space == mf_NEW) return mf_get_N(F);
   11704         175 :   gk = MF_get_gk(mf);
   11705         175 :   if (isint1(gk))
   11706             :   {
   11707           7 :     N = mf_get_N(F);
   11708           7 :     if (!wt1newlevel(N))
   11709             :     {
   11710           0 :       long s = space_is_cusp(space)? mf_CUSP: mf_FULL;
   11711           0 :       N = ugcd(N, wt1mulcond(F,-3,s));
   11712           0 :       if (!wt1newlevel(N)) N = ugcd(N, wt1mulcond(F,-4,s));
   11713             :     }
   11714           7 :     return gc_long(av,N);
   11715             :   }
   11716         168 :   if (typ(gk) != t_INT)
   11717             :   {
   11718          42 :     F = mfmultheta(F);
   11719          42 :     mf = obj_checkbuild(mf, MF_MF2INIT, &mf2init); /* mf_FULL */
   11720             :   }
   11721         168 :   N = 1;
   11722         168 :   if (space_is_cusp(space))
   11723             :   {
   11724           7 :     F = mftobasis_i(mf, F);
   11725           7 :     if (typ(gk) != t_INT) F = vecslice(F, lg(MF_get_E(mf)), lg(F) - 1);
   11726             :   }
   11727             :   else
   11728             :   {
   11729         161 :     GEN EF = mftobasisES(mf, F), vE = gel(EF,1), B = MF_get_E(mf);
   11730         161 :     long i, l = lg(B);
   11731        1267 :     for (i = 1; i < l; i++)
   11732        1106 :       if (!gequal0(gel(vE,i))) N = ulcm(N, mf_get_N(gel(B, i)));
   11733         161 :     F = gel(EF,2);
   11734             :   }
   11735         168 :   (void)mftonew_i(mf, F, &M); /* M = conductor of cuspidal part */
   11736         168 :   return gc_long(av, ulcm(M, N));
   11737             : }
   11738             : 
   11739             : static GEN
   11740        1386 : fs_get_MF(GEN fs) { return gel(fs,1); }
   11741             : static GEN
   11742         805 : fs_get_vES(GEN fs) { return gel(fs,2); }
   11743             : static GEN
   11744        1533 : fs_get_pols(GEN fs) { return gel(fs,3); }
   11745             : static GEN
   11746        2086 : fs_get_cosets(GEN fs) { return gel(fs,4); }
   11747             : static long
   11748         602 : fs_get_bitprec(GEN fs) { return itou(gel(fs,5)); }
   11749             : static GEN
   11750        1190 : fs_get_vE(GEN fs) { return gel(fs,6); }
   11751             : static GEN
   11752          56 : fs_get_EF(GEN fs) { return gel(fs,7); }
   11753             : static GEN
   11754        1792 : fs_get_expan(GEN fs) { return gel(fs,8); }
   11755             : static GEN
   11756          14 : fs_set_expan(GEN fs, GEN vanall)
   11757          14 : { GEN f = shallowcopy(fs); gel(f,8) = vanall; return f; }
   11758             : static int
   11759          42 : mfs_checkmf(GEN fs, GEN mf)
   11760          42 : { GEN mfF = fs_get_MF(fs); return gequal(gel(mfF,1), gel(mf,1)); }
   11761             : static long
   11762         756 : checkfs_i(GEN v)
   11763         756 : { return typ(v) == t_VEC && lg(v) == 9 && checkMF_i(fs_get_MF(v))
   11764         539 :     && typ(fs_get_vES(v)) == t_VEC
   11765         539 :     && typ(fs_get_pols(v)) == t_VEC
   11766         539 :     && typ(fs_get_cosets(v)) == t_VEC
   11767         539 :     && typ(fs_get_vE(v)) == t_VEC
   11768         539 :     && lg(fs_get_pols(v)) == lg(fs_get_cosets(v))
   11769         539 :     && typ(fs_get_expan(v)) == t_VEC
   11770         539 :     && lg(fs_get_expan(v)) == 3
   11771         539 :     && lg(gel(fs_get_expan(v), 1)) == lg(fs_get_cosets(v))
   11772        1512 :     && typ(gel(v,5)) == t_INT; }
   11773             : GEN
   11774       18697 : checkMF_i(GEN mf)
   11775             : {
   11776       18697 :   long l = lg(mf);
   11777             :   GEN v;
   11778       18697 :   if (typ(mf) != t_VEC) return NULL;
   11779       18669 :   if (l == 9) return checkMF_i(fs_get_MF(mf));
   11780       18669 :   if (l != 7) return NULL;
   11781        7483 :   v = gel(mf,1);
   11782        7483 :   if (typ(v) != t_VEC || lg(v) != 5) return NULL;
   11783        7483 :   return (typ(gel(v,1)) == t_INT
   11784        7483 :          && typ(gmul2n(gel(v,2), 1)) == t_INT
   11785        7483 :          && typ(gel(v,3)) == t_VEC
   11786       14966 :          && typ(gel(v,4)) == t_INT)? mf: NULL; }
   11787             : GEN
   11788        3864 : checkMF(GEN T)
   11789             : {
   11790        3864 :   GEN mf = checkMF_i(T);
   11791        3864 :   if (!mf) pari_err_TYPE("checkMF [please use mfinit]", T);
   11792        3864 :   return mf;
   11793             : }
   11794             : 
   11795             : /* c,d >= 0; c * Nc = N, find coset whose image in P1(Z/NZ) ~ (c, d + k(N/c)) */
   11796             : static GEN
   11797       11914 : coset_complete(long c, long d, long Nc)
   11798             : {
   11799             :   long a, b;
   11800       13258 :   while (ugcd(c, d) > 1) d += Nc;
   11801       11914 :   (void)cbezout(c, d, &b, &a);
   11802       11914 :   return mkmat22s(a, -b, c, d);
   11803             : }
   11804             : /* right cosets of $\G_0(N)$: $\G=\bigsqcup_j \G_0(N)\ga_j$. */
   11805             : /* We choose them with c\mid N and d mod N/c, not the reverse */
   11806             : GEN
   11807         154 : mfcosets(GEN gN)
   11808             : {
   11809         154 :   pari_sp av = avma;
   11810             :   GEN V, D, mf;
   11811         154 :   long l, i, ct, N = 0;
   11812         154 :   if (typ(gN) == t_INT) N = itos(gN);
   11813          14 :   else if ((mf = checkMF_i(gN))) N = MF_get_N(mf);
   11814           7 :   else pari_err_TYPE("mfcosets", gN);
   11815         147 :   if (N <= 0) pari_err_DOMAIN("mfcosets", "N", "<=", gen_0, stoi(N));
   11816         147 :   V = cgetg(mypsiu(N) + 1, t_VEC);
   11817         147 :   D = mydivisorsu(N); l = lg(D);
   11818         560 :   for (i = ct = 1; i < l; i++)
   11819             :   {
   11820         413 :     long d, c = D[i], Nc = D[l-i], e = ugcd(Nc, c);
   11821        3304 :     for (d = 0; d < Nc; d++)
   11822        2891 :       if (ugcd(d,e) == 1) gel(V, ct++) = coset_complete(c, d, Nc);
   11823             :   }
   11824         147 :   return gerepilecopy(av, V);
   11825             : }
   11826             : static int
   11827       35434 : cmp_coset(void *E, GEN A, GEN B)
   11828             : {
   11829       35434 :   ulong N = (ulong)E, Nc, c = itou(gcoeff(A,2,1));
   11830       35434 :   int r = cmpuu(c, itou(gcoeff(B,2,1)));
   11831       35434 :   if (r) return r;
   11832       30625 :   Nc = N / c;
   11833       30625 :   return cmpuu(umodiu(gcoeff(A,2,2), Nc), umodiu(gcoeff(B,2,2), Nc));
   11834             : }
   11835             : /* M in SL_2(Z) */
   11836             : static long
   11837        9163 : mftocoset_i(ulong N, GEN M, GEN cosets)
   11838             : {
   11839        9163 :   pari_sp av = avma;
   11840        9163 :   long A = itos(gcoeff(M,1,1)), c, u, v, Nc, i;
   11841        9163 :   long C = itos(gcoeff(M,2,1)), D = itos(gcoeff(M,2,2));
   11842             :   GEN ga;
   11843        9163 :   c = cbezout(N*A, C, &u, &v); Nc = N/c;
   11844        9163 :   ga = coset_complete(c, umodsu(v*D, Nc), Nc);
   11845        9163 :   i = gen_search(cosets, ga, (void*)N, &cmp_coset);
   11846        9163 :   if (i < 0) pari_err_BUG("mftocoset [no coset found]");
   11847        9163 :   return gc_long(av,i);
   11848             : }
   11849             : /* (U * V^(-1))[2,2] mod N, assuming V in SL2(Z) */
   11850             : static long
   11851        9149 : SL2_div_D(ulong N, GEN U, GEN V)
   11852             : {
   11853        9149 :   long c = umodiu(gcoeff(U,2,1), N), d = umodiu(gcoeff(U,2,2), N);
   11854        9149 :   long a2 = umodiu(gcoeff(V,1,1), N), b2 = umodiu(gcoeff(V,1,2), N);
   11855        9149 :   return (a2*d - b2*c) % (long)N;
   11856             : }
   11857             : static long
   11858        9149 : mftocoset_iD(ulong N, GEN M, GEN cosets, long *D)
   11859             : {
   11860        9149 :   long i = mftocoset_i(N, M, cosets);
   11861        9149 :   *D = SL2_div_D(N, M, gel(cosets,i)); return i;
   11862             : }
   11863             : GEN
   11864           7 : mftocoset(ulong N, GEN M, GEN cosets)
   11865             : {
   11866             :   long i;
   11867           7 :   if (!check_SL2Z(M)) pari_err_TYPE("mftocoset",M);
   11868           7 :   i = mftocoset_i(N, M, cosets);
   11869           7 :   retmkvec2(gdiv(M,gel(cosets,i)), utoipos(i));
   11870             : }
   11871             : 
   11872             : static long
   11873        2541 : getnlim2(long N, long w1, long w2, long nlim, long k, long bitprec)
   11874             : {
   11875        2541 :   if (w2 == N) return nlim;
   11876         483 :   return mfperiod_prelim_double(1./sqrt((double)w1*w2), k, bitprec + 32);
   11877             : }
   11878             : 
   11879             : /* g * S, g 2x2 */
   11880             : static GEN
   11881        1316 : ZM_mulS(GEN g)
   11882        1316 : { return mkmat2(gel(g,2), ZC_neg(gel(g,1))); }
   11883             : /* g * T, g 2x2 */
   11884             : static GEN
   11885        4634 : ZM_mulT(GEN g)
   11886        4634 : { return mkmat2(gel(g,1), ZC_add(gel(g,2), gel(g,1))); }
   11887             : /* g * T^(-1), g 2x2 */
   11888             : static GEN
   11889        2352 : ZM_mulTi(GEN g)
   11890        2352 : { return mkmat2(gel(g,1), ZC_sub(gel(g,2), gel(g,1))); }
   11891             : 
   11892             : /* Compute all slashexpansions for all cosets */
   11893             : static GEN
   11894         147 : mfgaexpansionall(GEN mf, GEN FE, GEN cosets, double height, long prec)
   11895             : {
   11896         147 :   GEN CHI = MF_get_CHI(mf), vres, vresaw;
   11897         147 :   long lco, j, k = MF_get_k(mf), N = MF_get_N(mf), bitprec = prec2nbits(prec);
   11898             : 
   11899         147 :   lco = lg(cosets);
   11900         147 :   vres = const_vec(lco-1, NULL);
   11901         147 :   vresaw = cgetg(lco, t_VEC);
   11902        2856 :   for (j = 1; j < lco; j++) if (!gel(vres,j))
   11903             :   {
   11904         427 :     GEN ga = gel(cosets, j), van, aw, al, z, gai;
   11905         427 :     long w1 = mfZC_width(N, gel(ga,1));
   11906         427 :     long w2 = mfZC_width(N, gel(ga,2));
   11907             :     long nlim, nlim2, daw, da, na, i;
   11908         427 :     double sqNinvdbl = height ? height/w1 : 1./sqrt((double)w1*N);
   11909         427 :     nlim = mfperiod_prelim_double(sqNinvdbl, k, bitprec + 32);
   11910         427 :     van = mfslashexpansion(mf, FE, ga, nlim, 0, &aw, prec + EXTRAPREC64);
   11911         427 :     van = vanembed(gel(FE, 1), van, prec + EXTRAPREC64);
   11912         427 :     al = gel(aw, 1);
   11913         427 :     nlim2 = height? nlim: getnlim2(N, w1, w2, nlim, k, bitprec);
   11914         427 :     gel(vres, j) = vecslice(van, 1, nlim2 + 1);
   11915         427 :     gel(vresaw, j) = aw;
   11916         427 :     Qtoss(al, &na, &da); daw = da*w1;
   11917         427 :     z = rootsof1powinit(1, daw, prec + EXTRAPREC64);
   11918         427 :     gai = ga;
   11919        2709 :     for (i = 1; i < w1; i++)
   11920             :     {
   11921             :       GEN V, coe;
   11922        2282 :       long Di, n, ind, w2, s = ((i*na) % da) * w1, t = i*da;
   11923        2282 :       gai = ZM_mulT(gai);
   11924        2282 :       ind = mftocoset_iD(N, gai, cosets, &Di);
   11925        2282 :       w2 = mfZC_width(N, gel(gel(cosets,ind), 2));
   11926        2282 :       nlim2 = height? nlim: getnlim2(N, w1, w2, nlim, k, bitprec);
   11927        2282 :       gel(vresaw, ind) = aw;
   11928        2282 :       V = cgetg(nlim2 + 2, t_VEC);
   11929      909034 :       for (n = 0; n <= nlim2; n++, s = Fl_add(s, t, daw))
   11930      906752 :         gel(V, n+1) = gmul(gel(van, n+1), rootsof1pow(z, s));
   11931        2282 :       coe = mfcharcxeval(CHI, Di, prec + EXTRAPREC64);
   11932        2282 :       if (!gequal1(coe)) V = RgV_Rg_mul(V, conj_i(coe));
   11933        2282 :       gel(vres, ind) = V;
   11934             :     }
   11935             :   }
   11936         147 :   return mkvec2(vres, vresaw);
   11937             : }
   11938             : 
   11939             : /* Compute all period pols of F|_k\ga_j, vF = mftobasis(F_S) */
   11940             : static GEN
   11941         154 : mfperiodpols_i(GEN mf, GEN FE, GEN cosets, GEN *pvan, long bit)
   11942             : {
   11943         154 :   long N, i, prec = nbits2prec(bit), k = MF_get_k(mf);
   11944         154 :   GEN vP, P, CHI, intall = gen_0;
   11945             : 
   11946         154 :   *pvan = gen_0;
   11947         154 :   if (k == 0 && gequal0(gel(FE,2)))
   11948           0 :     return cosets? const_vec(lg(cosets)-1, pol_0(0)): pol_0(0);
   11949         154 :   N = MF_get_N(mf);
   11950         154 :   CHI = MF_get_CHI(mf);
   11951         154 :   P = get_P(k, fetch_var(), prec);
   11952         154 :   if (!cosets)
   11953             :   { /* ga = id */
   11954          21 :     long nlim, PREC = prec + EXTRAPREC64;
   11955          21 :     GEN F = gel(FE,1), sqNinv = invr(sqrtr_abs(utor(N, PREC))); /* A/w */
   11956             :     GEN AR, v, van, T1, T2;
   11957             : 
   11958          21 :     nlim = mfperiod_prelim(sqNinv, k, bit + 32);
   11959             :     /* F|id: al = 0, w = 1 */
   11960          21 :     v = mfcoefs_i(F, nlim, 1);
   11961          21 :     van = vanembed(F, v, PREC);
   11962          21 :     AR = mkcomplex(gen_0, sqNinv);
   11963          21 :     T1 = intAoo(van, nlim, gen_0,1, P, AR, k, prec);
   11964          21 :     if (N == 1) T2 = T1;
   11965             :     else
   11966             :     { /* F|S: al = 0, w = N */
   11967           7 :       v = mfgaexpansion(mf, FE, mkS(), nlim, PREC);
   11968           7 :       van = vanembed(F, gel(v,3), PREC);
   11969           7 :       AR = mkcomplex(gen_0, mulur(N,sqNinv));
   11970           7 :       T2 = intAoo(van, nlim, gen_0,N, P, AR, k, prec);
   11971             :     }
   11972          21 :     T1 = gsub(T1, act_S(T2, k));
   11973          21 :     T1 = normalizeapprox_i(T1, bit-20);
   11974          21 :     vP = gprec_wtrunc(T1, prec);
   11975             :   }
   11976             :   else
   11977             :   {
   11978         133 :     long lco = lg(cosets);
   11979         133 :     GEN vanall = mfgaexpansionall(mf, FE, cosets, 0, prec);
   11980         133 :     *pvan = vanall;
   11981         133 :     intall = intAoowithvanall(mf, vanall, P, cosets, bit);
   11982         133 :     vP = const_vec(lco-1, NULL);
   11983        2674 :     for (i = 1; i < lco; i++)
   11984             :     {
   11985        2541 :       GEN P, P1, P2, c, ga = gel(cosets, i);
   11986             :       long iS, DS;
   11987        2618 :       if (gel(vP,i)) continue;
   11988        1309 :       P1 = gel(intall, i);
   11989        1309 :       iS = mftocoset_iD(N, ZM_mulS(ga), cosets, &DS);
   11990        1309 :       c = mfcharcxeval(CHI, DS, prec + EXTRAPREC64);
   11991        1309 :       P2 = gel(intall, iS);
   11992             : 
   11993        1309 :       P = act_S(isint1(c)? P2: gmul(c, P2), k);
   11994        1309 :       P = normalizeapprox_i(gsub(P1, P), bit-20);
   11995        1309 :       gel(vP,i) = gprec_wtrunc(P, prec);
   11996        1309 :       if (iS == i) continue;
   11997             : 
   11998        1232 :       P = act_S(isint1(c)? P1: gmul(conj_i(c), P1), k);
   11999        1232 :       if (!odd(k)) P = gneg(P);
   12000        1232 :       P = normalizeapprox_i(gadd(P, P2), bit-20);
   12001        1232 :       gel(vP,iS) = gprec_wtrunc(P, prec);
   12002             :     }
   12003             :   }
   12004         154 :   delete_var(); return vP;
   12005             : }
   12006             : 
   12007             : /* when cosets = NULL, return a "fake" symbol containing only fs(oo->0) */
   12008             : static GEN
   12009         154 : mfsymbol_i(GEN mf, GEN F, GEN cosets, long bit)
   12010             : {
   12011         154 :   GEN FE, van, vP, vE, Mvecj, vES = mftobasisES(mf,F);
   12012         154 :   long precnew, prec = nbits2prec(bit), k = MF_get_k(mf);
   12013         154 :   vE = mfgetembed(F, prec);
   12014         154 :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
   12015         154 :   if (lg(Mvecj) >= 5) precnew = prec;
   12016             :   else
   12017             :   {
   12018          14 :     long N = MF_get_N(mf), n = mfperiod_prelim_double(1/(double)N, k, bit + 32);
   12019          14 :     precnew = prec + inveis_extraprec(N, mkS(), Mvecj, n);
   12020             :   }
   12021         154 :   FE = mkcol2(F, mf_eisendec(mf,F,precnew));
   12022         154 :   vP = mfperiodpols_i(mf, FE, cosets, &van, bit);
   12023         154 :   return mkvecn(8, mf, vES, vP, cosets, utoi(bit), vE, FE, van);
   12024             : }
   12025             : 
   12026             : static GEN
   12027          56 : fs2_get_cusps(GEN f) { return gel(f,3); }
   12028             : static GEN
   12029          56 : fs2_get_MF(GEN f) { return gel(f,1); }
   12030             : static GEN
   12031          56 : fs2_get_W(GEN f) { return gel(f,2); }
   12032             : static GEN
   12033          56 : fs2_get_F(GEN f) { return gel(f,4); }
   12034             : static long
   12035           0 : fs2_get_bitprec(GEN f) { return itou(gel(f,5)); }
   12036             : static GEN
   12037          56 : fs2_get_al0(GEN f) { return gel(f,6); }
   12038             : static GEN
   12039          21 : fs2_get_den(GEN f) { return gel(f,7); }
   12040             : static int
   12041         196 : checkfs2_i(GEN f)
   12042             : {
   12043             :   GEN W, C, F, al0;
   12044             :   long l;
   12045         196 :   if (typ(f) != t_VEC || lg(f) != 8 || typ(gel(f,5)) != t_INT) return 0;
   12046          35 :   C = fs2_get_cusps(f); l = lg(C);
   12047          35 :   W = fs2_get_W(f);
   12048          35 :   F = fs2_get_F(f);
   12049          35 :   al0 = fs2_get_al0(f);
   12050          35 :   return checkMF_i(fs2_get_MF(f))
   12051          35 :       && typ(W) == t_VEC && typ(F) == t_VEC && typ(al0) == t_VECSMALL
   12052          70 :       && lg(W) == l && lg(F) == l && lg(al0) == l;
   12053             : }
   12054             : static GEN fs2_init(GEN mf, GEN F, long bit);
   12055             : GEN
   12056         161 : mfsymbol(GEN mf, GEN F, long bit)
   12057             : {
   12058         161 :   pari_sp av = avma;
   12059         161 :   GEN cosets = NULL;
   12060         161 :   if (!F)
   12061             :   {
   12062          21 :     F = mf;
   12063          21 :     if (!checkmf_i(F)) pari_err_TYPE("mfsymbol", F);
   12064          21 :     mf = mfinit_i(F, mf_FULL);
   12065             :   }
   12066         140 :   else if (!checkmf_i(F)) pari_err_TYPE("mfsymbol", F);
   12067         161 :   if (checkfs2_i(mf)) return fs2_init(mf, F, bit);
   12068         161 :   if (checkfs_i(mf))
   12069             :   {
   12070           0 :     cosets = fs_get_cosets(mf);
   12071           0 :     mf = fs_get_MF(mf);
   12072             :   }
   12073         161 :   else if (checkMF_i(mf))
   12074             :   {
   12075         161 :     GEN gk = MF_get_gk(mf);
   12076         161 :     if (typ(gk) != t_INT || equali1(gk)) return fs2_init(mf, F, bit);
   12077         140 :     if (signe(gk) <= 0) pari_err_TYPE("mfsymbol [k <= 0]", mf);
   12078         133 :     cosets = mfcosets(MF_get_gN(mf));
   12079             :   }
   12080           0 :   else pari_err_TYPE("mfsymbol",mf);
   12081         133 :   return gerepilecopy(av, mfsymbol_i(mf, F, cosets, bit));
   12082             : }
   12083             : 
   12084             : static GEN
   12085          14 : RgX_by_parity(GEN P, long odd)
   12086             : {
   12087          14 :   long i, l = lg(P);
   12088             :   GEN Q;
   12089          14 :   if (l < 4) return odd ? pol_x(0): P;
   12090          14 :   Q = cgetg(l, t_POL); Q[1] = P[1];
   12091          91 :   for (i = odd? 2: 3; i < l; i += 2) gel(Q,i) = gen_0;
   12092          91 :   for (i = odd? 3: 2; i < l; i += 2) gel(Q,i) = gel(P,i);
   12093          14 :   return normalizepol_lg(Q, l);
   12094             : }
   12095             : /* flag 0: period polynomial of F, >0 or <0 with corresponding parity */
   12096             : GEN
   12097          35 : mfperiodpol(GEN mf0, GEN F, long flag, long bit)
   12098             : {
   12099          35 :   pari_sp av = avma;
   12100          35 :   GEN pol, mf = checkMF_i(mf0);
   12101          35 :   if (!mf) pari_err_TYPE("mfperiodpol",mf0);
   12102          35 :   if (checkfs_i(F))
   12103             :   {
   12104          14 :     GEN mfpols = fs_get_pols(F);
   12105          14 :     if (!mfs_checkmf(F, mf)) pari_err_TYPE("mfperiodpol [different mf]",F);
   12106          14 :     pol = veclast(mfpols); /* trivial coset is last */
   12107             :   }
   12108             :   else
   12109             :   {
   12110          21 :     GEN gk = MF_get_gk(mf);
   12111          21 :     if (typ(gk) != t_INT) pari_err_TYPE("mfperiodpol [half-integral k]", mf);
   12112          21 :     if (equali1(gk)) pari_err_TYPE("mfperiodpol [k = 1]", mf);
   12113          21 :     F = mfsymbol_i(mf, F, NULL, bit);
   12114          21 :     pol = fs_get_pols(F);
   12115             :   }
   12116          35 :   if (flag) pol = RgX_by_parity(pol, flag < 0);
   12117          35 :   return gerepilecopy(av, RgX_embedall(pol, fs_get_vE(F)));
   12118             : }
   12119             : 
   12120             : static int
   12121          35 : mfs_iscusp(GEN mfs) { return gequal0(gmael(mfs,2,1)); }
   12122             : /* given cusps s1 and s2 (rationals or oo)
   12123             :  * compute $\int_{s1}^{s2}(X-\tau)^{k-2}F|_k\ga_j(\tau)\,d\tau$ */
   12124             : /* If flag = 1, do not give an error message if divergent, but
   12125             :    give the rational function as result. */
   12126             : 
   12127             : static GEN
   12128         119 : col2cusp(GEN v)
   12129             : {
   12130             :   GEN A, C;
   12131         119 :   if (lg(v) != 3 || !RgV_is_ZV(v)) pari_err_TYPE("col2cusp",v);
   12132         119 :   A = gel(v,1);
   12133         119 :   C = gel(v,2);
   12134         119 :   if (gequal0(C))
   12135             :   {
   12136           0 :     if (gequal0(A)) pari_err_TYPE("mfsymboleval", mkvec2(A, C));
   12137           0 :     return mkoo();
   12138             :   }
   12139         119 :   return gdiv(A, C);
   12140             : }
   12141             : /* g.oo */
   12142             : static GEN
   12143         105 : mat2cusp(GEN g) { return col2cusp(gel(g,1)); }
   12144             : 
   12145             : static GEN
   12146           7 : pathmattovec(GEN path)
   12147           7 : { return mkvec2(col2cusp(gel(path,1)), col2cusp(gel(path,2))); }
   12148             : 
   12149             : static void
   12150         532 : get_mf_F(GEN fs, GEN *mf, GEN *F)
   12151             : {
   12152         532 :   if (lg(fs) == 3) { *mf = gel(fs,1); *F = gel(fs,2); }
   12153         532 :   else { *mf = fs_get_MF(fs); *F = NULL; }
   12154         532 : }
   12155             : static GEN
   12156         175 : mfgetvan(GEN fs, GEN ga, GEN *pal, long nlim, long prec)
   12157             : {
   12158             :   GEN van, mf, F, W;
   12159             :   long PREC;
   12160         175 :   get_mf_F(fs, &mf, &F);
   12161         175 :   if (!F)
   12162             :   {
   12163         175 :     GEN vanall = fs_get_expan(fs), cosets = fs_get_cosets(fs);
   12164         175 :     long D, jga = mftocoset_iD(MF_get_N(mf), ga, cosets, &D);
   12165         175 :     van = gmael(vanall, 1, jga);
   12166         175 :     W   = gmael(vanall, 2, jga);
   12167         175 :     if (lg(van) >= nlim + 2)
   12168             :     {
   12169         168 :       GEN z = mfcharcxeval(MF_get_CHI(mf), D, prec);
   12170         168 :       if (!gequal1(z)) van = RgV_Rg_mul(van, z);
   12171         168 :       *pal = gel(W,1); return van;
   12172             :     }
   12173           7 :     F = gel(fs_get_EF(fs), 1);
   12174             :   }
   12175           7 :   PREC = prec + EXTRAPREC64;
   12176           7 :   van = mfslashexpansion(mf, F, ga, nlim, 0, &W, PREC);
   12177           7 :   van = vanembed(F, van, PREC);
   12178           7 :   *pal = gel(W,1); return van;
   12179             : }
   12180             : /* Computation of int_A^oo (f | ga)(t)(X-t)^{k-2} dt, assuming convergence;
   12181             :  * fs is either a symbol or a triple [mf,F,bitprec]. A != oo and im(A) > 0 */
   12182             : static GEN
   12183          77 : intAoo0(GEN fs, GEN A, GEN ga, GEN P, long bit)
   12184             : {
   12185          77 :   long nlim, N, k, w, prec = nbits2prec(bit);
   12186             :   GEN van, mf, F, al;
   12187          77 :   get_mf_F(fs, &mf,&F); N = MF_get_N(mf); k = MF_get_k(mf);
   12188          77 :   w = mfZC_width(N, gel(ga,1));
   12189          77 :   nlim = mfperiod_prelim(gdivgu(imag_i(A), w), k, bit + 32);
   12190          77 :   van = mfgetvan(fs, ga, &al, nlim, prec);
   12191          77 :   return intAoo(van, nlim, al,w, P, A, k, prec);
   12192             : }
   12193             : 
   12194             : /* fs symbol, naive summation, A != oo, im(A) > 0 and B = oo or im(B) > 0 */
   12195             : static GEN
   12196          98 : mfsymboleval_direct(GEN fs, GEN path, GEN ga, GEN P)
   12197             : {
   12198          98 :   GEN A, B, van, S, al, mf = fs_get_MF(fs);
   12199          98 :   long w, nlimA, nlimB = 0, N = MF_get_N(mf), k = MF_get_k(mf);
   12200          98 :   long bit = fs_get_bitprec(fs), prec = nbits2prec(bit);
   12201             : 
   12202          98 :   A = gel(path, 1);
   12203          98 :   B = gel(path, 2); if (typ(B) == t_INFINITY) B = NULL;
   12204          98 :   w = mfZC_width(N, gel(ga,1));
   12205          98 :   nlimA = mfperiod_prelim(gdivgu(imag_i(A),w), k, bit + 32);
   12206          98 :   if (B) nlimB = mfperiod_prelim(gdivgu(imag_i(B),w), k, bit + 32);
   12207          98 :   van = mfgetvan(fs, ga, &al, maxss(nlimA,nlimB), prec);
   12208          98 :   S = intAoo(van, nlimA, al,w, P, A, k, prec);
   12209          98 :   if (B) S = gsub(S, intAoo(van, nlimB, al,w, P, B, k, prec));
   12210          98 :   return RgX_embedall(S, fs_get_vE(fs));
   12211             : }
   12212             : 
   12213             : /* Computation of int_A^oo (f | ga)(t)(X-t)^{k-2} dt, assuming convergence;
   12214             :  * fs is either a symbol or a pair [mf,F]. */
   12215             : static GEN
   12216          77 : mfsymbolevalpartial(GEN fs, GEN A, GEN ga, long bit)
   12217             : {
   12218             :   GEN Y, F, S, P, mf;
   12219          77 :   long N, k, w, prec = nbits2prec(bit);
   12220             : 
   12221          77 :   get_mf_F(fs, &mf, &F);
   12222          77 :   N = MF_get_N(mf); w = mfZC_width(N, gel(ga,1));
   12223          77 :   k = MF_get_k(mf);
   12224          77 :   Y = gdivgu(imag_i(A), w);
   12225          77 :   P = get_P(k, fetch_var(), prec);
   12226          77 :   if (lg(fs) != 3 && gtodouble(Y)*(2*N) < 1)
   12227          21 :   { /* true symbol + low imaginary part: use GL_2 action to improve */
   12228          21 :     GEN U, ga2, czd, A2 = cxredga0N(N, A, &U, &czd, 1);
   12229          21 :     GEN vE = fs_get_vE(fs);
   12230          21 :     ga2 = ZM_mul(ga, ZM_inv(U, NULL));
   12231          21 :     S = RgX_embedall(intAoo0(fs, A2, ga2, P, bit), vE);
   12232          21 :     S = gsub(S, mfsymboleval(fs, mkvec2(mat2cusp(U), mkoo()), ga2, bit));
   12233          21 :     S = typ(S) == t_VEC? vecact_GL2(S, U, k): act_GL2(S, U, k);
   12234             :   }
   12235             :   else
   12236             :   {
   12237          56 :     S = intAoo0(fs, A, ga, P, bit);
   12238          56 :     S = RgX_embedall(S, F? mfgetembed(F,prec): fs_get_vE(fs));
   12239             :   }
   12240          77 :   delete_var(); return normalizeapprox_i(S, bit-20);
   12241             : }
   12242             : 
   12243             : static GEN
   12244          42 : actal(GEN x, GEN vabd)
   12245             : {
   12246          42 :   if (typ(x) == t_INFINITY) return x;
   12247          35 :   return gdiv(gadd(gmul(gel(vabd,1), x), gel(vabd,2)), gel(vabd,3));
   12248             : }
   12249             : 
   12250             : static GEN
   12251          14 : unact(GEN z, GEN vabd, long k, long prec)
   12252             : {
   12253          14 :   GEN res = gsubst(z, 0, actal(pol_x(0), vabd));
   12254          14 :   GEN CO = gpow(gdiv(gel(vabd,3), gel(vabd,1)), sstoQ(k-2, 2), prec);
   12255          14 :   return gmul(CO, res);
   12256             : }
   12257             : 
   12258             : GEN
   12259         210 : mfsymboleval(GEN fs, GEN path, GEN ga, long bitprec)
   12260             : {
   12261         210 :   pari_sp av = avma;
   12262         210 :   GEN tau, V, LM, S, CHI, mfpols, cosets, al, be, mf, F, vabd = NULL;
   12263             :   long D, B, m, u, v, a, b, c, d, j, k, N, prec, tsc1, tsc2;
   12264             : 
   12265         210 :   if (checkfs_i(fs))
   12266             :   {
   12267         203 :     get_mf_F(fs, &mf, &F);
   12268         203 :     bitprec = minss(bitprec, fs_get_bitprec(fs));
   12269             :   }
   12270             :   else
   12271             :   {
   12272           7 :     if (checkfs2_i(fs)) pari_err_TYPE("mfsymboleval [need integral k > 1]",fs);
   12273           0 :     if (typ(fs) != t_VEC || lg(fs) != 3) pari_err_TYPE("mfsymboleval",fs);
   12274           0 :     get_mf_F(fs, &mf, &F);
   12275           0 :     mf = checkMF_i(mf);
   12276           0 :     if (!mf ||!checkmf_i(F)) pari_err_TYPE("mfsymboleval",fs);
   12277             :   }
   12278         203 :   if (lg(path) != 3) pari_err_TYPE("mfsymboleval",path);
   12279         203 :   if (typ(path) == t_MAT) path = pathmattovec(path);
   12280         203 :   if (typ(path) != t_VEC) pari_err_TYPE("mfsymboleval",path);
   12281         203 :   al = gel(path,1);
   12282         203 :   be = gel(path,2);
   12283         203 :   ga = ga? GL2toSL2(ga, &vabd): matid(2);
   12284         203 :   if (vabd)
   12285             :   {
   12286          14 :     al = actal(al, vabd);
   12287          14 :     be = actal(be, vabd); path = mkvec2(al, be);
   12288             :   }
   12289         203 :   tsc1 = cusp_AC(al, &a, &c);
   12290         203 :   tsc2 = cusp_AC(be, &b, &d);
   12291         203 :   prec = nbits2prec(bitprec);
   12292         203 :   k = MF_get_k(mf);
   12293         203 :   if (!tsc1)
   12294             :   {
   12295          42 :     GEN z2, z = mfsymbolevalpartial(fs, al, ga, bitprec);
   12296          42 :     if (tsc2)
   12297          28 :       z2 = d? mfsymboleval(fs, mkvec2(be, mkoo()), ga, bitprec): gen_0;
   12298             :     else
   12299          14 :       z2 = mfsymbolevalpartial(fs, be, ga, bitprec);
   12300          42 :     z = gsub(z, z2);
   12301          42 :     if (vabd) z = unact(z, vabd, k, prec);
   12302          42 :     return gerepileupto(av, normalizeapprox_i(z, bitprec-20));
   12303             :   }
   12304         161 :   else if (!tsc2)
   12305             :   {
   12306          21 :     GEN z = mfsymbolevalpartial(fs, be, ga, bitprec);
   12307          21 :     if (c) z = gsub(mfsymboleval(fs, mkvec2(al, mkoo()), ga, bitprec), z);
   12308           7 :     else z = gneg(z);
   12309          21 :     if (vabd) z = unact(z, vabd, k, prec);
   12310          21 :     return gerepileupto(av, normalizeapprox_i(z, bitprec-20));
   12311             :   }
   12312         140 :   if (F) pari_err_TYPE("mfsymboleval", fs);
   12313         140 :   D = a*d-b*c;
   12314         140 :   if (!D) { set_avma(av); return RgX_embedall(gen_0, fs_get_vE(fs)); }
   12315         126 :   mfpols = fs_get_pols(fs);
   12316         126 :   cosets = fs_get_cosets(fs);
   12317         126 :   CHI = MF_get_CHI(mf); N = MF_get_N(mf);
   12318         126 :   cbezout(a, c, &u, &v); B = u*b + v*d; tau = mkmat22s(a, -v, c, u);
   12319         126 :   V = gcf(sstoQ(B, D));
   12320         126 :   LM = shallowconcat(mkcol2(gen_1, gen_0), contfracpnqn(V, lg(V)));
   12321         126 :   S = gen_0; m = lg(LM) - 2;
   12322         364 :   for (j = 0; j < m; j++)
   12323             :   {
   12324             :     GEN M, P;
   12325             :     long D, iN;
   12326         238 :     M = mkmat2(gel(LM, j+2), gel(LM, j+1));
   12327         238 :     if (!odd(j)) gel(M,1) = ZC_neg(gel(M,1));
   12328         238 :     M = ZM_mul(tau, M);
   12329         238 :     iN = mftocoset_iD(N, ZM_mul(ga, M), cosets, &D);
   12330         238 :     P = gmul(gel(mfpols,iN), mfcharcxeval(CHI,D,prec));
   12331         238 :     S = gadd(S, act_GL2(P, ZM_inv(M, NULL), k));
   12332             :   }
   12333         126 :   if (typ(S) == t_RFRAC)
   12334             :   {
   12335             :     GEN R, S1, co;
   12336          21 :     gel(S,2) = primitive_part(gel(S,2), &co);
   12337          21 :     if (co) gel(S,1) = gdiv(gel(S,1), gtofp(co,prec));
   12338          21 :     S1 = poldivrem(gel(S,1), gel(S,2), &R);
   12339          21 :     if (gexpo(R) < -bitprec + 20) S = S1;
   12340             :   }
   12341         126 :   if (vabd) S = unact(S, vabd, k, prec);
   12342         126 :   S = RgX_embedall(S, fs_get_vE(fs));
   12343         126 :   return gerepileupto(av, normalizeapprox_i(S, bitprec-20));
   12344             : }
   12345             : 
   12346             : /* v a scalar or t_POL; set *pw = a if expo(a) > E for some coefficient;
   12347             :  * take the 'a' with largest exponent */
   12348             : static void
   12349        5740 : improve(GEN v, GEN *pw, long *E)
   12350             : {
   12351        5740 :   if (typ(v) != t_POL)
   12352             :   {
   12353        4270 :     long e = gexpo(v);
   12354        4270 :     if (e > *E) { *E = e; *pw = v; }
   12355             :   }
   12356             :   else
   12357             :   {
   12358        1470 :     long j, l = lg(v);
   12359        5740 :     for (j = 2; j < l; j++) improve(gel(v,j), pw, E);
   12360             :   }
   12361        5740 : }
   12362             : static GEN
   12363         518 : polabstorel(GEN rnfeq, GEN x)
   12364             : {
   12365         518 :   if (typ(x) != t_POL) return x;
   12366        3500 :   pari_APPLY_pol_normalized(eltabstorel(rnfeq, gel(x,i)));
   12367             : }
   12368             : static GEN
   12369        1519 : bestapprnfrel(GEN x, GEN polabs, GEN roabs, GEN rnfeq, long prec)
   12370             : {
   12371        1519 :   x = bestapprnf(x, polabs, roabs, prec);
   12372        1519 :   if (rnfeq) x = polabstorel(rnfeq, liftpol_shallow(x));
   12373        1519 :   return x;
   12374             : }
   12375             : /* v vector of polynomials polynomial in C[X] (possibly scalar).
   12376             :  * Set *w = coeff with largest exponent and return T / *w, rationalized */
   12377             : static GEN
   12378          98 : normal(GEN v, GEN polabs, GEN roabs, GEN rnfeq, GEN *w, long prec)
   12379             : {
   12380          98 :   long i, l = lg(v), E = -(long)HIGHEXPOBIT;
   12381             :   GEN dv;
   12382        1568 :   for (i = 1; i < l; i++) improve(gel(v,i), w, &E);
   12383          98 :   v = RgV_Rg_mul(v, ginv(*w));
   12384        1568 :   for (i = 1; i < l; i++)
   12385        1470 :     gel(v,i) = bestapprnfrel(gel(v,i), polabs,roabs,rnfeq,prec);
   12386          98 :   v = Q_primitive_part(v,&dv);
   12387          98 :   if (dv) *w = gmul(*w,dv);
   12388          98 :   return v;
   12389             : }
   12390             : 
   12391             : static GEN mfpetersson_i(GEN FS, GEN GS);
   12392             : 
   12393             : GEN
   12394          42 : mfmanin(GEN FS, long bitprec)
   12395             : {
   12396          42 :   pari_sp av = avma;
   12397             :   GEN mf, M, vp, vm, cosets, CHI, vpp, vmm, f, T, P, vE, polabs, roabs, rnfeq;
   12398             :   GEN pet;
   12399             :   long N, k, lco, i, prec, lvE;
   12400             : 
   12401          42 :   if (!checkfs_i(FS))
   12402             :   {
   12403           7 :     if (checkfs2_i(FS)) pari_err_TYPE("mfmanin [need integral k > 1]",FS);
   12404           0 :     pari_err_TYPE("mfmanin",FS);
   12405             :   }
   12406          35 :   if (!mfs_iscusp(FS)) pari_err_TYPE("mfmanin [noncuspidal]",FS);
   12407          35 :   mf = fs_get_MF(FS);
   12408          35 :   vp = fs_get_pols(FS);
   12409          35 :   cosets = fs_get_cosets(FS);
   12410          35 :   bitprec = fs_get_bitprec(FS);
   12411          35 :   N = MF_get_N(mf); k = MF_get_k(mf); CHI = MF_get_CHI(mf);
   12412          35 :   lco = lg(cosets); vm = cgetg(lco, t_VEC);
   12413          35 :   prec = nbits2prec(bitprec);
   12414         476 :   for (i = 1; i < lco; i++)
   12415             :   {
   12416         441 :     GEN g = gel(cosets, i), c;
   12417         441 :     long A = itos(gcoeff(g,1,1)), B = itos(gcoeff(g,1,2));
   12418         441 :     long C = itos(gcoeff(g,2,1)), D = itos(gcoeff(g,2,2));
   12419         441 :     long Dbar, ibar = mftocoset_iD(N, mkmat22s(-B,-A,D,C), cosets, &Dbar);
   12420             : 
   12421         441 :     c = mfcharcxeval(CHI, Dbar, prec); if (odd(k)) c = gneg(c);
   12422         441 :     T = RgX_Rg_mul(gel(vp,ibar), c);
   12423         441 :     if (typ(T) == t_POL && varn(T) == 0) T = RgX_recip(T);
   12424         441 :     gel(vm,i) = T;
   12425             :   }
   12426          35 :   vpp = gadd(vp,vm);
   12427          35 :   vmm = gsub(vp,vm);
   12428             : 
   12429          35 :   vE = fs_get_vE(FS); lvE = lg(vE);
   12430          35 :   f = gel(fs_get_EF(FS), 1);
   12431          35 :   P = mf_get_field(f); if (degpol(P) == 1) P = NULL;
   12432          35 :   T = mfcharpol(CHI);  if (degpol(T) == 1) T = NULL;
   12433          35 :   if (T && P)
   12434             :   {
   12435           7 :     rnfeq = nf_rnfeqsimple(T, P);
   12436           7 :     polabs = gel(rnfeq,1);
   12437           7 :     roabs = gel(QX_complex_roots(polabs,prec), 1);
   12438             :   }
   12439             :   else
   12440             :   {
   12441          28 :     rnfeq = roabs = NULL;
   12442          28 :     polabs = P? P: T;
   12443             :   }
   12444          35 :   pet = mfpetersson_i(FS, NULL);
   12445          35 :   M = cgetg(lvE, t_VEC);
   12446          84 :   for (i = 1; i < lvE; i++)
   12447             :   {
   12448          49 :     GEN p, m, wp, wm, petdiag, r, E = gel(vE,i);
   12449          49 :     p = normal(RgXV_embed(vpp, E), polabs, roabs, rnfeq, &wp, prec);
   12450          49 :     m = normal(RgXV_embed(vmm, E), polabs, roabs, rnfeq, &wm, prec);
   12451          49 :     petdiag = typ(pet)==t_MAT? gcoeff(pet,i,i): pet;
   12452          49 :     r = gdiv(imag_i(gmul(wp, conj_i(wm))), petdiag);
   12453          49 :     r = bestapprnfrel(r, polabs, roabs, rnfeq, prec);
   12454          49 :     gel(M,i) = mkvec2(mkvec2(p,m), mkvec3(wp,wm,r));
   12455             :   }
   12456          35 :   return gerepilecopy(av, lvE == 2? gel(M,1): M);
   12457             : }
   12458             : 
   12459             : /* flag = 0: full, flag = +1 or -1, odd/even */
   12460             : /* Basis of period polynomials in level 1. */
   12461             : GEN
   12462          49 : mfperiodpolbasis(long k, long flag)
   12463             : {
   12464          49 :   pari_sp av = avma;
   12465          49 :   long i, j, n = k - 2;
   12466             :   GEN M, C, v;
   12467          49 :   if (k <= 4) return cgetg(1,t_VEC);
   12468          35 :   M = cgetg(k, t_MAT);
   12469          35 :   C = matpascal(n);
   12470          35 :   if (!flag)
   12471         392 :     for (j = 0; j <= n; j++)
   12472             :     {
   12473         371 :       gel(M, j+1) = v = cgetg(k, t_COL);
   12474        4767 :       for (i = 0; i <= j; i++) gel(v, i+1) = gcoeff(C, j+1, i+1);
   12475        4396 :       for (; i <= n; i++) gel(v, i+1) = gcoeff(C, n-j+1, i-j+1);
   12476             :     }
   12477             :   else
   12478         168 :     for (j = 0; j <= n; j++)
   12479             :     {
   12480         154 :       gel(M, j+1) = v = cgetg(k, t_COL);
   12481        1848 :       for (i = 0; i <= n; i++)
   12482             :       {
   12483        1694 :         GEN a = i < j ? gcoeff(C, j+1, i+1) : gen_0;
   12484        1694 :         if (i + j >= n)
   12485             :         {
   12486         924 :           GEN b = gcoeff(C, j+1, i+j-n+1);
   12487         924 :           a = flag < 0 ? addii(a,b) : subii(a,b);
   12488             :         }
   12489        1694 :         gel(v, i+1) = a;
   12490             :       }
   12491             :     }
   12492          35 :   return gerepilecopy(av, RgM_to_RgXV(ZM_ker(M), 0));
   12493             : }
   12494             : 
   12495             : static int
   12496         161 : zero_at_cusp(GEN mf, GEN F, GEN c)
   12497             : {
   12498         161 :   GEN v = evalcusp(mf, F, c, LOWDEFAULTPREC);
   12499         161 :   return (gequal0(v) || gexpo(v) <= -62);
   12500             : }
   12501             : /* Compute list E of j such that F|_k g_j vanishes at oo: return [E, s(E)] */
   12502             : static void
   12503           7 : mffvanish(GEN mf, GEN F, GEN G, GEN cosets, GEN *pres, GEN *press)
   12504             : {
   12505           7 :   long j, lc = lg(cosets), N = MF_get_N(mf);
   12506             :   GEN v, vs;
   12507           7 :   *pres = v  = zero_zv(lc-1);
   12508           7 :   *press= vs = zero_zv(lc-1);
   12509          91 :   for (j = 1; j < lc; j++)
   12510             :   {
   12511          84 :     GEN ga = gel(cosets,j), c = mat2cusp(ga);
   12512          84 :     if (zero_at_cusp(mf, F, c))
   12513           7 :       v[j] = vs[ mftocoset_i(N, ZM_mulS(ga), cosets) ] = 1;
   12514          77 :     else if (!zero_at_cusp(mf, G, c))
   12515           0 :       pari_err_IMPL("divergent Petersson product");
   12516             :   }
   12517           7 : }
   12518             : static GEN
   12519         133 : Haberland(GEN PF, GEN PG, GEN vEF, GEN vEG, long k)
   12520             : {
   12521         133 :   GEN S = gen_0, vC = vecbinomial(k-2); /* vC[n+1] = (-1)^n binom(k-2,n) */
   12522         133 :   long n, j, l = lg(PG);
   12523         364 :   for (n = 2; n < k; n+=2) gel(vC,n) = negi(gel(vC,n));
   12524        2569 :   for (j = 1; j < l; j++)
   12525             :   {
   12526        2436 :     GEN PFj = gel(PF,j), PGj = gel(PG,j);
   12527        9954 :     for (n = 0; n <= k-2; n++)
   12528             :     {
   12529        7518 :       GEN a = RgX_coeff(PGj, k-2-n), b = RgX_coeff(PFj, n);
   12530        7518 :       a = Rg_embedall(a, vEG);
   12531        7518 :       b = Rg_embedall(b, vEF);
   12532        7518 :       a = conj_i(a); if (typ(a) == t_VEC) settyp(a, t_COL);
   12533             :       /* a*b = scalar or t_VEC or t_COL or t_MAT */
   12534        7518 :       S = gadd(S, gdiv(gmul(a,b), gel(vC,n+1)));
   12535             :     }
   12536             :   }
   12537         133 :   S = mulcxpowIs(gmul2n(S, 1-k), 1+k);
   12538         133 :   return vEF==vEG? real_i(S): S;
   12539             : }
   12540             : /* F1S, F2S both symbols, same mf */
   12541             : static GEN
   12542           7 : mfpeterssonnoncusp(GEN F1S, GEN F2S)
   12543             : {
   12544           7 :   pari_sp av = avma;
   12545             :   GEN mf, F1, F2, GF1, GF2, P2, cosets, vE1, vE2, FE1, FE2, P;
   12546             :   GEN I, IP1, RHO, RHOP1, INF, res, ress;
   12547           7 :   const double height = sqrt(3.)/2;
   12548             :   long k, r, j, bitprec, prec;
   12549             : 
   12550           7 :   mf = fs_get_MF(F1S);
   12551           7 :   FE1 = fs_get_EF(F1S); F1 = gel(FE1, 1);
   12552           7 :   FE2 = fs_get_EF(F2S); F2 = gel(FE2, 1);
   12553           7 :   cosets = fs_get_cosets(F1S);
   12554           7 :   bitprec = minuu(fs_get_bitprec(F1S), fs_get_bitprec(F2S));
   12555           7 :   prec = nbits2prec(bitprec);
   12556           7 :   F1S = fs_set_expan(F1S, mfgaexpansionall(mf, FE1, cosets, height, prec));
   12557           7 :   if (F2S != F1S)
   12558           7 :     F2S = fs_set_expan(F2S, mfgaexpansionall(mf, FE2, cosets, height, prec));
   12559           7 :   k = MF_get_k(mf); r = lg(cosets) - 1;
   12560           7 :   vE1 = fs_get_vE(F1S);
   12561           7 :   vE2 = fs_get_vE(F2S);
   12562           7 :   I = gen_I();
   12563           7 :   IP1 = mkcomplex(gen_1,gen_1);
   12564           7 :   RHO = rootsof1u_cx(3, prec+EXTRAPREC64);
   12565           7 :   RHOP1 = gaddsg(1, RHO);
   12566           7 :   INF = mkoo();
   12567           7 :   mffvanish(mf, F1, F2, cosets, &res, &ress);
   12568           7 :   P2 = fs_get_pols(F2S);
   12569           7 :   GF1 = cgetg(r+1, t_VEC);
   12570           7 :   GF2 = cgetg(r+1, t_VEC); P = get_P(k, fetch_var(), prec);
   12571          91 :   for (j = 1; j <= r; j++)
   12572             :   {
   12573          84 :     GEN g = gel(cosets,j);
   12574          84 :     if (res[j]) {
   12575           7 :       gel(GF1,j) = mfsymboleval_direct(F1S, mkvec2(RHOP1,INF), g, P);
   12576           7 :       gel(GF2,j) = mfsymboleval_direct(F2S, mkvec2(I,IP1), g, P);
   12577          77 :     } else if (ress[j]) {
   12578           7 :       gel(GF1,j) = mfsymboleval_direct(F1S, mkvec2(RHOP1,RHO), g, P);
   12579           7 :       gel(GF2,j) = mfsymboleval_direct(F2S, mkvec2(I,INF), g, P);
   12580             :     } else {
   12581          70 :       gel(GF1,j) = mfsymboleval_direct(F1S, mkvec2(RHO,I), g, P);
   12582          70 :       gel(GF2,j) = gneg(gel(P2,j)); /* - symboleval(F2S, [0,oo] */
   12583             :     }
   12584             :   }
   12585           7 :   delete_var();
   12586           7 :   return gerepileupto(av, gdivgu(Haberland(GF1,GF2, vE1,vE2, k), r));
   12587             : }
   12588             : 
   12589             : /* Petersson product of F and G, given by mfsymbol's [k > 1 integral] */
   12590             : static GEN
   12591         133 : mfpetersson_i(GEN FS, GEN GS)
   12592             : {
   12593         133 :   pari_sp av = avma;
   12594             :   GEN mf, ESF, ESG, PF, PG, PH, CHI, cosets, vEF, vEG;
   12595             :   long k, r, j, N, bitprec, prec;
   12596             : 
   12597         133 :   if (!checkfs_i(FS)) pari_err_TYPE("mfpetersson",FS);
   12598         133 :   mf = fs_get_MF(FS);
   12599         133 :   ESF = fs_get_vES(FS);
   12600         133 :   if (!GS) GS = FS;
   12601             :   else
   12602             :   {
   12603          28 :     if (!checkfs_i(GS)) pari_err_TYPE("mfpetersson",GS);
   12604          28 :     if (!mfs_checkmf(GS, mf))
   12605           0 :       pari_err_TYPE("mfpetersson [different mf]", mkvec2(FS,GS));
   12606             :   }
   12607         133 :   ESG = fs_get_vES(GS);
   12608         133 :   if (!gequal0(gel(ESF,1)) && !gequal0(gel(ESG,1)))
   12609           7 :     return mfpeterssonnoncusp(FS, GS);
   12610         126 :   if (gequal0(gel(ESF,2)) || gequal0(gel(ESG,2))) return gc_const(av, gen_0);
   12611         126 :   N = MF_get_N(mf);
   12612         126 :   k = MF_get_k(mf);
   12613         126 :   CHI = MF_get_CHI(mf);
   12614         126 :   PF = fs_get_pols(FS); vEF = fs_get_vE(FS);
   12615         126 :   PG = fs_get_pols(GS); vEG = fs_get_vE(GS);
   12616         126 :   cosets = fs_get_cosets(FS);
   12617         126 :   bitprec = minuu(fs_get_bitprec(FS), fs_get_bitprec(GS));
   12618         126 :   prec = nbits2prec(bitprec);
   12619         126 :   r = lg(PG)-1;
   12620         126 :   PH = cgetg(r+1, t_VEC);
   12621        2478 :   for (j = 1; j <= r; j++)
   12622             :   {
   12623        2352 :     GEN ga = gel(cosets,j), PGj1, PGjm1;
   12624             :     long iT, D;
   12625        2352 :     iT = mftocoset_iD(N, ZM_mulTi(ga), cosets, &D);
   12626        2352 :     PGj1 = RgX_translate(gel(PG, iT), gen_1);
   12627        2352 :     PGj1 = RgX_Rg_mul(PGj1, mfcharcxeval(CHI, D, prec));
   12628        2352 :     iT = mftocoset_iD(N, ZM_mulT(ga), cosets, &D);
   12629        2352 :     PGjm1 = RgX_translate(gel(PG,iT), gen_m1);
   12630        2352 :     PGjm1 = RgX_Rg_mul(PGjm1, mfcharcxeval(CHI, D, prec));
   12631        2352 :     gel(PH,j) = gsub(PGj1, PGjm1);
   12632             :   }
   12633         126 :   return gerepileupto(av, gdivgu(Haberland(PF, PH, vEF, vEG, k), 6*r));
   12634             : }
   12635             : 
   12636             : /****************************************************************/
   12637             : /*           Petersson products using Nelson-Collins            */
   12638             : /****************************************************************/
   12639             : /* Compute W(k,z) = sum_{m >= 1} (mz)^{k-1}(mzK_{k-2}(mz)-K_{k-1}(mz))
   12640             :  * for z>0 and absolute accuracy < 2^{-B}.
   12641             :  * K_k(x) ~ (Pi/(2x))^{1/2} e^{-x} */
   12642             : 
   12643             : static void
   12644       10304 : Wparams(GEN *ph, long *pN, long k, double x, long prec)
   12645             : {
   12646       10304 :   double B = prec2nbits(prec) + 10;
   12647       10304 :   double C = B + k*log(x)/M_LN2 + 1, D = C*M_LN2 + 2.065;
   12648       10304 :   double F = 2 * ((C - 1) * M_LN2 + log(gtodouble(mpfact(k)))) / x;
   12649       10304 :   double T = log(F) * (1 + 2*k/x/F), PI2 = M_PI*M_PI;
   12650       10304 :   *pN = (long)ceil((T/PI2) * (D + log(D/PI2)));
   12651       10304 :   *ph = gprec_w(dbltor(T / *pN), prec);
   12652       10304 : }
   12653             : 
   12654             : static void
   12655       10304 : Wcoshall(GEN *pCH, GEN *pCHK, GEN *pCHK1, GEN h, long N, long k, long prec)
   12656             : {
   12657       10304 :   GEN CH, CHK, CHK1, z = gexp(h, prec);
   12658       10304 :   GEN PO = gpowers(z, N), POK1 = gpowers(gpowgs(z, k-1), N);
   12659       10304 :   GEN E = ginv(gel(PO, N + 1)); /* exp(-hN) */
   12660       10304 :   GEN E1 = ginv(gel(POK1, N + 1)); /* exp(-(k-1)h) */
   12661             :   long j;
   12662       10304 :   *pCH = CH = cgetg(N+1, t_VEC);
   12663       10304 :   *pCHK = CHK = cgetg(N+1, t_VEC);
   12664       10304 :   *pCHK1 = CHK1 = cgetg(N+1, t_VEC);
   12665      146048 :   for (j = 1; j <= N; j++)
   12666             :   {
   12667      135744 :     GEN eh = gel(PO, j+1), emh = gmul(gel(PO, N-j+1), E); /* e^{jh}, e^{-jh} */
   12668      135744 :     GEN ek1h = gel(POK1, j+1), ek1mh = gmul(gel(POK1, N-j+1), E1);
   12669      135744 :     gel(CH, j) = gmul2n(gadd(eh, emh), -1); /* cosh(jh) */
   12670      135744 :     gel(CHK1,j) = gmul2n(gadd(ek1h, ek1mh), -1); /* cosh((k-1)jh) */
   12671      135744 :     gel(CHK, j) = gmul2n(gadd(gmul(eh, ek1h), gmul(emh, ek1mh)), -1);
   12672             :   }
   12673       10304 : }
   12674             : 
   12675             : /* computing W(k,x) via integral */
   12676             : static GEN
   12677       10304 : Wint(long k, GEN vP, GEN x, long prec)
   12678             : {
   12679             :   GEN P, P1, S1, S, h, CH, CHK, CHK1;
   12680             :   long N, j;
   12681       10304 :   Wparams(&h, &N, k, gtodouble(x), prec);
   12682       10304 :   Wcoshall(&CH, &CHK, &CHK1, h, N, k, prec);
   12683       10304 :   P = gel(vP, k+1); P1 = gel(vP, k); S = S1 = NULL;
   12684      156352 :   for (j = 0; j <= N; j++)
   12685             :   {
   12686      146048 :     GEN eh = gexp(j? gmul(x, gel(CH, j)): x, prec);
   12687      146048 :     GEN eh1 = gsubgs(eh, 1), eh1k = gpowgs(eh1, k), t1, t;
   12688      146048 :     t = gdiv(poleval(P, eh), gmul(eh1, eh1k));
   12689      146048 :     t1 = gdiv(poleval(P1, eh), eh1k);
   12690      146048 :     if (j)
   12691             :     {
   12692      135744 :       S = gadd(S, gmul(t, gel(CHK, j)));
   12693      135744 :       S1 = gadd(S1, gmul(t1, gel(CHK1, j)));
   12694             :     }
   12695             :     else
   12696             :     {
   12697       10304 :       S = gmul2n(t, -1);
   12698       10304 :       S1 = gmul2n(t1, -1);
   12699             :     }
   12700             :   }
   12701       10304 :   return gmul(gmul(h, gpowgs(x, k-1)), gsub(gmul(x, S), gmulsg(2*k-1, S1)));
   12702             : }
   12703             : 
   12704             : static GEN
   12705          21 : get_vP(long k)
   12706             : {
   12707          21 :   GEN P, v = cgetg(k+2, t_VEC), Q = deg1pol_shallow(gen_1,gen_m1,0);
   12708             :   long j;
   12709          21 :   gel(v,1) = gen_1;
   12710          21 :   gel(v,2) = P = pol_x(0);
   12711          28 :   for (j = 2; j <= k; j++)
   12712           7 :     gel(v,j+1) = P = RgX_shift_shallow(gsub(gmulsg(j, P),
   12713             :                                             gmul(Q, ZX_deriv(P))), 1);
   12714          21 :   return v;
   12715             : }
   12716             : /* vector of (-1)^j(1/(exp(x)-1))^(j) [x = z] * z^j for 0<=j<=r */
   12717             : static GEN
   12718       63742 : VS(long r, GEN z, GEN V, long prec)
   12719             : {
   12720       63742 :   GEN e = gexp(z, prec), c = ginv(gsubgs(e,1));
   12721       63742 :   GEN T = gpowers0(gmul(c, z), r, c);
   12722             :   long j;
   12723       63742 :   V = gsubst(V, 0, e);
   12724      143864 :   for (j = 1; j <= r + 1; j++) gel(V,j) = gmul(gel(V,j), gel(T,j));
   12725       63742 :   return V;
   12726             : }
   12727             : 
   12728             : /* U(r,x)=sum_{m >= 1} (mx)^k K_k(mx), k = r+1/2 */
   12729             : static GEN
   12730       71932 : Unelson(long r, GEN V)
   12731             : {
   12732       71932 :   GEN S = gel(V,r+1), C = gen_1; /* (r+j)! / j! / (r-j)! */
   12733             :   long j;
   12734       71932 :   if (!r) return S;
   12735       40950 :   for (j = 1; j <= r; j++)
   12736             :   {
   12737       24570 :     C = gdivgu(gmulgu(C, (r+j)*(r-j+1)), j);
   12738       24570 :     S = gadd(S, gmul2n(gmul(C, gel(V, r-j+1)), -j));
   12739             :   }
   12740       16380 :   return S;
   12741             : }
   12742             : /* W(r+1/2,z) / sqrt(Pi/2) */
   12743             : static GEN
   12744       63742 : Wint2(long r, GEN vP, GEN z, long prec)
   12745             : {
   12746       63742 :   GEN R, V = VS(r, z, vP, prec);
   12747       63742 :   R = Unelson(r, V);
   12748       63742 :   if (r) R = gsub(R, gmulsg(2*r, Unelson(r-1, V)));
   12749       63742 :   return R;
   12750             : }
   12751             : typedef GEN(*Wfun_t)(long, GEN, GEN, long);
   12752             : static GEN
   12753       74046 : WfromZ(GEN Z, GEN vP, GEN gkm1, Wfun_t W, long k, GEN pi4, long prec)
   12754             : {
   12755       74046 :   pari_sp av = avma;
   12756       74046 :   GEN Zk = gpow(Z, gkm1, prec), z = gmul(pi4, gsqrt(Z,prec));
   12757       74046 :   return gerepileupto(av, gdiv(W(k, vP, z, prec), Zk));
   12758             : }
   12759             : /* mf a true mf or an fs2 */
   12760             : static GEN
   12761          21 : fs2_init(GEN mf, GEN F, long bit)
   12762             : {
   12763          21 :   pari_sp av = avma;
   12764          21 :   long i, l, lim, N, k, k2, prec = nbits2prec(bit);
   12765             :   GEN DEN, cusps, tab, gk, gkm1, W0, vW, vVW, vVF, vP, al0;
   12766          21 :   GEN vE = mfgetembed(F, prec), pi4 = Pi2n(2, prec);
   12767             :   Wfun_t Wf;
   12768             : 
   12769          21 :   if (lg(mf) == 7)
   12770             :   {
   12771          21 :     vW = cusps = NULL; /* true mf */
   12772          21 :     DEN = tab = NULL; /* -Wall */
   12773             :   }
   12774             :   else
   12775             :   { /* mf already an fs2, reset if its precision is too low */
   12776           0 :     vW = (fs2_get_bitprec(mf) < bit)? NULL: fs2_get_W(mf);
   12777           0 :     cusps = fs2_get_cusps(mf);
   12778           0 :     DEN = fs2_get_den(mf);
   12779           0 :     mf = fs2_get_MF(mf);
   12780             :   }
   12781          21 :   N = MF_get_N(mf);
   12782          21 :   gk = MF_get_gk(mf); gkm1 = gsubgs(gk, 1);
   12783          21 :   k2 = itos(gmul2n(gk,1));
   12784          21 :   Wf = odd(k2)? Wint2: Wint;
   12785          21 :   k = k2 >> 1; vP = get_vP(k);
   12786          21 :   if (vW) lim = (lg(gel(vW,1)) - 2) / N; /* vW[1] attached to cusp 0, width N */
   12787             :   else
   12788             :   { /* true mf */
   12789          21 :     double B = (bit + 10)*M_LN2;
   12790          21 :     double L = (B + k2*log(B)/2 + k2*k2*log(B)/(4*B)) / (4*M_PI);
   12791             :     long n, Lw;
   12792          21 :     lim = ((long)ceil(L*L));
   12793          21 :     Lw = N*lim;
   12794          21 :     tab = cgetg(Lw+1,t_VEC);
   12795       59157 :     for (n = 1; n <= Lw; n++)
   12796       59136 :       gel(tab,n) = WfromZ(uutoQ(n,N), vP, gkm1, Wf, k, pi4, prec);
   12797          21 :     if (!cusps) cusps = mfcusps_i(N);
   12798          21 :     DEN = gmul2n(gmulgu(gpow(Pi2n(3, prec), gkm1, prec), mypsiu(N)), -2);
   12799          21 :     if (odd(k2)) DEN = gdiv(DEN, sqrtr_abs(Pi2n(-1,prec)));
   12800             :   }
   12801          21 :   l = lg(cusps);
   12802          21 :   vVF = cgetg(l, t_VEC);
   12803          21 :   vVW = cgetg(l, t_VEC);
   12804          21 :   al0 = cgetg(l, t_VECSMALL);
   12805          21 :   W0 = k2==1? ginv(pi4): gen_0;
   12806         203 :   for (i = 1; i < l; i++)
   12807             :   {
   12808             :     long A, C, w, wi, Lw, n;
   12809             :     GEN VF, W, paramsF, al;
   12810         182 :     (void)cusp_AC(gel(cusps,i), &A,&C);
   12811         182 :     wi = ugcd(N, C*C); w = N / wi; Lw = w * lim;
   12812         182 :     VF = mfslashexpansion(mf, F, cusp2mat(A,C), Lw, 0, &paramsF, prec);
   12813             :     /* paramsF[2] = w */
   12814         182 :     al = gel(paramsF, 1); if (gequal0(al)) al = NULL;
   12815      100240 :     for (n = 0; n <= Lw; n++)
   12816             :     {
   12817      100058 :       GEN a = gel(VF,n+1);
   12818      100058 :       gel(VF,n+1) = gequal0(a)? gen_0: Rg_embedall(a, vE);
   12819             :     }
   12820         182 :     if (vW)
   12821           0 :       W = gel(vW, i);
   12822             :     else
   12823             :     {
   12824         182 :       W = cgetg(Lw+2, t_VEC);
   12825      100240 :       for (n = 0; n <= Lw; n++)
   12826      100058 :         gel(W, n+1) = al? WfromZ(gadd(al,uutoQ(n,w)),vP,gkm1,Wf,k,pi4, prec)
   12827      100058 :                         : (n? gel(tab, n * wi): W0);
   12828             :     }
   12829         182 :     al0[i] = !al;
   12830         182 :     gel(vVF, i) = VF;
   12831         182 :     gel(vVW, i) = W;
   12832             :   }
   12833          21 :   if (k2 <= 1) al0 = zero_zv(l-1); /* no need to test for convergence */
   12834          21 :   return gerepilecopy(av, mkvecn(7, mf,vVW,cusps,vVF,utoipos(bit),al0,DEN));
   12835             : }
   12836             : 
   12837             : static GEN
   12838          21 : mfpetersson2(GEN Fs, GEN Gs)
   12839             : {
   12840          21 :   pari_sp av = avma;
   12841          21 :   GEN VC, RES, vF, vG, vW = fs2_get_W(Fs), al0 = fs2_get_al0(Fs);
   12842          21 :   long N = MF_get_N(fs2_get_MF(Fs)), j, lC;
   12843             : 
   12844          21 :   VC = fs2_get_cusps(Fs); lC = lg(VC);
   12845          21 :   vF = fs2_get_F(Fs);
   12846          21 :   vG = Gs? fs2_get_F(Gs): vF;
   12847          21 :   RES = gen_0;
   12848         203 :   for (j = 1; j < lC; j++)
   12849             :   {
   12850         182 :     GEN W = gel(vW,j), VF = gel(vF,j), VG = gel(vG,j), T = gen_0;
   12851         182 :     long A, C, w, n, L = lg(W);
   12852         182 :     pari_sp av = avma;
   12853         182 :     (void)cusp_AC(gel(VC,j), &A,&C); w = N/ugcd(N, C*C);
   12854         182 :     if (al0[j] && !isintzero(gel(VF,1)) && !isintzero(gel(VG,1)))
   12855           0 :       pari_err_IMPL("divergent Petersson product");
   12856      100240 :     for (n = 1; n < L; n++)
   12857             :     {
   12858      100058 :       GEN b = gel(VF,n), a = gel(VG,n);
   12859      100058 :       if (!isintzero(a) && !isintzero(b))
   12860             :       {
   12861       79964 :         T = gadd(T, gmul(gel(W,n), gmul(conj_i(a),b)));
   12862       79964 :         if (gc_needed(av,2)) T = gerepileupto(av,T);
   12863             :       }
   12864             :     }
   12865         182 :     if (w != 1) T = gmulgu(T,w);
   12866         182 :     RES = gerepileupto(av, gadd(RES, T));
   12867             :   }
   12868          21 :   if (!Gs) RES = real_i(RES);
   12869          21 :   return gerepileupto(av, gdiv(RES, fs2_get_den(Fs)));
   12870             : }
   12871             : 
   12872             : static long
   12873         147 : symbol_type(GEN F)
   12874             : {
   12875         147 :   if (checkfs_i(F)) return 1;
   12876          21 :   if (checkfs2_i(F)) return 2;
   12877           0 :   return 0;
   12878             : }
   12879             : static int
   12880          28 : symbol_same_mf(GEN F, GEN G) { return gequal(gmael(F,1,1), gmael(G,1,1)); }
   12881             : GEN
   12882         119 : mfpetersson(GEN F, GEN G)
   12883             : {
   12884         119 :   long tF = symbol_type(F);
   12885         119 :   if (!tF) pari_err_TYPE("mfpetersson",F);
   12886         119 :   if (G)
   12887             :   {
   12888          28 :     long tG = symbol_type(G);
   12889          28 :     if (!tG) pari_err_TYPE("mfpetersson",F);
   12890          28 :     if (tF != tG || !symbol_same_mf(F,G))
   12891           0 :       pari_err_TYPE("mfpetersson [incompatible symbols]", mkvec2(F,G));
   12892             :   }
   12893         119 :   return (tF == 1)? mfpetersson_i(F, G): mfpetersson2(F, G);
   12894             : }
   12895             : 
   12896             : /****************************************************************/
   12897             : /*         projective Galois representation, weight 1           */
   12898             : /****************************************************************/
   12899             : static void
   12900         392 : moreorders(long N, GEN CHI, GEN F, GEN *pP, GEN *pO, ulong *bound)
   12901             : {
   12902         392 :   pari_sp av = avma;
   12903             :   forprime_t iter;
   12904         392 :   ulong a = *bound+1, b = 2*(*bound), p;
   12905         392 :   long i = 1;
   12906         392 :   GEN P, O, V = mfcoefs_i(F, b, 1);
   12907         392 :   *bound = b;
   12908         392 :   P = cgetg(b-a+2, t_VECSMALL);
   12909         392 :   O = cgetg(b-a+2, t_VECSMALL);
   12910         392 :   u_forprime_init(&iter, a, b);
   12911        2310 :   while((p = u_forprime_next(&iter))) if (N % p)
   12912             :   {
   12913        1813 :     O[i] = mffindrootof1(V, p, CHI);
   12914        1813 :     P[i++] = p;
   12915             :   }
   12916         392 :   setlg(P, i); *pP = shallowconcat(*pP, P);
   12917         392 :   setlg(O, i); *pO = shallowconcat(*pO, O);
   12918         392 :   gerepileall(av, 2, pP, pO);
   12919         392 : }
   12920             : 
   12921             : static GEN
   12922         182 : search_abelian(GEN nf, long n, long k, GEN N, GEN CHI, GEN F,
   12923             :                GEN *pP, GEN *pO, ulong *bound, long prec)
   12924             : {
   12925         182 :   pari_sp av = avma;
   12926             :   GEN bnr, cond, H, cyc, gn, T, Bquo, P, E;
   12927         182 :   long sN = itos(N), r1 = nf_get_r1(nf), i, j, d;
   12928             : 
   12929         182 :   cond = idealfactor(nf, N);
   12930         182 :   P = gel(cond,1);
   12931         182 :   E = gel(cond,2);
   12932         679 :   for (i = j = 1; i < lg(P); i++)
   12933             :   {
   12934         497 :     GEN pr = gel(P,i), Ej = gen_1;
   12935         497 :     long p = itos(pr_get_p(pr));
   12936         497 :     if (p == n)
   12937             :     {
   12938          98 :       long e = pr_get_e(pr); /* 1 + [e*p/(p-1)] */
   12939          98 :       Ej = utoipos(1 + (e*p) / (p-1));
   12940             :     }
   12941             :     else
   12942             :     {
   12943         399 :       long f = pr_get_f(pr);
   12944         399 :       if (Fl_powu(p % n, f, n) != 1) continue;
   12945             :     }
   12946         462 :     gel(P,j) = pr;
   12947         462 :     gel(E,j) = Ej; j++;
   12948             :   }
   12949         182 :   setlg(P,j);
   12950         182 :   setlg(E,j);
   12951         182 :   cond = mkvec2(cond, const_vec(r1, gen_1));
   12952         182 :   bnr = Buchraymod(Buchall(nf, nf_FORCE, prec), cond, nf_INIT, utoipos(n));
   12953         182 :   cyc = bnr_get_cyc(bnr);
   12954         182 :   d = lg(cyc)-1;
   12955         182 :   H = zv_diagonal(ZV_to_Flv(cyc, n));
   12956         182 :   gn = utoi(n);
   12957         182 :   for (i = 1;;)
   12958             :   {
   12959        2646 :     for(j = 2; i < lg(*pO); i++)
   12960             :     {
   12961        2072 :       long o, q = (*pP)[i];
   12962        2072 :       GEN pr = idealprimedec_galois(nf, stoi(q));
   12963        2072 :       o = ((*pO)[i] / pr_get_f(pr)) % n;
   12964        2072 :       if (o)
   12965             :       {
   12966        1442 :         GEN v = ZV_to_Flv(isprincipalray(bnr, pr), n);
   12967        1442 :         H = vec_append(H, Flv_Fl_mul(v, o, n));
   12968             :       }
   12969             :     }
   12970         574 :     H = Flm_image(H, n); if (lg(cyc)-lg(H) <= k) break;
   12971         392 :     moreorders(sN, CHI, F, pP, pO, bound);
   12972             :   }
   12973         182 :   H = hnfmodid(shallowconcat(zm_to_ZM(H), diagonal_shallow(cyc)), gn);
   12974             : 
   12975         182 :   Bquo = cgetg(k+1, t_MAT);
   12976         812 :   for (i = j = 1; i <= d; i++)
   12977         630 :     if (!equali1(gcoeff(H,i,i))) gel(Bquo,j++) = col_ei(d,i);
   12978             : 
   12979         441 :   for (i = 1, T = NULL; i<=k; i++)
   12980             :   {
   12981         259 :     GEN Hi = hnfmodid(shallowconcat(H, vecsplice(Bquo,i)), gn);
   12982         259 :     GEN pol = rnfkummer(bnr, Hi, prec);
   12983         259 :     T = T? nfcompositum(nf, T, pol, 2): pol;
   12984             :   }
   12985         182 :   T = rnfequation(nf, T); return gc_all(av, 3, &T, pP, pO);
   12986             : }
   12987             : 
   12988             : static GEN
   12989          77 : search_solvable(GEN LG, GEN mf, GEN F, long prec)
   12990             : {
   12991          77 :   GEN N = MF_get_gN(mf), CHI = MF_get_CHI(mf), pol, O, P, nf, Nfa;
   12992          77 :   long i, l = lg(LG), v = fetch_var();
   12993          77 :   ulong bound = 1;
   12994          77 :   O = cgetg(1, t_VECSMALL); /* projective order of rho(Frob_p) */
   12995          77 :   P = cgetg(1, t_VECSMALL);
   12996          77 :   Nfa = Z_factor(N);
   12997          77 :   pol = pol_x(v);
   12998         259 :   for (i = 1; i < l; i++)
   12999             :   { /* n prime, find a (Z/nZ)^k - extension */
   13000         182 :     GEN G = gel(LG,i);
   13001         182 :     long n = G[1], k = G[2];
   13002         182 :     nf = nfinitred(mkvec2(pol,Nfa), prec);
   13003         182 :     pol = search_abelian(nf, n, k, N, CHI, F, &P, &O, &bound, prec);
   13004         182 :     setvarn(pol,v);
   13005             :   }
   13006          77 :   delete_var(); setvarn(pol,0); return pol;
   13007             : }
   13008             : 
   13009             : static GEN
   13010           0 : search_A5(GEN mf, GEN F)
   13011             : {
   13012           0 :   GEN CHI = MF_get_CHI(mf), O, P, L;
   13013           0 :   long N = MF_get_N(mf), i, j, lL, nd, r;
   13014           0 :   ulong bound = 1;
   13015           0 :   r = radicalu(N);
   13016           0 :   L = veccond_to_A5(zv_z_mul(divisorsu(N/r),r), 2); lL = lg(L); nd = lL-1;
   13017           0 :   if (nd == 1) return gmael(L,1,1);
   13018           0 :   O = cgetg(1, t_VECSMALL); /* projective order of rho(Frob_p) */
   13019           0 :   P = cgetg(1, t_VECSMALL);
   13020           0 :   for(i = 1; nd > 1; )
   13021             :   {
   13022             :     long l;
   13023           0 :     moreorders(N, CHI, F, &P, &O, &bound);
   13024           0 :     l = lg(P);
   13025           0 :     for (   ; i < l; i++)
   13026             :     {
   13027           0 :       ulong p = P[i], f = O[i];
   13028           0 :       for (j = 1; j < lL; j++)
   13029           0 :         if (gel(L,j))
   13030             :         {
   13031           0 :           GEN FE = ZpX_primedec(gmael(L,j,1), utoi(p)), F = gel(FE,1);
   13032           0 :           long nF = lg(F)-1;
   13033           0 :           if (!equaliu(gel(F, nF), f)) { gel(L,j) = NULL; nd--; }
   13034             :         }
   13035           0 :       if (nd <= 1) break;
   13036             :     }
   13037             :   }
   13038           0 :   for (j = 1; j < lL; j++)
   13039           0 :     if (gel(L,j)) return gmael(L,j,1);
   13040           0 :   return NULL;
   13041             : }
   13042             : 
   13043             : GEN
   13044          77 : mfgaloisprojrep(GEN mf, GEN F, long prec)
   13045             : {
   13046          77 :   pari_sp av = avma;
   13047          77 :   GEN LG = NULL;
   13048          77 :   if (!checkMF_i(mf) && !checkmf_i(F)) pari_err_TYPE("mfgaloisrep", F);
   13049          77 :   switch( itos(mfgaloistype(mf,F)) )
   13050             :   {
   13051          49 :     case 0: case -12:
   13052          49 :       LG = mkvec2(mkvecsmall2(3,1), mkvecsmall2(2,2)); break;
   13053          28 :     case -24:
   13054          28 :       LG = mkvec3(mkvecsmall2(2,1), mkvecsmall2(3,1), mkvecsmall2(2,2)); break;
   13055           0 :     case -60: return gerepilecopy(av, search_A5(mf, F));
   13056           0 :     default: pari_err_IMPL("mfgaloisprojrep for types D_n");
   13057             :   }
   13058          77 :   return gerepilecopy(av, search_solvable(LG, mf, F, prec));
   13059             : }

Generated by: LCOV version 1.16