Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - mftrace.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 23222-06b1652be) Lines: 7201 7364 97.8 %
Date: 2018-11-15 05:40:53 Functions: 738 740 99.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2016  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : /*************************************************************************/
      15             : /*                                                                       */
      16             : /*              Modular forms package based on trace formulas            */
      17             : /*                                                                       */
      18             : /*************************************************************************/
      19             : #include "pari.h"
      20             : #include "paripriv.h"
      21             : 
      22             : enum {
      23             :   MF_SPLIT = 1,
      24             :   MF_EISENSPACE,
      25             :   MF_FRICKE,
      26             :   MF_MF2INIT,
      27             :   MF_SPLITN
      28             : };
      29             : 
      30             : typedef struct {
      31             :   GEN vnew, vfull, DATA, VCHIP;
      32             :   long n, newHIT, newTOTAL, cuspHIT, cuspTOTAL;
      33             : } cachenew_t;
      34             : 
      35             : static void init_cachenew(cachenew_t *c, long n, long N, GEN f);
      36             : static GEN mfinit_i(GEN NK, long space);
      37             : static GEN mfinit_Nkchi(long N, long k, GEN CHI, long space, long flraw);
      38             : static GEN mf2init_Nkchi(long N, long k, GEN CHI, long space, long flraw);
      39             : static GEN mf2basis(long N, long r, GEN CHI, long space);
      40             : static GEN mfeisensteinbasis(long N, long k, GEN CHI);
      41             : static GEN mfeisensteindec(GEN mf, GEN F);
      42             : static GEN initwt1newtrace(GEN mf);
      43             : static GEN initwt1trace(GEN mf);
      44             : static GEN myfactoru(long N);
      45             : static GEN mydivisorsu(long N);
      46             : static GEN mygmodulo_lift(long k, long ord, GEN C, long vt);
      47             : static GEN mfcoefs_i(GEN F, long n, long d);
      48             : static GEN bhnmat_extend(GEN M, long m,long l, GEN S, cachenew_t *cache);
      49             : static GEN initnewtrace(long N, GEN CHI);
      50             : static void dbg_cachenew(cachenew_t *C);
      51             : static GEN hecke_i(long m, long l, GEN V, GEN F, GEN DATA);
      52             : static GEN c_Ek(long n, long d, GEN F);
      53             : static GEN RgV_heckef2(long n, long d, GEN V, GEN F, GEN DATA);
      54             : static GEN mfcusptrace_i(long N, long k, long n, GEN Dn, GEN TDATA);
      55             : static GEN mfnewtracecache(long N, long k, long n, cachenew_t *cache);
      56             : static GEN colnewtrace(long n0, long n, long d, long N, long k, cachenew_t *c);
      57             : static GEN dihan(GEN bnr, GEN w, GEN k0j, ulong n);
      58             : static GEN sigchi(long k, GEN CHI, long n);
      59             : static GEN sigchi2(long k, GEN CHI1, GEN CHI2, long n, long ord);
      60             : static GEN mflineardivtomat(long N, GEN vF, long n);
      61             : static GEN mfdihedralcusp(long N, GEN CHI);
      62             : static long mfdihedralcuspdim(long N, GEN CHI);
      63             : static GEN mfdihedralnew(long N, GEN CHI);
      64             : static GEN mfdihedralall(GEN LIM);
      65             : static long mfwt1cuspdim(long N, GEN CHI);
      66             : static long mf2dim_Nkchi(long N, long k, GEN CHI, ulong space);
      67             : static long mfdim_Nkchi(long N, long k, GEN CHI, long space);
      68             : static GEN charLFwtk(long k, GEN CHI, long ord);
      69             : static GEN mfeisensteingacx(GEN E,long w,GEN ga,long n,long prec);
      70             : static GEN mfgaexpansion(GEN mf, GEN F, GEN gamma, long n, long prec);
      71             : static GEN mfEHmat(long n, long r);
      72             : static GEN mfEHcoef(long r, long N);
      73             : static GEN mftobasis_i(GEN mf, GEN F);
      74             : 
      75             : static GEN
      76       29113 : mkgNK(GEN N, GEN k, GEN CHI, GEN P) { return mkvec4(N, k, CHI, P); }
      77             : static GEN
      78       12628 : mkNK(long N, long k, GEN CHI) { return mkgNK(stoi(N), stoi(k), CHI, pol_x(1)); }
      79             : GEN
      80        7077 : MF_get_CHI(GEN mf) { return gmael(mf,1,3); }
      81             : GEN
      82       17381 : MF_get_gN(GEN mf) { return gmael(mf,1,1); }
      83             : long
      84       16611 : MF_get_N(GEN mf) { return itou(MF_get_gN(mf)); }
      85             : GEN
      86       11774 : MF_get_gk(GEN mf) { return gmael(mf,1,2); }
      87             : long
      88        5992 : MF_get_k(GEN mf)
      89             : {
      90        5992 :   GEN gk = MF_get_gk(mf);
      91        5992 :   if (typ(gk)!=t_INT) pari_err_IMPL("half-integral weight");
      92        5992 :   return itou(gk);
      93             : }
      94             : long
      95         147 : MF_get_r(GEN mf)
      96             : {
      97         147 :   GEN gk = MF_get_gk(mf);
      98         147 :   if (typ(gk) == t_INT) pari_err_IMPL("integral weight");
      99         147 :   return itou(gel(gk, 1)) >> 1;
     100             : }
     101             : long
     102       11991 : MF_get_space(GEN mf) { return itos(gmael(mf,1,4)); }
     103             : GEN
     104        3626 : MF_get_E(GEN mf) { return gel(mf,2); }
     105             : GEN
     106       18375 : MF_get_S(GEN mf) { return gel(mf,3); }
     107             : GEN
     108        1183 : MF_get_basis(GEN mf) { return shallowconcat(gel(mf,2), gel(mf,3)); }
     109             : long
     110        4445 : MF_get_dim(GEN mf)
     111             : {
     112        4445 :   switch(MF_get_space(mf))
     113             :   {
     114             :     case mf_FULL:
     115         560 :       return lg(MF_get_S(mf)) - 1 + lg(MF_get_E(mf))-1;
     116             :     case mf_EISEN:
     117         140 :       return lg(MF_get_E(mf))-1;
     118             :     default: /* mf_NEW, mf_CUSP, mf_OLD */
     119        3745 :       return lg(MF_get_S(mf)) - 1;
     120             :   }
     121             : }
     122             : GEN
     123        6678 : MFnew_get_vj(GEN mf) { return gel(mf,4); }
     124             : GEN
     125         476 : MFcusp_get_vMjd(GEN mf) { return gel(mf,4); }
     126             : GEN
     127        6069 : MF_get_M(GEN mf) { return gmael(mf,5,3); }
     128             : GEN
     129        2345 : MF_get_Minv(GEN mf) { return gmael(mf,5,2); }
     130             : GEN
     131        7742 : MF_get_Mindex(GEN mf) { return gmael(mf,5,1); }
     132             : 
     133             : /* ordinary gtocol forgets about initial 0s */
     134             : GEN
     135        1596 : sertocol(GEN S) { return gtocol0(S, -(lg(S) - 2 + valp(S))); }
     136             : /*******************************************************************/
     137             : /*     Linear algebra in cyclotomic fields (TODO: export this)     */
     138             : /*******************************************************************/
     139             : /* return r and split prime p giving projection Q(zeta_n) -> Fp, zeta -> r */
     140             : static ulong
     141         665 : QabM_init(long n, ulong *p)
     142             : {
     143         665 :   ulong pinit = 1000000007;
     144             :   forprime_t T;
     145         665 :   if (n <= 1) { *p = pinit; return 0; }
     146         511 :   u_forprime_arith_init(&T, pinit, ULONG_MAX, 1, n);
     147         511 :   *p = u_forprime_next(&T);
     148         511 :   return Flx_oneroot(ZX_to_Flx(polcyclo(n, 0), *p), *p);
     149             : }
     150             : static ulong
     151      676956 : Qab_to_Fl(GEN P, ulong r, ulong p)
     152             : {
     153             :   ulong t;
     154             :   GEN den;
     155      676956 :   P = Q_remove_denom(liftpol_shallow(P), &den);
     156      676956 :   if (typ(P) == t_POL) { GEN Pp = ZX_to_Flx(P, p); t = Flx_eval(Pp, r, p); }
     157      654591 :   else t = umodiu(P, p);
     158      676956 :   if (den) t = Fl_div(t, umodiu(den, p), p);
     159      676956 :   return t;
     160             : }
     161             : static GEN
     162       15386 : QabC_to_Flc(GEN C, ulong r, ulong p)
     163             : {
     164       15386 :   long i, l = lg(C);
     165       15386 :   GEN A = cgetg(l, t_VECSMALL);
     166       15386 :   for (i = 1; i < l; i++) uel(A,i) = Qab_to_Fl(gel(C,i), r, p);
     167       15386 :   return A;
     168             : }
     169             : static GEN
     170         385 : QabM_to_Flm(GEN M, ulong r, ulong p)
     171             : {
     172             :   long i, l;
     173         385 :   GEN A = cgetg_copy(M, &l);
     174       15771 :   for (i = 1; i < l; i++)
     175       15386 :     gel(A, i) = QabC_to_Flc(gel(M, i), r, p);
     176         385 :   return A;
     177             : }
     178             : /* A a t_POL */
     179             : static GEN
     180         553 : QabX_to_Flx(GEN A, ulong r, ulong p)
     181             : {
     182         553 :   long i, l = lg(A);
     183         553 :   GEN a = cgetg(l, t_VECSMALL);
     184         553 :   a[1] = ((ulong)A[1])&VARNBITS;
     185         553 :   for (i = 2; i < l; i++) uel(a,i) = Qab_to_Fl(gel(A,i), r, p);
     186         553 :   return Flx_renormalize(a, l);
     187             : }
     188             : 
     189             : /* FIXME: remove */
     190             : static GEN
     191         952 : ZabM_pseudoinv_i(GEN M, GEN P, long n, GEN *pv, GEN *den, int ratlift)
     192             : {
     193         952 :   GEN v = ZabM_indexrank(M, P, n);
     194         952 :   if (pv) *pv = v;
     195         952 :   M = shallowmatextract(M,gel(v,1),gel(v,2));
     196         952 :   return ratlift? ZabM_inv_ratlift(M, P, n, den): ZabM_inv(M, P, n, den);
     197             : }
     198             : 
     199             : /* M matrix with coeff in Q(\chi)), where Q(\chi) = Q(X)/(P) for
     200             :  * P = cyclotomic Phi_n. Assume M rational if n <= 2 */
     201             : static GEN
     202        1617 : QabM_ker(GEN M, GEN P, long n)
     203             : {
     204             :   GEN B;
     205        1617 :   if (n <= 2)
     206         882 :     B = ZM_ker(Q_primpart(M));
     207             :   else
     208         735 :     B = ZabM_ker(Q_primpart(liftpol_shallow(M)), P, n);
     209        1617 :   return B;
     210             : }
     211             : /* pseudo-inverse of M */
     212             : static GEN
     213        1162 : QabM_pseudoinv(GEN M, GEN P, long n, GEN *pv, GEN *pden)
     214             : {
     215             :   GEN cM, Mi;
     216        1162 :   if (n <= 2)
     217             :   {
     218         875 :     M = Q_primitive_part(M, &cM);
     219         875 :     Mi = ZM_pseudoinv(M, pv, pden); /* M^(-1) = Mi / (cM * den) */
     220             :   }
     221             :   else
     222             :   {
     223         287 :     M = Q_primitive_part(liftpol_shallow(M), &cM);
     224         287 :     Mi = ZabM_pseudoinv(M, P, n, pv, pden);
     225         287 :     Mi = gmodulo(Mi, P);
     226             :   }
     227        1162 :   *pden = mul_content(*pden, cM);
     228        1162 :   return Mi;
     229             : }
     230             : 
     231             : static GEN
     232        9583 : QabM_indexrank(GEN M, GEN P, long n)
     233             : {
     234             :   GEN z;
     235        9583 :   if (n <= 2)
     236             :   {
     237        8477 :     M = vec_Q_primpart(M);
     238        8477 :     z = ZM_indexrank(M); /* M^(-1) = Mi / (cM * den) */
     239             :   }
     240             :   else
     241             :   {
     242        1106 :     M = vec_Q_primpart(liftpol_shallow(M));
     243        1106 :     z = ZabM_indexrank(M, P, n);
     244             :   }
     245        9583 :   return z;
     246             : }
     247             : 
     248             : /*********************************************************************/
     249             : /*                    Simple arithmetic functions                    */
     250             : /*********************************************************************/
     251             : /* TODO: most of these should be exported and used in ifactor1.c */
     252             : /* phi(n) */
     253             : static ulong
     254      142352 : myeulerphiu(ulong n)
     255             : {
     256             :   pari_sp av;
     257      142352 :   if (n == 1) return 1;
     258      127120 :   av = avma; return gc_ulong(av, eulerphiu_fact(myfactoru(n)));
     259             : }
     260             : static long
     261      154742 : mymoebiusu(ulong n)
     262             : {
     263             :   pari_sp av;
     264      154742 :   if (n == 1) return 1;
     265      140217 :   av = avma; return gc_long(av, moebiusu_fact(myfactoru(n)));
     266             : }
     267             : 
     268             : static long
     269        2786 : mynumdivu(long N)
     270             : {
     271             :   pari_sp av;
     272        2786 :   if (N == 1) return 1;
     273        2681 :   av = avma; return gc_long(av, numdivu_fact(myfactoru(N)));
     274             : }
     275             : 
     276             : /* N\prod_{p|N} (1+1/p) */
     277             : static long
     278      337155 : mypsiu(ulong N)
     279             : {
     280      337155 :   pari_sp av = avma;
     281      337155 :   GEN P = gel(myfactoru(N), 1);
     282      337155 :   long j, l = lg(P), res = N;
     283      337155 :   for (j = 1; j < l; j++) res += res/P[j];
     284      337155 :   return gc_long(av,res);
     285             : }
     286             : /* write n = mf^2. Return m, set f. */
     287             : static ulong
     288         210 : mycore(ulong n, long *pf)
     289             : {
     290         210 :   pari_sp av = avma;
     291         210 :   GEN fa = myfactoru(n), P = gel(fa,1), E = gel(fa,2);
     292         210 :   long i, l = lg(P), m = 1, f = 1;
     293         850 :   for (i = 1; i < l; i++)
     294             :   {
     295         640 :     long j, p = P[i], e = E[i];
     296         640 :     if (e & 1) m *= p;
     297         640 :     for (j = 2; j <= e; j+=2) f *= p;
     298             :   }
     299         210 :   *pf = f; return gc_long(av,m);
     300             : }
     301             : 
     302             : /* fa = factorization of -D > 0, return -D0 > 0 (where D0 is fundamental) */
     303             : static long
     304     7903259 : corediscs_fact(GEN fa)
     305             : {
     306     7903259 :   GEN P = gel(fa,1), E = gel(fa,2);
     307     7903259 :   long i, l = lg(P), m = 1;
     308    26177550 :   for (i = 1; i < l; i++)
     309             :   {
     310    18274291 :     long p = P[i], e = E[i];
     311    18274291 :     if (e & 1) m *= p;
     312             :   }
     313     7903259 :   if ((m&3L) != 3) m <<= 2;
     314     7903259 :   return m;
     315             : }
     316             : static long
     317        6328 : mubeta(long n)
     318             : {
     319        6328 :   pari_sp av = avma;
     320        6328 :   GEN E = gel(myfactoru(n), 2);
     321        6328 :   long i, s = 1, l = lg(E);
     322       13118 :   for (i = 1; i < l; i++)
     323             :   {
     324        6790 :     long e = E[i];
     325        6790 :     if (e >= 3) return gc_long(av,0);
     326        6790 :     if (e == 1) s *= -2;
     327             :   }
     328        6328 :   return gc_long(av,s);
     329             : }
     330             : 
     331             : /* n = n1*n2, n1 = ppo(n, m); return mubeta(n1)*moebiusu(n2).
     332             :  * N.B. If n from newt_params we, in fact, never return 0 */
     333             : static long
     334     5957350 : mubeta2(long n, long m)
     335             : {
     336     5957350 :   pari_sp av = avma;
     337     5957350 :   GEN fa = myfactoru(n), P = gel(fa,1), E = gel(fa,2);
     338     5957350 :   long i, s = 1, l = lg(P);
     339    11928756 :   for (i = 1; i < l; i++)
     340             :   {
     341     5971406 :     long p = P[i], e = E[i];
     342     5971406 :     if (m % p)
     343             :     { /* p^e in n1 */
     344     4876753 :       if (e >= 3) return gc_long(av,0);
     345     4876753 :       if (e == 1) s *= -2;
     346             :     }
     347             :     else
     348             :     { /* in n2 */
     349     1094653 :       if (e >= 2) return gc_long(av,0);
     350     1094653 :       s = -s;
     351             :     }
     352             :   }
     353     5957350 :   return gc_long(av,s);
     354             : }
     355             : 
     356             : /* write N = prod p^{ep} and n = df^2, d squarefree.
     357             :  * set g  = ppo(gcd(sqfpart(N), f), FC)
     358             :  *     N2 = prod p^if(e==1 || p|n, ep-1, ep-2) */
     359             : static void
     360     1434615 : newt_params(long N, long n, long FC, long *pg, long *pN2)
     361             : {
     362     1434615 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
     363     1434615 :   long i, g = 1, N2 = 1, l = lg(P);
     364     3799593 :   for (i = 1; i < l; i++)
     365             :   {
     366     2364978 :     long p = P[i], e = E[i];
     367     2364978 :     if (e == 1)
     368     2034375 :     { if (FC % p && n % (p*p) == 0) g *= p; }
     369             :     else
     370      330603 :       N2 *= upowuu(p,(n % p)? e-2: e-1);
     371             :   }
     372     1434615 :   *pg = g; *pN2 = N2;
     373     1434615 : }
     374             : /* simplified version of newt_params for n = 1 (newdim) */
     375             : static void
     376       36526 : newd_params(long N, long *pN2)
     377             : {
     378       36526 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
     379       36526 :   long i, N2 = 1, l = lg(P);
     380       93093 :   for (i = 1; i < l; i++)
     381             :   {
     382       56567 :     long p = P[i], e = E[i];
     383       56567 :     if (e > 2) N2 *= upowuu(p, e-2);
     384             :   }
     385       36526 :   *pN2 = N2;
     386       36526 : }
     387             : 
     388             : static long
     389          21 : newd_params2(long N)
     390             : {
     391          21 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
     392          21 :   long i, N2 = 1, l = lg(P);
     393          56 :   for (i = 1; i < l; i++)
     394             :   {
     395          35 :     long p = P[i], e = E[i];
     396          35 :     if (e >= 2) N2 *= upowuu(p, e);
     397             :   }
     398          21 :   return N2;
     399             : }
     400             : 
     401             : /*******************************************************************/
     402             : /*   Relative trace between cyclotomic fields (TODO: export this)  */
     403             : /*******************************************************************/
     404             : /* g>=1; return g * prod_{p | g, (p,q) = 1} (1-1/p) */
     405             : static long
     406       85218 : phipart(long g, long q)
     407             : {
     408       85218 :   if (g > 1)
     409             :   {
     410       33215 :     GEN P = gel(myfactoru(g), 1);
     411       33215 :     long i, l = lg(P);
     412       33215 :     for (i = 1; i < l; i++) { long p = P[i]; if (q % p) g -= g / p; }
     413             :   }
     414       85218 :   return g;
     415             : }
     416             : /* Trace(zeta_n^k) from Q(\zeta_n) to Q(\zeta_m) with n = m*d; k > 0 */
     417             : static GEN
     418      154651 : tracerelz(long d, long m, long k, long vt)
     419             : {
     420      154651 :   long s, v, g = ugcd(k, d), q = d / g, muq = mymoebiusu(q);
     421      154651 :   if (!muq) return gen_0;
     422      100618 :   if (m == 1)
     423             :   {
     424       35686 :     s = phipart(g, q); if (muq < 0) s = -s;
     425       35686 :     return stoi(s);
     426             :   }
     427       64932 :   if (ugcd(q, m) > 1) return gen_0;
     428       49532 :   s = phipart(g, m*q); if (muq < 0) s = -s;
     429       49532 :   v = Fl_inv(q % m, m);
     430       49532 :   v = (v*(k/g)) % m;
     431       49532 :   return mygmodulo_lift(v, m, stoi(s), vt);
     432             : }
     433             : /* m | n, both not 2 mod 4. Pn = polcyclo(n) */
     434             : GEN
     435       35266 : Qab_trace_init(GEN Pn, long n, long m)
     436             : {
     437             :   GEN T, Pm;
     438             :   long a, i, d, vt;
     439       35266 :   if (m == n) return mkvec(Pn);
     440       25137 :   d = degpol(Pn);
     441       25137 :   vt = varn(Pn);
     442       25137 :   Pm = polcyclo(m, vt);
     443       25137 :   T = cgetg(d+1, t_VEC);
     444       25137 :   gel(T,1) = utoipos(d / degpol(Pm)); /* Tr 1 */
     445       25137 :   a = n / m;
     446       25137 :   for (i = 1; i < d; i++) gel(T,i+1) = tracerelz(a, m, i, vt);
     447       25137 :   return mkvec3(Pm, Pn, T);
     448             : }
     449             : /* x a t_POL modulo Phi_n; n, m not 2 mod 4, degrel != 1*/
     450             : static GEN
     451       48034 : tracerel_i(GEN T, GEN x)
     452             : {
     453       48034 :   long k, l = lg(x);
     454       48034 :   GEN S = gen_0;
     455       48034 :   for (k = 2; k < l; k++) S = gadd(S, gmul(gel(T,k-1), gel(x,k)));
     456       48034 :   return S;
     457             : }
     458             : /* v = Qab_trace_init(n,m); x is a t_VEC of polmodulo Phi_n
     459             :  * Tr_{Q(zeta_n)/Q(zeta_m)} (zeta_n^t * x) */
     460             : GEN
     461        4081 : QabV_tracerel(GEN v, long t, GEN x)
     462             : {
     463             :   long l, j, degrel;
     464             :   GEN y, z, Pm, Pn, T;
     465        4081 :   if (lg(v) != 4) return x;
     466        4081 :   y = cgetg_copy(x, &l);
     467        4081 :   Pm = gel(v,1);
     468        4081 :   Pn = gel(v,2);
     469        4081 :   T  = gel(v,3);
     470        4081 :   degrel = degpol(Pn) / degpol(Pm);
     471        4081 :   z = RgX_rem(pol_xn(t, varn(Pn)), Pn);
     472      100184 :   for (j = 1; j < l; j++)
     473             :   {
     474       96103 :     GEN a = liftpol_shallow(gel(x,j));
     475       96103 :     a = simplify_shallow( gmul(a, z) );
     476       96103 :     if (typ(a) == t_POL)
     477             :     {
     478       48034 :       a = gdivgs(tracerel_i(T, RgX_rem(a, Pn)), degrel);
     479       48034 :       if (typ(a) == t_POL) a = RgX_rem(a, Pm);
     480             :     }
     481       96103 :     gel(y,j) = a;
     482             :   }
     483        4081 :   return y;
     484             : }
     485             : 
     486             : /*              Operations on Dirichlet characters                       */
     487             : 
     488             : /* A Dirichlet character can be given in GP in different formats, but in this
     489             :  * package, it will be a vector CHI=[G,chi,ord], where G is the (Z/MZ)^* to
     490             :  * which the character belongs, chi is the character in Conrey format, ord is
     491             :  * the order */
     492             : 
     493             : static GEN
     494     1396752 : gmfcharorder(GEN CHI) { return gel(CHI, 3); }
     495             : long
     496     1361318 : mfcharorder(GEN CHI) { return itou(gmfcharorder(CHI)); }
     497             : static long
     498        8463 : mfcharistrivial(GEN CHI) { return !CHI || mfcharorder(CHI) == 1; }
     499             : static GEN
     500      737779 : gmfcharmodulus(GEN CHI) { return gmael3(CHI, 1, 1, 1); }
     501             : long
     502      737779 : mfcharmodulus(GEN CHI) { return itou(gmfcharmodulus(CHI)); }
     503             : GEN
     504      249536 : mfcharpol(GEN CHI) { return gel(CHI,4); }
     505             : static long
     506      497791 : ord_canon(long ord)
     507             : {
     508      497791 :   if ((ord & 3L) == 2) ord >>= 1;
     509      497791 :   return ord;
     510             : }
     511             : static long
     512       28350 : mfcharorder_canon(GEN CHI) { return ord_canon(mfcharorder(CHI)); }
     513             : 
     514             : /* t^k mod polcyclo(ord), ord = order(CHI) > 1 */
     515             : static GEN
     516        5992 : mygmodulo(GEN CHI, long k)
     517             : {
     518             :   GEN C, Pn;
     519             :   long ord;
     520        5992 :   if (!k) return gen_1;
     521        5488 :   ord = mfcharorder(CHI);
     522        5488 :   if ((k << 1) == ord) return gen_m1;
     523        4865 :   Pn = mfcharpol(CHI);
     524        4865 :   if ((ord&3L) != 2)
     525         259 :     C = gen_1;
     526             :   else
     527             :   {
     528        4606 :     ord >>= 1;
     529        4606 :     if (odd(k)) { C = gen_m1; k += ord; } else C = gen_1;
     530        4606 :     k >>= 1;
     531             :   }
     532        4865 :   return gmodulo(monomial(C, k, varn(Pn)), Pn);
     533             : }
     534             : /* C*zeta_ord^k */
     535             : static GEN
     536      876764 : mygmodulo_lift(long k, long ord, GEN C, long vt)
     537             : {
     538      876764 :   if (!k) return C;
     539      500234 :   if ((k << 1) == ord) return gneg(C);
     540      358890 :   if ((ord&3L) == 2)
     541             :   {
     542      167636 :     if (odd(k)) { C = gneg(C); k += ord >> 1; }
     543      167636 :     k >>= 1;
     544             :   }
     545      358890 :   return monomial(C, k, vt);
     546             : }
     547             : /* vz[i+1] = image of (zeta_ord)^i in Fp */
     548             : static ulong
     549      171437 : mygmodulo_Fl(long k, GEN vz, ulong C, ulong p)
     550             : {
     551             :   long ord;
     552      171437 :   if (!k) return C;
     553      111069 :   ord = lg(vz)-2;
     554      111069 :   if ((k << 1) == ord) return Fl_neg(C,p);
     555       89446 :   if ((ord&3L) == 2)
     556             :   {
     557       70140 :     if (odd(k)) { C = Fl_neg(C,p); k += ord >> 1; }
     558       70140 :     k >>= 1;
     559             :   }
     560       89446 :   return Fl_mul(C, vz[k+1], p);
     561             : }
     562             : 
     563             : static long
     564      703920 : znchareval_i(GEN CHI, long n, GEN ord)
     565      703920 : { return itos(znchareval(gel(CHI,1), gel(CHI,2), stoi(n), ord)); }
     566             : 
     567             : /* G a znstar, L a Conrey log: return a 'mfchar' */
     568             : static GEN
     569      369677 : mfcharGL(GEN G, GEN L)
     570             : {
     571      369677 :   GEN o = zncharorder(G,L);
     572      369677 :   long ord = ord_canon(itou(o)), vt = fetch_user_var("t");
     573      369677 :   return mkvec4(G, L, o, polcyclo(ord,vt));
     574             : }
     575             : static GEN
     576        4081 : mfchartrivial()
     577        4081 : { return mfcharGL(znstar0(gen_1,1), cgetg(1,t_COL)); }
     578             : /* convert a generic character into an 'mfchar' */
     579             : static GEN
     580        3927 : get_mfchar(GEN CHI)
     581             : {
     582             :   GEN G, L;
     583        3927 :   if (typ(CHI) != t_VEC) CHI = znchar(CHI);
     584             :   else
     585             :   {
     586         833 :     long l = lg(CHI);
     587         833 :     if ((l != 3 && l != 5) || !checkznstar_i(gel(CHI,1)))
     588           7 :       pari_err_TYPE("checkNF [chi]", CHI);
     589         826 :     if (l == 5) return CHI;
     590             :   }
     591        3892 :   G = gel(CHI,1);
     592        3892 :   L = gel(CHI,2); if (typ(L) != t_COL) L = znconreylog(G,L);
     593        3892 :   return mfcharGL(G, L);
     594             : }
     595             : 
     596             : /* parse [N], [N,k], [N,k,CHI]. If 'joker' is set, allow wildcard for CHI */
     597             : static GEN
     598        9009 : checkCHI(GEN NK, long N, int joker)
     599             : {
     600             :   GEN CHI;
     601        9009 :   if (lg(NK) == 3)
     602         602 :     CHI = mfchartrivial();
     603             :   else
     604             :   {
     605             :     long i, l;
     606        8407 :     CHI = gel(NK,3); l = lg(CHI);
     607        8407 :     if (isintzero(CHI) && joker)
     608        4095 :       CHI = NULL; /* all character orbits */
     609        4312 :     else if (isintm1(CHI) && joker > 1)
     610        2373 :       CHI = gen_m1; /* sum over all character orbits */
     611        2072 :     else if ((typ(CHI) == t_VEC &&
     612         189 :              (l == 1 || l != 3 || !checkznstar_i(gel(CHI,1)))) && joker)
     613             :     {
     614         133 :       CHI = shallowtrans(CHI); /* list of characters */
     615         133 :       for (i = 1; i < l; i++) gel(CHI,i) = get_mfchar(gel(CHI,i));
     616             :     }
     617             :     else
     618             :     {
     619        1806 :       CHI = get_mfchar(CHI); /* single char */
     620        1806 :       if (N % mfcharmodulus(CHI)) pari_err_TYPE("checkNF [chi]", NK);
     621             :     }
     622             :   }
     623        8995 :   return CHI;
     624             : }
     625             : /* support half-integral weight */
     626             : static void
     627        9016 : checkNK2(GEN NK, long *N, long *nk, long *dk, GEN *CHI, int joker)
     628             : {
     629        9016 :   long l = lg(NK);
     630             :   GEN T;
     631        9016 :   if (typ(NK) != t_VEC || l < 3 || l > 4) pari_err_TYPE("checkNK", NK);
     632        9016 :   T = gel(NK,1); if (typ(T) != t_INT) pari_err_TYPE("checkNF [N]", NK);
     633        9016 :   *N = itos(T); if (*N <= 0) pari_err_TYPE("checkNF [N <= 0]", NK);
     634        9016 :   T = gel(NK,2);
     635        9016 :   switch(typ(T))
     636             :   {
     637        5656 :     case t_INT:  *nk = itos(T); *dk = 1; break;
     638             :     case t_FRAC:
     639        3353 :       *nk = itos(gel(T,1));
     640        3353 :       *dk = itou(gel(T,2)); if (*dk == 2) break;
     641           7 :     default: pari_err_TYPE("checkNF [k]", NK);
     642             :   }
     643        9009 :   *CHI = checkCHI(NK, *N, joker);
     644        8995 : }
     645             : /* don't support half-integral weight */
     646             : static void
     647         126 : checkNK(GEN NK, long *N, long *k, GEN *CHI, int joker)
     648             : {
     649             :   long d;
     650         126 :   checkNK2(NK, N, k, &d, CHI, joker);
     651         126 :   if (d != 1) pari_err_TYPE("checkNF [k]", NK);
     652         126 : }
     653             : 
     654             : static GEN
     655        4851 : mfchargalois(long N, int odd, GEN flagorder)
     656             : {
     657        4851 :   GEN G = znstar0(utoi(N), 1), L = chargalois(G, flagorder);
     658        4851 :   long l = lg(L), i, j;
     659      112735 :   for (i = j = 1; i < l; i++)
     660             :   {
     661      107884 :     GEN chi = znconreyfromchar(G, gel(L,i));
     662      107884 :     if (zncharisodd(G,chi) == odd) gel(L,j++) = mfcharGL(G,chi);
     663             :   }
     664        4851 :   setlg(L, j); return L;
     665             : }
     666             : /* possible characters for non-trivial S_1(N, chi) */
     667             : static GEN
     668        1708 : mfwt1chars(long N, GEN vCHI)
     669             : {
     670        1708 :   if (vCHI) return vCHI; /*do not filter, user knows best*/
     671             :   /* Tate's theorem */
     672        1638 :   return mfchargalois(N, 1, uisprime(N)? mkvecsmall2(2,4): NULL);
     673             : }
     674             : static GEN
     675        3255 : mfchars(long N, long k, long dk, GEN vCHI)
     676        3255 : { return vCHI? vCHI: mfchargalois(N, (dk == 2)? 0: (k & 1), NULL); }
     677             : 
     678             : /* wrappers from mfchar to znchar */
     679             : static long
     680       66129 : mfcharparity(GEN CHI)
     681             : {
     682       66129 :   if (!CHI) return 1;
     683       66129 :   return zncharisodd(gel(CHI,1), gel(CHI,2)) ? -1 : 1;
     684             : }
     685             : /* if CHI is primitive, return CHI itself, not a copy */
     686             : static GEN
     687       67942 : mfchartoprimitive(GEN CHI, long *pF)
     688             : {
     689             :   pari_sp av;
     690             :   GEN chi, F;
     691       67942 :   if (!CHI) { if (pF) *pF = 1; return mfchartrivial(); }
     692       67942 :   av = avma; F = znconreyconductor(gel(CHI,1), gel(CHI,2), &chi);
     693       67942 :   if (typ(F) == t_INT) set_avma(av);
     694             :   else
     695             :   {
     696        7357 :     CHI = leafcopy(CHI);
     697        7357 :     gel(CHI,1) = znstar0(F, 1);
     698        7357 :     gel(CHI,2) = chi;
     699             :   }
     700       67942 :   if (pF) *pF = mfcharmodulus(CHI);
     701       67942 :   return CHI;
     702             : }
     703             : static long
     704      392784 : mfcharconductor(GEN CHI)
     705             : {
     706      392784 :   pari_sp av = avma;
     707      392784 :   GEN res = znconreyconductor(gel(CHI,1), gel(CHI,2), NULL);
     708      392784 :   if (typ(res) == t_VEC) res = gel(res, 1);
     709      392784 :   return gc_long(av, itos(res));
     710             : }
     711             : 
     712             : /* n coprime with the modulus of CHI */
     713             : static GEN
     714       10416 : mfchareval_i(GEN CHI, long n)
     715             : {
     716       10416 :   GEN ord = gmfcharorder(CHI);
     717       10416 :   if (equali1(ord)) return gen_1;
     718        5992 :   return mygmodulo(CHI, znchareval_i(CHI, n, ord));
     719             : }
     720             : /* d a multiple of ord(CHI); n coprime with char modulus;
     721             :  * return x s.t. CHI(n) = \zeta_d^x] */
     722             : static long
     723     1253644 : mfcharevalord(GEN CHI, long n, long d)
     724             : {
     725     1253644 :   if (mfcharorder(CHI) == 1) return 0;
     726      690242 :   return znchareval_i(CHI, n, utoi(d));
     727             : }
     728             : 
     729             : /*                      Operations on mf closures                    */
     730             : static GEN
     731       50456 : tagparams(long t, GEN NK) { return mkvec2(mkvecsmall(t), NK); }
     732             : static GEN
     733         952 : lfuntag(long t, GEN x) { return mkvec2(mkvecsmall(t), x); }
     734             : static GEN
     735          49 : tag0(long t, GEN NK) { retmkvec(tagparams(t,NK)); }
     736             : static GEN
     737        8855 : tag(long t, GEN NK, GEN x) { retmkvec2(tagparams(t,NK), x); }
     738             : static GEN
     739       29393 : tag2(long t, GEN NK, GEN x, GEN y) { retmkvec3(tagparams(t,NK), x,y); }
     740             : static GEN
     741       12047 : tag3(long t, GEN NK, GEN x,GEN y,GEN z) { retmkvec4(tagparams(t,NK), x,y,z); }
     742             : /* is F a "modular form" ? */
     743             : int
     744       14805 : checkmf_i(GEN F)
     745       14805 : { return typ(F) == t_VEC
     746       14315 :     && lg(F) > 1 && typ(gel(F,1)) == t_VEC
     747       10206 :     && lg(gel(F,1)) == 3
     748       10045 :     && typ(gmael(F,1,1)) == t_VECSMALL
     749       24850 :     && typ(gmael(F,1,2)) == t_VEC; }
     750      169022 : long mf_get_type(GEN F) { return gmael(F,1,1)[1]; }
     751      125622 : GEN mf_get_gN(GEN F) { return gmael3(F,1,2,1); }
     752      102676 : GEN mf_get_gk(GEN F) { return gmael3(F,1,2,2); }
     753             : /* k - 1/2, assume k in 1/2 + Z */
     754         266 : long mf_get_r(GEN F) { return itou(gel(mf_get_gk(F),1)) >> 1; }
     755       89761 : long mf_get_N(GEN F) { return itou(mf_get_gN(F)); }
     756       56917 : long mf_get_k(GEN F)
     757             : {
     758       56917 :   GEN gk = mf_get_gk(F);
     759       56917 :   if (typ(gk)!=t_INT) pari_err_IMPL("half-integral weight");
     760       56917 :   return itou(gk);
     761             : }
     762       38451 : GEN mf_get_CHI(GEN F) { return gmael3(F,1,2,3); }
     763       19215 : GEN mf_get_field(GEN F) { return gmael3(F,1,2,4); }
     764       16352 : GEN mf_get_NK(GEN F) { return gmael(F,1,2); }
     765             : static void
     766         462 : mf_setfield(GEN f, GEN P)
     767             : {
     768         462 :   gel(f,1) = leafcopy(gel(f,1));
     769         462 :   gmael(f,1,2) = leafcopy(gmael(f,1,2));
     770         462 :   gmael3(f,1,2,4) = P;
     771         462 : }
     772             : 
     773             : /* UTILITY FUNCTIONS */
     774             : GEN
     775        4368 : mftocol(GEN F, long lim, long d)
     776        4368 : { GEN c = mfcoefs_i(F, lim, d); settyp(c,t_COL); return c; }
     777             : GEN
     778        1295 : mfvectomat(GEN vF, long lim, long d)
     779             : {
     780        1295 :   long j, l = lg(vF);
     781        1295 :   GEN M = cgetg(l, t_MAT);
     782        1295 :   for (j = 1; j < l; j++) gel(M,j) = mftocol(gel(vF,j), lim, d);
     783        1295 :   return M;
     784             : }
     785             : 
     786             : static GEN
     787        3619 : RgV_to_ser_full(GEN x) { return RgV_to_ser(x, 0, lg(x)+1); }
     788             : /* TODO: delete */
     789             : static GEN
     790        2086 : mfcoefsser(GEN F, long n) { return RgV_to_ser_full(mfcoefs_i(F,n,1)); }
     791             : static GEN
     792         777 : sertovecslice(GEN S, long n)
     793             : {
     794         777 :   GEN v = gtovec0(S, -(lg(S) - 2 + valp(S)));
     795         777 :   long l = lg(v), n2 = n + 2;
     796         777 :   if (l < n2) pari_err_BUG("sertovecslice [n too large]");
     797         777 :   return (l == n2)? v: vecslice(v, 1, n2-1);
     798             : }
     799             : 
     800             : /* a, b two RgV of the same length, multiply as truncated power series */
     801             : static GEN
     802        2954 : RgV_mul_RgXn(GEN a, GEN b)
     803             : {
     804        2954 :   long n = lg(a)-1;
     805             :   GEN c;
     806        2954 :   a = RgV_to_RgX(a,0);
     807        2954 :   b = RgV_to_RgX(b,0); c = RgXn_mul(a, b, n);
     808        2954 :   c = RgX_to_RgC(c,n); settyp(c,t_VEC); return c;
     809             : }
     810             : /* divide as truncated power series */
     811             : static GEN
     812         259 : RgV_div_RgXn(GEN a, GEN b)
     813             : {
     814         259 :   long n = lg(a)-1;
     815             :   GEN c;
     816         259 :   a = RgV_to_RgX(a,0);
     817         259 :   b = RgV_to_RgX(b,0); c = RgXn_mul(a, RgXn_inv(b,n), n);
     818         259 :   c = RgX_to_RgC(c,n); settyp(c,t_VEC); return c;
     819             : }
     820             : /* a^b */
     821             : static GEN
     822          77 : RgV_pows_RgXn(GEN a, long b)
     823             : {
     824          77 :   long n = lg(a)-1;
     825             :   GEN c;
     826          77 :   a = RgV_to_RgX(a,0);
     827          77 :   if (b < 0) { a = RgXn_inv(a, n); b = -b; }
     828          77 :   c = RgXn_powu_i(a,b,n);
     829          77 :   c = RgX_to_RgC(c,n); settyp(c,t_VEC); return c;
     830             : }
     831             : 
     832             : /* assume lg(V) >= n*d + 2 */
     833             : static GEN
     834        6069 : c_deflate(long n, long d, GEN v)
     835             : {
     836        6069 :   long i, id, l = n+2;
     837             :   GEN w;
     838        6069 :   if (d == 1) return lg(v) == l ? v: vecslice(v, 1, l-1);
     839         322 :   w = cgetg(l, typ(v));
     840         322 :   for (i = id = 1; i < l; i++, id += d) gel(w, i) = gel(v, id);
     841         322 :   return w;
     842             : }
     843             : static GEN
     844         518 : c_mul(long n, long d, GEN F, GEN G)
     845             : {
     846         518 :   pari_sp av = avma;
     847         518 :   long nd = n*d;
     848         518 :   GEN VF = mfcoefs_i(F, nd, 1);
     849         518 :   GEN VG = mfcoefs_i(G, nd, 1);
     850         518 :   return gerepilecopy(av, c_deflate(n, d, RgV_mul_RgXn(VF,VG)));
     851             : }
     852             : static GEN
     853          77 : c_pow(long n, long d, GEN F, GEN a)
     854             : {
     855          77 :   pari_sp av = avma;
     856          77 :   long nd = n*d;
     857          77 :   GEN f = RgV_pows_RgXn(mfcoefs_i(F,nd,1), itos(a));
     858          77 :   return gerepilecopy(av, c_deflate(n, d, f));
     859             : }
     860             : 
     861             : /* F * Theta */
     862             : static GEN
     863         336 : mfmultheta(GEN F)
     864             : {
     865         336 :   if (typ(mf_get_gk(F)) == t_FRAC && mf_get_type(F) == t_MF_DIV)
     866             :   {
     867         112 :     GEN T = gel(F,3); /* hopefully mfTheta() */
     868         112 :     if (mf_get_type(T) == t_MF_THETA && mf_get_N(T) == 4) return gel(F,2);
     869             :   }
     870         224 :   return mfmul(F, mfTheta(NULL));
     871             : }
     872             : 
     873             : static GEN
     874          21 : c_bracket(long n, long d, GEN F, GEN G, GEN gm)
     875             : {
     876          21 :   pari_sp av = avma;
     877          21 :   long i, nd = n*d;
     878          21 :   GEN VF = mfcoefs_i(F, nd, 1), tF = cgetg(nd+2, t_VEC);
     879          21 :   GEN VG = mfcoefs_i(G, nd, 1), tG = cgetg(nd+2, t_VEC);
     880          21 :   GEN C, mpow, res = NULL, gk = mf_get_gk(F), gl = mf_get_gk(G);
     881          21 :   ulong j, m = itou(gm);
     882             :   /* pow[i,j+1] = i^j */
     883          21 :   mpow = cgetg(m+2, t_MAT);
     884          21 :   gel(mpow,1) = const_col(nd, gen_1);
     885          49 :   for (j = 1; j <= m; j++)
     886             :   {
     887          28 :     GEN c = cgetg(nd+1, t_COL);
     888          28 :     gel(mpow,j+1) = c;
     889          28 :     for (i = 1; i <= nd; i++) gel(c,i) = muliu(gcoeff(mpow,i,j), i);
     890             :   }
     891          21 :   C = binomial(gaddgs(gk, m-1), m);
     892          21 :   if (odd(m)) C = gneg(C);
     893          70 :   for (j = 0; j <= m; j++)
     894             :   { /* C = (-1)^(m-j) binom(m+l-1, j) binom(m+k-1,m-j) */
     895             :     GEN c;
     896          49 :     gel(tF,1) = j == 0? gel(VF,1): gen_0;
     897          49 :     gel(tG,1) = j == m? gel(VG,1): gen_0;
     898          49 :     gel(tF,2) = gel(VF,2);
     899          49 :     gel(tG,2) = gel(VG,2);
     900         413 :     for (i = 2; i <= nd; i++)
     901             :     {
     902         364 :       gel(tF, i+1) = gmul(gcoeff(mpow,i,j+1),   gel(VF, i+1));
     903         364 :       gel(tG, i+1) = gmul(gcoeff(mpow,i,m-j+1), gel(VG, i+1));
     904             :     }
     905          49 :     c = gmul(C, c_deflate(n, d, RgV_mul_RgXn(tF, tG)));
     906          49 :     res = res? gadd(res, c): c;
     907          49 :     if (j < m)
     908          56 :       C = gdiv(gmul(C, gmulsg(m-j, gaddgs(gl,m-j-1))),
     909          28 :                gmulsg(-(j+1), gaddgs(gk,j)));
     910             :   }
     911          21 :   return gerepileupto(av, res);
     912             : }
     913             : /* linear combination \sum L[j] vecF[j] */
     914             : static GEN
     915        2387 : c_linear(long n, long d, GEN F, GEN L, GEN dL)
     916             : {
     917        2387 :   pari_sp av = avma;
     918        2387 :   long j, l = lg(L);
     919        2387 :   GEN S = NULL;
     920        7546 :   for (j = 1; j < l; j++)
     921             :   {
     922        5159 :     GEN c = gel(L,j);
     923        5159 :     if (gequal0(c)) continue;
     924        4578 :     c = gmul(c, mfcoefs_i(gel(F,j), n, d));
     925        4578 :     S = S? gadd(S,c): c;
     926             :   }
     927        2387 :   if (!S) return zerovec(n+1);
     928        2387 :   if (!is_pm1(dL)) S = gdiv(S, dL);
     929        2387 :   return gerepileupto(av, S);
     930             : }
     931             : 
     932             : /* B_d(T_j Trace^new) as t_MF_BD(t_MF_HECKE(t_MF_NEWTRACE)) or
     933             :  * t_MF_HECKE(t_MF_NEWTRACE)
     934             :  * or t_MF_NEWTRACE in level N. Set d and j, return t_MF_NEWTRACE component*/
     935             : static GEN
     936       67732 : bhn_parse(GEN f, long *d, long *j)
     937             : {
     938       67732 :   long t = mf_get_type(f);
     939       67732 :   *d = *j = 1;
     940       67732 :   if (t == t_MF_BD) { *d = itos(gel(f,3)); f = gel(f,2); t = mf_get_type(f); }
     941       67732 :   if (t == t_MF_HECKE) { *j = gel(f,2)[1]; f = gel(f,3); }
     942       67732 :   return f;
     943             : }
     944             : /* f as above, return the t_MF_NEWTRACE component */
     945             : static GEN
     946       21224 : bhn_newtrace(GEN f)
     947             : {
     948       21224 :   long t = mf_get_type(f);
     949       21224 :   if (t == t_MF_BD) { f = gel(f,2); t = mf_get_type(f); }
     950       21224 :   if (t == t_MF_HECKE) f = gel(f,3);
     951       21224 :   return f;
     952             : }
     953             : static int
     954        2807 : ok_bhn_linear(GEN vf)
     955             : {
     956        2807 :   long i, N0 = 0, l = lg(vf);
     957             :   GEN CHI, gk;
     958        2807 :   if (l == 1) return 1;
     959        2807 :   gk = mf_get_gk(gel(vf,1));
     960        2807 :   CHI = mf_get_CHI(gel(vf,1));
     961       16702 :   for (i = 1; i < l; i++)
     962             :   {
     963       15365 :     GEN f = bhn_newtrace(gel(vf,i));
     964       15365 :     long N = mf_get_N(f);
     965       15365 :     if (mf_get_type(f) != t_MF_NEWTRACE) return 0;
     966       13895 :     if (N < N0) return 0; /* largest level must come last */
     967       13895 :     N0 = N;
     968       13895 :     if (!gequal(gk,mf_get_gk(f))) return 0; /* same k */
     969       13895 :     if (!gequal(gel(mf_get_CHI(f),2), gel(CHI,2))) return 0; /* same CHI */
     970             :   }
     971        1337 :   return 1;
     972             : }
     973             : 
     974             : /* vF not empty, same hypotheses as bhnmat_extend */
     975             : static GEN
     976        5936 : bhnmat_extend_nocache(GEN M, long N, long n, long d, GEN vF)
     977             : {
     978             :   cachenew_t cache;
     979        5936 :   long l = lg(vF);
     980             :   GEN f;
     981        5936 :   if (l == 1) return M? M: cgetg(1, t_MAT);
     982        5859 :   f = bhn_newtrace(gel(vF,1)); /* N.B. mf_get_N(f) divides N */
     983        5859 :   init_cachenew(&cache, n*d, N, f);
     984        5859 :   M = bhnmat_extend(M, n, d, vF, &cache);
     985        5859 :   dbg_cachenew(&cache); return M;
     986             : }
     987             : /* c_linear of "bhn" mf closures, same hypotheses as bhnmat_extend */
     988             : static GEN
     989        1561 : c_linear_bhn(long n, long d, GEN F)
     990             : {
     991             :   pari_sp av;
     992        1561 :   GEN M, v, vF = gel(F,2), L = gel(F,3), dL = gel(F,4);
     993        1561 :   if (lg(L) == 1) return zerovec(n+1);
     994        1561 :   av = avma;
     995        1561 :   M = bhnmat_extend_nocache(NULL, mf_get_N(F), n, d, vF);
     996        1561 :   v = RgM_RgC_mul(M,L); settyp(v, t_VEC);
     997        1561 :   if (!is_pm1(dL)) v = gdiv(v, dL);
     998        1561 :   return gerepileupto(av, v);
     999             : }
    1000             : 
    1001             : /* c in K, K := Q[X]/(T) vz = vector of consecutive powers of root z of T
    1002             :  * attached to an embedding s: K -> C. Return s(c) in C */
    1003             : static GEN
    1004       74235 : Rg_embed1(GEN c, GEN vz)
    1005             : {
    1006       74235 :   long t = typ(c);
    1007       74235 :   if (t == t_POLMOD) { c = gel(c,2); t = typ(c); }
    1008       74235 :   if (t == t_POL) c = RgX_RgV_eval(c, vz);
    1009       74235 :   return c;
    1010             : }
    1011             : /* return s(P) in C[X] */
    1012             : static GEN
    1013         882 : RgX_embed1(GEN P, GEN vz)
    1014             : {
    1015             :   long i, l;
    1016         882 :   GEN Q = cgetg_copy(P, &l);
    1017         882 :   Q[1] = P[1];
    1018         882 :   for (i = 2; i < l; i++) gel(Q,i) = Rg_embed1(gel(P,i), vz);
    1019         882 :   return normalizepol_lg(Q,l); /* normally a no-op */
    1020             : }
    1021             : /* return s(P) in C^n */
    1022             : static GEN
    1023         567 : vecembed1(GEN P, GEN vz)
    1024             : {
    1025             :   long i, l;
    1026         567 :   GEN Q = cgetg_copy(P, &l);
    1027         567 :   for (i = 1; i < l; i++) gel(Q,i) = Rg_embed1(gel(P,i), vz);
    1028         567 :   return Q;
    1029             : }
    1030             : /* P in L = K[X]/(U), K = Q[t]/T; s an embedding of K -> C attached
    1031             :  * to a root of T, extended to an embedding of L -> C attached to a root
    1032             :  * of s(U); vT powers of the root of T, vU powers of the root of s(U).
    1033             :  * Return s(P) in C^n */
    1034             : static GEN
    1035       13314 : Rg_embed2(GEN P, long vt, GEN vT, GEN vU)
    1036             : {
    1037             :   long i, l;
    1038             :   GEN Q;
    1039       13314 :   P = liftpol_shallow(P);
    1040       13314 :   if (typ(P) != t_POL) return P;
    1041       13300 :   if (varn(P) == vt) return Rg_embed1(P, vT);
    1042             :   /* varn(P) == vx */
    1043       13293 :   Q = cgetg_copy(P, &l); Q[1] = P[1];
    1044       13293 :   for (i = 2; i < l; i++) gel(Q,i) = Rg_embed1(gel(P,i), vT);
    1045       13293 :   return Rg_embed1(Q, vU);
    1046             : }
    1047             : static GEN
    1048          42 : vecembed2(GEN P, long vt, GEN vT, GEN vU)
    1049             : {
    1050             :   long i, l;
    1051          42 :   GEN Q = cgetg_copy(P, &l);
    1052          42 :   for (i = 1; i < l; i++) gel(Q,i) = Rg_embed2(gel(P,i), vt, vT, vU);
    1053          42 :   return Q;
    1054             : }
    1055             : static GEN
    1056         532 : RgX_embed2(GEN P, long vt, GEN vT, GEN vU)
    1057             : {
    1058             :   long i, l;
    1059         532 :   GEN Q = cgetg_copy(P, &l);
    1060         532 :   for (i = 2; i < l; i++) gel(Q,i) = Rg_embed2(gel(P,i), vt, vT, vU);
    1061         532 :   Q[1] = P[1]; return normalizepol_lg(Q,l);
    1062             : }
    1063             : /* embed polynomial f in variable vx [ may be a scalar ], E from getembed */
    1064             : static GEN
    1065        1596 : RgX_embed(GEN f, long vx, GEN E)
    1066             : {
    1067             :   GEN vT;
    1068        1596 :   if (typ(f) != t_POL || varn(f) != vx) return mfembed(E, f);
    1069        1575 :   if (lg(E) == 1) return f;
    1070        1379 :   vT = gel(E,2);
    1071        1379 :   if (lg(E) == 3)
    1072         847 :     f = RgX_embed1(f, vT);
    1073             :   else
    1074         532 :     f = RgX_embed2(f, varn(gel(E,1)), vT, gel(E,3));
    1075        1379 :   return f;
    1076             : }
    1077             : /* embed vector, E from getembed */
    1078             : GEN
    1079        1407 : mfvecembed(GEN E, GEN v)
    1080             : {
    1081             :   GEN vT;
    1082        1407 :   if (lg(E) == 1) return v;
    1083         609 :   vT = gel(E,2);
    1084         609 :   if (lg(E) == 3)
    1085         567 :     v = vecembed1(v, vT);
    1086             :   else
    1087          42 :     v = vecembed2(v, varn(gel(E,1)), vT, gel(E,3));
    1088         609 :   return v;
    1089             : }
    1090             : GEN
    1091           7 : mfmatembed(GEN E, GEN f)
    1092             : {
    1093             :   long i, l;
    1094             :   GEN g;
    1095           7 :   if (lg(E) == 1) return f;
    1096           7 :   g = cgetg_copy(f, &l);
    1097           7 :   for (i = 1; i < l; i++) gel(g,i) = mfvecembed(E, gel(f,i));
    1098           7 :   return g;
    1099             : }
    1100             : /* embed vector of polynomials in var vx */
    1101             : static GEN
    1102          98 : RgXV_embed(GEN f, long vx, GEN E)
    1103             : {
    1104             :   long i, l;
    1105             :   GEN v;
    1106          98 :   if (lg(E) == 1) return f;
    1107          70 :   v = cgetg_copy(f, &l);
    1108          70 :   for (i = 1; i < l; i++) gel(v,i) = RgX_embed(gel(f,i), vx, E);
    1109          70 :   return v;
    1110             : }
    1111             : 
    1112             : /* embed scalar */
    1113             : GEN
    1114       95927 : mfembed(GEN E, GEN f)
    1115             : {
    1116             :   GEN vT;
    1117       95927 :   if (lg(E) == 1) return f;
    1118       13468 :   vT = gel(E,2);
    1119       13468 :   if (lg(E) == 3)
    1120        4354 :     f = Rg_embed1(f, vT);
    1121             :   else
    1122        9114 :     f = Rg_embed2(f, varn(gel(E,1)), vT, gel(E,3));
    1123       13468 :   return f;
    1124             : }
    1125             : /* vector of the sigma(f), sigma in vE */
    1126             : static GEN
    1127         294 : RgX_embedall(GEN f, long vx, GEN vE)
    1128             : {
    1129         294 :   long i, l = lg(vE);
    1130         294 :   GEN v = cgetg(l, t_VEC);
    1131         294 :   for (i = 1; i < l; i++) gel(v,i) = RgX_embed(f, vx, gel(vE,i));
    1132         294 :   return l == 2? gel(v,1): v;
    1133             : }
    1134             : /* matrix whose colums are the sigma(v), sigma in vE */
    1135             : static GEN
    1136         329 : RgC_embedall(GEN v, GEN vE)
    1137             : {
    1138         329 :   long j, l = lg(vE);
    1139         329 :   GEN M = cgetg(l, t_MAT);
    1140         329 :   for (j = 1; j < l; j++) gel(M,j) = mfvecembed(gel(vE,j), v);
    1141         329 :   return M;
    1142             : }
    1143             : /* vector of the sigma(v), sigma in vE */
    1144             : static GEN
    1145        4907 : Rg_embedall_i(GEN v, GEN vE)
    1146             : {
    1147        4907 :   long j, l = lg(vE);
    1148        4907 :   GEN M = cgetg(l, t_VEC);
    1149        4907 :   for (j = 1; j < l; j++) gel(M,j) = mfembed(gel(vE,j), v);
    1150        4907 :   return M;
    1151             : }
    1152             : /* vector of the sigma(v), sigma in vE; if #vE == 1, return v */
    1153             : static GEN
    1154       90446 : Rg_embedall(GEN v, GEN vE)
    1155       90446 : { return (lg(vE) == 2)? mfembed(gel(vE,1), v): Rg_embedall_i(v, vE); }
    1156             : 
    1157             : static GEN
    1158         777 : c_div_i(long n, GEN F, GEN G)
    1159             : {
    1160             :   GEN VF, VG, a0, a0i, H;
    1161         777 :   VF = mfcoefsser(F, n); VG = mfcoefsser(G, n);
    1162         777 :   a0 = polcoef_i(VG, 0, -1);
    1163         777 :   if (gequal0(a0) || gequal1(a0)) a0 = a0i = NULL;
    1164             :   else
    1165             :   {
    1166         602 :     a0i = ginv(a0);
    1167         602 :     VG = gmul(ser_unscale(VG,a0), a0i);
    1168         602 :     VF = gmul(ser_unscale(VF,a0), a0i);
    1169             :   }
    1170         777 :   H = gdiv(VF, VG);
    1171         777 :   if (a0) H = ser_unscale(H,a0i);
    1172         777 :   return sertovecslice(H, n);
    1173             : }
    1174             : static GEN
    1175         777 : c_div(long n, long d, GEN F, GEN G)
    1176             : {
    1177         777 :   pari_sp av = avma;
    1178         777 :   GEN D = (d==1)? c_div_i(n, F,G): c_deflate(n, d, c_div_i(n*d, F,G));
    1179         777 :   return gerepilecopy(av, D);
    1180             : }
    1181             : 
    1182             : static GEN
    1183          35 : c_shift(long n, long d, GEN F, GEN gsh)
    1184             : {
    1185          35 :   pari_sp av = avma;
    1186             :   GEN vF;
    1187          35 :   long sh = itos(gsh), n1 = n*d + sh;
    1188          35 :   if (n1 < 0) return zerovec(n+1);
    1189          35 :   vF = mfcoefs_i(F, n1, 1);
    1190          35 :   if (sh < 0) vF = shallowconcat(zerovec(-sh), vF);
    1191          35 :   else vF = vecslice(vF, sh+1, n1+1);
    1192          35 :   return gerepilecopy(av, c_deflate(n, d, vF));
    1193             : }
    1194             : 
    1195             : static GEN
    1196          21 : c_deriv(long n, long d, GEN F, GEN gm)
    1197             : {
    1198          21 :   pari_sp av = avma;
    1199          21 :   GEN V = mfcoefs_i(F, n, d), res;
    1200          21 :   long i, m = itos(gm);
    1201          21 :   if (!m) return V;
    1202          21 :   res = cgetg(n+2, t_VEC); gel(res,1) = gen_0;
    1203          21 :   if (m < 0)
    1204           7 :   { for (i=1; i <= n; i++) gel(res, i+1) = gdiv(gel(V, i+1), powuu(i,-m)); }
    1205             :   else
    1206          14 :   { for (i=1; i <= n; i++) gel(res, i+1) = gmul(gel(V,i+1), powuu(i,m)); }
    1207          21 :   return gerepileupto(av, res);
    1208             : }
    1209             : 
    1210             : static GEN
    1211          14 : c_derivE2(long n, long d, GEN F, GEN gm)
    1212             : {
    1213          14 :   pari_sp av = avma;
    1214             :   GEN VF, VE, res, tmp, gk;
    1215          14 :   long i, m = itos(gm), nd;
    1216          14 :   if (m == 0) return mfcoefs_i(F, n, d);
    1217          14 :   nd = n*d;
    1218          14 :   VF = mfcoefs_i(F, nd, 1); VE = mfcoefs_i(mfEk(2), nd, 1);
    1219          14 :   gk = mf_get_gk(F);
    1220          14 :   if (m == 1)
    1221             :   {
    1222           7 :     res = cgetg(n+2, t_VEC);
    1223           7 :     for (i = 0; i <= n; i++) gel(res, i+1) = gmulsg(i, gel(VF, i*d+1));
    1224           7 :     tmp = c_deflate(n, d, RgV_mul_RgXn(VF, VE));
    1225           7 :     return gerepileupto(av, gsub(res, gmul(gdivgs(gk, 12), tmp)));
    1226             :   }
    1227             :   else
    1228             :   {
    1229             :     long j;
    1230          35 :     for (j = 1; j <= m; j++)
    1231             :     {
    1232          28 :       tmp = RgV_mul_RgXn(VF, VE);
    1233          28 :       for (i = 0; i <= nd; i++) gel(VF, i+1) = gmulsg(i, gel(VF, i+1));
    1234          28 :       VF = gsub(VF, gmul(gdivgs(gaddgs(gk, 2*(j-1)), 12), tmp));
    1235             :     }
    1236           7 :     return gerepilecopy(av, c_deflate(n, d, VF));
    1237             :   }
    1238             : }
    1239             : 
    1240             : /* Twist by the character (D/.) */
    1241             : static GEN
    1242           7 : c_twist(long n, long d, GEN F, GEN D)
    1243             : {
    1244           7 :   pari_sp av = avma;
    1245           7 :   GEN V = mfcoefs_i(F, n, d), res = cgetg(n+2, t_VEC);
    1246             :   long i;
    1247         119 :   for (i = 0; i <= n; i++)
    1248         112 :     gel(res, i + 1) = gmulsg(krois(D, i), gel(V, i+1));
    1249           7 :   return gerepileupto(av, res);
    1250             : }
    1251             : 
    1252             : /* form F given by closure, compute T(n)(F) as closure */
    1253             : static GEN
    1254         434 : c_hecke(long m, long l, GEN DATA, GEN F)
    1255             : {
    1256         434 :   pari_sp av = avma;
    1257         434 :   return gerepilecopy(av, hecke_i(m, l, NULL, F, DATA));
    1258             : }
    1259             : static GEN
    1260         140 : c_const(long n, long d, GEN C)
    1261             : {
    1262         140 :   GEN V = zerovec(n+1);
    1263         140 :   long i, j, l = lg(C);
    1264         140 :   if (l > d*n+2) l = d*n+2;
    1265         140 :   for (i = j = 1; i < l; i+=d, j++) gel(V, j) = gcopy(gel(C,i));
    1266         140 :   return V;
    1267             : }
    1268             : 
    1269             : /* m > 0 */
    1270             : static GEN
    1271         455 : eta3_ZXn(long m)
    1272             : {
    1273         455 :   long l = m+2, n, k;
    1274         455 :   GEN P = cgetg(l,t_POL);
    1275         455 :   P[1] = evalsigne(1)|evalvarn(0);
    1276         455 :   for (n = 2; n < l; n++) gel(P,n) = gen_0;
    1277        2471 :   for (n = k = 0;; n++)
    1278             :   {
    1279        4487 :     if (k + n >= m) { setlg(P, k+3); return P; }
    1280        2016 :     k += n;
    1281             :     /* now k = n(n+1) / 2 */
    1282        2016 :     gel(P, k+2) = odd(n)? utoineg(2*n+1): utoipos(2*n+1);
    1283             :   }
    1284             : }
    1285             : 
    1286             : static GEN
    1287         462 : c_delta(long n, long d)
    1288             : {
    1289         462 :   pari_sp ltop = avma;
    1290         462 :   long N = n*d;
    1291             :   GEN e;
    1292         462 :   if (!N) return mkvec(gen_0);
    1293         455 :   e = eta3_ZXn(N);
    1294         455 :   e = ZXn_sqr(e,N);
    1295         455 :   e = ZXn_sqr(e,N);
    1296         455 :   e = ZXn_sqr(e,N); /* eta(x)^24 */
    1297         455 :   settyp(e, t_VEC);
    1298         455 :   gel(e,1) = gen_0; /* Delta(x) = x*eta(x)^24 as a t_VEC */
    1299         455 :   return gerepilecopy(ltop, c_deflate(n, d, e));
    1300             : }
    1301             : 
    1302             : /* return s(d) such that s|f <=> d | f^2 */
    1303             : static long
    1304          42 : mysqrtu(ulong d)
    1305             : {
    1306          42 :   GEN fa = myfactoru(d), P = gel(fa,1), E = gel(fa,2);
    1307          42 :   long l = lg(P), i, s = 1;
    1308          42 :   for (i = 1; i < l; i++) s *= upowuu(P[i], (E[i]+1)>>1);
    1309          42 :   return s;
    1310             : }
    1311             : static GEN
    1312        1428 : c_theta(long n, long d, GEN psi)
    1313             : {
    1314        1428 :   long lim = usqrt(n*d), F = mfcharmodulus(psi), par = mfcharparity(psi);
    1315        1428 :   long f, d2 = d == 1? 1: mysqrtu(d);
    1316        1428 :   GEN V = zerovec(n + 1);
    1317        5558 :   for (f = d2; f <= lim; f += d2)
    1318        4130 :     if (ugcd(F, f) == 1)
    1319             :     {
    1320        4123 :       pari_sp av = avma;
    1321        4123 :       GEN c = mfchareval_i(psi, f);
    1322        4123 :       gel(V, f*f/d + 1) = gerepileupto(av, par < 0 ? gmulgs(c,2*f) : gmul2n(c,1));
    1323             :     }
    1324        1428 :   if (F == 1) gel(V, 1) = gen_1;
    1325        1428 :   return V; /* no gerepile needed */
    1326             : }
    1327             : 
    1328             : static GEN
    1329         133 : c_etaquo(long n, long d, GEN eta, GEN gs)
    1330             : {
    1331         133 :   pari_sp av = avma;
    1332         133 :   long s = itos(gs), nd = n*d, nds = nd - s + 1;
    1333             :   GEN c;
    1334         133 :   if (nds <= 0) return zerovec(n+1);
    1335         112 :   c = RgX_to_RgC(eta_product_ZXn(eta, nds), nds); settyp(c, t_VEC);
    1336         112 :   if (s > 0) c = shallowconcat(zerovec(s), c);
    1337         112 :   return gerepilecopy(av, c_deflate(n, d, c));
    1338             : }
    1339             : 
    1340             : static GEN
    1341          63 : c_ell(long n, long d, GEN E)
    1342             : {
    1343          63 :   pari_sp av = avma;
    1344             :   GEN v;
    1345          63 :   if (d == 1) return concat(gen_0, anell(E, n));
    1346           7 :   v = shallowconcat(gen_0, anell(E, n*d));
    1347           7 :   return gerepilecopy(av, c_deflate(n, d, v));
    1348             : }
    1349             : 
    1350             : static GEN
    1351          21 : c_cusptrace(long n, long d, GEN F)
    1352             : {
    1353          21 :   pari_sp av = avma;
    1354          21 :   GEN D = gel(F,2), res = cgetg(n+2, t_VEC);
    1355          21 :   long i, N = mf_get_N(F), k = mf_get_k(F);
    1356          21 :   gel(res, 1) = gen_0;
    1357         140 :   for (i = 1; i <= n; i++)
    1358         119 :     gel(res, i+1) = mfcusptrace_i(N, k, i*d, mydivisorsu(i*d), D);
    1359          21 :   return gerepilecopy(av, res);
    1360             : }
    1361             : 
    1362             : static GEN
    1363         749 : c_newtrace(long n, long d, GEN F)
    1364             : {
    1365         749 :   pari_sp av = avma;
    1366             :   cachenew_t cache;
    1367         749 :   long N = mf_get_N(F);
    1368             :   GEN v;
    1369         749 :   init_cachenew(&cache, n*d, N, F);
    1370         749 :   v = colnewtrace(0, n, d, N, mf_get_k(F), &cache);
    1371         749 :   settyp(v, t_VEC); return gerepilecopy(av, v);
    1372             : }
    1373             : 
    1374             : static GEN
    1375        3864 : c_Bd(long n, long d, GEN F, GEN A)
    1376             : {
    1377        3864 :   pari_sp av = avma;
    1378        3864 :   long a = itou(A), ad = ugcd(a,d), aad = a/ad, i, j;
    1379        3864 :   GEN w, v = mfcoefs_i(F, n/aad, d/ad);
    1380        3864 :   if (a == 1) return v;
    1381        3864 :   n++; w = zerovec(n);
    1382        3864 :   for (i = j = 1; j <= n; i++, j += aad) gel(w,j) = gcopy(gel(v,i));
    1383        3864 :   return gerepileupto(av, w);
    1384             : }
    1385             : 
    1386             : static GEN
    1387        3591 : c_dihedral(long n, long d, GEN bnr, GEN w, GEN k0j)
    1388             : {
    1389        3591 :   pari_sp av = avma;
    1390        3591 :   GEN V = dihan(bnr, w, k0j, n*d);
    1391        3591 :   GEN Tinit = gel(w,3), Pm = gel(Tinit,1);
    1392        3591 :   GEN A = c_deflate(n, d, V);
    1393        3591 :   if (degpol(Pm) == 1 || RgV_is_ZV(A)) return gerepilecopy(av, A);
    1394         749 :   return gerepileupto(av, gmodulo(A, Pm));
    1395             : }
    1396             : 
    1397             : static GEN
    1398         140 : c_mfEH(long n, long d, GEN F)
    1399             : {
    1400         140 :   pari_sp av = avma;
    1401             :   GEN v, M, A;
    1402         140 :   long i, r = mf_get_r(F);
    1403         140 :   if (n == 1)
    1404          14 :     return gerepilecopy(av, mkvec2(mfEHcoef(r,0),mfEHcoef(r,d)));
    1405             :   /* speedup mfcoef */
    1406         126 :   if (r == 1)
    1407             :   {
    1408          70 :     v = cgetg(n+2, t_VEC);
    1409          70 :     gel(v,1) = sstoQ(-1,12);
    1410       83258 :     for (i = 1; i <= n; i++)
    1411             :     {
    1412       83188 :       long id = i*d, a = id & 3;
    1413       83188 :       gel(v,i+1) = (a==1 || a==2)? gen_0: sstoQ(hclassno6u(id), 6);
    1414             :     }
    1415          70 :     return v; /* no gerepile needed */
    1416             :   }
    1417          56 :   M = mfEHmat(n*d+1,r);
    1418          56 :   if (d > 1)
    1419             :   {
    1420           7 :     long l = lg(M);
    1421           7 :     for (i = 1; i < l; i++) gel(M,i) = c_deflate(n, d, gel(M,i));
    1422             :   }
    1423          56 :   A = gel(F,2); /* [num(B), den(B)] */
    1424          56 :   v = RgC_Rg_div(RgM_RgC_mul(M, gel(A,1)), gel(A,2));
    1425          56 :   settyp(v,t_VEC); return gerepileupto(av, v);
    1426             : }
    1427             : 
    1428             : static GEN
    1429        6664 : c_mfeisen(long n, long d, GEN F)
    1430             : {
    1431        6664 :   pari_sp av = avma;
    1432        6664 :   GEN v, vchi, E0, P, T, CHI, gk = mf_get_gk(F);
    1433             :   long i, k;
    1434        6664 :   if (typ(gk) != t_INT) return c_mfEH(n, d, F);
    1435        6524 :   k = itou(gk);
    1436        6524 :   vchi = gel(F,2);
    1437        6524 :   E0 = gel(vchi,1);
    1438        6524 :   T = gel(vchi,2);
    1439        6524 :   P = gel(T,1);
    1440        6524 :   CHI = gel(vchi,3);
    1441        6524 :   v = cgetg(n+2, t_VEC);
    1442        6524 :   gel(v, 1) = gcopy(E0); /* E(0) */
    1443        6524 :   if (lg(vchi) == 5)
    1444             :   { /* E_k(chi1,chi2) */
    1445        4753 :     GEN CHI2 = gel(vchi,4), F3 = gel(F,3);
    1446        4753 :     long ord = F3[1], j = F3[2];
    1447        4753 :     for (i = 1; i <= n; i++) gel(v, i+1) = sigchi2(k, CHI, CHI2, i*d, ord);
    1448        4753 :     if (lg(T) == 4) v = QabV_tracerel(T, j, v);
    1449             :   }
    1450             :   else
    1451             :   { /* E_k(chi) */
    1452        1771 :     for (i = 1; i <= n; i++) gel(v, i+1) = sigchi(k, CHI, i*d);
    1453             :   }
    1454        6524 :   if (degpol(P) != 1 && !RgV_is_QV(v)) return gerepileupto(av, gmodulo(v, P));
    1455        4942 :   return gerepilecopy(av, v);
    1456             : }
    1457             : 
    1458             : /* L(chi_D, 1-k) */
    1459             : static GEN
    1460          28 : lfunquadneg(long D, long k)
    1461             : {
    1462          28 :   GEN B, dS, S = gen_0;
    1463          28 :   long r, N = labs(D);
    1464             :   pari_sp av;
    1465          28 :   if (k == 1 && N == 1) return gneg(ghalf);
    1466             :   /* B = N^k * denom(B) * B(x/N) */
    1467          28 :   B = ZX_rescale(Q_remove_denom(bernpol(k, 0), &dS), utoi(N));
    1468          28 :   dS = mul_denom(dS, stoi(-N*k));
    1469          28 :   av = avma;
    1470        7175 :   for (r = 0; r < N; r++)
    1471             :   {
    1472        7147 :     long c = kross(D, r);
    1473        7147 :     if (c)
    1474             :     {
    1475        5152 :       GEN tmp = poleval(B, utoi(r));
    1476        5152 :       S = c > 0 ? addii(S, tmp) : subii(S, tmp);
    1477        5152 :       S = gerepileuptoint(av, S);
    1478             :     }
    1479             :   }
    1480          28 :   return gdiv(S, dS);
    1481             : }
    1482             : 
    1483             : /* Returns vector of coeffs from F[0], F[d], ..., F[d*n] */
    1484             : static GEN
    1485       23562 : mfcoefs_i(GEN F, long n, long d)
    1486             : {
    1487       23562 :   if (n < 0) return gen_0;
    1488       23562 :   switch(mf_get_type(F))
    1489             :   {
    1490         140 :     case t_MF_CONST: return c_const(n, d, gel(F,2));
    1491        6664 :     case t_MF_EISEN: return c_mfeisen(n, d, F);
    1492         658 :     case t_MF_Ek: return c_Ek(n, d, F);
    1493         462 :     case t_MF_DELTA: return c_delta(n, d);
    1494        1365 :     case t_MF_THETA: return c_theta(n, d, gel(F,2));
    1495         133 :     case t_MF_ETAQUO: return c_etaquo(n, d, gel(F,2), gel(F,3));
    1496          63 :     case t_MF_ELL: return c_ell(n, d, gel(F,2));
    1497         518 :     case t_MF_MUL: return c_mul(n, d, gel(F,2), gel(F,3));
    1498          77 :     case t_MF_POW: return c_pow(n, d, gel(F,2), gel(F,3));
    1499          21 :     case t_MF_BRACKET: return c_bracket(n, d, gel(F,2), gel(F,3), gel(F,4));
    1500        2387 :     case t_MF_LINEAR: return c_linear(n, d, gel(F,2), gel(F,3), gel(F,4));
    1501        1561 :     case t_MF_LINEAR_BHN: return c_linear_bhn(n, d, F);
    1502         777 :     case t_MF_DIV: return c_div(n, d, gel(F,2), gel(F,3));
    1503          35 :     case t_MF_SHIFT: return c_shift(n, d, gel(F,2), gel(F,3));
    1504          21 :     case t_MF_DERIV: return c_deriv(n, d, gel(F,2), gel(F,3));
    1505          14 :     case t_MF_DERIVE2: return c_derivE2(n, d, gel(F,2), gel(F,3));
    1506           7 :     case t_MF_TWIST: return c_twist(n, d, gel(F,2), gel(F,3));
    1507         434 :     case t_MF_HECKE: return c_hecke(n, d, gel(F,2), gel(F,3));
    1508        3864 :     case t_MF_BD: return c_Bd(n, d, gel(F,2), gel(F,3));
    1509          21 :     case t_MF_TRACE: return c_cusptrace(n, d, F);
    1510         749 :     case t_MF_NEWTRACE: return c_newtrace(n, d, F);
    1511        3591 :     case t_MF_DIHEDRAL: return c_dihedral(n, d, gel(F,2), gel(F,3), gel(F,4));
    1512             :     default: pari_err_TYPE("mfcoefs",F); return NULL;/*LCOV_EXCL_LINE*/
    1513             :   }
    1514             : }
    1515             : 
    1516             : static GEN
    1517         308 : matdeflate(long n, long d, GEN M)
    1518             : {
    1519             :   long i, l;
    1520             :   GEN A;
    1521             :   /*  if (d == 1) return M; */
    1522         308 :   A = cgetg_copy(M,&l);
    1523         308 :   for (i = 1; i < l; i++) gel(A,i) = c_deflate(n,d,gel(M,i));
    1524         308 :   return A;
    1525             : }
    1526             : static int
    1527        5355 : space_is_cusp(long space) { return space != mf_FULL && space != mf_EISEN; }
    1528             : /* safe with flraw mf */
    1529             : static GEN
    1530        2156 : mfcoefs_mf(GEN mf, long n, long d)
    1531             : {
    1532        2156 :   GEN MS, ME, E = MF_get_E(mf), S = MF_get_S(mf), M = MF_get_M(mf);
    1533        2156 :   long lE = lg(E), lS = lg(S), l = lE+lS-1;
    1534             : 
    1535        2156 :   if (l == 1) return cgetg(1, t_MAT);
    1536        2044 :   if (typ(M) == t_MAT && lg(M) != 1 && (n+1)*d < nbrows(M))
    1537          21 :     return matdeflate(n, d, M); /*cached; lg = 1 is possible from mfinit */
    1538        2023 :   ME = (lE == 1)? cgetg(1, t_MAT): mfvectomat(E, n, d);
    1539        2023 :   if (lS == 1)
    1540         357 :     MS = cgetg(1, t_MAT);
    1541        1666 :   else if (mf_get_type(gel(S,1)) == t_MF_DIV) /*k 1/2-integer or k=1 (exotic)*/
    1542         287 :     MS = matdeflate(n,d, mflineardivtomat(MF_get_N(mf), S, n*d));
    1543        1379 :   else if (MF_get_k(mf) == 1) /* k = 1 (dihedral) */
    1544             :   {
    1545         140 :     GEN M = mfvectomat(gmael(S,1,2), n, d);
    1546             :     long i;
    1547         140 :     MS = cgetg(lS, t_MAT);
    1548         448 :     for (i = 1; i < lS; i++)
    1549             :     {
    1550         308 :       GEN f = gel(S,i), dc = gel(f,4), c = RgM_RgC_mul(M, gel(f,3));
    1551         308 :       if (!equali1(dc)) c = RgC_Rg_div(c,dc);
    1552         308 :       gel(MS,i) = c;
    1553             :     }
    1554             :   }
    1555             :   else /* k >= 2 integer */
    1556        1239 :     MS = bhnmat_extend_nocache(NULL, MF_get_N(mf), n, d, S);
    1557        2023 :   return shallowconcat(ME,MS);
    1558             : }
    1559             : GEN
    1560        3255 : mfcoefs(GEN F, long n, long d)
    1561             : {
    1562        3255 :   if (!checkmf_i(F))
    1563             :   {
    1564          42 :     pari_sp av = avma;
    1565          42 :     GEN mf = checkMF_i(F); if (!mf) pari_err_TYPE("mfcoefs", F);
    1566          42 :     return gerepilecopy(av, mfcoefs_mf(mf,n,d));
    1567             :   }
    1568        3213 :   if (d <= 0) pari_err_DOMAIN("mfcoefs", "d", "<=", gen_0, stoi(d));
    1569        3213 :   if (n < 0) return cgetg(1, t_VEC);
    1570        3213 :   return mfcoefs_i(F, n, d);
    1571             : }
    1572             : 
    1573             : /* assume k >= 0 */
    1574             : static GEN
    1575         210 : mfak_i(GEN F, long k)
    1576             : {
    1577         210 :   if (!k) return gel(mfcoefs_i(F,0,1), 1);
    1578         154 :   return gel(mfcoefs_i(F,1,k), 2);
    1579             : }
    1580             : GEN
    1581          70 : mfcoef(GEN F, long n)
    1582             : {
    1583          70 :   pari_sp av = avma;
    1584          70 :   if (!checkmf_i(F)) pari_err_TYPE("mfcoef",F);
    1585          70 :   return n < 0? gen_0: gerepilecopy(av, mfak_i(F, n));
    1586             : }
    1587             : 
    1588             : static GEN
    1589         112 : paramconst() { return tagparams(t_MF_CONST, mkNK(1,0,mfchartrivial())); }
    1590             : static GEN
    1591          70 : mftrivial(void) { retmkvec2(paramconst(), cgetg(1,t_VEC)); }
    1592             : static GEN
    1593          42 : mf1(void) { retmkvec2(paramconst(), mkvec(gen_1)); }
    1594             : 
    1595             : /* induce mfchar CHI to G */
    1596             : static GEN
    1597      307055 : induce(GEN G, GEN CHI)
    1598             : {
    1599             :   GEN o, chi;
    1600      307055 :   if (typ(CHI) == t_INT) /* Kronecker */
    1601             :   {
    1602      300860 :     chi = znchar_quad(G, CHI);
    1603      300860 :     o = ZV_equal0(chi)? gen_1: gen_2;
    1604      300860 :     CHI = mkvec4(G,chi,o,cgetg(1,t_VEC));
    1605             :   }
    1606             :   else
    1607             :   {
    1608        6195 :     if (mfcharmodulus(CHI) == itos(znstar_get_N(G))) return CHI;
    1609        5796 :     CHI = leafcopy(CHI);
    1610        5796 :     chi = zncharinduce(gel(CHI,1), gel(CHI,2), G);
    1611        5796 :     gel(CHI,1) = G;
    1612        5796 :     gel(CHI,2) = chi;
    1613             :   }
    1614      306656 :   return CHI;
    1615             : }
    1616             : /* induce mfchar CHI to znstar(G) */
    1617             : static GEN
    1618       42441 : induceN(long N, GEN CHI)
    1619             : {
    1620       42441 :   if (mfcharmodulus(CHI) != N) CHI = induce(znstar0(utoipos(N),1), CHI);
    1621       42441 :   return CHI;
    1622             : }
    1623             : /* *pCHI1 and *pCHI2 are mfchar, induce to common modulus */
    1624             : static void
    1625        5691 : char2(GEN *pCHI1, GEN *pCHI2)
    1626             : {
    1627        5691 :   GEN CHI1 = *pCHI1, G1 = gel(CHI1,1), N1 = znstar_get_N(G1);
    1628        5691 :   GEN CHI2 = *pCHI2, G2 = gel(CHI2,1), N2 = znstar_get_N(G2);
    1629        5691 :   if (!equalii(N1,N2))
    1630             :   {
    1631        4284 :     GEN G, d = gcdii(N1,N2);
    1632        4284 :     if      (equalii(N2,d)) *pCHI2 = induce(G1, CHI2);
    1633        1701 :     else if (equalii(N1,d)) *pCHI1 = induce(G2, CHI1);
    1634             :     else
    1635             :     {
    1636         154 :       if (!equali1(d)) N2 = diviiexact(N2,d);
    1637         154 :       G = znstar0(mulii(N1,N2), 1);
    1638         154 :       *pCHI1 = induce(G, CHI1);
    1639         154 :       *pCHI2 = induce(G, CHI2);
    1640             :     }
    1641             :   }
    1642        5691 : }
    1643             : /* mfchar or charinit wrt same modulus; outputs a mfchar */
    1644             : static GEN
    1645      301875 : mfcharmul_i(GEN CHI1, GEN CHI2)
    1646             : {
    1647      301875 :   GEN G = gel(CHI1,1), chi3 = zncharmul(G, gel(CHI1,2), gel(CHI2,2));
    1648      301875 :   return mfcharGL(G, chi3);
    1649             : }
    1650             : /* mfchar or charinit; outputs a mfchar */
    1651             : static GEN
    1652        1022 : mfcharmul(GEN CHI1, GEN CHI2)
    1653             : {
    1654        1022 :   char2(&CHI1, &CHI2); return mfcharmul_i(CHI1,CHI2);
    1655             : }
    1656             : /* mfchar or charinit; outputs a mfchar */
    1657             : static GEN
    1658         105 : mfcharpow(GEN CHI, GEN n)
    1659             : {
    1660             :   GEN G, chi;
    1661         105 :   G = gel(CHI,1); chi = zncharpow(G, gel(CHI,2), n);
    1662         105 :   return mfcharGL(G, chi);
    1663             : }
    1664             : /* mfchar or charinit wrt same modulus; outputs a mfchar */
    1665             : static GEN
    1666        4669 : mfchardiv_i(GEN CHI1, GEN CHI2)
    1667             : {
    1668        4669 :   GEN G = gel(CHI1,1), chi3 = znchardiv(G, gel(CHI1,2), gel(CHI2,2));
    1669        4669 :   return mfcharGL(G, chi3);
    1670             : }
    1671             : /* mfchar or charinit; outputs a mfchar */
    1672             : static GEN
    1673        4669 : mfchardiv(GEN CHI1, GEN CHI2)
    1674             : {
    1675        4669 :   char2(&CHI1, &CHI2); return mfchardiv_i(CHI1,CHI2);
    1676             : }
    1677             : static GEN
    1678          28 : mfcharconj(GEN CHI)
    1679             : {
    1680          28 :   CHI = leafcopy(CHI);
    1681          28 :   gel(CHI,2) = zncharconj(gel(CHI,1), gel(CHI,2));
    1682          28 :   return CHI;
    1683             : }
    1684             : 
    1685             : /* CHI mfchar, assume 4 | N. Multiply CHI by \chi_{-4} */
    1686             : static GEN
    1687        1071 : mfchilift(GEN CHI, long N)
    1688             : {
    1689        1071 :   CHI = induceN(N, CHI);
    1690        1071 :   return mfcharmul_i(CHI, induce(gel(CHI,1), stoi(-4)));
    1691             : }
    1692             : /* CHI defined mod N, N4 = N/4;
    1693             :  * if CHI is defined mod N4 return CHI;
    1694             :  * else if CHI' = CHI*(-4,.) is defined mod N4, return CHI' (primitive)
    1695             :  * else return NULL */
    1696             : static GEN
    1697          70 : mfcharchiliftprim(GEN CHI, long N4)
    1698             : {
    1699          70 :   long FC = mfcharconductor(CHI);
    1700          70 :   if (N4 % FC == 0) return CHI;
    1701          14 :   CHI = mfchilift(CHI, N4 << 2);
    1702          14 :   CHI = mfchartoprimitive(CHI, &FC);
    1703          14 :   return (N4 % FC == 0)? CHI: NULL;
    1704             : }
    1705             : static GEN
    1706        2632 : mfchiadjust(GEN CHI, GEN gk, long N)
    1707             : {
    1708        2632 :   long par = mfcharparity(CHI);
    1709        2632 :   if (typ(gk) == t_INT &&  mpodd(gk)) par = -par;
    1710        2632 :   return par == 1 ? CHI : mfchilift(CHI, N);
    1711             : }
    1712             : 
    1713             : static GEN
    1714        3437 : mfsamefield(GEN P, GEN Q)
    1715             : {
    1716        3437 :   if (degpol(P) == 1) return Q;
    1717         455 :   if (degpol(Q) == 1) return P;
    1718         427 :   if (!gequal(P,Q)) pari_err_TYPE("mfsamefield [different fields]",mkvec2(P,Q));
    1719         420 :   return P;
    1720             : }
    1721             : 
    1722             : GEN
    1723         336 : mfmul(GEN f, GEN g)
    1724             : {
    1725         336 :   pari_sp av = avma;
    1726             :   GEN N, K, NK, CHI;
    1727         336 :   if (!checkmf_i(f)) pari_err_TYPE("mfmul",f);
    1728         336 :   if (!checkmf_i(g)) pari_err_TYPE("mfmul",g);
    1729         336 :   N = lcmii(mf_get_gN(f), mf_get_gN(g));
    1730         336 :   K = gadd(mf_get_gk(f), mf_get_gk(g));
    1731         336 :   CHI = mfcharmul(mf_get_CHI(f), mf_get_CHI(g));
    1732         336 :   CHI = mfchiadjust(CHI, K, itos(N));
    1733         336 :   NK = mkgNK(N, K, CHI, mfsamefield(mf_get_field(f), mf_get_field(g)));
    1734         329 :   return gerepilecopy(av, tag2(t_MF_MUL, NK, f, g));
    1735             : }
    1736             : GEN
    1737          49 : mfpow(GEN f, long n)
    1738             : {
    1739          49 :   pari_sp av = avma;
    1740             :   GEN KK, NK, gn, CHI;
    1741          49 :   if (!checkmf_i(f)) pari_err_TYPE("mfpow",f);
    1742          49 :   if (!n) return mf1();
    1743          49 :   if (n == 1) return gcopy(f);
    1744          49 :   KK = gmulsg(n,mf_get_gk(f));
    1745          49 :   gn = stoi(n);
    1746          49 :   CHI = mfcharpow(mf_get_CHI(f), gn);
    1747          49 :   CHI = mfchiadjust(CHI, KK, mf_get_N(f));
    1748          49 :   NK = mkgNK(mf_get_gN(f), KK, CHI, mf_get_field(f));
    1749          49 :   return gerepilecopy(av, tag2(t_MF_POW, NK, f, gn));
    1750             : }
    1751             : GEN
    1752          21 : mfbracket(GEN f, GEN g, long m)
    1753             : {
    1754          21 :   pari_sp av = avma;
    1755             :   GEN N, K, NK, CHI;
    1756          21 :   if (!checkmf_i(f)) pari_err_TYPE("mfbracket",f);
    1757          21 :   if (!checkmf_i(g)) pari_err_TYPE("mfbracket",g);
    1758          21 :   if (m < 0) pari_err_TYPE("mfbracket [m<0]",stoi(m));
    1759          21 :   K = gaddgs(gadd(mf_get_gk(f), mf_get_gk(g)), 2*m);
    1760          21 :   if (gsigne(K) < 0) pari_err_IMPL("mfbracket for this form");
    1761          21 :   N = lcmii(mf_get_gN(f), mf_get_gN(g));
    1762          21 :   CHI = mfcharmul(mf_get_CHI(f), mf_get_CHI(g));
    1763          21 :   CHI = mfchiadjust(CHI, K, itou(N));
    1764          21 :   NK = mkgNK(N, K, CHI, mfsamefield(mf_get_field(f), mf_get_field(g)));
    1765          21 :   return gerepilecopy(av, tag3(t_MF_BRACKET, NK, f, g, utoi(m)));
    1766             : }
    1767             : 
    1768             : /* remove 0 entries in L */
    1769             : static int
    1770        1078 : mflinear_strip(GEN *pF, GEN *pL)
    1771             : {
    1772        1078 :   pari_sp av = avma;
    1773        1078 :   GEN F = *pF, L = *pL;
    1774        1078 :   long i, j, l = lg(L);
    1775        1078 :   GEN F2 = cgetg(l, t_VEC), L2 = cgetg(l, t_VEC);
    1776        6545 :   for (i = j = 1; i < l; i++)
    1777             :   {
    1778        5467 :     if (gequal0(gel(L,i))) continue;
    1779        3031 :     gel(F2,j) = gel(F,i);
    1780        3031 :     gel(L2,j) = gel(L,i); j++;
    1781             :   }
    1782        1078 :   if (j == l) set_avma(av);
    1783             :   else
    1784             :   {
    1785         280 :     setlg(F2,j); *pF = F2;
    1786         280 :     setlg(L2,j); *pL = L2;
    1787             :   }
    1788        1078 :   return (j > 1);
    1789             : }
    1790             : static GEN
    1791        4536 : taglinear_i(long t, GEN NK, GEN F, GEN L)
    1792             : {
    1793             :   GEN dL;
    1794        4536 :   L = Q_remove_denom(L, &dL); if (!dL) dL = gen_1;
    1795        4536 :   return tag3(t, NK, F, L, dL);
    1796             : }
    1797             : static GEN
    1798        1736 : taglinear(GEN NK, GEN F, GEN L)
    1799             : {
    1800        1736 :   long t = ok_bhn_linear(F)? t_MF_LINEAR_BHN: t_MF_LINEAR;
    1801        1736 :    return taglinear_i(t, NK, F, L);
    1802             : }
    1803             : /* assume F has parameters NK = [N,K,CHI] */
    1804             : static GEN
    1805         301 : mflinear_i(GEN NK, GEN F, GEN L)
    1806             : {
    1807         301 :   if (!mflinear_strip(&F,&L)) return mftrivial();
    1808         301 :   return taglinear(NK, F,L);
    1809             : }
    1810             : static GEN
    1811         462 : mflinear_bhn(GEN mf, GEN L)
    1812             : {
    1813             :   long i, l;
    1814         462 :   GEN P, NK, F = MF_get_S(mf);
    1815         462 :   if (!mflinear_strip(&F,&L)) return mftrivial();
    1816         455 :   l = lg(L); P = pol_x(1);
    1817        2520 :   for (i = 1; i < l; i++)
    1818             :   {
    1819        2065 :     GEN c = gel(L,i);
    1820        2065 :     if (typ(c) == t_POLMOD && varn(gel(c,1)) == 1) P = mfsamefield(P,gel(c,1));
    1821             :   }
    1822         455 :   NK = mkgNK(MF_get_gN(mf), MF_get_gk(mf), MF_get_CHI(mf), P);
    1823         455 :   return taglinear_i(t_MF_LINEAR_BHN,  NK, F,L);
    1824             : }
    1825             : 
    1826             : /* F vector of forms with same weight and character but varying level, return
    1827             :  * global [N,k,chi,P] */
    1828             : static GEN
    1829        2345 : vecmfNK(GEN F)
    1830             : {
    1831        2345 :   long i, l = lg(F);
    1832             :   GEN N, f;
    1833        2345 :   if (l == 1) return mkNK(1, 0, mfchartrivial());
    1834        2345 :   f = gel(F,1); N = mf_get_gN(f);
    1835        2345 :   for (i = 2; i < l; i++) N = lcmii(N, mf_get_gN(gel(F,i)));
    1836        2345 :   return mkgNK(N, mf_get_gk(f), mf_get_CHI(f), mf_get_field(f));
    1837             : }
    1838             : /* do not use mflinear: mflineardivtomat rely on F being constant across the
    1839             :  * basis where mflinear strips the ones matched by 0 coeffs. Assume k and CHI
    1840             :  * constant, N is allowed to vary. */
    1841             : static GEN
    1842        1071 : vecmflinear(GEN F, GEN C)
    1843             : {
    1844        1071 :   long i, t, l = lg(C);
    1845        1071 :   GEN NK, v = cgetg(l, t_VEC);
    1846        1071 :   if (l == 1) return v;
    1847        1071 :   t = ok_bhn_linear(F)? t_MF_LINEAR_BHN: t_MF_LINEAR;
    1848        1071 :   NK = vecmfNK(F);
    1849        1071 :   for (i = 1; i < l; i++) gel(v,i) = taglinear_i(t, NK, F, gel(C,i));
    1850        1071 :   return v;
    1851             : }
    1852             : /* vecmflinear(F,C), then divide everything by E, which has valuation 0 */
    1853             : static GEN
    1854         343 : vecmflineardiv0(GEN F, GEN C, GEN E)
    1855             : {
    1856         343 :   GEN v = vecmflinear(F, C);
    1857         343 :   long i, l = lg(v);
    1858         343 :   for (i = 1; i < l; i++) gel(v,i) = mfdiv_val(gel(v,i), E, 0);
    1859         343 :   return v;
    1860             : }
    1861             : 
    1862             : /* Non empty linear combination of linear combinations of same
    1863             :  * F_j=\sum_i \mu_{i,j}G_i so R = \sum_i (\sum_j(\la_j\mu_{i,j})) G_i */
    1864             : static GEN
    1865        1274 : mflinear_linear(GEN F, GEN L, int strip)
    1866             : {
    1867        1274 :   long l = lg(F), j;
    1868        1274 :   GEN vF, M = cgetg(l, t_MAT);
    1869        1274 :   L = shallowcopy(L);
    1870        7686 :   for (j = 1; j < l; j++)
    1871             :   {
    1872        6412 :     GEN f = gel(F,j), c = gel(f,3), d = gel(f,4);
    1873        6412 :     if (typ(c) == t_VEC) c = shallowtrans(c);
    1874        6412 :     if (!isint1(d)) gel(L,j) = gdiv(gel(L,j),d);
    1875        6412 :     gel(M,j) = c;
    1876             :   }
    1877        1274 :   vF = gmael(F,1,2); L = RgM_RgC_mul(M,L);
    1878        1274 :   if (strip && !mflinear_strip(&vF,&L)) return mftrivial();
    1879        1274 :   return taglinear(vecmfNK(vF), vF, L);
    1880             : }
    1881             : /* F non-empty vector of forms of the form mfdiv(mflinear(B,v), E) where E
    1882             :  * does not vanish at oo, or mflinear(B,v). Apply mflinear(F, L) */
    1883             : static GEN
    1884        1274 : mflineardiv_linear(GEN F, GEN L, int strip)
    1885             : {
    1886        1274 :   long l = lg(F), j;
    1887             :   GEN v, E, f;
    1888        1274 :   if (lg(L) != l) pari_err_DIM("mflineardiv_linear");
    1889        1274 :   f = gel(F,1); /* l > 1 */
    1890        1274 :   if (mf_get_type(f) != t_MF_DIV) return mflinear_linear(F,L,strip);
    1891        1106 :   E = gel(f,3);
    1892        1106 :   v = cgetg(l, t_VEC);
    1893        1106 :   for (j = 1; j < l; j++) { GEN f = gel(F,j); gel(v,j) = gel(f,2); }
    1894        1106 :   return mfdiv_val(mflinear_linear(v,L,strip), E, 0);
    1895             : }
    1896             : static GEN
    1897         399 : vecmflineardiv_linear(GEN F, GEN M)
    1898             : {
    1899         399 :   long i, l = lg(M);
    1900         399 :   GEN v = cgetg(l, t_VEC);
    1901         399 :   for (i = 1; i < l; i++) gel(v,i) = mflineardiv_linear(F, gel(M,i), 0);
    1902         399 :   return v;
    1903             : }
    1904             : 
    1905             : static GEN
    1906         476 : tobasis(GEN mf, GEN F, GEN L)
    1907             : {
    1908         476 :   if (checkmf_i(L) && mf) return mftobasis(mf, L, 0);
    1909         469 :   if (typ(F) != t_VEC) pari_err_TYPE("mflinear",F);
    1910         469 :   if (!is_vec_t(typ(L))) pari_err_TYPE("mflinear",L);
    1911         469 :   if (lg(L) != lg(F)) pari_err_DIM("mflinear");
    1912         469 :   return L;
    1913             : }
    1914             : GEN
    1915         504 : mflinear(GEN F, GEN L)
    1916             : {
    1917         504 :   pari_sp av = avma;
    1918         504 :   GEN G, NK, P, mf = checkMF_i(F), N = NULL, K = NULL, CHI = NULL;
    1919             :   long i, l;
    1920         504 :   if (mf)
    1921             :   {
    1922         378 :     GEN gk = MF_get_gk(mf);
    1923         378 :     F = MF_get_basis(F);
    1924         378 :     if (typ(gk) != t_INT)
    1925          28 :       return gerepilecopy(av, mflineardiv_linear(F, L, 1));
    1926         350 :     if (itou(gk) > 1 && space_is_cusp(MF_get_space(mf)))
    1927             :     {
    1928         238 :       L = tobasis(mf, F, L);
    1929         238 :       return gerepilecopy(av, mflinear_bhn(mf, L));
    1930             :     }
    1931             :   }
    1932         238 :   L = tobasis(mf, F, L);
    1933         238 :   if (!mflinear_strip(&F,&L)) return mftrivial();
    1934             : 
    1935         231 :   l = lg(F);
    1936         231 :   if (l == 2 && gequal1(gel(L,1))) return gerepilecopy(av, gel(F,1));
    1937         175 :   P = pol_x(1);
    1938         581 :   for (i = 1; i < l; i++)
    1939             :   {
    1940         413 :     GEN f = gel(F,i), c = gel(L,i), Ni, Ki;
    1941         413 :     if (!checkmf_i(f)) pari_err_TYPE("mflinear", f);
    1942         413 :     Ni = mf_get_gN(f); N = N? lcmii(N, Ni): Ni;
    1943         413 :     Ki = mf_get_gk(f);
    1944         413 :     if (!K) K = Ki;
    1945         238 :     else if (!gequal(K, Ki))
    1946           7 :       pari_err_TYPE("mflinear [different weights]", mkvec2(K,Ki));
    1947         406 :     P = mfsamefield(P, mf_get_field(f));
    1948         406 :     if (typ(c) == t_POLMOD && varn(gel(c,1)) == 1) P = mfsamefield(P, gel(c,1));
    1949             :   }
    1950         168 :   G = znstar0(N,1);
    1951         560 :   for (i = 1; i < l; i++)
    1952             :   {
    1953         399 :     GEN CHI2 = mf_get_CHI(gel(F,i));
    1954         399 :     CHI2 = induce(G, CHI2);
    1955         399 :     if (!CHI) CHI = CHI2;
    1956         231 :     else if (!gequal(CHI, CHI2))
    1957           7 :       pari_err_TYPE("mflinear [different characters]", mkvec2(CHI,CHI2));
    1958             :   }
    1959         161 :   NK = mkgNK(N, K, CHI, P);
    1960         161 :   return gerepilecopy(av, taglinear(NK,F,L));
    1961             : }
    1962             : 
    1963             : GEN
    1964          42 : mfshift(GEN F, long sh)
    1965             : {
    1966          42 :   pari_sp av = avma;
    1967          42 :   if (!checkmf_i(F)) pari_err_TYPE("mfshift",F);
    1968          42 :   return gerepilecopy(av, tag2(t_MF_SHIFT, mf_get_NK(F), F, stoi(sh)));
    1969             : }
    1970             : static long
    1971          42 : mfval(GEN F)
    1972             : {
    1973          42 :   pari_sp av = avma;
    1974          42 :   long i = 0, n, sb;
    1975             :   GEN gk, gN;
    1976          42 :   if (!checkmf_i(F)) pari_err_TYPE("mfval", F);
    1977          42 :   gN = mf_get_gN(F);
    1978          42 :   gk = mf_get_gk(F);
    1979          42 :   sb = mfsturmNgk(itou(gN), gk);
    1980         105 :   for (n = 1; n <= sb;)
    1981             :   {
    1982             :     GEN v;
    1983          56 :     if (n > 0.5*sb) n = sb+1;
    1984          56 :     v = mfcoefs_i(F, n, 1);
    1985         112 :     for (; i <= n; i++)
    1986          91 :       if (!gequal0(gel(v, i+1))) return gc_long(av,i);
    1987          21 :     n <<= 1;
    1988             :   }
    1989           7 :   return gc_long(av,-1);
    1990             : }
    1991             : 
    1992             : GEN
    1993        2156 : mfdiv_val(GEN f, GEN g, long vg)
    1994             : {
    1995             :   GEN N, K, NK, CHI;
    1996        2156 :   if (vg) { f = mfshift(f,vg); g = mfshift(g,vg); }
    1997        2156 :   N = lcmii(mf_get_gN(f), mf_get_gN(g));
    1998        2156 :   K = gsub(mf_get_gk(f), mf_get_gk(g));
    1999        2156 :   CHI = mfchardiv(mf_get_CHI(f), mf_get_CHI(g));
    2000        2156 :   CHI = mfchiadjust(CHI, K, itos(N));
    2001        2156 :   NK = mkgNK(N, K, CHI, mfsamefield(mf_get_field(f), mf_get_field(g)));
    2002        2156 :   return tag2(t_MF_DIV, NK, f, g);
    2003             : }
    2004             : GEN
    2005          42 : mfdiv(GEN F, GEN G)
    2006             : {
    2007          42 :   pari_sp av = avma;
    2008          42 :   long v = mfval(G);
    2009          42 :   if (!checkmf_i(F)) pari_err_TYPE("mfdiv", F);
    2010          35 :   if (v < 0 || (v && !gequal0(mfcoefs(F, v-1, 1))))
    2011          14 :     pari_err_DOMAIN("mfdiv", "ord(G)", ">", strtoGENstr("ord(F)"),
    2012             :                     mkvec2(F, G));
    2013          21 :   return gerepilecopy(av, mfdiv_val(F, G, v));
    2014             : }
    2015             : GEN
    2016          28 : mfderiv(GEN F, long m)
    2017             : {
    2018          28 :   pari_sp av = avma;
    2019             :   GEN NK, gk;
    2020          28 :   if (!checkmf_i(F)) pari_err_TYPE("mfderiv",F);
    2021          28 :   gk = gaddgs(mf_get_gk(F), 2*m);
    2022          28 :   NK = mkgNK(mf_get_gN(F), gk, mf_get_CHI(F), mf_get_field(F));
    2023          28 :   return gerepilecopy(av, tag2(t_MF_DERIV, NK, F, stoi(m)));
    2024             : }
    2025             : GEN
    2026          21 : mfderivE2(GEN F, long m)
    2027             : {
    2028          21 :   pari_sp av = avma;
    2029             :   GEN NK, gk;
    2030          21 :   if (!checkmf_i(F)) pari_err_TYPE("mfderivE2",F);
    2031          21 :   if (m < 0) pari_err_DOMAIN("mfderivE2","m","<",gen_0,stoi(m));
    2032          21 :   gk = gaddgs(mf_get_gk(F), 2*m);
    2033          21 :   NK = mkgNK(mf_get_gN(F), gk, mf_get_CHI(F), mf_get_field(F));
    2034          21 :   return gerepilecopy(av, tag2(t_MF_DERIVE2, NK, F, stoi(m)));
    2035             : }
    2036             : 
    2037             : GEN
    2038          14 : mftwist(GEN F, GEN D)
    2039             : {
    2040          14 :   pari_sp av = avma;
    2041             :   GEN NK, CHI, NT, Da;
    2042             :   long q;
    2043          14 :   if (!checkmf_i(F)) pari_err_TYPE("mftwist", F);
    2044          14 :   if (typ(D) != t_INT) pari_err_TYPE("mftwist", D);
    2045          14 :   Da = mpabs_shallow(D);
    2046          14 :   CHI = mf_get_CHI(F); q = mfcharconductor(CHI);
    2047          14 :   NT = glcm(glcm(mf_get_gN(F), mulsi(q, Da)), sqri(Da));
    2048          14 :   NK = mkgNK(NT, mf_get_gk(F), CHI, mf_get_field(F));
    2049          14 :   return gerepilecopy(av, tag2(t_MF_TWIST, NK, F, D));
    2050             : }
    2051             : 
    2052             : /***************************************************************/
    2053             : /*                 Generic cache handling                      */
    2054             : /***************************************************************/
    2055             : enum { cache_FACT, cache_DIV, cache_H, cache_D, cache_DIH };
    2056             : typedef struct {
    2057             :   const char *name;
    2058             :   GEN cache;
    2059             :   ulong minself;
    2060             :   ulong maxself;
    2061             :   void (*init)(long);
    2062             :   ulong miss;
    2063             :   ulong maxmiss;
    2064             : } cache;
    2065             : 
    2066             : static void constfact(long lim);
    2067             : static void constdiv(long lim);
    2068             : static void consttabh(long lim);
    2069             : static void consttabdihedral(long lim);
    2070             : static void constcoredisc(long lim);
    2071             : static THREAD cache caches[] = {
    2072             : { "Factors",  NULL,  50000,    50000, &constfact, 0, 0 },
    2073             : { "Divisors", NULL,  50000,    50000, &constdiv, 0, 0 },
    2074             : { "H",        NULL, 100000, 10000000, &consttabh, 0, 0 },
    2075             : { "CorediscF",NULL, 100000, 10000000, &constcoredisc, 0, 0 },
    2076             : { "Dihedral", NULL,   1000,     3000, &consttabdihedral, 0, 0 },
    2077             : };
    2078             : 
    2079             : static void
    2080         343 : cache_reset(long id) { caches[id].miss = caches[id].maxmiss = 0; }
    2081             : static void
    2082        6196 : cache_delete(long id) { if (caches[id].cache) gunclone(caches[id].cache); }
    2083             : static void
    2084         357 : cache_set(long id, GEN S)
    2085             : {
    2086         357 :   GEN old = caches[id].cache;
    2087         357 :   caches[id].cache = gclone(S);
    2088         357 :   if (old) gunclone(old);
    2089         357 : }
    2090             : 
    2091             : /* handle a cache miss: store stats, possibly reset table; return value
    2092             :  * if (now) cached; return NULL on failure. HACK: some caches contain an
    2093             :  * ulong where the 0 value is impossible, and return it (typecase to GEN) */
    2094             : static GEN
    2095   234043119 : cache_get(long id, ulong D)
    2096             : {
    2097   234043119 :   cache *S = &caches[id];
    2098             :   /* cache_H is compressed: D=0,1 mod 4 */
    2099   234043119 :   const ulong d = (id == cache_H)? D>>1: D;
    2100             :   ulong max, l;
    2101             : 
    2102   234043119 :   if (!S->cache)
    2103             :   {
    2104         217 :     max = maxuu(minuu(D, S->maxself), S->minself);
    2105         217 :     S->init(max);
    2106         217 :     l = lg(S->cache);
    2107             :   }
    2108             :   else
    2109             :   {
    2110   234042902 :     l = lg(S->cache);
    2111   234042902 :     if (l <= d)
    2112             :     {
    2113         994 :       if (D > S->maxmiss) S->maxmiss = D;
    2114         994 :       if (DEBUGLEVEL >= 3)
    2115           0 :         err_printf("miss in cache %s: %lu, max = %lu\n",
    2116             :                    S->name, D, S->maxmiss);
    2117         994 :       if (S->miss++ >= 5 && D < S->maxself)
    2118             :       {
    2119          84 :         max = minuu(S->maxself, (long)(S->maxmiss * 1.2));
    2120          84 :         if (max <= S->maxself)
    2121             :         {
    2122          84 :           if (DEBUGLEVEL >= 3)
    2123           0 :             err_printf("resetting cache %s to %lu\n", S->name, max);
    2124          84 :           S->init(max); l = lg(S->cache);
    2125             :         }
    2126             :       }
    2127             :     }
    2128             :   }
    2129   234043119 :   return (l <= d)? NULL: gel(S->cache, d);
    2130             : }
    2131             : static GEN
    2132          70 : cache_report(long id)
    2133             : {
    2134          70 :   cache *S = &caches[id];
    2135          70 :   GEN v = zerocol(5);
    2136          70 :   gel(v,1) = strtoGENstr(S->name);
    2137          70 :   if (S->cache)
    2138             :   {
    2139          35 :     gel(v,2) = utoi(lg(S->cache)-1);
    2140          35 :     gel(v,3) = utoi(S->miss);
    2141          35 :     gel(v,4) = utoi(S->maxmiss);
    2142          35 :     gel(v,5) = utoi(gsizebyte(S->cache));
    2143             :   }
    2144          70 :   return v;
    2145             : }
    2146             : GEN
    2147          14 : getcache(void)
    2148             : {
    2149          14 :   pari_sp av = avma;
    2150          14 :   GEN M = cgetg(6, t_MAT);
    2151          14 :   gel(M,1) = cache_report(cache_FACT);
    2152          14 :   gel(M,2) = cache_report(cache_DIV);
    2153          14 :   gel(M,3) = cache_report(cache_H);
    2154          14 :   gel(M,4) = cache_report(cache_D);
    2155          14 :   gel(M,5) = cache_report(cache_DIH);
    2156          14 :   return gerepilecopy(av, shallowtrans(M));
    2157             : }
    2158             : 
    2159             : void
    2160        1549 : pari_close_mf(void)
    2161             : {
    2162        1549 :   cache_delete(cache_DIH);
    2163        1549 :   cache_delete(cache_DIV);
    2164        1549 :   cache_delete(cache_FACT);
    2165        1549 :   cache_delete(cache_H);
    2166        1549 : }
    2167             : 
    2168             : /*************************************************************************/
    2169             : /* a odd, update local cache (recycle memory) */
    2170             : static GEN
    2171        1948 : update_factor_cache(long a, long lim, long *pb)
    2172             : {
    2173        1948 :   const long step = 16000; /* even; don't increase this: RAM cache thrashing */
    2174        1948 :   if (a + 2*step > lim)
    2175         203 :     *pb = lim; /* fuse last 2 chunks */
    2176             :   else
    2177        1745 :     *pb = a + step;
    2178        1948 :   return vecfactoroddu_i(a, *pb);
    2179             : }
    2180             : /* assume lim < MAX_LONG/8 */
    2181             : static void
    2182          70 : constcoredisc(long lim)
    2183             : {
    2184          70 :   pari_sp av2, av = avma;
    2185          70 :   GEN D = caches[cache_D].cache, CACHE = NULL;
    2186          70 :   long cachea, cacheb, N, LIM = !D ? 4 : lg(D)-1;
    2187          70 :   if (lim <= 0) lim = 5;
    2188          70 :   if (lim <= LIM) return;
    2189          70 :   cache_reset(cache_D);
    2190          70 :   D = zero_zv(lim);
    2191          70 :   av2 = avma;
    2192          70 :   cachea = cacheb = 0;
    2193     7903329 :   for (N = 1; N <= lim; N+=2)
    2194             :   { /* N odd */
    2195             :     long i, d, d2;
    2196             :     GEN F;
    2197     7903259 :     if (N > cacheb)
    2198             :     {
    2199         966 :       set_avma(av2); cachea = N;
    2200         966 :       CACHE = update_factor_cache(N, lim, &cacheb);
    2201             :     }
    2202     7903259 :     F = gel(CACHE, ((N-cachea)>>1)+1); /* factoru(N) */
    2203     7903259 :     D[N] = d = corediscs_fact(F); /* = 3 mod 4 or 4 mod 16 */
    2204     7903259 :     d2 = odd(d)? d<<3: d<<1;
    2205     7903259 :     for (i = 1;;)
    2206             :     {
    2207    13172075 :       if ((N << i) > lim) break;
    2208     5268822 :       D[N<<i] = d2; i++;
    2209     5268822 :       if ((N << i) > lim) break;
    2210     2634408 :       D[N<<i] = d; i++;
    2211             :     }
    2212             :   }
    2213          70 :   cache_set(cache_D, D);
    2214          70 :   set_avma(av);
    2215             : }
    2216             : 
    2217             : static void
    2218          84 : constfact(long lim)
    2219             : {
    2220             :   pari_sp av;
    2221          84 :   GEN VFACT = caches[cache_FACT].cache;
    2222          84 :   long LIM = VFACT? lg(VFACT)-1: 4;
    2223          84 :   if (lim <= 0) lim = 5;
    2224          84 :   if (lim <= LIM) return;
    2225          70 :   cache_reset(cache_FACT); av = avma;
    2226          70 :   cache_set(cache_FACT, vecfactoru_i(1,lim)); set_avma(av);
    2227             : }
    2228             : static void
    2229          70 : constdiv(long lim)
    2230             : {
    2231             :   pari_sp av;
    2232          70 :   GEN VFACT, VDIV = caches[cache_DIV].cache;
    2233          70 :   long N, LIM = VDIV? lg(VDIV)-1: 4;
    2234          70 :   if (lim <= 0) lim = 5;
    2235          70 :   if (lim <= LIM) return;
    2236          70 :   constfact(lim);
    2237          70 :   VFACT = caches[cache_FACT].cache;
    2238          70 :   cache_reset(cache_DIV); av = avma;
    2239          70 :   VDIV  = cgetg(lim+1, t_VEC);
    2240          70 :   for (N = 1; N <= lim; N++) gel(VDIV,N) = divisorsu_fact(gel(VFACT,N));
    2241          70 :   cache_set(cache_DIV, VDIV); set_avma(av);
    2242             : }
    2243             : 
    2244             : /* n > 1, D = divisors(n); sets L = 2*lambda(n), S = sigma(n) */
    2245             : static void
    2246     9867614 : lamsig(GEN D, long *pL, long *pS)
    2247             : {
    2248     9867614 :   pari_sp av = avma;
    2249     9867614 :   long i, l = lg(D), L = 1, S = D[l-1]+1;
    2250    35523778 :   for (i = 2; i < l; i++) /* skip d = 1 */
    2251             :   {
    2252    35523778 :     long d = D[i], nd = D[l-i]; /* nd = n/d */
    2253    35523778 :     if (d < nd) { L += d; S += d + nd; }
    2254             :     else
    2255             :     {
    2256     9867614 :       L <<= 1; if (d == nd) { L += d; S += d; }
    2257     9867614 :       break;
    2258             :     }
    2259             :   }
    2260     9867614 :   set_avma(av); *pL = L; *pS = S;
    2261     9867614 : }
    2262             : /* table of 6 * Hurwitz class numbers D <= lim */
    2263             : static void
    2264         133 : consttabh(long lim)
    2265             : {
    2266         133 :   pari_sp av = avma, av2;
    2267         133 :   GEN VHDH0, VDIV, CACHE = NULL;
    2268         133 :   GEN VHDH = caches[cache_H].cache;
    2269         133 :   long r, N, cachea, cacheb, lim0 = VHDH? lg(VHDH)-1: 2, LIM = lim0 << 1;
    2270             : 
    2271         133 :   if (lim <= 0) lim = 5;
    2272         133 :   if (lim <= LIM) return;
    2273         133 :   cache_reset(cache_H);
    2274         133 :   r = lim&3L; if (r) lim += 4-r;
    2275         133 :   cache_get(cache_DIV, lim);
    2276         133 :   VDIV = caches[cache_DIV].cache;
    2277         133 :   VHDH0 = cgetg(lim/2 + 1, t_VECSMALL);
    2278         133 :   VHDH0[1] = 2;
    2279         133 :   VHDH0[2] = 3;
    2280         133 :   for (N = 3; N <= lim0; N++) VHDH0[N] = VHDH[N];
    2281         133 :   av2 = avma;
    2282         133 :   cachea = cacheb = 0;
    2283     4933940 :   for (N = LIM + 3; N <= lim; N += 4)
    2284             :   {
    2285     4933807 :     long s = 0, limt = usqrt(N>>2), flsq = 0, ind, t, L, S;
    2286             :     GEN DN, DN2;
    2287     4933807 :     if (N + 2 >= lg(VDIV))
    2288             :     { /* use local cache */
    2289             :       GEN F;
    2290     4058947 :       if (N + 2 > cacheb)
    2291             :       {
    2292         982 :         set_avma(av2); cachea = N;
    2293         982 :         CACHE = update_factor_cache(N, lim+2, &cacheb);
    2294             :       }
    2295     4058947 :       F = gel(CACHE, ((N-cachea)>>1)+1); /* factoru(N) */
    2296     4058947 :       DN = divisorsu_fact(F);
    2297     4058947 :       F = gel(CACHE, ((N-cachea)>>1)+2); /* factoru(N+2) */
    2298     4058947 :       DN2 = divisorsu_fact(F);
    2299             :     }
    2300             :     else
    2301             :     { /* use global cache */
    2302      874860 :       DN = gel(VDIV,N);
    2303      874860 :       DN2 = gel(VDIV,N+2);
    2304             :     }
    2305     4933807 :     ind = N >> 1;
    2306  1078343441 :     for (t = 1; t <= limt; t++)
    2307             :     {
    2308  1073409634 :       ind -= (t<<2)-2; /* N/2 - 2t^2 */
    2309  1073409634 :       if (ind) s += VHDH0[ind]; else flsq = 1;
    2310             :     }
    2311     4933807 :     lamsig(DN, &L,&S);
    2312     4933807 :     VHDH0[N >> 1] = 2*S - 3*L - 2*s + flsq;
    2313     4933807 :     s = 0; flsq = 0; limt = (usqrt(N+2) - 1) >> 1;
    2314     4933807 :     ind = (N+1) >> 1;
    2315  1075899401 :     for (t = 1; t <= limt; t++)
    2316             :     {
    2317  1070965594 :       ind -= t<<2; /* (N+1)/2 - 2t(t+1) */
    2318  1070965594 :       if (ind) s += VHDH0[ind]; else flsq = 1;
    2319             :     }
    2320     4933807 :     lamsig(DN2, &L,&S);
    2321     4933807 :     VHDH0[(N+1) >> 1] = S - 3*(L >> 1) - s - flsq;
    2322             :   }
    2323         133 :   cache_set(cache_H, VHDH0); set_avma(av);
    2324             : }
    2325             : 
    2326             : /*************************************************************************/
    2327             : /* Core functions using factorizations, divisors of class numbers caches */
    2328             : /* TODO: myfactoru and factorization cache should be exported */
    2329             : static GEN
    2330    21744674 : myfactoru(long N)
    2331             : {
    2332    21744674 :   GEN z = cache_get(cache_FACT, N);
    2333    21744674 :   return z? gcopy(z): factoru(N);
    2334             : }
    2335             : static GEN
    2336    50429393 : mydivisorsu(long N)
    2337             : {
    2338    50429393 :   GEN z = cache_get(cache_DIV, N);
    2339    50429393 :   return z? leafcopy(z): divisorsu(N);
    2340             : }
    2341             : /* write -n = Df^2, D < 0 fundamental discriminant. Return D, set f. */
    2342             : static long
    2343    85412432 : mycoredisc2neg(ulong n, long *pf)
    2344             : {
    2345    85412432 :   ulong m, D = (ulong)cache_get(cache_D, n);
    2346    85412432 :   if (D) { *pf = usqrt(n/D); return -(long)D; }
    2347         196 :   m = mycore(n, pf);
    2348         196 :   if ((m&3) != 3) { m <<= 2; *pf >>= 1; }
    2349         196 :   return (long)-m;
    2350             : }
    2351             : /* write n = Df^2, D > 0 fundamental discriminant. Return D, set f. */
    2352             : static long
    2353          14 : mycoredisc2pos(ulong n, long *pf)
    2354             : {
    2355          14 :   ulong m = mycore(n, pf);
    2356          14 :   if ((m&3) != 1) { m <<= 2; *pf >>= 1; }
    2357          14 :   return (long)m;
    2358             : }
    2359             : 
    2360             : /* 1+p+...+p^e, e >= 1 */
    2361             : static ulong
    2362          49 : usumpow(ulong p, long e)
    2363             : {
    2364          49 :   ulong q = 1+p;
    2365             :   long i;
    2366          49 :   for (i = 1; i < e; i++) q = p*q + 1;
    2367          49 :   return q;
    2368             : }
    2369             : /* Hurwitz(D0 F^2)/ Hurwitz(D0)
    2370             :  * = \sum_{f|F}  f \prod_{p|f} (1-kro(D0/p)/p)
    2371             :  * = \prod_{p^e || F} (1 + (p^e-1) / (p-1) * (p-kro(D0/p))) */
    2372             : static long
    2373         294 : get_sh(long F, long D0)
    2374             : {
    2375         294 :   GEN fa = myfactoru(F), P = gel(fa,1), E = gel(fa,2);
    2376         294 :   long i, l = lg(P), t = 1;
    2377         794 :   for (i = 1; i < l; i++)
    2378             :   {
    2379         500 :     long p = P[i], e = E[i], s = kross(D0,p);
    2380         500 :     if (e == 1) { t *= 1 + p - s; continue; }
    2381         153 :     if (s == 1) { t *= upowuu(p,e); continue; }
    2382          49 :     t *= 1 + usumpow(p,e-1)*(p-s);
    2383             :   }
    2384         294 :   return t;
    2385             : }
    2386             : /* d > 0, d = 0,3 (mod 4). Return 6*hclassno(d); -d must be fundamental
    2387             :  * Faster than quadclassunit up to 5*10^5 or so */
    2388             : static ulong
    2389          42 : hclassno6u_count(ulong d)
    2390             : {
    2391          42 :   ulong a, b, b2, h = 0;
    2392          42 :   int f = 0;
    2393             : 
    2394          42 :   if (d > 500000)
    2395           7 :     return 6 * itou(gel(quadclassunit0(utoineg(d), 0, NULL, 0), 1));
    2396             : 
    2397             :   /* this part would work with -d non fundamental */
    2398          35 :   b = d&1; b2 = (1+d)>>2;
    2399          35 :   if (!b)
    2400             :   {
    2401           0 :     for (a=1; a*a<b2; a++)
    2402           0 :       if (b2%a == 0) h++;
    2403           0 :     f = (a*a==b2); b=2; b2=(4+d)>>2;
    2404             :   }
    2405        7168 :   while (b2*3 < d)
    2406             :   {
    2407        7098 :     if (b2%b == 0) h++;
    2408     1188551 :     for (a=b+1; a*a < b2; a++)
    2409     1181453 :       if (b2%a == 0) h += 2;
    2410        7098 :     if (a*a == b2) h++;
    2411        7098 :     b += 2; b2 = (b*b+d)>>2;
    2412             :   }
    2413          35 :   if (b2*3 == d) return 6*h+2;
    2414          35 :   if (f) return 6*h+3;
    2415          35 :   return 6*h;
    2416             : }
    2417             : /* D > 0; 6 * hclassno(D), using D = D0*F^2 */
    2418             : static long
    2419         336 : hclassno6u_2(ulong D, long D0, long F)
    2420             : {
    2421             :   long h;
    2422         336 :   if (F == 1) h = hclassno6u_count(D);
    2423             :   else
    2424             :   { /* second chance */
    2425         294 :     h = (ulong)cache_get(cache_H, -D0);
    2426         294 :     if (!h) h = hclassno6u_count(-D0);
    2427         294 :     h *= get_sh(F,D0);
    2428             :   }
    2429         336 :   return h;
    2430             : }
    2431             : /* D > 0; 6 * hclassno(D) (6*Hurwitz). Beware, cached value for D (=0,3 mod 4)
    2432             :  * is stored at D>>1 */
    2433             : ulong
    2434      155745 : hclassno6u(ulong D)
    2435             : {
    2436      155745 :   ulong z = (ulong)cache_get(cache_H, D);
    2437             :   long D0, F;
    2438      155745 :   if (z) return z;
    2439         336 :   D0 = mycoredisc2neg(D, &F);
    2440         336 :   return hclassno6u_2(D,D0,F);
    2441             : }
    2442             : /* same, where the decomposition D = D0*F^2 is already known */
    2443             : static ulong
    2444    69622133 : hclassno6u_i(ulong D, long D0, long F)
    2445             : {
    2446    69622133 :   ulong z = (ulong)cache_get(cache_H, D);
    2447    69622133 :   if (z) return z;
    2448           0 :   return hclassno6u_2(D,D0,F);
    2449             : }
    2450             : 
    2451             : #if 0
    2452             : /* D > 0, return h(-D) [ordinary class number].
    2453             :  * Assume consttabh(D or more) was previously called */
    2454             : static long
    2455             : hfromH(long D)
    2456             : {
    2457             :   pari_sp ltop = avma;
    2458             :   GEN m, d, fa = myfactoru(D), P = gel(fa,1), E = gel(fa,2);
    2459             :   GEN VH = caches[cache_H].cache;
    2460             :   long i, nd, S, l = lg(P);
    2461             : 
    2462             :   /* n = d[i] loops through squarefree divisors of f, where f^2 = largest square
    2463             :    * divisor of N = |D|; m[i] = moebius(n) */
    2464             :   nd = 1 << (l-1);
    2465             :   d = cgetg(nd+1, t_VECSMALL);
    2466             :   m = cgetg(nd+1, t_VECSMALL);
    2467             :   d[1] = 1; S = VH[D >> 1]; /* 6 hclassno(-D) */
    2468             :   m[1] = 1; nd = 1;
    2469             :   i = 1;
    2470             :   if (P[1] == 2 && E[1] <= 3) /* need D/n^2 to be a discriminant */
    2471             :   { if (odd(E[1]) || (E[1] == 2 && (D & 15) == 4)) i = 2; }
    2472             :   for (; i<l; i++)
    2473             :   {
    2474             :     long j, p = P[i];
    2475             :     if (E[i] == 1) continue;
    2476             :     for (j=1; j<=nd; j++)
    2477             :     {
    2478             :       long n, s, hn;
    2479             :       d[nd+j] = n = d[j] * p;
    2480             :       m[nd+j] = s = - m[j]; /* moebius(n) */
    2481             :       hn = VH[(D/(n*n)) >> 1]; /* 6 hclassno(-D/n^2) */
    2482             :       if (s > 0) S += hn; else S -= hn;
    2483             :     }
    2484             :     nd <<= 1;
    2485             :   }
    2486             :   return gc_long(ltop, S/6);
    2487             : }
    2488             : #endif
    2489             : /* D < -4 fundamental, h(D), ordinary class number */
    2490             : static long
    2491     6654543 : myh(long D)
    2492             : {
    2493     6654543 :   ulong z = (ulong)cache_get(cache_H, -D);
    2494     6654543 :   if (z) return z/6; /* should be hfromH(-D) if D non-fundamental */
    2495           0 :   return itou(quadclassno(stoi(D)));
    2496             : }
    2497             : 
    2498             : /*************************************************************************/
    2499             : /*                          TRACE FORMULAS                               */
    2500             : /* CHIP primitive, initialize for t_POLMOD output */
    2501             : static GEN
    2502       28385 : mfcharinit(GEN CHIP)
    2503             : {
    2504       28385 :   long n, o, l, vt, N = mfcharmodulus(CHIP);
    2505             :   GEN c, v, V, G, Pn;
    2506       28385 :   if (N == 1) return mkvec2(mkvec(gen_1), pol_x(0));
    2507        4011 :   G = gel(CHIP,1);
    2508        4011 :   v = ncharvecexpo(G, znconrey_normalized(G, gel(CHIP,2)));
    2509        4011 :   l = lg(v); V = cgetg(l, t_VEC);
    2510        4011 :   o = mfcharorder(CHIP);
    2511        4011 :   Pn = mfcharpol(CHIP); vt = varn(Pn);
    2512        4011 :   if (o <= 2)
    2513             :   {
    2514       29176 :     for (n = 1; n < l; n++)
    2515             :     {
    2516       26103 :       if (v[n] < 0) c = gen_0; else c = v[n]? gen_m1: gen_1;
    2517       26103 :       gel(V,n) = c;
    2518             :     }
    2519             :   }
    2520             :   else
    2521             :   {
    2522       16723 :     for (n = 1; n < l; n++)
    2523             :     {
    2524       15785 :       if (v[n] < 0) c = gen_0;
    2525             :       else
    2526             :       {
    2527        8806 :         c = mygmodulo_lift(v[n], o, gen_1, vt);
    2528        8806 :         if (typ(c) == t_POL && lg(c) >= lg(Pn)) c = RgX_rem(c, Pn);
    2529             :       }
    2530       15785 :       gel(V,n) = c;
    2531             :     }
    2532             :   }
    2533        4011 :   return mkvec2(V, Pn);
    2534             : }
    2535             : static GEN
    2536      421071 : vchip_lift(GEN VCHI, long x, GEN C)
    2537             : {
    2538      421071 :   GEN V = gel(VCHI,1);
    2539      421071 :   long F = lg(V)-1;
    2540      421071 :   if (F == 1) return C;
    2541       27370 :   x %= F;
    2542       27370 :   if (!x) return C;
    2543       27370 :   if (x <= 0) x += F;
    2544       27370 :   return gmul(C, gel(V, x));
    2545             : }
    2546             : static long
    2547   129506615 : vchip_FC(GEN VCHI) { return lg(gel(VCHI,1))-1; }
    2548             : static GEN
    2549     4734786 : vchip_mod(GEN VCHI, GEN S)
    2550     4734786 : { return (typ(S) == t_POL)? RgX_rem(S, gel(VCHI,2)): S; }
    2551             : static GEN
    2552     1474879 : vchip_polmod(GEN VCHI, GEN S)
    2553     1474879 : { return (typ(S) == t_POL)? mkpolmod(S, gel(VCHI,2)): S; }
    2554             : 
    2555             : /* ceil(m/d) */
    2556             : static long
    2557      134925 : ceildiv(long m, long d)
    2558             : {
    2559             :   long q;
    2560      134925 :   if (!m) return 0;
    2561       40334 :   q = m/d; return m%d? q+1: q;
    2562             : }
    2563             : 
    2564             : /* contribution of scalar matrices in dimension formula */
    2565             : static GEN
    2566      297717 : A1(long N, long k)
    2567      297717 : { return sstoQ(mypsiu(N)*(k-1), 12); }
    2568             : static long
    2569        7455 : ceilA1(long N, long k)
    2570        7455 : { return ceildiv(mypsiu(N) * (k-1), 12); }
    2571             : 
    2572             : /* sturm bound, slightly larger than dimension */
    2573             : long
    2574       29491 : mfsturmNk(long N, long k) { return 1 + (mypsiu(N)*k)/12; }
    2575             : long
    2576        1925 : mfsturmNgk(long N, GEN k)
    2577             : {
    2578        1925 :   long n,d; Qtoss(k,&n,&d);
    2579        1925 :   return (d == 1)? mfsturmNk(N,n): 1 + (mypsiu(N)*n)/24;
    2580             : }
    2581             : 
    2582             : /* List of all solutions of x^2 + x + 1 = 0 modulo N, x modulo N */
    2583             : static GEN
    2584         504 : sqrtm3modN(long N)
    2585             : {
    2586             :   pari_sp av;
    2587             :   GEN fa, P, E, B, mB, A, Q, T, R, v, gen_m3;
    2588         504 :   long l, i, n, ct, fl3 = 0, Ninit;
    2589         504 :   if (!odd(N) || (N%9) == 0) return cgetg(1,t_VECSMALL);
    2590         476 :   Ninit = N;
    2591         476 :   if ((N%3) == 0) { N /= 3; fl3 = 1; }
    2592         476 :   fa = myfactoru(N); P = gel(fa, 1); E = gel(fa, 2);
    2593         476 :   l = lg(P);
    2594         665 :   for (i = 1; i < l; i++)
    2595         483 :     if ((P[i]%3) == 2) return cgetg(1,t_VECSMALL);
    2596         182 :   A = cgetg(l, t_VECSMALL);
    2597         182 :   B = cgetg(l, t_VECSMALL);
    2598         182 :   mB= cgetg(l, t_VECSMALL);
    2599         182 :   Q = cgetg(l, t_VECSMALL); gen_m3 = utoineg(3);
    2600         371 :   for (i = 1; i < l; i++)
    2601             :   {
    2602         189 :     long p = P[i], e = E[i];
    2603         189 :     Q[i] = upowuu(p,e);
    2604         189 :     B[i] = itou( Zp_sqrt(gen_m3, utoipos(p), e) );
    2605         189 :     mB[i]= Q[i] - B[i];
    2606             :   }
    2607         182 :   ct = 1 << (l-1);
    2608         182 :   T = ZV_producttree(Q);
    2609         182 :   R = ZV_chinesetree(Q,T);
    2610         182 :   v = cgetg(ct+1, t_VECSMALL);
    2611         182 :   av = avma;
    2612         560 :   for (n = 1; n <= ct; n++)
    2613             :   {
    2614         378 :     long m = n-1, r;
    2615         784 :     for (i = 1; i < l; i++)
    2616             :     {
    2617         406 :       A[i] = (m&1L)? mB[i]: B[i];
    2618         406 :       m >>= 1;
    2619             :     }
    2620         378 :     r = itou( ZV_chinese_tree(A, Q, T, R) );
    2621         378 :     if (fl3) while (r%3) r += N;
    2622         378 :     set_avma(av); v[n] = odd(r) ? (r-1) >> 1 : (r+Ninit-1) >> 1;
    2623             :   }
    2624         182 :   return v;
    2625             : }
    2626             : 
    2627             : /* number of elliptic points of order 3 in X0(N) */
    2628             : static long
    2629        9597 : nu3(long N)
    2630             : {
    2631             :   long i, l;
    2632             :   GEN P;
    2633        9597 :   if (!odd(N) || (N%9) == 0) return 0;
    2634        8547 :   if ((N%3) == 0) N /= 3;
    2635        8547 :   P = gel(myfactoru(N), 1); l = lg(P);
    2636        8547 :   for (i = 1; i < l; i++) if ((P[i]%3) == 2) return 0;
    2637        3857 :   return 1L<<(l-1);
    2638             : }
    2639             : /* number of elliptic points of order 2 in X0(N) */
    2640             : static long
    2641       16555 : nu2(long N)
    2642             : {
    2643             :   long i, l;
    2644             :   GEN P;
    2645       16555 :   if ((N&3L) == 0) return 0;
    2646       16555 :   if (!odd(N)) N >>= 1;
    2647       16555 :   P = gel(myfactoru(N), 1); l = lg(P);
    2648       16555 :   for (i = 1; i < l; i++) if ((P[i]&3L) == 3) return 0;
    2649        3766 :   return 1L<<(l-1);
    2650             : }
    2651             : 
    2652             : /* contribution of elliptic matrices of order 3 in dimension formula
    2653             :  * Only depends on CHIP the primitive char attached to CHI */
    2654             : static GEN
    2655       40775 : A21(long N, long k, GEN CHI)
    2656             : {
    2657             :   GEN res, G, chi, o;
    2658             :   long a21, i, limx, S;
    2659       40775 :   if ((N&1L) == 0) return gen_0;
    2660       19950 :   a21 = k%3 - 1;
    2661       19950 :   if (!a21) return gen_0;
    2662       19306 :   if (N <= 3) return sstoQ(a21, 3);
    2663       10101 :   if (!CHI) return sstoQ(nu3(N) * a21, 3);
    2664         504 :   res = sqrtm3modN(N); limx = (N - 1) >> 1;
    2665         504 :   G = gel(CHI,1); chi = gel(CHI,2);
    2666         504 :   o = gmfcharorder(CHI);
    2667         882 :   for (S = 0, i = 1; i < lg(res); i++)
    2668             :   { /* (x,N) = 1; S += chi(x) + chi(x^2) */
    2669         378 :     long x = res[i];
    2670         378 :     if (x <= limx)
    2671             :     { /* CHI(x)=e(c/o), 3rd-root of 1 */
    2672         189 :       GEN c = znchareval(G, chi, utoi(x), o);
    2673         189 :       if (!signe(c)) S += 2; else S--;
    2674             :     }
    2675             :   }
    2676         504 :   return sstoQ(a21 * S, 3);
    2677             : }
    2678             : 
    2679             : /* List of all square roots of -1 modulo N */
    2680             : static GEN
    2681         567 : sqrtm1modN(long N)
    2682             : {
    2683             :   pari_sp av;
    2684             :   GEN fa, P, E, B, mB, A, Q, T, R, v;
    2685         567 :   long l, i, n, ct, fleven = 0;
    2686         567 :   if ((N&3L) == 0) return cgetg(1,t_VECSMALL);
    2687         567 :   if ((N&1L) == 0) { N >>= 1; fleven = 1; }
    2688         567 :   fa = myfactoru(N); P = gel(fa,1); E = gel(fa,2);
    2689         567 :   l = lg(P);
    2690         917 :   for (i = 1; i < l; i++)
    2691         637 :     if ((P[i]&3L) == 3) return cgetg(1,t_VECSMALL);
    2692         280 :   A = cgetg(l, t_VECSMALL);
    2693         280 :   B = cgetg(l, t_VECSMALL);
    2694         280 :   mB= cgetg(l, t_VECSMALL);
    2695         280 :   Q = cgetg(l, t_VECSMALL);
    2696         574 :   for (i = 1; i < l; i++)
    2697             :   {
    2698         294 :     long p = P[i], e = E[i];
    2699         294 :     Q[i] = upowuu(p,e);
    2700         294 :     B[i] = itou( Zp_sqrt(gen_m1, utoipos(p), e) );
    2701         294 :     mB[i]= Q[i] - B[i];
    2702             :   }
    2703         280 :   ct = 1 << (l-1);
    2704         280 :   T = ZV_producttree(Q);
    2705         280 :   R = ZV_chinesetree(Q,T);
    2706         280 :   v = cgetg(ct+1, t_VECSMALL);
    2707         280 :   av = avma;
    2708         868 :   for (n = 1; n <= ct; n++)
    2709             :   {
    2710         588 :     long m = n-1, r;
    2711        1232 :     for (i = 1; i < l; i++)
    2712             :     {
    2713         644 :       A[i] = (m&1L)? mB[i]: B[i];
    2714         644 :       m >>= 1;
    2715             :     }
    2716         588 :     r = itou( ZV_chinese_tree(A, Q, T, R) );
    2717         588 :     if (fleven && !odd(r)) r += N;
    2718         588 :     set_avma(av); v[n] = r;
    2719             :   }
    2720         280 :   return v;
    2721             : }
    2722             : 
    2723             : /* contribution of elliptic matrices of order 4 in dimension formula.
    2724             :  * Only depends on CHIP the primitive char attached to CHI */
    2725             : static GEN
    2726       40775 : A22(long N, long k, GEN CHI)
    2727             : {
    2728             :   GEN G, chi, o, res;
    2729             :   long S, a22, i, limx, o2;
    2730       40775 :   if ((N&3L) == 0) return gen_0;
    2731       28525 :   a22 = (k & 3L) - 1; /* (k % 4) - 1 */
    2732       28525 :   if (!a22) return gen_0;
    2733       28525 :   if (N <= 2) return sstoQ(a22, 4);
    2734       17325 :   if (!CHI) return sstoQ(nu2(N)*a22, 4);
    2735         770 :   if (mfcharparity(CHI) == -1) return gen_0;
    2736         567 :   res = sqrtm1modN(N); limx = (N - 1) >> 1;
    2737         567 :   G = gel(CHI,1); chi = gel(CHI,2);
    2738         567 :   o = gmfcharorder(CHI);
    2739         567 :   o2 = itou(o)>>1;
    2740        1155 :   for (S = 0, i = 1; i < lg(res); i++)
    2741             :   { /* (x,N) = 1, S += real(chi(x)) */
    2742         588 :     long x = res[i];
    2743         588 :     if (x <= limx)
    2744             :     { /* CHI(x)=e(c/o), 4th-root of 1 */
    2745         294 :       long c = itou( znchareval(G, chi, utoi(x), o) );
    2746         294 :       if (!c) S++; else if (c == o2) S--;
    2747             :     }
    2748             :   }
    2749         567 :   return sstoQ(a22 * S, 2);
    2750             : }
    2751             : 
    2752             : /* sumdiv(N,d,eulerphi(gcd(d,N/d))) */
    2753             : static long
    2754       36834 : nuinf(long N)
    2755             : {
    2756       36834 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
    2757       36834 :   long i, t = 1, l = lg(P);
    2758       78225 :   for (i=1; i<l; i++)
    2759             :   {
    2760       41391 :     long p = P[i], e = E[i];
    2761       41391 :     if (odd(e))
    2762       33180 :       t *= upowuu(p,e>>1) << 1;
    2763             :     else
    2764        8211 :       t *= upowuu(p,(e>>1)-1) * (p+1);
    2765             :   }
    2766       36834 :   return t;
    2767             : }
    2768             : 
    2769             : /* contribution of hyperbolic matrices in dimension formula */
    2770             : static GEN
    2771       41174 : A3(long N, long FC)
    2772             : {
    2773             :   long i, S, NF, l;
    2774             :   GEN D;
    2775       41174 :   if (FC == 1) return sstoQ(nuinf(N),2);
    2776        4340 :   D = mydivisorsu(N); l = lg(D);
    2777        4340 :   S = 0; NF = N/FC;
    2778       33173 :   for (i = 1; i < l; i++)
    2779             :   {
    2780       28833 :     long g = ugcd(D[i], D[l-i]);
    2781       28833 :     if (NF%g == 0) S += myeulerphiu(g);
    2782             :   }
    2783        4340 :   return sstoQ(S, 2);
    2784             : }
    2785             : 
    2786             : /* special contribution in weight 2 in dimension formula */
    2787             : static long
    2788       40439 : A4(long k, long FC)
    2789       40439 : { return (k==2 && FC==1)? 1: 0; }
    2790             : /* gcd(x,N) */
    2791             : static long
    2792   154604303 : myugcd(GEN GCD, ulong x)
    2793             : {
    2794   154604303 :   ulong N = lg(GCD)-1;
    2795   154604303 :   if (x >= N) x %= N;
    2796   154604303 :   return GCD[x+1];
    2797             : }
    2798             : /* 1_{gcd(x,N) = 1} * chi(x), return NULL if 0 */
    2799             : static GEN
    2800   202770652 : mychicgcd(GEN GCD, GEN VCHI, long x)
    2801             : {
    2802   202770652 :   long N = lg(GCD)-1;
    2803   202770652 :   if (N == 1) return gen_1;
    2804   166609345 :   x = smodss(x, N);
    2805   166609345 :   if (GCD[x+1] != 1) return NULL;
    2806   124099808 :   x %= vchip_FC(VCHI); if (!x) return gen_1;
    2807     6835458 :   return gel(gel(VCHI,1), x);
    2808             : }
    2809             : 
    2810             : /* contribution of scalar matrices to trace formula */
    2811             : static GEN
    2812     4637227 : TA1(long N, long k, GEN VCHI, GEN GCD, long n)
    2813             : {
    2814             :   GEN S;
    2815             :   ulong m;
    2816     4637227 :   if (!uissquareall(n, &m)) return gen_0;
    2817      322084 :   if (m == 1) return A1(N,k); /* common */
    2818      287525 :   S = mychicgcd(GCD, VCHI, m);
    2819      287525 :   return S? gmul(gmul(powuu(m, k-2), A1(N,k)), S): gen_0;
    2820             : }
    2821             : 
    2822             : /* All square roots modulo 4N, x modulo 2N, precomputed to accelerate TA2 */
    2823             : static GEN
    2824      113218 : mksqr(long N)
    2825             : {
    2826      113218 :   pari_sp av = avma;
    2827      113218 :   long x, N2 = N << 1, N4 = N << 2;
    2828      113218 :   GEN v = const_vec(N2, cgetg(1, t_VECSMALL));
    2829      113218 :   gel(v, N2) = mkvecsmall(0); /* x = 0 */
    2830     2862965 :   for (x = 1; x <= N; x++)
    2831             :   {
    2832     2749747 :     long r = (((x*x - 1)%N4) >> 1) + 1;
    2833     2749747 :     gel(v,r) = vecsmall_append(gel(v,r), x);
    2834             :   }
    2835      113218 :   return gerepilecopy(av, v);
    2836             : }
    2837             : 
    2838             : static GEN
    2839      113218 : mkgcd(long N)
    2840             : {
    2841             :   GEN GCD, d;
    2842             :   long i, N2;
    2843      113218 :   if (N == 1) return mkvecsmall(N);
    2844       93058 :   GCD = cgetg(N + 1, t_VECSMALL);
    2845       93058 :   d = GCD+1; /* GCD[i+1] = d[i] = gcd(i,N) = gcd(N-i,N), i = 0..N-1 */
    2846       93058 :   d[0] = N; d[1] = d[N-1] = 1; N2 = N>>1;
    2847       93058 :   for (i = 2; i <= N2; i++) d[i] = d[N-i] = ugcd(N, i);
    2848       93058 :   return GCD;
    2849             : }
    2850             : 
    2851             : /* Table of \sum_{x^2-tx+n=0 mod Ng}chi(x) for all g dividing gcd(N,F),
    2852             :  * F^2 largest such that (t^2-4n)/F^2=0 or 1 mod 4; t >= 0 */
    2853             : static GEN
    2854    12545512 : mutglistall(long t, long N, long NF, GEN VCHI, long n, GEN MUP, GEN li, GEN GCD)
    2855             : {
    2856    12545512 :   long i, lx = lg(li);
    2857    12545512 :   GEN DNF = mydivisorsu(NF), v = zerovec(NF);
    2858    12545512 :   long j, g, lDNF = lg(DNF);
    2859    33263363 :   for (i = 1; i < lx; i++)
    2860             :   {
    2861    20717851 :     long x = (li[i] + t) >> 1, y, lD;
    2862    20717851 :     GEN D, c = mychicgcd(GCD, VCHI, x);
    2863    20717851 :     if (li[i] && li[i] != N)
    2864             :     {
    2865    13050968 :       GEN c2 = mychicgcd(GCD, VCHI, t - x);
    2866    13050968 :       if (c2) c = c? gadd(c, c2): c2;
    2867             :     }
    2868    20717851 :     if (!c) continue;
    2869    12900951 :     y = (x*(x - t) + n) / N; /* exact division */
    2870    12900951 :     D = mydivisorsu(ugcd(labs(y), NF)); lD = lg(D);
    2871    12900951 :     for (j=1; j < lD; j++) { g = D[j]; gel(v,g) = gadd(gel(v,g), c); }
    2872             :   }
    2873             :   /* j = 1 corresponds to g = 1, and MUP[1] = 1 */
    2874    12545512 :   for (j=2; j < lDNF; j++) { g = DNF[j]; gel(v,g) = gmulsg(MUP[g], gel(v,g)); }
    2875    12545512 :   return v;
    2876             : }
    2877             : 
    2878             : /* special case (N,F) = 1: easier */
    2879             : static GEN
    2880    72866570 : mutg1(long t, long N, GEN VCHI, GEN li, GEN GCD)
    2881             : { /* (N,F) = 1 */
    2882    72866570 :   GEN S = NULL;
    2883    72866570 :   long i, lx = lg(li);
    2884   152325264 :   for (i = 1; i < lx; i++)
    2885             :   {
    2886    79458694 :     long x = (li[i] + t) >> 1;
    2887    79458694 :     GEN c = mychicgcd(GCD, VCHI, x);
    2888    79458694 :     if (c) S = S? gadd(S, c): c;
    2889    79458694 :     if (li[i] && li[i] != N)
    2890             :     {
    2891    42133378 :       c = mychicgcd(GCD, VCHI, t - x);
    2892    42133378 :       if (c) S = S? gadd(S, c): c;
    2893             :     }
    2894    79458694 :     if (S && !signe(S)) S = NULL; /* strive hard to add gen_0 */
    2895             :   }
    2896    72866570 :   return S; /* single value */
    2897             : }
    2898             : 
    2899             : /* Gegenbauer pol; n > 2, P = \sum_{0<=j<=n/2} (-1)^j (n-j)!/j!(n-2*j)! X^j */
    2900             : static GEN
    2901      348761 : mfrhopol(long n)
    2902             : {
    2903             : #ifdef LONG_IS_64BIT
    2904      298938 :   const long M = 2642249;
    2905             : #else
    2906       49823 :   const long M = 1629;
    2907             : #endif
    2908      348761 :   long j, d = n >> 1; /* >= 1 */
    2909      348761 :   GEN P = cgetg(d + 3, t_POL);
    2910             : 
    2911      348761 :   if (n > M) pari_err_IMPL("mfrhopol for large weight"); /* avoid overflow */
    2912      348761 :   P[1] = evalvarn(0)|evalsigne(1);
    2913      348761 :   gel(P,2) = gen_1;
    2914      348761 :   gel(P,3) = utoineg(n-1); /* j = 1 */
    2915      348761 :   if (d > 1) gel(P,4) = utoipos(((n-3)*(n-2)) >> 1); /* j = 2 */
    2916      348761 :   if (d > 2) gel(P,5) = utoineg(((n-5)*(n-4)*(n-3)) / 6); /* j = 3 */
    2917     1287594 :   for (j = 4; j <= d; j++)
    2918      938833 :     gel(P,j+2) = divis(mulis(gel(P,j+1), (n-2*j+1)*(n-2*j+2)), (n-j+1)*(-j));
    2919      348761 :   return P;
    2920             : }
    2921             : 
    2922             : /* polrecip(Q)(t2), assume Q(0) = 1 */
    2923             : static GEN
    2924     2976470 : ZXrecip_u_eval(GEN Q, ulong t2)
    2925             : {
    2926     2976470 :   GEN T = addiu(gel(Q,3), t2);
    2927     2976470 :   long l = lg(Q), j;
    2928     2976470 :   for (j = 4; j < l; j++) T = addii(gel(Q,j), mului(t2, T));
    2929     2976470 :   return T;
    2930             : }
    2931             : /* return sh * sqrt(n)^nu * G_nu(t/(2*sqrt(n))) for t != 0
    2932             :  * else (sh/2) * sqrt(n)^nu * G_nu(0) [ implies nu is even ]
    2933             :  * G_nu(z) = \sum_{0<=j<=nu/2} (-1)^j (nu-j)!/j!(nu-2*j)! * (2z)^(nu-2*j)) */
    2934             : static GEN
    2935    77004081 : mfrhopowsimp(GEN Q, GEN sh, long nu, long t, long t2, long n)
    2936             : {
    2937             :   GEN T;
    2938    77004081 :   switch (nu)
    2939             :   {
    2940    69132910 :     case 0: return t? sh: gmul2n(sh,-1);
    2941     3462326 :     case 1: return gmulsg(t, sh);
    2942     1394197 :     case 2: return t? gmulsg(t2 - n, sh): gmul(gmul2n(stoi(-n), -1), sh);
    2943         469 :     case 3: return gmul(mulss(t, t2 - 2*n), sh);
    2944             :     default:
    2945     3014179 :       if (!t) return gmul(gmul2n(gel(Q, lg(Q) - 1), -1), sh);
    2946     2976470 :       T = ZXrecip_u_eval(Q, t2); if (odd(nu)) T = mulsi(t, T);
    2947     2976470 :       return gmul(T, sh);
    2948             :   }
    2949             : }
    2950             : 
    2951             : /* contribution of elliptic matrices to trace formula */
    2952             : static GEN
    2953     4637227 : TA2(long N, long k, GEN VCHI, long n, GEN SQRTS, GEN MUP, GEN GCD)
    2954             : {
    2955     4637227 :   const long n4 = n << 2, N4 = N << 2, nu = k - 2;
    2956     4637227 :   const long st = (!odd(N) && odd(n)) ? 2 : 1;
    2957             :   long limt, t;
    2958             :   GEN S, Q;
    2959             : 
    2960     4637227 :   limt = usqrt(n4);
    2961     4637227 :   if (limt*limt == n4) limt--;
    2962     4637227 :   Q = nu > 3 ? ZX_z_unscale(mfrhopol(nu), n) : NULL;
    2963     4637227 :   S = gen_0;
    2964   146669474 :   for (t = odd(k)? st: 0; t <= limt; t += st) /* t^2 < 4n */
    2965             :   {
    2966   142032247 :     pari_sp av = avma;
    2967   142032247 :     long t2 = t*t, D = n4 - t2, F, D0, NF;
    2968             :     GEN sh, li;
    2969             : 
    2970   142032247 :     li = gel(SQRTS, (smodss(-D - 1, N4) >> 1) + 1);
    2971   207060413 :     if (lg(li) == 1) continue;
    2972    85412082 :     D0 = mycoredisc2neg(D, &F);
    2973    85412082 :     NF = myugcd(GCD, F);
    2974    85412082 :     if (NF == 1)
    2975             :     { /* (N,F) = 1 => single value in mutglistall */
    2976    72866570 :       GEN mut = mutg1(t, N, VCHI, li, GCD);
    2977    72866570 :       if (!mut) { set_avma(av); continue; }
    2978    69622133 :       sh = gmul(sstoQ(hclassno6u_i(D,D0,F),6), mut);
    2979             :     }
    2980             :     else
    2981             :     {
    2982    12545512 :       GEN v = mutglistall(t, N, NF, VCHI, n, MUP, li, GCD);
    2983    12545512 :       GEN DF = mydivisorsu(F);
    2984    12545512 :       long i, lDF = lg(DF);
    2985    12545512 :       sh = gen_0;
    2986    48497540 :       for (i = 1; i < lDF; i++)
    2987             :       {
    2988    35952028 :         long Ff, f = DF[i], g = myugcd(GCD, f);
    2989    35952028 :         GEN mut = gel(v, g);
    2990    35952028 :         if (gequal0(mut)) continue;
    2991    18292379 :         Ff = DF[lDF-i]; /* F/f */
    2992    18292379 :         if (Ff == 1) sh = gadd(sh, mut);
    2993             :         else
    2994             :         {
    2995    12946612 :           GEN P = gel(myfactoru(Ff), 1);
    2996    12946612 :           long j, lP = lg(P);
    2997    12946612 :           for (j = 1; j < lP; j++) { long p = P[j]; Ff -= kross(D0, p)*Ff/p; }
    2998    12946612 :           sh = gadd(sh, gmulsg(Ff, mut));
    2999             :         }
    3000             :       }
    3001    12545512 :       if (gequal0(sh)) { set_avma(av); continue; }
    3002     7381948 :       if (D0 == -3) sh = gdivgs(sh, 3);
    3003     7004326 :       else if (D0 == -4) sh = gdivgs(sh, 2);
    3004     6654543 :       else sh = gmulgs(sh, myh(D0));
    3005             :     }
    3006    77004081 :     S = gerepileupto(av, gadd(S, mfrhopowsimp(Q,sh,nu,t,t2,n)));
    3007             :   }
    3008     4637227 :   return S;
    3009             : }
    3010             : 
    3011             : /* compute global auxiliary data for TA3 */
    3012             : static GEN
    3013      113218 : mkbez(long N, long FC)
    3014             : {
    3015      113218 :   long ct, i, NF = N/FC;
    3016      113218 :   GEN w, D = mydivisorsu(N);
    3017      113218 :   long l = lg(D);
    3018             : 
    3019      113218 :   w = cgetg(l, t_VEC);
    3020      323568 :   for (i = ct = 1; i < l; i++)
    3021             :   {
    3022      303408 :     long u, v, h, c = D[i], Nc = D[l-i];
    3023      303408 :     if (c > Nc) break;
    3024      210350 :     h = cbezout(c, Nc, &u, &v);
    3025      210350 :     if (h == 1) /* shortcut */
    3026      154707 :       gel(w, ct++) = mkvecsmall4(1,u*c,1,i);
    3027       55643 :     else if (!(NF%h))
    3028       49987 :       gel(w, ct++) = mkvecsmall4(h,u*(c/h),myeulerphiu(h),i);
    3029             :   }
    3030      113218 :   setlg(w,ct); stackdummy((pari_sp)(w+ct),(pari_sp)(w+l));
    3031      113218 :   return w;
    3032             : }
    3033             : 
    3034             : /* contribution of hyperbolic matrices to trace formula, d * nd = n,
    3035             :  * DN = divisorsu(N) */
    3036             : static GEN
    3037    19736934 : auxsum(GEN VCHI, GEN GCD, long d, long nd, GEN DN, GEN BEZ)
    3038             : {
    3039    19736934 :   GEN S = gen_0;
    3040    19736934 :   long ct, g = nd - d, lDN = lg(DN), lBEZ = lg(BEZ);
    3041    51899554 :   for (ct = 1; ct < lBEZ; ct++)
    3042             :   {
    3043    32162620 :     GEN y, B = gel(BEZ, ct);
    3044    32162620 :     long ic, c, Nc, uch, h = B[1];
    3045    32162620 :     if (g%h) continue;
    3046    31481947 :     uch = B[2];
    3047    31481947 :     ic  = B[4];
    3048    31481947 :     c = DN[ic];
    3049    31481947 :     Nc= DN[lDN - ic]; /* Nc = N/c */
    3050    31481947 :     if (ugcd(Nc, nd) == 1)
    3051    24912517 :       y = mychicgcd(GCD, VCHI, d + uch*g); /* 0 if (c,d) > 1 */
    3052             :     else
    3053     6569430 :       y = NULL;
    3054    31481947 :     if (c != Nc && ugcd(Nc, d) == 1)
    3055             :     {
    3056    22209719 :       GEN y2 = mychicgcd(GCD, VCHI, nd - uch*g); /* 0 if (c,nd) > 1 */
    3057    22209719 :       if (y2) y = y? gadd(y, y2): y2;
    3058             :     }
    3059    31481947 :     if (y) S = gadd(S, gmulsg(B[3], y));
    3060             :   }
    3061    19736934 :   return S;
    3062             : }
    3063             : 
    3064             : static GEN
    3065     4637227 : TA3(long N, long k, GEN VCHI, GEN GCD, GEN Dn, GEN BEZ)
    3066             : {
    3067     4637227 :   GEN S = gen_0, DN = mydivisorsu(N);
    3068     4637227 :   long i, l = lg(Dn);
    3069    24374161 :   for (i = 1; i < l; i++)
    3070             :   {
    3071    24339602 :     long d = Dn[i], nd = Dn[l-i]; /* = n/d */
    3072             :     GEN t, u;
    3073    24339602 :     if (d > nd) break;
    3074    19736934 :     t = auxsum(VCHI, GCD, d, nd, DN, BEZ);
    3075    19736934 :     if (isintzero(t)) continue;
    3076    18433611 :     u = powuu(d,k-1); if (d == nd) u = gmul2n(u,-1);
    3077    18433611 :     S = gadd(S, gmul(u,t));
    3078             :   }
    3079     4637227 :   return S;
    3080             : }
    3081             : 
    3082             : /* special contribution in weight 2 in trace formula */
    3083             : static long
    3084     4637227 : TA4(long k, GEN VCHIP, GEN Dn, GEN GCD)
    3085             : {
    3086             :   long i, l, S;
    3087     4637227 :   if (k != 2 || vchip_FC(VCHIP) != 1) return 0;
    3088     3953250 :   l = lg(Dn); S = 0;
    3089    37193443 :   for (i = 1; i < l; i++)
    3090             :   {
    3091    33240193 :     long d = Dn[i]; /* gcd(N,n/d) == 1? */
    3092    33240193 :     if (myugcd(GCD, Dn[l-i]) == 1) S += d;
    3093             :   }
    3094     3953250 :   return S;
    3095             : }
    3096             : 
    3097             : /* precomputation of products occurring im mutg, again to accelerate TA2 */
    3098             : static GEN
    3099      113218 : mkmup(long N)
    3100             : {
    3101      113218 :   GEN fa = myfactoru(N), P = gel(fa,1), D = divisorsu_fact(fa);
    3102      113218 :   long i, lP = lg(P), lD = lg(D);
    3103      113218 :   GEN MUP = zero_zv(N);
    3104      113218 :   MUP[1] = 1;
    3105      387345 :   for (i = 2; i < lD; i++)
    3106             :   {
    3107      274127 :     long j, g = D[i], Ng = D[lD-i]; /*  N/g */
    3108      274127 :     for (j = 1; j < lP; j++) { long p = P[j]; if (Ng%p) g += g/p; }
    3109      274127 :     MUP[D[i]] = g;
    3110             :   }
    3111      113218 :   return MUP;
    3112             : }
    3113             : 
    3114             : /* quadratic non-residues mod p; p odd prime, p^2 fits in a long */
    3115             : static GEN
    3116        1400 : non_residues(long p)
    3117             : {
    3118        1400 :   long i, j, p2 = p >> 1;
    3119        1400 :   GEN v = cgetg(p2+1, t_VECSMALL), w = const_vecsmall(p-1, 1);
    3120        1400 :   for (i = 2; i <= p2; i++) w[(i*i) % p] = 0; /* no need to check 1 */
    3121        1400 :   for (i = 2, j = 1; i < p; i++) if (w[i]) v[j++] = i;
    3122        1400 :   return v;
    3123             : }
    3124             : 
    3125             : /* CHIP primitive. Return t_VECSMALL v of length q such that
    3126             :  * Tr^new_{N,CHIP}(n) = 0 whenever v[(n%q) + 1] is non-zero */
    3127             : static GEN
    3128       28413 : mfnewzerodata(long N, GEN CHIP)
    3129             : {
    3130       28413 :   GEN V, M, L, faN = myfactoru(N), PN = gel(faN,1), EN = gel(faN,2);
    3131       28413 :   GEN G = gel(CHIP,1), chi = gel(CHIP,2);
    3132       28413 :   GEN fa = znstar_get_faN(G), P = ZV_to_zv(gel(fa,1)), E = gel(fa,2);
    3133       28413 :   long i, mod, j = 1, l = lg(PN);
    3134             : 
    3135       28413 :   M = cgetg(l, t_VECSMALL); M[1] = 0;
    3136       28413 :   V = cgetg(l, t_VEC);
    3137             :   /* Tr^new(n) = 0 if (n mod M[i]) in V[i]  */
    3138       28413 :   if ((N & 3) == 0)
    3139             :   {
    3140       10668 :     long e = EN[1];
    3141       10668 :     long c = (lg(P) > 1 && P[1] == 2)? E[1]: 0; /* c = v_2(FC) */
    3142             :     /* e >= 2 */
    3143       10668 :     if (c == e-1) return NULL; /* Tr^new = 0 */
    3144       10633 :     if (c == e)
    3145             :     {
    3146        2422 :       if (e == 2)
    3147             :       { /* sc: -4 */
    3148        1652 :         gel(V,1) = mkvecsmall(3);
    3149        1652 :         M[1] = 4;
    3150             :       }
    3151         770 :       else if (e == 3)
    3152             :       { /* sc: -8 (CHI_2(-1)=-1<=>chi[1]=1) and 8 (CHI_2(-1)=1 <=> chi[1]=0) */
    3153         770 :         long t = signe(gel(chi,1))? 7: 3;
    3154         770 :         gel(V,1) = mkvecsmall2(5, t);
    3155         770 :         M[1] = 8;
    3156             :       }
    3157             :     }
    3158        8211 :     else if (e == 5 && c == 3)
    3159         154 :     { /* sc: -8 (CHI_2(-1)=-1<=>chi[1]=1) and 8 (CHI_2(-1)=1 <=> chi[1]=0) */
    3160         154 :       long t = signe(gel(chi,1))? 7: 3;
    3161         154 :       gel(V,1) = mkvecsmalln(6, 2L,4L,5L,6L,8L,t);
    3162         154 :       M[1] = 8;
    3163             :     }
    3164        8057 :     else if ((e == 4 && c == 2) || (e == 5 && c <= 2) || (e == 6 && c <= 2)
    3165        6629 :          || (e >= 7 && c == e - 3))
    3166             :     { /* sc: 4 */
    3167        1428 :       gel(V,1) = mkvecsmall3(0,2,3);
    3168        1428 :       M[1] = 4;
    3169             :     }
    3170        6629 :     else if ((e <= 4 && c == 0) || (e >= 5 && c == e - 2))
    3171             :     { /* sc: 2 */
    3172        6363 :       gel(V,1) = mkvecsmall(0);
    3173        6363 :       M[1] = 2;
    3174             :     }
    3175         266 :     else if ((e == 6 && c == 3) || (e >= 7 && c <= e - 4))
    3176             :     { /* sc: -2 */
    3177         266 :       gel(V,1) = mkvecsmalln(7, 0L,2L,3L,4L,5L,6L,7L);
    3178         266 :       M[1] = 8;
    3179             :     }
    3180             :   }
    3181       28378 :   j = M[1]? 2: 1;
    3182       61173 :   for (i = odd(N)? 1: 2; i < l; i++) /* skip p=2, done above */
    3183             :   {
    3184       32795 :     long p = PN[i], e = EN[i];
    3185       32795 :     long z = zv_search(P, p), c = z? E[z]: 0; /* c = v_p(FC) */
    3186       32795 :     if ((e <= 2 && c == 1 && itos(gel(chi,z)) == (p>>1)) /* ord(CHI_p)=2 */
    3187       31857 :         || (e >= 3 && c <= e - 2))
    3188        1400 :     { /* sc: -p */
    3189        1400 :       GEN v = non_residues(p);
    3190        1400 :       if (e != 1) v = vecsmall_prepend(v, 0);
    3191        1400 :       gel(V,j) = v;
    3192        1400 :       M[j] = p; j++;
    3193             :     }
    3194       31395 :     else if (e >= 2 && c < e)
    3195             :     { /* sc: p */
    3196        2107 :       gel(V,j) = mkvecsmall(0);
    3197        2107 :       M[j] = p; j++;
    3198             :     }
    3199             :   }
    3200       28378 :   if (j == 1) return cgetg(1, t_VECSMALL);
    3201       12614 :   setlg(V,j); setlg(M,j); mod = zv_prod(M);
    3202       12614 :   L = zero_zv(mod);
    3203       26754 :   for (i = 1; i < j; i++)
    3204             :   {
    3205       14140 :     GEN v = gel(V,i);
    3206       14140 :     long s, m = M[i], lv = lg(v);
    3207       36400 :     for (s = 1; s < lv; s++)
    3208             :     {
    3209       22260 :       long a = v[s] + 1;
    3210       32046 :       do { L[a] = 1; a += m; } while (a <= mod);
    3211             :     }
    3212             :   }
    3213       12614 :   return L;
    3214             : }
    3215             : /* v=mfnewzerodata(N,CHI); returns TRUE if newtrace(n) must be zero,
    3216             :  * (but newtrace(n) may still be zero if we return FALSE) */
    3217             : static long
    3218     1860593 : mfnewchkzero(GEN v, long n) { long q = lg(v)-1; return q && v[(n%q) + 1]; }
    3219             : 
    3220             : /* if (!VCHIP): from mftraceform_cusp;
    3221             :  * else from initnewtrace and CHI is known to be primitive */
    3222             : static GEN
    3223      113218 : inittrace(long N, GEN CHI, GEN VCHIP)
    3224             : {
    3225             :   long FC;
    3226      113218 :   if (VCHIP)
    3227      113211 :     FC = mfcharmodulus(CHI);
    3228             :   else
    3229           7 :     VCHIP = mfcharinit(mfchartoprimitive(CHI, &FC));
    3230      113218 :   return mkvecn(5, mksqr(N), mkmup(N), mkgcd(N), VCHIP, mkbez(N, FC));
    3231             : }
    3232             : 
    3233             : /* p > 2 prime; return a sorted t_VECSMALL of primes s.t Tr^new(p) = 0 for all
    3234             :  * weights > 2 */
    3235             : static GEN
    3236       28378 : inittrconj(long N, long FC)
    3237             : {
    3238             :   GEN fa, P, E, v;
    3239             :   long i, k, l;
    3240             : 
    3241       28378 :   if (FC != 1) return cgetg(1,t_VECSMALL);
    3242             : 
    3243       24367 :   fa = myfactoru(N >> vals(N));
    3244       24367 :   P = gel(fa,1); l = lg(P);
    3245       24367 :   E = gel(fa,2);
    3246       24367 :   v = cgetg(l, t_VECSMALL);
    3247       53487 :   for (i = k = 1; i < l; i++)
    3248             :   {
    3249       29120 :     long j, p = P[i]; /* > 2 */
    3250       70644 :     for (j = 1; j < l; j++)
    3251       41524 :       if (j != i && E[j] == 1 && kross(-p, P[j]) == 1) v[k++] = p;
    3252             :   }
    3253       24367 :   setlg(v,k); return v;
    3254             : }
    3255             : 
    3256             : /* assume CHIP primitive, f(CHIP) | N; NZ = mfnewzerodata(N,CHIP) */
    3257             : static GEN
    3258       28378 : initnewtrace_i(long N, GEN CHIP, GEN NZ)
    3259             : {
    3260       28378 :   GEN T = const_vec(N, cgetg(1,t_VEC)), D, VCHIP;
    3261       28378 :   long FC = mfcharmodulus(CHIP), N1, N2, i, l;
    3262             : 
    3263       28378 :   if (!NZ) NZ = mkvecsmall(1); /*Tr^new = 0; initialize data nevertheless*/
    3264       28378 :   VCHIP = mfcharinit(CHIP);
    3265       28378 :   N1 = N/FC; newd_params(N1, &N2);
    3266       28378 :   D = mydivisorsu(N1/N2); l = lg(D);
    3267       28378 :   N2 *= FC;
    3268      141589 :   for (i = 1; i < l; i++)
    3269             :   {
    3270      113211 :     long M = D[i]*N2;
    3271      113211 :     gel(T,M) = inittrace(M, CHIP, VCHIP);
    3272             :   }
    3273       28378 :   gel(T,N) = shallowconcat(gel(T,N), mkvec2(NZ, inittrconj(N,FC)));
    3274       28378 :   return T;
    3275             : }
    3276             : /* don't initialize if Tr^new = 0, return NULL */
    3277             : static GEN
    3278       28413 : initnewtrace(long N, GEN CHI)
    3279             : {
    3280       28413 :   GEN CHIP = mfchartoprimitive(CHI, NULL), NZ = mfnewzerodata(N,CHIP);
    3281       28413 :   return NZ? initnewtrace_i(N, CHIP, NZ): NULL;
    3282             : }
    3283             : 
    3284             : /* (-1)^k */
    3285             : static long
    3286        7126 : m1pk(long k) { return odd(k)? -1 : 1; }
    3287             : static long
    3288        6881 : badchar(long N, long k, GEN CHI)
    3289        6881 : { return mfcharparity(CHI) != m1pk(k) || (CHI && N % mfcharconductor(CHI)); }
    3290             : 
    3291             : /* dimension of space of cusp forms S_k(\G_0(N),CHI)
    3292             :  * Only depends on CHIP the primitive char attached to CHI */
    3293             : long
    3294       40446 : mfcuspdim(long N, long k, GEN CHI)
    3295             : {
    3296       40446 :   pari_sp av = avma;
    3297             :   long FC;
    3298             :   GEN s;
    3299       40446 :   if (k <= 0) return 0;
    3300       40446 :   if (k == 1) return mfwt1cuspdim(N, CHI);
    3301       40257 :   FC = CHI? mfcharconductor(CHI): 1;
    3302       40257 :   if (FC == 1) CHI = NULL;
    3303       40257 :   s = gsub(A1(N, k), gadd(A21(N, k, CHI), A22(N, k, CHI)));
    3304       40257 :   s = gadd(s, gsubsg(A4(k, FC), A3(N, FC)));
    3305       40257 :   return gc_long(av, itos(s));
    3306             : }
    3307             : 
    3308             : /* dimension of whole space M_k(\G_0(N),CHI)
    3309             :  * Only depends on CHIP the primitive char attached to CHI; assumes !badchar */
    3310             : long
    3311         686 : mffulldim(long N, long k, GEN CHI)
    3312             : {
    3313         686 :   pari_sp av = avma;
    3314         686 :   long FC = CHI? mfcharconductor(CHI): 1;
    3315             :   GEN s;
    3316         686 :   if (k <= 0) return (k == 0 && FC == 1)? 1: 0;
    3317         686 :   if (k == 1) return gc_long(av, itos(A3(N, FC)) + mfwt1cuspdim(N, CHI));
    3318         518 :   if (FC == 1) CHI = NULL;
    3319         518 :   s = gsub(A1(N, k), gadd(A21(N, k, CHI), A22(N, k, CHI)));
    3320         518 :   s = gadd(s, A3(N, FC));
    3321         518 :   return gc_long(av, itos(s));
    3322             : }
    3323             : 
    3324             : /* Dimension of the space of Eisenstein series */
    3325             : long
    3326         231 : mfeisensteindim(long N, long k, GEN CHI)
    3327             : {
    3328         231 :   pari_sp av = avma;
    3329         231 :   long s, FC = CHI? mfcharconductor(CHI): 1;
    3330         231 :   if (k <= 0) return (k == 0 && FC == 1)? 1: 0;
    3331         231 :   s = itos(gmul2n(A3(N, FC), 1));
    3332         231 :   if (k > 1) s -= A4(k, FC); else s >>= 1;
    3333         231 :   return gc_long(av,s);
    3334             : }
    3335             : 
    3336             : enum { _SQRTS = 1, _MUP, _GCD, _VCHIP, _BEZ, _NEWLZ, _TRCONJ };
    3337             : /* Trace of T(n) on space of cuspforms; only depends on CHIP the primitive char
    3338             :  * attached to CHI */
    3339             : static GEN
    3340     4637227 : mfcusptrace_i(long N, long k, long n, GEN Dn, GEN S)
    3341             : {
    3342     4637227 :   pari_sp av = avma;
    3343             :   GEN a, b, VCHIP, GCD;
    3344             :   long t;
    3345     4637227 :   if (!n) return gen_0;
    3346     4637227 :   VCHIP = gel(S,_VCHIP);
    3347     4637227 :   GCD = gel(S,_GCD);
    3348     4637227 :   t = TA4(k, VCHIP, Dn, GCD);
    3349     4637227 :   a = TA1(N, k, VCHIP, GCD, n); if (t) a = gaddgs(a,t);
    3350     4637227 :   b = TA2(N, k, VCHIP, n, gel(S,_SQRTS), gel(S,_MUP), GCD);
    3351     4637227 :   b = gadd(b, TA3(N, k, VCHIP, GCD, Dn, gel(S,_BEZ)));
    3352     4637227 :   b = gsub(a,b);
    3353     4637227 :   if (typ(b) != t_POL) return gerepileupto(av, b);
    3354       40264 :   return gerepilecopy(av, vchip_polmod(VCHIP, b));
    3355             : }
    3356             : 
    3357             : static GEN
    3358     5957350 : mfcusptracecache(long N, long k, long n, GEN Dn, GEN S, cachenew_t *cache)
    3359             : {
    3360     5957350 :   GEN C = NULL, T = gel(cache->vfull,N);
    3361     5957350 :   long lcache = lg(T);
    3362     5957350 :   if (n < lcache) C = gel(T, n);
    3363     5957350 :   if (C) cache->cuspHIT++; else C = mfcusptrace_i(N, k, n, Dn, S);
    3364     5957350 :   cache->cuspTOTAL++;
    3365     5957350 :   if (n < lcache) gel(T,n) = C;
    3366     5957350 :   return C;
    3367             : }
    3368             : 
    3369             : /* return the divisors of n, known to be among the elements of D */
    3370             : static GEN
    3371      361333 : div_restrict(GEN D, ulong n)
    3372             : {
    3373             :   long i, j, l;
    3374      361333 :   GEN v, VDIV = caches[cache_DIV].cache;
    3375      361333 :   if (lg(VDIV) > n) return gel(VDIV,n);
    3376           0 :   l = lg(D);
    3377           0 :   v = cgetg(l, t_VECSMALL);
    3378           0 :   for (i = j = 1; i < l; i++)
    3379             :   {
    3380           0 :     ulong d = D[i];
    3381           0 :     if (n % d == 0) v[j++] = d;
    3382             :   }
    3383           0 :   setlg(v,j); return v;
    3384             : }
    3385             : 
    3386             : /* for some prime divisors of N, Tr^new(p) = 0 */
    3387             : static int
    3388      223237 : trconj(GEN T, long N, long n)
    3389      223237 : { return (lg(T) > 1 && N % n == 0 && zv_search(T, n)); }
    3390             : 
    3391             : /* n > 0; trace formula on new space */
    3392             : static GEN
    3393     1860593 : mfnewtrace_i(long N, long k, long n, cachenew_t *cache)
    3394             : {
    3395     1860593 :   GEN VCHIP, s, Dn, DN1, SN, S = cache->DATA;
    3396             :   long FC, N1, N2, N1N2, g, i, j, lDN1;
    3397             : 
    3398     1860593 :   if (!S) return gen_0;
    3399     1860593 :   SN = gel(S,N);
    3400     1860593 :   if (mfnewchkzero(gel(SN,_NEWLZ), n)) return gen_0;
    3401     1434650 :   if (k > 2 && trconj(gel(SN,_TRCONJ), N, n)) return gen_0;
    3402     1434615 :   VCHIP = gel(SN, _VCHIP); FC = vchip_FC(VCHIP);
    3403     1434615 :   N1 = N/FC; newt_params(N1, n, FC, &g, &N2);
    3404     1434615 :   N1N2 = N1/N2;
    3405     1434615 :   DN1 = mydivisorsu(N1N2); lDN1 = lg(DN1);
    3406     1434615 :   N2 *= FC;
    3407     1434615 :   Dn = mydivisorsu(n); /* this one is probably out of cache */
    3408     1434615 :   s = gmulsg(mubeta2(N1N2,n), mfcusptracecache(N2, k, n, Dn, gel(S,N2), cache));
    3409     5596017 :   for (i = 2; i < lDN1; i++)
    3410             :   { /* skip M1 = 1, done above */
    3411     4161402 :     long M1 = DN1[i], N1M1 = DN1[lDN1-i];
    3412     4161402 :     GEN Dg = mydivisorsu(ugcd(M1, g));
    3413     4161402 :     M1 *= N2;
    3414     4161402 :     s = gadd(s, gmulsg(mubeta2(N1M1,n),
    3415     4161402 :                        mfcusptracecache(M1, k, n, Dn, gel(S,M1), cache)));
    3416     4522735 :     for (j = 2; j < lg(Dg); j++) /* skip d = 1, done above */
    3417             :     {
    3418      361333 :       long d = Dg[j], ndd = n/(d*d), M = M1/d;
    3419      361333 :       GEN z = mulsi(mubeta2(N1M1,ndd), powuu(d,k-1)), C = vchip_lift(VCHIP,d,z);
    3420      361333 :       GEN Dndd = div_restrict(Dn, ndd);
    3421      361333 :       s = gadd(s, gmul(C, mfcusptracecache(M, k, ndd, Dndd, gel(S,M), cache)));
    3422             :     }
    3423     4161402 :     s = vchip_mod(VCHIP, s);
    3424             :   }
    3425     1434615 :   return vchip_polmod(VCHIP, s);
    3426             : }
    3427             : 
    3428             : /* mfcuspdim(N,k,CHI) - mfnewdim(N,k,CHI); CHIP primitive (for efficiency) */
    3429             : static long
    3430        7679 : mfolddim_i(long N, long k, GEN CHIP)
    3431             : {
    3432        7679 :   long S, i, l, FC = mfcharmodulus(CHIP), N1 = N/FC, N2;
    3433             :   GEN D;
    3434        7679 :   newd_params(N1, &N2); /* will ensure mubeta != 0 */
    3435        7679 :   D = mydivisorsu(N1/N2); l = lg(D);
    3436        7679 :   N2 *= FC; S = 0;
    3437       30163 :   for (i = 2; i < l; i++)
    3438             :   {
    3439       22484 :     long M = D[l-i]*N2, d = mfcuspdim(M, k, CHIP);
    3440       22484 :     if (d) S -= mubeta(D[i]) * d;
    3441             :   }
    3442        7679 :   return S;
    3443             : }
    3444             : long
    3445         399 : mfolddim(long N, long k, GEN CHI)
    3446             : {
    3447         399 :   pari_sp av = avma;
    3448         399 :   GEN CHIP = mfchartoprimitive(CHI, NULL);
    3449         399 :   return gc_long(av, mfolddim_i(N, k, CHIP));
    3450             : }
    3451             : /* Only depends on CHIP the primitive char attached to CHI; assumes !badchar */
    3452             : long
    3453       14819 : mfnewdim(long N, long k, GEN CHI)
    3454             : {
    3455             :   pari_sp av;
    3456             :   long S;
    3457       14819 :   GEN CHIP = mfchartoprimitive(CHI, NULL);
    3458       14819 :   S = mfcuspdim(N, k, CHIP); if (!S) return 0;
    3459        7266 :   av = avma; return gc_long(av, S - mfolddim_i(N, k, CHIP));
    3460             : }
    3461             : 
    3462             : /* trace form, given as closure */
    3463             : static GEN
    3464         896 : mftraceform_new(long N, long k, GEN CHI)
    3465             : {
    3466             :   GEN T;
    3467         896 :   if (k == 1) return initwt1newtrace(mfinit_Nkchi(N, 1, CHI, mf_CUSP, 0));
    3468         875 :   T = initnewtrace(N,CHI); if (!T) return mftrivial();
    3469         875 :   return tag(t_MF_NEWTRACE, mkNK(N,k,CHI), T);
    3470             : }
    3471             : static GEN
    3472          14 : mftraceform_cusp(long N, long k, GEN CHI)
    3473             : {
    3474          14 :   if (k == 1) return initwt1trace(mfinit_Nkchi(N, 1, CHI, mf_CUSP, 0));
    3475           7 :   return tag(t_MF_TRACE, mkNK(N,k,CHI), inittrace(N,CHI,NULL));
    3476             : }
    3477             : static GEN
    3478          91 : mftraceform_i(GEN NK, long space)
    3479             : {
    3480             :   GEN CHI;
    3481             :   long N, k;
    3482          91 :   checkNK(NK, &N, &k, &CHI, 0);
    3483          91 :   if (!mfdim_Nkchi(N, k, CHI, space)) return mftrivial();
    3484          70 :   switch(space)
    3485             :   {
    3486          49 :     case mf_NEW: return mftraceform_new(N, k, CHI);
    3487          14 :     case mf_CUSP:return mftraceform_cusp(N, k, CHI);
    3488             :   }
    3489           7 :   pari_err_DOMAIN("mftraceform", "space", "=", utoi(space), NK);
    3490             :   return NULL;/*LCOV_EXCL_LINE*/
    3491             : }
    3492             : GEN
    3493          91 : mftraceform(GEN NK, long space)
    3494          91 : { pari_sp av = avma; return gerepilecopy(av, mftraceform_i(NK,space)); }
    3495             : 
    3496             : static GEN
    3497       15141 : hecke_data(long N, long n)
    3498       15141 : { return mkvecsmall3(n, u_ppo(n, N), N); }
    3499             : /* 1/2-integral weight */
    3500             : static GEN
    3501          84 : heckef2_data(long N, long n)
    3502             : {
    3503             :   ulong f, fN, fN2;
    3504          84 :   if (!uissquareall(n, &f)) return NULL;
    3505          77 :   fN = u_ppo(f, N); fN2 = fN*fN;
    3506          77 :   return mkvec2(myfactoru(fN), mkvecsmall4(n, N, fN2, n/fN2));
    3507             : }
    3508             : /* N = mf_get_N(F) or a multiple */
    3509             : static GEN
    3510       21854 : mfhecke_i(long n, long N, GEN F)
    3511             : {
    3512       21854 :   if (n == 1) return F;
    3513       14931 :   return tag2(t_MF_HECKE, mf_get_NK(F), hecke_data(N,n), F);
    3514             : }
    3515             : 
    3516             : GEN
    3517         105 : mfhecke(GEN mf, GEN F, long n)
    3518             : {
    3519         105 :   pari_sp av = avma;
    3520             :   GEN NK, CHI, gk, DATA;
    3521             :   long N, nk, dk;
    3522         105 :   mf = checkMF(mf);
    3523         105 :   if (!checkmf_i(F)) pari_err_TYPE("mfhecke",F);
    3524         105 :   if (n <= 0) pari_err_TYPE("mfhecke [n <= 0]", stoi(n));
    3525         105 :   if (n == 1) return gcopy(F);
    3526         105 :   gk = mf_get_gk(F);
    3527         105 :   Qtoss(gk,&nk,&dk);
    3528         105 :   CHI = mf_get_CHI(F);
    3529         105 :   N = MF_get_N(mf);
    3530         105 :   if (dk == 2)
    3531             :   {
    3532          77 :     DATA = heckef2_data(N,n);
    3533          77 :     if (!DATA) return mftrivial();
    3534             :   }
    3535             :   else
    3536          28 :     DATA = hecke_data(N,n);
    3537          98 :   NK = mkgNK(lcmii(stoi(N), mf_get_gN(F)), gk, CHI, mf_get_field(F));
    3538          98 :   return gerepilecopy(av, tag2(t_MF_HECKE, NK, DATA, F));
    3539             : }
    3540             : 
    3541             : /* form F given by closure, compute B(d)(F) as closure (q -> q^d) */
    3542             : static GEN
    3543       28427 : mfbd_i(GEN F, long d)
    3544             : {
    3545             :   GEN D, NK, gk, CHI;
    3546       28427 :   if (d == 1) return F;
    3547       10122 :   if (d <= 0) pari_err_TYPE("mfbd [d <= 0]", stoi(d));
    3548       10122 :   if (mf_get_type(F) != t_MF_BD) D = utoi(d);
    3549           7 :   else { D = mului(d, gel(F,3)); F = gel(F,2); }
    3550       10122 :   gk = mf_get_gk(F); CHI = mf_get_CHI(F);
    3551       10122 :   if (typ(gk) != t_INT) CHI = mfcharmul(CHI, get_mfchar(utoi(d << 2)));
    3552       10122 :   NK = mkgNK(muliu(mf_get_gN(F), d), gk, CHI, mf_get_field(F));
    3553       10122 :   return tag2(t_MF_BD, NK, F, D);
    3554             : }
    3555             : GEN
    3556          35 : mfbd(GEN F, long d)
    3557             : {
    3558          35 :   pari_sp av = avma;
    3559          35 :   if (!checkmf_i(F)) pari_err_TYPE("mfbd",F);
    3560          35 :   return gerepilecopy(av, mfbd_i(F, d));
    3561             : }
    3562             : 
    3563             : /* CHI is a character defined modulo N4 */
    3564             : static GEN
    3565          98 : RgV_shimura(GEN V, long n, long D, long N4, long r, GEN CHI)
    3566             : {
    3567          98 :   GEN R, a0, Pn = mfcharpol(CHI);
    3568          98 :   long m, Da, ND, ord = mfcharorder(CHI), vt = varn(Pn), d4 = D & 3L;
    3569             : 
    3570          98 :   if (d4 == 2 || d4 == 3) D *= 4;
    3571          98 :   Da = labs(D); ND = N4*Da;
    3572          98 :   R = cgetg(n + 2, t_VEC);
    3573          98 :   a0 = gel(V, 1);
    3574          98 :   if (!gequal0(a0))
    3575             :   {
    3576           7 :     long D4 = D << 2;
    3577           7 :     GEN CHID = induceN(ulcm(mfcharmodulus(CHI), labs(D4)), CHI);
    3578           7 :     CHID = mfcharmul_i(CHID, induce(gel(CHID,1), stoi(D4)));
    3579           7 :     a0 = gmul(a0, charLFwtk(r, CHID, mfcharorder(CHID)));
    3580             :   }
    3581          98 :   if (odd(ND) && !odd(mfcharmodulus(CHI))) ND <<= 1;
    3582          98 :   gel(R, 1) = a0;
    3583         567 :   for (m = 1; m <= n; m++)
    3584             :   {
    3585         469 :     GEN Dm = mydivisorsu(u_ppo(m, ND)), S = gel(V, m*m + 1);
    3586         469 :     long i, l = lg(Dm);
    3587         770 :     for (i = 2; i < l; i++)
    3588             :     { /* (e,ND) = 1; skip i = 1: e = 1, done above */
    3589         301 :       long e = Dm[i], me = m / e;
    3590         301 :       long a = mfcharevalord(CHI, e, ord);
    3591         301 :       GEN c, C = powuu(e, r - 1);
    3592         301 :       if (kross(D, e) == -1) C = negi(C);
    3593         301 :       c = mygmodulo_lift(a, ord, C, vt);
    3594         301 :       S = gadd(S, gmul(c, gel(V, me*me + 1)));
    3595             :     }
    3596         469 :     gel(R, m+1) = S;
    3597             :   }
    3598          98 :   return degpol(Pn) > 1? gmodulo(R, Pn): R;
    3599             : }
    3600             : static GEN
    3601          28 : c_shimura(long n, GEN F, long D, GEN CHI)
    3602             : {
    3603          28 :   GEN v = mfcoefs_i(F, n*n, labs(D));
    3604          28 :   return RgV_shimura(v, n, D, mf_get_N(F)>>2, mf_get_r(F), CHI);
    3605             : }
    3606             : 
    3607             : static long
    3608          14 : mfisinkohnen(GEN mf, GEN F)
    3609             : {
    3610          14 :   GEN v, gk = MF_get_gk(mf), CHI = MF_get_CHI(mf);
    3611          14 :   long i, sb, eps, N4 = MF_get_N(mf) >> 2, r = MF_get_r(mf);
    3612          14 :   sb = mfsturmNgk(N4 << 4, gk) + 1;
    3613          14 :   eps = N4 % mfcharconductor(CHI)? -1 : 1;
    3614          14 :   if (odd(r)) eps = -eps;
    3615          14 :   v = mfcoefs(F, sb, 1);
    3616         896 :   for (i = 0; i <= sb; i++)
    3617             :   {
    3618         882 :     long j = i & 3L;
    3619         882 :     if ((j == 2 || j == 2 + eps) && !gequal0(gel(v,i+1))) return 0;
    3620             :   }
    3621          14 :   return 1;
    3622             : }
    3623             : 
    3624             : static long
    3625          35 : mfshimura_space_cusp(GEN mf)
    3626             : {
    3627          35 :   long fl = 1, r = MF_get_r(mf), M = MF_get_N(mf) >> 2;
    3628          35 :   if (r == 1 && M >= 4)
    3629             :   {
    3630          14 :     GEN E = gel(myfactoru(M), 2);
    3631          14 :     long ma = vecsmall_max(E);
    3632          14 :     if (ma > 2 || (ma == 2 && !mfcharistrivial(MF_get_CHI(mf)))) fl = 0;
    3633             :   }
    3634          35 :   return fl;
    3635             : }
    3636             : 
    3637             : /* D is either a discriminant (not necessarily fundamental) with
    3638             :    sign(D)=(-1)^{k-1/2}*eps, or a positive squarefree integer t, which is then
    3639             :    transformed into a fundamental discriminant of the correct sign. */
    3640             : GEN
    3641          35 : mfshimura(GEN mf, GEN F, long D)
    3642             : {
    3643          35 :   pari_sp av = avma;
    3644             :   GEN gk, G, res, mf2, CHI, CHIP;
    3645          35 :   long M, r, space, cusp, N4, flagdisc = 0;
    3646          35 :   if (!checkmf_i(F)) pari_err_TYPE("mfshimura",F);
    3647          35 :   gk = mf_get_gk(F);
    3648          35 :   if (typ(gk) != t_FRAC) pari_err_TYPE("mfshimura [integral weight]", F);
    3649          35 :   r = MF_get_r(mf);
    3650          35 :   if (r <= 0) pari_err_DOMAIN("mfshimura", "weight", "<=", ghalf, gk);
    3651          35 :   N4 = MF_get_N(mf) >> 2; CHI = MF_get_CHI(mf);
    3652          35 :   CHIP = mfcharchiliftprim(CHI, N4);
    3653          35 :   if (!CHIP) CHIP = CHI;
    3654             :   else
    3655             :   {
    3656          35 :     long epsD = CHI == CHIP? D: -D, rd = D & 3L;
    3657          35 :     if (odd(r)) epsD = -epsD;
    3658          35 :     if (epsD > 0 && (rd == 0 || rd == 1)) flagdisc = 1;
    3659             :     else
    3660             :     {
    3661          14 :       if (D < 0 || !uissquarefree(D))
    3662           7 :         pari_err_TYPE("shimura [incorrect D]", stoi(D));
    3663           7 :       D = epsD;
    3664             :     }
    3665             :   }
    3666          28 :   M = N4;
    3667          28 :   cusp = mfiscuspidal(mf,F);
    3668          28 :   space = cusp && mfshimura_space_cusp(mf)? mf_CUSP : mf_FULL;
    3669          28 :   if (!cusp || !flagdisc || !mfisinkohnen(mf,F)) M <<= 1;
    3670          28 :   mf2 = mfinit_Nkchi(M, r << 1, mfcharpow(CHI, gen_2), space, 0);
    3671          28 :   G = c_shimura(mfsturm(mf2), F, D, CHIP);
    3672          28 :   res = mftobasis_i(mf2, G);
    3673             :   /* not mflinear(mf2,): we want lowest possible level */
    3674          28 :   G = mflinear(MF_get_basis(mf2), res);
    3675          28 :   return gerepilecopy(av, mkvec3(mf2, G, res));
    3676             : }
    3677             : 
    3678             : /* W ZabM (ZM if n = 1), a t_INT or NULL, b t_INT, ZXQ mod P or NULL.
    3679             :  * Write a/b = A/d with d t_INT and A Zab return [W,d,A,P] */
    3680             : static GEN
    3681        6895 : mkMinv(GEN W, GEN a, GEN b, GEN P)
    3682             : {
    3683        6895 :   GEN A = (b && typ(b) == t_POL)? Q_remove_denom(QXQ_inv(b,P), &b): NULL;
    3684        6895 :   if (a && b)
    3685             :   {
    3686        1001 :     a = Qdivii(a,b);
    3687        1001 :     if (typ(a) == t_INT) b = gen_1; else { b = gel(a,2); a = gel(a,1); }
    3688        1001 :     if (is_pm1(a)) a = NULL;
    3689             :   }
    3690        6895 :   if (a) A = A? ZX_Z_mul(A,a): a; else if (!A) A = gen_1;
    3691        6895 :   if (!b) b = gen_1;
    3692        6895 :   if (!P) P = gen_0;
    3693        6895 :   return mkvec4(W,b,A,P);
    3694             : }
    3695             : /* M square invertible QabM, return [M',d], M*M' = d*Id */
    3696             : static GEN
    3697         441 : QabM_Minv(GEN M, GEN P, long n)
    3698             : {
    3699             :   GEN dW, W, dM;
    3700         441 :   M = Q_remove_denom(M, &dM);
    3701         441 :   W = P? ZabM_inv(liftpol_shallow(M), P, n, &dW): ZM_inv(M, &dW);
    3702         441 :   return mkMinv(W, dM, dW, P);
    3703             : }
    3704             : /* Simplified form of mfclean, after a QabM_indexrank: M a ZabM with full
    3705             :  * column rank and z = indexrank(M) is known */
    3706             : static GEN
    3707         805 : mfclean2(GEN M, GEN z, GEN P, long n)
    3708             : {
    3709         805 :   GEN d, Minv, y = gel(z,1), W = rowpermute(M, y);
    3710         805 :   W = P? ZabM_inv(liftpol_shallow(W), P, n, &d): ZM_inv(W, &d);
    3711         805 :   M = rowslice(M, 1, y[lg(y)-1]);
    3712         805 :   Minv = mkMinv(W, NULL, d, P);
    3713         805 :   return mkvec3(y, Minv, M);
    3714             : }
    3715             : /* M QabM, lg(M)>1 and [y,z] its rank profile. Let Minv be the inverse of the
    3716             :  * invertible square matrix in mkMinv format. Return [y,Minv, M[..y[#y],]]
    3717             :  * P cyclotomic polynomial of order n != 2 mod 4 or NULL */
    3718             : static GEN
    3719        4508 : mfclean(GEN M, GEN P, long n, int ratlift)
    3720             : {
    3721        4508 :   GEN W, v, y, z, d, Minv, dM, MdM = Q_remove_denom(M, &dM);
    3722        4508 :   if (n == 1)
    3723        3556 :     W = ZM_pseudoinv(MdM, &v, &d);
    3724             :   else
    3725         952 :     W = ZabM_pseudoinv_i(liftpol_shallow(MdM), P, n, &v, &d, ratlift);
    3726        4508 :   y = gel(v,1);
    3727        4508 :   z = gel(v,2);
    3728        4508 :   if (lg(z) != lg(MdM)) M = vecpermute(M,z);
    3729        4508 :   M = rowslice(M, 1, y[lg(y)-1]);
    3730        4508 :   Minv = mkMinv(W, dM, d, P);
    3731        4508 :   return mkvec3(y, Minv, M);
    3732             : }
    3733             : /* call mfclean using only CHI */
    3734             : static GEN
    3735        3640 : mfcleanCHI(GEN M, GEN CHI, int ratlift)
    3736             : {
    3737        3640 :   long n = mfcharorder_canon(CHI);
    3738        3640 :   GEN P = (n == 1)? NULL: mfcharpol(CHI);
    3739        3640 :   return mfclean(M, P, n, ratlift);
    3740             : }
    3741             : 
    3742             : /* DATA component of a t_MF_NEWTRACE. Was it stripped to save memory ? */
    3743             : static int
    3744       29274 : newtrace_stripped(GEN DATA)
    3745       29274 : { return DATA && (lg(DATA) == 5 && typ(gel(DATA,3)) == t_INT); }
    3746             : /* f a t_MF_NEWTRACE */
    3747             : static GEN
    3748       29274 : newtrace_DATA(long N, GEN f)
    3749             : {
    3750       29274 :   GEN DATA = gel(f,2);
    3751       29274 :   return newtrace_stripped(DATA)? initnewtrace(N, DATA): DATA;
    3752             : }
    3753             : /* reset cachenew for new level incorporating new DATA, tf a t_MF_NEWTRACE
    3754             :  * (+ possibly initialize 'full' for new allowed levels) */
    3755             : static void
    3756       29274 : reset_cachenew(cachenew_t *cache, long N, GEN tf)
    3757             : {
    3758             :   long i, n, l;
    3759       29274 :   GEN v, DATA = newtrace_DATA(N,tf);
    3760       29274 :   cache->DATA = DATA;
    3761       29274 :   if (!DATA) return;
    3762       29239 :   n = cache->n;
    3763       29239 :   v = cache->vfull; l = N+1; /* = lg(DATA) */
    3764     1771161 :   for (i = 1; i < l; i++)
    3765     1741922 :     if (typ(gel(v,i)) == t_INT && lg(gel(DATA,i)) != 1)
    3766       45633 :       gel(v,i) = const_vec(n, NULL);
    3767       29239 :   cache->VCHIP = gel(gel(DATA,N),_VCHIP);
    3768             : }
    3769             : /* initialize a cache of newtrace / cusptrace up to index n and level | N;
    3770             :  * DATA may be NULL (<=> Tr^new = 0). tf a t_MF_NEWTRACE */
    3771             : static void
    3772       10717 : init_cachenew(cachenew_t *cache, long n, long N, GEN tf)
    3773             : {
    3774       10717 :   long i, l = N+1; /* = lg(tf.DATA) when DATA != NULL */
    3775             :   GEN v;
    3776       10717 :   cache->n = n;
    3777       10717 :   cache->vnew = v = cgetg(l, t_VEC);
    3778       10717 :   for (i = 1; i < l; i++) gel(v,i) = (N % i)? gen_0: const_vec(n, NULL);
    3779       10717 :   cache->newHIT = cache->newTOTAL = cache->cuspHIT = cache->cuspTOTAL = 0;
    3780       10717 :   cache->vfull = v = zerovec(N);
    3781       10717 :   reset_cachenew(cache, N, tf);
    3782       10717 : }
    3783             : static void
    3784       15666 : dbg_cachenew(cachenew_t *C)
    3785             : {
    3786       15666 :   if (DEBUGLEVEL >= 2 && C)
    3787           0 :     err_printf("newtrace cache hits: new = %ld/%ld, cusp = %ld/%ld\n",
    3788             :                     C->newHIT, C->newTOTAL, C->cuspHIT, C->cuspTOTAL);
    3789       15666 : }
    3790             : 
    3791             : /* newtrace_{N,k}(d*i), i = n0, ..., n */
    3792             : static GEN
    3793      134540 : colnewtrace(long n0, long n, long d, long N, long k, cachenew_t *cache)
    3794             : {
    3795      134540 :   GEN v = cgetg(n-n0+2, t_COL);
    3796             :   long i;
    3797      134540 :   for (i = n0; i <= n; i++) gel(v, i-n0+1) = mfnewtracecache(N, k, i*d, cache);
    3798      134540 :   return v;
    3799             : }
    3800             : /* T_n(l*m0, l*(m0+1), ..., l*m) F, F = t_MF_NEWTRACE [N,k],DATA, cache
    3801             :  * contains DATA != NULL as well as cached values of F */
    3802             : static GEN
    3803       74053 : heckenewtrace(long m0, long m, long l, long N, long NBIG, long k, long n, cachenew_t *cache)
    3804             : {
    3805       74053 :   long lD, a, k1, nl = n*l;
    3806       74053 :   GEN D, V, v = colnewtrace(m0, m, nl, N, k, cache); /* d=1 */
    3807             :   GEN VCHIP;
    3808       74053 :   if (n == 1) return v;
    3809       48615 :   VCHIP = cache->VCHIP;
    3810       48615 :   D = mydivisorsu(u_ppo(n, NBIG)); lD = lg(D);
    3811       48615 :   k1 = k - 1;
    3812      108353 :   for (a = 2; a < lD; a++)
    3813             :   { /* d > 1, (d,NBIG) = 1 */
    3814       59738 :     long i, j, d = D[a], c = ugcd(l, d), dl = d/c, m0d = ceildiv(m0, dl);
    3815       59738 :     GEN C = vchip_lift(VCHIP, d, powuu(d, k1));
    3816             :     /* m0=0: i = 1 => skip F(0) = 0 */
    3817       59738 :     if (!m0) { i = 1; j = dl; } else { i = 0; j = m0d*dl; }
    3818       59738 :     V = colnewtrace(m0d, m/dl, nl/(d*c), N, k, cache);
    3819             :     /* C = chi(d) d^(k-1) */
    3820      633122 :     for (; j <= m; i++, j += dl)
    3821      573384 :       gel(v,j-m0+1) = gadd(gel(v,j-m0+1), vchip_mod(VCHIP, gmul(C,gel(V,i+1))));
    3822             :   }
    3823       48615 :   return v;
    3824             : }
    3825             : 
    3826             : /* Given v = an[i], return an[d*i] */
    3827             : static GEN
    3828         658 : anextract(GEN v, long n, long d)
    3829             : {
    3830         658 :   GEN w = cgetg(n+2, t_VEC);
    3831             :   long i;
    3832         658 :   for (i = 0; i <= n; i++) gel(w, i+1) = gel(v, i*d+1);
    3833         658 :   return w;
    3834             : }
    3835             : /* T_n(F)(0, l, ..., l*m) */
    3836             : static GEN
    3837         854 : hecke_i(long m, long l, GEN V, GEN F, GEN DATA)
    3838             : {
    3839             :   long k, n, nNBIG, NBIG, lD, M, a, t, nl;
    3840             :   GEN D, v, CHI;
    3841         854 :   if (typ(DATA) == t_VEC)
    3842             :   { /* 1/2-integral k */
    3843          98 :     if (!V) { GEN S = gel(DATA,2); V = mfcoefs_i(F, m*l*S[3], S[4]); }
    3844          98 :     return RgV_heckef2(m, l, V, F, DATA);
    3845             :   }
    3846         756 :   k = mf_get_k(F);
    3847         756 :   n = DATA[1]; nl = n*l;
    3848         756 :   nNBIG = DATA[2];
    3849         756 :   NBIG = DATA[3];
    3850         756 :   if (nNBIG == 1) return V? V: mfcoefs_i(F,m,nl);
    3851         539 :   if (!V && mf_get_type(F) == t_MF_NEWTRACE)
    3852             :   { /* inline F to allow cache, T_n at level NBIG acting on Tr^new(N,k,CHI) */
    3853             :     cachenew_t cache;
    3854         210 :     long N = mf_get_N(F);
    3855         210 :     init_cachenew(&cache, m*nl, N, F);
    3856         210 :     v = heckenewtrace(0, m, l, N, NBIG, k, n, &cache);
    3857         210 :     dbg_cachenew(&cache);
    3858         210 :     settyp(v, t_VEC); return v;
    3859             :   }
    3860         329 :   CHI = mf_get_CHI(F);
    3861         329 :   D = mydivisorsu(nNBIG); lD = lg(D);
    3862         329 :   M = m + 1;
    3863         329 :   t = nNBIG * ugcd(nNBIG, l);
    3864         329 :   if (!V) V = mfcoefs_i(F, m * t, nl / t); /* usually nl = t */
    3865         329 :   v = anextract(V, m, t); /* mfcoefs(F, m, nl); d = 1 */
    3866         658 :   for (a = 2; a < lD; a++)
    3867             :   { /* d > 1, (d, NBIG) = 1 */
    3868         329 :     long d = D[a], c = ugcd(l, d), dl = d/c, i, idl;
    3869         329 :     GEN C = gmul(mfchareval_i(CHI, d), powuu(d, k-1));
    3870         329 :     GEN w = anextract(V, m/dl, t/(d*c)); /* mfcoefs(F, m/dl, nl/(d*c)) */
    3871        1008 :     for (i = idl = 1; idl <= M; i++, idl += dl)
    3872         679 :       gel(v,idl) = gadd(gel(v,idl), gmul(C, gel(w,i)));
    3873             :   }
    3874         329 :   return v;
    3875             : }
    3876             : 
    3877             : static GEN
    3878       11396 : mkmf(GEN x1, GEN x2, GEN x3, GEN x4, GEN x5)
    3879             : {
    3880       11396 :   GEN MF = obj_init(5, MF_SPLITN);
    3881       11396 :   gel(MF,1) = x1;
    3882       11396 :   gel(MF,2) = x2;
    3883       11396 :   gel(MF,3) = x3;
    3884       11396 :   gel(MF,4) = x4;
    3885       11396 :   gel(MF,5) = x5; return MF;
    3886             : }
    3887             : 
    3888             : /* return an integer b such that p | b => T_p^k Tr^new = 0, for all k > 0 */
    3889             : static long
    3890        7063 : get_badj(long N, long FC)
    3891             : {
    3892        7063 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
    3893        7063 :   long i, b = 1, l = lg(P);
    3894       18788 :   for (i = 1; i < l; i++)
    3895       11725 :     if (E[i] > 1 && u_lval(FC, P[i]) < E[i]) b *= P[i];
    3896        7063 :   return b;
    3897             : }
    3898             : /* in place, assume perm strictly increasing */
    3899             : static void
    3900        1162 : vecpermute_inplace(GEN v, GEN perm)
    3901             : {
    3902        1162 :   long i, l = lg(perm);
    3903        1162 :   for (i = 1; i < l; i++) gel(v,i) = gel(v,perm[i]);
    3904        1162 : }
    3905             : 
    3906             : /* Find basis of newspace using closures; assume k >= 2 and !badchar.
    3907             :  * Return NULL if space is empty, else
    3908             :  * [mf1, list of closures T(j)traceform, list of corresponding j, matrix] */
    3909             : static GEN
    3910       14581 : mfnewinit(long N, long k, GEN CHI, cachenew_t *cache, long init)
    3911             : {
    3912             :   GEN S, vj, M, CHIP, mf1, listj, P, tf;
    3913             :   long j, ct, ctlj, dim, jin, SB, sb, two, ord, FC, badj;
    3914             : 
    3915       14581 :   dim = mfnewdim(N, k, CHI);
    3916       14581 :   if (!dim && !init) return NULL;
    3917        7063 :   sb = mfsturmNk(N, k);
    3918        7063 :   CHIP = mfchartoprimitive(CHI, &FC);
    3919             :   /* remove newtrace data from S to save space in output: negligible slowdown */
    3920        7063 :   tf = tag(t_MF_NEWTRACE, mkNK(N,k,CHIP), CHIP);
    3921        7063 :   badj = get_badj(N, FC);
    3922             :   /* try sbsmall first: Sturm bound not sharp for new space */
    3923        7063 :   SB = ceilA1(N, k);
    3924        7063 :   listj = cgetg(2*sb + 3, t_VECSMALL);
    3925      330078 :   for (j = ctlj = 1; ctlj < 2*sb + 3; j++)
    3926      323015 :     if (ugcd(j, badj) == 1) listj[ctlj++] = j;
    3927        7063 :   if (init)
    3928             :   {
    3929        3899 :     init_cachenew(cache, (SB+1)*listj[dim+1], N, tf);
    3930        3899 :     if (init == -1 || !dim) return NULL; /* old space or dim = 0 */
    3931             :   }
    3932             :   else
    3933        3164 :     reset_cachenew(cache, N, tf);
    3934             :   /* cache.DATA is not NULL */
    3935        6636 :   ord = mfcharorder_canon(CHIP);
    3936        6636 :   P = ord == 1? NULL: mfcharpol(CHIP);
    3937        6636 :   vj = cgetg(dim+1, t_VECSMALL);
    3938        6636 :   M = cgetg(dim+1, t_MAT);
    3939        6643 :   for (two = 1, ct = 0, jin = 1; two <= 2; two++)
    3940             :   {
    3941        6643 :     long a, jlim = jin + sb;
    3942       19236 :     for (a = jin; a <= jlim; a++)
    3943             :     {
    3944             :       GEN z, vecz;
    3945       19229 :       ct++; vj[ct] = listj[a];
    3946       19229 :       gel(M, ct) = heckenewtrace(0, SB, 1, N, N, k, vj[ct], cache);
    3947       19229 :       if (ct < dim) continue;
    3948             : 
    3949        7217 :       z = QabM_indexrank(M, P, ord);
    3950        7217 :       vecz = gel(z, 2); ct = lg(vecz) - 1;
    3951        7217 :       if (ct == dim) { M = mkvec3(z, gen_0, M); break; } /*maximal rank, done*/
    3952         581 :       vecpermute_inplace(M, vecz);
    3953         581 :       vecpermute_inplace(vj, vecz);
    3954             :     }
    3955        6643 :     if (a <= jlim) break;
    3956             :     /* sbsmall was not sufficient, use Sturm bound: must extend M */
    3957          70 :     for (j = 1; j <= ct; j++)
    3958             :     {
    3959          63 :       GEN t = heckenewtrace(SB + 1, sb, 1, N, N, k, vj[j], cache);
    3960          63 :       gel(M,j) = shallowconcat(gel(M, j), t);
    3961             :     }
    3962           7 :     jin = jlim + 1; SB = sb;
    3963             :   }
    3964        6636 :   S = cgetg(dim + 1, t_VEC);
    3965        6636 :   for (j = 1; j <= dim; j++) gel(S, j) = mfhecke_i(vj[j], N, tf);
    3966        6636 :   dbg_cachenew(cache);
    3967        6636 :   mf1 = mkvec4(utoipos(N), utoipos(k), CHI, utoi(mf_NEW));
    3968        6636 :   return mkmf(mf1, cgetg(1,t_VEC), S, vj, M);
    3969             : }
    3970             : /* k > 1 integral, mf space is mf_CUSP or mf_FULL */
    3971             : static GEN
    3972          28 : mfinittonew(GEN mf)
    3973             : {
    3974          28 :   GEN CHI = MF_get_CHI(mf), S = MF_get_S(mf), vMjd = MFcusp_get_vMjd(mf);
    3975          28 :   GEN M = MF_get_M(mf), vj, mf1;
    3976          28 :   long i, j, l, l0 = lg(S), N0 = MF_get_N(mf);
    3977         161 :   for (i = l0-1; i > 0; i--)
    3978             :   {
    3979         161 :     long N = gel(vMjd,i)[1];
    3980         161 :     if (N != N0) break;
    3981             :   }
    3982          28 :   if (i == l0-1) return NULL;
    3983          28 :   S = vecslice(S, i+1, l0-1); /* forms of conductor N0 */
    3984          28 :   l = lg(S); vj = cgetg(l, t_VECSMALL);
    3985          28 :   for (j = 1; j < l; j++) vj[j] = gel(vMjd,j+i)[2];
    3986          28 :   M = vecslice(M, lg(M)-lg(S)+1, lg(M)-1); /* their coefficients */
    3987          28 :   M = mfcleanCHI(M, CHI, 0);
    3988          28 :   mf1 = mkvec4(utoipos(N0), MF_get_gk(mf), CHI, utoi(mf_NEW));
    3989          28 :   return mkmf(mf1, cgetg(1,t_VEC), S, vj, M);
    3990             : }
    3991             : 
    3992             : /* Bd(f)[m0..m], v = f[ceil(m0/d)..floor(m/d)], m0d = ceil(m0/d) */
    3993             : static GEN
    3994       67732 : RgC_Bd_expand(long m0, long m, GEN v, long d, long m0d)
    3995             : {
    3996             :   long i, j;
    3997             :   GEN w;
    3998       67732 :   if (d == 1) return v;
    3999       19523 :   w = zerocol(m-m0+1);
    4000       19523 :   if (!m0) { i = 1; j = d; } else { i = 0; j = m0d*d; }
    4001       19523 :   for (; j <= m; i++, j += d) gel(w,j-m0+1) = gel(v,i+1);
    4002       19523 :   return w;
    4003             : }
    4004             : /* S a non-empty vector of t_MF_BD(t_MF_HECKE(t_MF_NEWTRACE)); M the matrix
    4005             :  * of their coefficients r*0, r*1, ..., r*m0 (~ mfvectomat) or NULL (empty),
    4006             :  * extend it to coeffs up to m > m0. The forms B_d(T_j(tf_N))in S should be
    4007             :  * sorted by level N, then j, then increasing d. No reordering here. */
    4008             : static GEN
    4009        7994 : bhnmat_extend(GEN M, long m, long r, GEN S, cachenew_t *cache)
    4010             : {
    4011        7994 :   long i, mr, m0, m0r, Nold = 0, jold = 0, l = lg(S);
    4012        7994 :   GEN MAT = cgetg(l, t_MAT), v = NULL;
    4013        7994 :   if (M) { m0 = nbrows(M); m0r = m0 * r; } else m0 = m0r = 0;
    4014        7994 :   mr = m*r;
    4015       75726 :   for (i = 1; i < l; i++)
    4016             :   {
    4017             :     long d, j, md, N;
    4018       67732 :     GEN c, f = bhn_parse(gel(S,i), &d,&j); /* t_MF_NEWTRACE */
    4019       67732 :     N = mf_get_N(f);
    4020       67732 :     md = ceildiv(m0r,d);
    4021       67732 :     if (N != Nold) { reset_cachenew(cache, N, f); Nold = N; jold = 0; }
    4022       67732 :     if (!cache->DATA) { gel(MAT,i) = zerocol(m+1); continue; }
    4023       67732 :     if (j != jold || md)
    4024       54551 :     { v = heckenewtrace(md, mr/d, 1, N, N, mf_get_k(f), j,cache); jold=j; }
    4025       67732 :     c = RgC_Bd_expand(m0r, mr, v, d, md);
    4026       67732 :     if (r > 1) c = c_deflate(m-m0, r, c);
    4027       67732 :     if (M) c = shallowconcat(gel(M,i), c);
    4028       67732 :     gel(MAT,i) = c;
    4029             :   }
    4030        7994 :   return MAT;
    4031             : }
    4032             : 
    4033             : static GEN
    4034        2961 : mfinitcusp(long N, long k, GEN CHI, cachenew_t *cache, long space)
    4035             : {
    4036             :   long L, l, lDN1, FC, N1, d1, i, init;
    4037        2961 :   GEN vS, vMjd, DN1, vmf, CHIP = mfchartoprimitive(CHI, &FC);
    4038             : 
    4039        2961 :   d1 = (space == mf_OLD)? mfolddim_i(N, k, CHIP): mfcuspdim(N, k, CHIP);
    4040        2961 :   if (!d1) return NULL;
    4041        2723 :   N1 = N/FC; DN1 = mydivisorsu(N1); lDN1 = lg(DN1);
    4042        2723 :   init = (space == mf_OLD)? -1: 1;
    4043        2723 :   vmf = cgetg(lDN1, t_VEC);
    4044       16128 :   for (i = lDN1 - 1, l = 1; i; i--)
    4045             :   { /* by decreasing level to allow cache */
    4046       13405 :     GEN mf = mfnewinit(FC*DN1[i], k, CHIP, cache, init);
    4047       13405 :     if (mf) gel(vmf, l++) = mf;
    4048       13405 :     init = 0;
    4049             :   }
    4050        2723 :   setlg(vmf,l); vmf = vecreverse(vmf); /* reorder by increasing level */
    4051             : 
    4052        2723 :   L = mfsturmNk(N, k)+1;
    4053        2723 :   vS = vectrunc_init(L);
    4054        2723 :   vMjd = vectrunc_init(L);
    4055        8554 :   for (i = 1; i < l; i++)
    4056             :   {
    4057        5831 :     GEN DNM, mf = gel(vmf,i), S = MF_get_S(mf), vj = MFnew_get_vj(mf);
    4058        5831 :     long a, lDNM, lS = lg(S), M = MF_get_N(mf);
    4059        5831 :     DNM = mydivisorsu(N / M); lDNM = lg(DNM);
    4060       22484 :     for (a = 1; a < lS; a++)
    4061             :     {
    4062       16653 :       GEN tf = gel(S,a);
    4063       16653 :       long b, j = vj[a];
    4064       41048 :       for (b = 1; b < lDNM; b++)
    4065             :       {
    4066       24395 :         long d = DNM[b];
    4067       24395 :         vectrunc_append(vS, mfbd_i(tf, d));
    4068       24395 :         vectrunc_append(vMjd, mkvecsmall3(M, j, d));
    4069             :       }
    4070             :     }
    4071             :   }
    4072        2723 :   return mkmf(NULL, cgetg(1, t_VEC), vS, vMjd, NULL);
    4073             : }
    4074             : 
    4075             : long
    4076        3416 : mfsturm_mf(GEN mf)
    4077             : {
    4078        3416 :   GEN Mindex = MF_get_Mindex(mf);
    4079        3416 :   long n = lg(Mindex)-1;
    4080        3416 :   return n? Mindex[n]: 0;
    4081             : }
    4082             : 
    4083             : long
    4084         532 : mfsturm(GEN T)
    4085             : {
    4086             :   long N, nk, dk;
    4087         532 :   GEN CHI, mf = checkMF_i(T);
    4088         532 :   if (mf) return mfsturm_mf(mf);
    4089           7 :   checkNK2(T, &N, &nk, &dk, &CHI, 0);
    4090           7 :   return dk == 1 ? mfsturmNk(N, nk) : mfsturmNk(N, (nk + 1) >> 1);
    4091             : }
    4092             : 
    4093             : long
    4094           7 : mfisequal(GEN F, GEN G, long lim)
    4095             : {
    4096           7 :   pari_sp av = avma;
    4097             :   long sb;
    4098           7 :   if (!checkmf_i(F)) pari_err_TYPE("mfisequal",F);
    4099           7 :   if (!checkmf_i(G)) pari_err_TYPE("mfisequal",G);
    4100           7 :   if (lim) sb = lim;
    4101             :   else
    4102             :   {
    4103             :     GEN gN, gk;
    4104           7 :     gN = mf_get_gN(F); gk = mf_get_gk(F);
    4105           7 :     sb = mfsturmNgk(itou(gN), gk);
    4106           7 :     gN = mf_get_gN(G); gk = mf_get_gk(G);
    4107           7 :     sb = maxss(sb, mfsturmNgk(itou(gN), gk));
    4108             :   }
    4109           7 :   return gc_long(av, gequal(mfcoefs_i(F, sb+1, 1), mfcoefs_i(G, sb+1, 1)));
    4110             : }
    4111             : 
    4112             : GEN
    4113          35 : mffields(GEN mf)
    4114             : {
    4115          35 :   if (checkmf_i(mf)) return gcopy(mf_get_field(mf));
    4116          35 :   mf = checkMF(mf); return gcopy(MF_get_fields(mf));
    4117             : }
    4118             : 
    4119             : GEN
    4120         294 : mfeigenbasis(GEN mf)
    4121             : {
    4122         294 :   pari_sp ltop = avma;
    4123             :   GEN F, S, v, vP;
    4124             :   long i, l, k, dS;
    4125             : 
    4126         294 :   mf = checkMF(mf);
    4127         294 :   k = MF_get_k(mf);
    4128         294 :   S = MF_get_S(mf); dS = lg(S)-1;
    4129         294 :   if (!dS) return cgetg(1, t_VEC);
    4130         287 :   F = MF_get_newforms(mf);
    4131         287 :   vP = MF_get_fields(mf);
    4132         287 :   if (k == 1)
    4133             :   {
    4134         189 :     if (MF_get_space(mf) == mf_FULL)
    4135             :     {
    4136           7 :       long dE = lg(MF_get_E(mf)) - 1;
    4137           7 :       if (dE) F = rowslice(F, dE+1, dE+dS);
    4138             :     }
    4139         189 :     v = vecmflineardiv_linear(S, F);
    4140         189 :     l = lg(v);
    4141             :   }
    4142             :   else
    4143             :   {
    4144          98 :     GEN (*L)(GEN, GEN) = (MF_get_space(mf) == mf_FULL)? mflinear: mflinear_bhn;
    4145          98 :     l = lg(F); v = cgetg(l, t_VEC);
    4146          98 :     for (i = 1; i < l; i++) gel(v,i) = L(mf, gel(F,i));
    4147             :   }
    4148         287 :   for (i = 1; i < l; i++) mf_setfield(gel(v,i), gel(vP,i));
    4149         287 :   return gerepilecopy(ltop, v);
    4150             : }
    4151             : 
    4152             : /* Minv = [M, d, A], v a t_COL; A a Zab, d a t_INT; return (A/d) * M*v */
    4153             : static GEN
    4154        5502 : Minv_RgC_mul(GEN Minv, GEN v)
    4155             : {
    4156        5502 :   GEN M = gel(Minv,1), d = gel(Minv,2), A = gel(Minv,3);
    4157        5502 :   v = RgM_RgC_mul(M, v);
    4158        5502 :   if (!equali1(A))
    4159             :   {
    4160        1379 :     if (typ(A) == t_POL && degpol(A) > 0) A = mkpolmod(A, gel(Minv,4));
    4161        1379 :     v = RgC_Rg_mul(v, A);
    4162             :   }
    4163        5502 :   if (!equali1(d)) v = RgC_Rg_div(v, d);
    4164        5502 :   return v;
    4165             : }
    4166             : static GEN
    4167        1071 : Minv_RgM_mul(GEN Minv, GEN B)
    4168             : {
    4169        1071 :   long j, l = lg(B);
    4170        1071 :   GEN M = cgetg(l, t_MAT);
    4171        1071 :   for (j = 1; j < l; j++) gel(M,j) = Minv_RgC_mul(Minv, gel(B,j));
    4172        1071 :   return M;
    4173             : }
    4174             : /* B * Minv; allow B = NULL for Id */
    4175             : static GEN
    4176        2156 : RgM_Minv_mul(GEN B, GEN Minv)
    4177             : {
    4178        2156 :   GEN M = gel(Minv,1), d = gel(Minv,2), A = gel(Minv,3);
    4179        2156 :   if (B) M = RgM_mul(B, M);
    4180        2156 :   if (!equali1(A))
    4181             :   {
    4182         868 :     if (typ(A) == t_POL) A = mkpolmod(A, gel(Minv,4));
    4183         868 :     M = RgM_Rg_mul(M, A);
    4184             :   }
    4185        2156 :   if (!equali1(d)) M = RgM_Rg_div(M,d);
    4186        2156 :   return M;
    4187             : }
    4188             : 
    4189             : /* perm vector of strictly increasing indices, v a vector or arbitrary length;
    4190             :  * the last r entries of perm fall beyond v.
    4191             :  * Return v o perm[1..(-r)], discarding the last r entries of v */
    4192             : static GEN
    4193        1043 : vecpermute_partial(GEN v, GEN perm, long *r)
    4194             : {
    4195        1043 :   long i, n = lg(v)-1, l = lg(perm);
    4196             :   GEN w;
    4197        1043 :   if (perm[l-1] <= n) { *r = 0; return vecpermute(v,perm); }
    4198          63 :   for (i = 1; i < l; i++)
    4199          63 :     if (perm[i] > n) break;
    4200          21 :   *r = l - i; l = i;
    4201          21 :   w = cgetg(l, typ(v));
    4202          21 :   for (i = 1; i < l; i++) gel(w,i) = gel(v,perm[i]);
    4203          21 :   return w;
    4204             : }
    4205             : 
    4206             : /* given form F, find coeffs of F on mfbasis(mf). If power series, not
    4207             :  * guaranteed correct if precision less than Sturm bound */
    4208             : static GEN
    4209        1050 : mftobasis_i(GEN mf, GEN F)
    4210             : {
    4211             :   GEN v, Mindex, Minv;
    4212        1050 :   if (!MF_get_dim(mf)) return cgetg(1, t_COL);
    4213        1050 :   Mindex = MF_get_Mindex(mf);
    4214        1050 :   Minv = MF_get_Minv(mf);
    4215        1050 :   if (checkmf_i(F))
    4216             :   {
    4217         154 :     long n = Mindex[lg(Mindex)-1];
    4218         154 :     v = vecpermute(mfcoefs_i(F, n, 1), Mindex);
    4219         154 :     return Minv_RgC_mul(Minv, v);
    4220             :   }
    4221             :   else
    4222             :   {
    4223         896 :     GEN A = gel(Minv,1), d = gel(Minv,2);
    4224             :     long r;
    4225         896 :     v = F;
    4226         896 :     switch(typ(F))
    4227             :     {
    4228           0 :       case t_SER: v = sertocol(v);
    4229         896 :       case t_VEC: case t_COL: break;
    4230           0 :       default: pari_err_TYPE("mftobasis", F);
    4231             :     }
    4232         896 :     if (lg(v) == 1) pari_err_TYPE("mftobasis",v);
    4233         896 :     v = vecpermute_partial(v, Mindex, &r);
    4234         896 :     if (!r) return Minv_RgC_mul(Minv, v); /* single solution */
    4235             :     /* affine space of dimension r */
    4236          21 :     v = RgM_RgC_mul(vecslice(A, 1, lg(v)-1), v);
    4237          21 :     if (!equali1(d)) v = RgC_Rg_div(v,d);
    4238          21 :     return mkvec2(v, vecslice(A, lg(A)-r, lg(A)-1));
    4239             :   }
    4240             : }
    4241             : 
    4242             : static GEN
    4243         560 : const_mat(long n, GEN x)
    4244             : {
    4245         560 :   long j, l = n+1;
    4246         560 :   GEN A = cgetg(l,t_MAT);
    4247         560 :   for (j = 1; j < l; j++) gel(A,j) = const_col(n, x);
    4248         560 :   return A;
    4249             : }
    4250             : 
    4251             : /* L is the mftobasis of a form on CUSP space. We allow mf_FULL or mf_CUSP */
    4252             : static GEN
    4253         280 : mftonew_i(GEN mf, GEN L, long *plevel)
    4254             : {
    4255             :   GEN S, listMjd, CHI, res, Aclos, Acoef, D, perm;
    4256         280 :   long N1, LC, lD, i, l, t, level, N = MF_get_N(mf);
    4257             : 
    4258         280 :   if (MF_get_k(mf) == 1) pari_err_IMPL("mftonew in weight 1");
    4259         280 :   listMjd = MFcusp_get_vMjd(mf);
    4260         280 :   CHI = MF_get_CHI(mf); LC = mfcharconductor(CHI);
    4261         280 :   S = MF_get_S(mf);
    4262             : 
    4263         280 :   N1 = N/LC;
    4264         280 :   D = mydivisorsu(N1); lD = lg(D);
    4265         280 :   perm = cgetg(N1+1, t_VECSMALL);
    4266         280 :   for (i = 1; i < lD; i++) perm[D[i]] = i;
    4267         280 :   Aclos = const_mat(lD-1, cgetg(1,t_VEC));
    4268         280 :   Acoef = const_mat(lD-1, cgetg(1,t_VEC));
    4269         280 :   l = lg(listMjd);
    4270        2877 :   for (i = 1; i < l; i++)
    4271             :   {
    4272             :     long M, d;
    4273             :     GEN v;
    4274        2597 :     if (gequal0(gel(L,i))) continue;
    4275         273 :     v = gel(listMjd, i);
    4276         273 :     M = perm[ v[1]/LC ];
    4277         273 :     d = perm[ v[3] ];
    4278         273 :     gcoeff(Aclos,M,d) = vec_append(gcoeff(Aclos,M,d), gel(S,i));
    4279         273 :     gcoeff(Acoef,M,d) = shallowconcat(gcoeff(Acoef,M,d), gel(L,i));
    4280             :   }
    4281         280 :   res = cgetg(l, t_VEC); level = 1;
    4282        2009 :   for (i = t = 1; i < lD; i++)
    4283             :   {
    4284        1729 :     long j, M = D[i]*LC;
    4285        1729 :     GEN gM = utoipos(M);
    4286       15134 :     for (j = 1; j < lD; j++)
    4287             :     {
    4288       13405 :       GEN f = gcoeff(Aclos,i,j), C, NK;
    4289             :       long d;
    4290       13405 :       if (lg(f) == 1) continue;
    4291         245 :       NK = mf_get_NK(gel(f,1));
    4292         245 :       d = D[j];
    4293         245 :       C = gcoeff(Acoef,i,j);
    4294         245 :       level = ulcm(level, M*d);
    4295         245 :       gel(res,t++) = mkvec3(gM, utoipos(d), mflinear_i(NK,f,C));
    4296             :     }
    4297             :   }
    4298         280 :   if (plevel) *plevel = level;
    4299         280 :   setlg(res, t); return res;
    4300             : }
    4301             : GEN
    4302          35 : mftonew(GEN mf, GEN F)
    4303             : {
    4304          35 :   pari_sp av = avma;
    4305             :   GEN ES;
    4306             :   long s;
    4307          35 :   mf = checkMF(mf);
    4308          35 :   s = MF_get_space(mf);
    4309          35 :   if (s != mf_FULL && s != mf_CUSP)
    4310           7 :     pari_err_TYPE("mftonew [not a full or cuspidal space]", mf);
    4311          28 :   ES = mftobasisES(mf,F);
    4312          21 :   if (!gequal0(gel(ES,1)))
    4313           0 :     pari_err_TYPE("mftonew [not a cuspidal form]", F);
    4314          21 :   F = gel(ES,2);
    4315          21 :   return gerepilecopy(av, mftonew_i(mf,F, NULL));
    4316             : }
    4317             : 
    4318             : static GEN mfeisenstein_i(long k, GEN CHI1, GEN CHI2);
    4319             : 
    4320             : /* mfinit(F * Theta) */
    4321             : static GEN
    4322          70 : mf2init(GEN mf)
    4323             : {
    4324          70 :   GEN CHI = MF_get_CHI(mf), gk = gadd(MF_get_gk(mf), ghalf);
    4325          70 :   long N = MF_get_N(mf);
    4326          70 :   return mfinit_Nkchi(N, itou(gk), mfchiadjust(CHI, gk, N), mf_FULL, 0);
    4327             : }
    4328             : 
    4329             : static long
    4330         490 : mfvec_first_cusp(GEN v)
    4331             : {
    4332         490 :   long i, l = lg(v);
    4333         959 :   for (i = 1; i < l; i++)
    4334             :   {
    4335         882 :     GEN F = gel(v,i);
    4336         882 :     long t = mf_get_type(F);
    4337         882 :     if (t == t_MF_BD) { F = gel(F,2); t = mf_get_type(F); }
    4338         882 :     if (t == t_MF_HECKE) { F = gel(F,3); t = mf_get_type(F); }
    4339         882 :     if (t == t_MF_NEWTRACE) break;
    4340             :   }
    4341         490 :   return i;
    4342             : }
    4343             : /* vF a vector of mf F of type DIV(LINEAR(BAS,L), f) in (lcm) level N,
    4344             :  * F[2]=LINEAR(BAS,L), F[2][2]=BAS=fixed basis (Eisentstein or bhn type),
    4345             :  * F[2][3]=L, F[3]=f; mfvectomat(vF, n) */
    4346             : static GEN
    4347         497 : mflineardivtomat(long N, GEN vF, long n)
    4348             : {
    4349             :   GEN F, M, f, fc, ME, dB, B, a0;
    4350         497 :   long lM, lF = lg(vF), j;
    4351             : 
    4352         497 :   if (lF == 1) return cgetg(1,t_MAT);
    4353         490 :   F = gel(vF,1);
    4354         490 :   M = gmael(F,2,2); /* BAS */
    4355         490 :   lM = lg(M);
    4356         490 :   j = mfvec_first_cusp(M);
    4357         490 :   if (j == 1) ME = NULL;
    4358             :   else
    4359             :   { /* BAS starts by Eisenstein */
    4360         105 :     ME = mfvectomat(vecslice(M,1,j-1), n, 1);
    4361         105 :     M = vecslice(M, j,lM-1);
    4362             :   }
    4363         490 :   M = bhnmat_extend_nocache(NULL, N, n, 1, M);
    4364         490 :   if (ME) M = shallowconcat(ME,M);
    4365             :   /* M = mfcoefs of BAS */
    4366         490 :   B = cgetg(lF, t_MAT);
    4367         490 :   dB= cgetg(lF, t_VEC);
    4368        2023 :   for (j = 1; j < lF; j++)
    4369             :   {
    4370        1533 :     GEN g = gel(vF, j); /* t_MF_DIV */
    4371        1533 :     gel(B,j) = RgM_RgC_mul(M, gmael(g,2,3));
    4372        1533 :     gel(dB,j)= gmael(g,2,4);
    4373             :   }
    4374         490 :   f = mfcoefsser(gel(F,3),n);
    4375         490 :   a0 = polcoef_i(f, 0, -1);
    4376         490 :   if (gequal0(a0) || gequal1(a0))
    4377         266 :     a0 = NULL;
    4378             :   else
    4379         224 :     f = gdiv(ser_unscale(f, a0), a0);
    4380         490 :   fc = ginv(f);
    4381        2023 :   for (j = 1; j < lF; j++)
    4382             :   {
    4383        1533 :     pari_sp av = avma;
    4384        1533 :     GEN LISer = RgV_to_ser_full(gel(B,j)), f;
    4385        1533 :     if (a0) LISer = gdiv(ser_unscale(LISer, a0), a0);
    4386        1533 :     f = gmul(LISer, fc);
    4387        1533 :     if (a0) f = ser_unscale(f, ginv(a0));
    4388        1533 :     f = sertocol(f); setlg(f, n+2);
    4389        1533 :     if (!gequal1(gel(dB,j))) f = RgC_Rg_div(f, gel(dB,j));
    4390        1533 :     gel(B,j) = gerepileupto(av,f);
    4391             :   }
    4392         490 :   return B;
    4393             : }
    4394             : 
    4395             : static GEN
    4396         189 : mfheckemat_mfcoefs(GEN mf, GEN B, GEN DATA)
    4397             : {
    4398         189 :   GEN Mindex = MF_get_Mindex(mf), Minv = MF_get_Minv(mf);
    4399         189 :   long j, l = lg(B), sb = mfsturm_mf(mf)-1;
    4400         189 :   GEN b = MF_get_basis(mf), Q = cgetg(l, t_VEC);
    4401         609 :   for (j = 1; j < l; j++)
    4402             :   {
    4403         420 :     GEN v = hecke_i(sb, 1, gel(B,j), gel(b,j), DATA); /* Tn b[j] */
    4404         420 :     settyp(v,t_COL); gel(Q,j) = vecpermute(v, Mindex);
    4405             :   }
    4406         189 :   return Minv_RgM_mul(Minv,Q);
    4407             : }
    4408             : /* T_p^2, p prime, 1/2-integral weight; B = mfcoefs(mf,sb*p^2,1) or (mf,sb,p^2)
    4409             :  * if p|N */
    4410             : static GEN
    4411           7 : mfheckemat_mfcoefs_p2(GEN mf, long p, GEN B)
    4412             : {
    4413           7 :   pari_sp av = avma;
    4414           7 :   GEN DATA = heckef2_data(MF_get_N(mf), p*p);
    4415           7 :   return gerepileupto(av, mfheckemat_mfcoefs(mf, B, DATA));
    4416             : }
    4417             : /* convert Mindex from row-index to mfcoef indexation: a(n) is stored in
    4418             :  * mfcoefs()[n+1], so subtract 1 from all indices */
    4419             : static GEN
    4420          49 : Mindex_as_coef(GEN mf)
    4421             : {
    4422          49 :   GEN v, Mindex = MF_get_Mindex(mf);
    4423          49 :   long i, l = lg(Mindex);
    4424          49 :   v = cgetg(l, t_VECSMALL);
    4425          49 :   for (i = 1; i < l; i++) v[i] = Mindex[i]-1;
    4426          49 :   return v;
    4427             : }
    4428             : /* T_p, p prime; B = mfcoefs(mf,sb*p,1) or (mf,sb,p) if p|N; integral weight */
    4429             : static GEN
    4430          35 : mfheckemat_mfcoefs_p(GEN mf, long p, GEN B)
    4431             : {
    4432          35 :   pari_sp av = avma;
    4433          35 :   GEN vm, Q, C, Minv = MF_get_Minv(mf);
    4434          35 :   long lm, k, i, j, l = lg(B), N = MF_get_N(mf);
    4435             : 
    4436          35 :   if (N % p == 0) return Minv_RgM_mul(Minv, rowpermute(B, MF_get_Mindex(mf)));
    4437          21 :   k = MF_get_k(mf);
    4438          21 :   C = gmul(mfchareval_i(MF_get_CHI(mf), p), powuu(p, k-1));
    4439          21 :   vm = Mindex_as_coef(mf); lm = lg(vm);
    4440          21 :   Q = cgetg(l, t_MAT);
    4441          21 :   for (j = 1; j < l; j++) gel(Q,j) = cgetg(lm, t_COL);
    4442         147 :   for (i = 1; i < lm; i++)
    4443             :   {
    4444         126 :     long m = vm[i], mp = m*p;
    4445         126 :     GEN Cm = (m % p) == 0? C : NULL;
    4446        1260 :     for (j = 1; j < l; j++)
    4447             :     {
    4448        1134 :       GEN S = gel(B,j), s = gel(S, mp + 1);
    4449        1134 :       if (Cm) s = gadd(s, gmul(C, gel(S, m/p + 1)));
    4450        1134 :       gcoeff(Q, i, j) = s;
    4451             :     }
    4452             :   }
    4453          21 :   return gerepileupto(av, Minv_RgM_mul(Minv,Q));
    4454             : }
    4455             : /* Matrix of T(p), p prime, dim(mf) > 0 and integral weight */
    4456             : static GEN
    4457         182 : mfheckemat_p(GEN mf, long p)
    4458             : {
    4459         182 :   pari_sp av = avma;
    4460         182 :   long N = MF_get_N(mf), sb = mfsturm_mf(mf)-1;
    4461         182 :   GEN B = (N % p)? mfcoefs_mf(mf, sb * p, 1): mfcoefs_mf(mf, sb, p);
    4462         182 :   return gerepileupto(av, mfheckemat_mfcoefs(mf, B, hecke_data(N,p)));
    4463             : }
    4464             : 
    4465             : /* mf_NEW != (0), weight > 1, p prime. Use
    4466             :  * T(p) T(j) = T(j*p) + p^{k-1} \chi(p) 1_{p | j, p \nmid N} T(j/p) */
    4467             : static GEN
    4468         847 : mfnewmathecke_p(GEN mf, long p)
    4469             : {
    4470         847 :   pari_sp av = avma;
    4471         847 :   GEN tf, vj = MFnew_get_vj(mf), CHI = MF_get_CHI(mf);
    4472         847 :   GEN Mindex = MF_get_Mindex(mf), Minv = MF_get_Minv(mf);
    4473         847 :   long N = MF_get_N(mf), k = MF_get_k(mf);
    4474         847 :   long i, j, lvj = lg(vj), lim = vj[lvj-1] * p;
    4475         847 :   GEN M, perm, V, need = zero_zv(lim);
    4476         847 :   GEN C = (N % p)? gmul(mfchareval_i(CHI,p), powuu(p,k-1)): NULL;
    4477         847 :   tf = mftraceform_new(N, k, CHI);
    4478        3647 :   for (i = 1; i < lvj; i++)
    4479             :   {
    4480        2800 :     j = vj[i]; need[j*p] = 1;
    4481        2800 :     if (N % p && j % p == 0) need[j/p] = 1;
    4482             :   }
    4483         847 :   perm = zero_zv(lim);
    4484         847 :   V = cgetg(lim+1, t_VEC);
    4485       11851 :   for (i = j = 1; i <= lim; i++)
    4486       11004 :     if (need[i]) { gel(V,j) = mfhecke_i(i, N, tf); perm[i] = j; j++; }
    4487         847 :   setlg(V, j);
    4488         847 :   V = bhnmat_extend_nocache(NULL, N, mfsturm_mf(mf)-1, 1, V);
    4489         847 :   V = rowpermute(V, Mindex); /* V[perm[i]] = coeffs(T_i newtrace) */
    4490         847 :   M = cgetg(lvj, t_MAT);
    4491        3647 :   for (i = 1; i < lvj; i++)
    4492             :   {
    4493             :     GEN t;
    4494        2800 :     j = vj[i]; t = gel(V, perm[j*p]);
    4495        2800 :     if (C && j % p == 0) t = RgC_add(t, RgC_Rg_mul(gel(V, perm[j/p]),C));
    4496        2800 :     gel(M,i) = t;
    4497             :   }
    4498         847 :   return gerepileupto(av, Minv_RgM_mul(Minv, M));
    4499             : }
    4500             : 
    4501             : GEN
    4502          77 : mfheckemat(GEN mf, GEN vn)
    4503             : {
    4504          77 :   pari_sp av = avma;
    4505          77 :   long lv, lvP, i, N, dim, nk, dk, p, sb, flint = (typ(vn)==t_INT);
    4506             :   GEN CHI, res, vT, FA, B, vP;
    4507             : 
    4508          77 :   mf = checkMF(mf);
    4509          77 :   if (typ(vn) != t_VECSMALL) vn = gtovecsmall(vn);
    4510          77 :   N = MF_get_N(mf); CHI = MF_get_CHI(mf); Qtoss(MF_get_gk(mf), &nk, &dk);
    4511          77 :   dim = MF_get_dim(mf);
    4512          77 :   lv = lg(vn);
    4513          77 :   res = cgetg(lv, t_VEC);
    4514          77 :   FA = cgetg(lv, t_VEC);
    4515          77 :   vP = cgetg(lv, t_VEC);
    4516          77 :   vT = const_vec(vecsmall_max(vn), NULL);
    4517         182 :   for (i = 1; i < lv; i++)
    4518             :   {
    4519         105 :     ulong n = (ulong)labs(vn[i]);
    4520             :     GEN fa;
    4521         105 :     if (!n) pari_err_TYPE("mfheckemat", vn);
    4522         105 :     if (dk == 1 || uissquareall(n, &n)) fa = myfactoru(n);
    4523           0 :     else { n = 0; fa = myfactoru(1); } /* dummy: T_{vn[i]} = 0 */
    4524         105 :     vn[i] = n;
    4525         105 :     gel(FA,i) = fa;
    4526         105 :     gel(vP,i) = gel(fa,1);
    4527             :   }
    4528          77 :   vP = shallowconcat1(vP); vecsmall_sort(vP);
    4529          77 :   vP = vecsmall_uniq_sorted(vP); /* all primes occurring in vn */
    4530          77 :   lvP = lg(vP); if (lvP == 1) goto END;
    4531          56 :   p = vP[lvP-1];
    4532          56 :   sb = mfsturm_mf(mf)-1;
    4533          56 :   if (dk == 1 && nk != 1 && MF_get_space(mf) == mf_NEW)
    4534          21 :     B = NULL; /* special purpose mfnewmathecke_p is faster */
    4535          35 :   else if (lvP == 2 && N % p == 0)
    4536          21 :     B = mfcoefs_mf(mf, sb, dk==2? p*p: p); /* single prime | N, can optimize */
    4537             :   else
    4538          14 :     B = mfcoefs_mf(mf, sb * (dk==2? p*p: p), 1); /* general initialization */
    4539         126 :   for (i = 1; i < lvP; i++)
    4540             :   {
    4541          70 :     long j, l, q, e = 1;
    4542             :     GEN C, Tp, u1, u0;
    4543          70 :     p = vP[i];
    4544          70 :     for (j = 1; j < lv; j++) e = maxss(e, z_lval(vn[j], p));
    4545          70 :     if (!B)
    4546          28 :       Tp = mfnewmathecke_p(mf, p);
    4547          42 :     else if (dk == 2)
    4548           7 :       Tp = mfheckemat_mfcoefs_p2(mf,p, (lvP==2||N%p)? B: matdeflate(sb,p*p,B));
    4549             :     else
    4550          35 :       Tp = mfheckemat_mfcoefs_p(mf, p, (lvP==2||N%p)? B: matdeflate(sb,p,B));
    4551          70 :     gel(vT, p) = Tp;
    4552          70 :     if (e == 1) continue;
    4553          14 :     u0 = gen_1;
    4554          14 :     if (dk == 2)
    4555             :     {
    4556           0 :       C = N % p? gmul(mfchareval_i(CHI,p*p), powuu(p, nk-2)): NULL;
    4557           0 :       if (e == 2) u0 = sstoQ(p+1,p); /* special case T_{p^4} */
    4558             :     }
    4559             :     else
    4560          14 :       C = N % p? gmul(mfchareval_i(CHI,p),   powuu(p, nk-1)): NULL;
    4561          28 :     for (u1=Tp, q=p, l=2; l <= e; l++)
    4562             :     { /* u0 = T_{p^{l-2}}, u1 = T_{p^{l-1}} for l > 2 */
    4563          14 :       GEN v = gmul(Tp, u1);
    4564          14 :       if (C) v = gsub(v, gmul(C, u0));
    4565             :       /* q = p^l, vT[q] = T_q for k integer else T_{q^2} */
    4566          14 :       q *= p; u0 = u1; gel(vT, q) = u1 = v;
    4567             :     }
    4568             :   }
    4569             : END:
    4570             :   /* vT[p^e] = T_{p^e} for all p^e occurring below */
    4571         182 :   for (i = 1; i < lv; i++)
    4572             :   {
    4573         105 :     long n = vn[i], j, lP;
    4574             :     GEN fa, P, E, M;
    4575         105 :     if (n == 0) { gel(res,i) = zeromat(dim,dim); continue; }
    4576         105 :     if (n == 1) { gel(res,i) = matid(dim); continue; }
    4577          77 :     fa = gel(FA,i);
    4578          77 :     P = gel(fa,1); lP = lg(P);
    4579          77 :     E = gel(fa,2); M = gel(vT, upowuu(P[1], E[1]));
    4580          77 :     for (j = 2; j < lP; j++) M = RgM_mul(M, gel(vT, upowuu(P[j], E[j])));
    4581          77 :     gel(res,i) = M;
    4582             :   }
    4583          77 :   if (flint) res = gel(res,1);
    4584          77 :   return gerepilecopy(av, res);
    4585             : }
    4586             : 
    4587             : 
    4588             : /* f = \sum_i v[i] T_listj[i] (Trace Form) attached to v; replace by f/a_1(f) */
    4589             : static GEN
    4590        1281 : mf_normalize(GEN mf, GEN v)
    4591             : {
    4592        1281 :   GEN c, dc = NULL, M = MF_get_M(mf), Mindex = MF_get_Mindex(mf);
    4593        1281 :   v = Q_primpart(v);
    4594        1281 :   c = RgMrow_RgC_mul(M, v, 2); /* a_1(f) */
    4595        1281 :   if (gequal1(c)) return v;
    4596         749 :   if (typ(c) == t_POL) c = gmodulo(c, mfcharpol(MF_get_CHI(mf)));
    4597         749 :   if (typ(c) == t_POLMOD && varn(gel(c,1)) == 1 && degpol(gel(c,1)) >= 40
    4598           7 :                          && Mindex[1] == 2
    4599           7 :                          && mfcharorder(MF_get_CHI(mf)) <= 2)
    4600           7 :   { /* normalize using expansion at infinity (small coefficients) */
    4601           7 :     GEN w, P = gel(c,1), a1 = gel(c,2);
    4602           7 :     long i, l = lg(Mindex);
    4603           7 :     w = cgetg(l, t_COL);
    4604           7 :     gel(w,1) = gen_1;
    4605         280 :     for (i = 2; i < l; i++)
    4606             :     {
    4607         273 :       c = liftpol_shallow(RgMrow_RgC_mul(M, v, Mindex[i]));
    4608         273 :       gel(w,i) = QXQ_div_ratlift(c, a1, P);
    4609             :     }
    4610             :     /* w = expansion at oo of normalized form */
    4611           7 :     v = Minv_RgC_mul(MF_get_Minv(mf), Q_remove_denom(w, &dc));
    4612           7 :     v = gmodulo(v, P); /* back to mfbasis coefficients */
    4613             :   }
    4614             :   else
    4615             :   {
    4616         742 :     c = ginv(c);
    4617         742 :     if (typ(c) == t_POLMOD) c = Q_remove_denom(c, &dc);
    4618         742 :     v = RgC_Rg_mul(v, c);
    4619             :   }
    4620         749 :   if (dc) v = RgC_Rg_div(v, dc);
    4621         749 :   return v;
    4622             : }
    4623             : static void
    4624         322 : pol_red(GEN NF, GEN *pP, GEN *pa, long flag)
    4625             : {
    4626         322 :   GEN dP, a, P = *pP;
    4627         322 :   long d = degpol(P);
    4628             : 
    4629         322 :   *pa = a = pol_x(varn(P));
    4630         322 :   if (d > 30) return;
    4631             : 
    4632         315 :   dP = RgX_disc(P);
    4633         315 :   if (typ(dP) != t_INT)
    4634          77 :   { dP = gnorm(dP); if (typ(dP) != t_INT) pari_err_BUG("mfnewsplit"); }
    4635         315 :   if (d == 2 || expi(dP) < 62)
    4636             :   {
    4637         287 :     if (expi(dP) < 31)
    4638         287 :       P = NF? rnfpolredabs(NF, P,flag): polredabs0(P,flag);
    4639             :     else
    4640           0 :       P = NF? rnfpolredbest(NF,P,flag): polredbest(P,flag);
    4641         287 :     if (flag)
    4642             :     {
    4643         259 :       a = gel(P,2); if (typ(a) == t_POLMOD) a = gel(a,2);
    4644         259 :       P = gel(P,1);
    4645             :     }
    4646             :   }
    4647         315 :   *pP = P;
    4648         315 :   *pa = a;
    4649             : }
    4650             : 
    4651             : /* Diagonalize and normalize. See mfsplit for meaning of flag. */
    4652             : static GEN
    4653         938 : mfspclean(GEN mf, GEN mf0, GEN NF, long ord, GEN simplesp, long flag)
    4654             : {
    4655         938 :   const long vz = 1;
    4656         938 :   long i, l = lg(simplesp), dim = MF_get_dim(mf);
    4657         938 :   GEN res = cgetg(l, t_MAT), pols = cgetg(l, t_VEC);
    4658         938 :   GEN zeros = (mf == mf0)? NULL: zerocol(dim - MF_get_dim(mf0));
    4659        2247 :   for (i = 1; i < l; i++)
    4660             :   {
    4661        1309 :     GEN ATP = gel(simplesp, i), A = gel(ATP,1), P = gel(ATP,3);
    4662        1309 :     long d = degpol(P);
    4663        1309 :     GEN a, v = (flag && d > flag)? NULL: gel(A,1);
    4664        1309 :     if (d == 1) P = pol_x(vz);
    4665             :     else
    4666             :     {
    4667         322 :       pol_red(NF, &P, &a, !!v);
    4668         322 :       if (v)
    4669             :       { /* Mod(a,P) root of charpoly(T), K*gpowers(a) = eigenvector of T */
    4670         294 :         GEN K, den, M = cgetg(d+1, t_MAT), T = gel(ATP,2);
    4671             :         long j;
    4672         294 :         T = shallowtrans(T);
    4673         294 :         gel(M,1) = vec_ei(d,1); /* basis of cyclic vectors */
    4674         294 :         for (j = 2; j <= d; j++) gel(M,j) = RgM_RgC_mul(T, gel(M,j-1));
    4675         294 :         M = Q_primpart(M);
    4676         399 :         K = NF? ZabM_inv(liftpol_shallow(M), nf_get_pol(NF), ord, &den)
    4677         399 :               : ZM_inv(M,&den);
    4678         294 :         K = shallowtrans(K);
    4679         294 :         v = gequalX(a)? pol_x_powers(d, vz): RgXQ_powers(a, d-1, P);
    4680         294 :         v = gmodulo(RgM_RgC_mul(A, RgM_RgC_mul(K,v)), P);
    4681             :       }
    4682             :     }
    4683        1309 :     if (v)
    4684             :     {
    4685        1281 :       v = mf_normalize(mf0, v); if (zeros) v = shallowconcat(zeros,v);
    4686        1281 :       gel(res,i) = v; if (flag) setlg(res,i+1);
    4687             :     }
    4688             :     else
    4689          28 :       gel(res,i) = zerocol(dim);
    4690        1309 :     gel(pols,i) = P;
    4691             :   }
    4692         938 :   return mkvec2(res, pols);
    4693             : }
    4694             : 
    4695             : /* return v = v_{X-r}(P), and set Z = P / (X-r)^v */
    4696             : static long
    4697          63 : RgX_valrem_root(GEN P, GEN r, GEN *Z)
    4698             : {
    4699             :   long v;
    4700         126 :   for (v = 0; degpol(P); v++)
    4701             :   {
    4702         126 :     GEN t, Q = RgX_div_by_X_x(P, r, &t);
    4703         126 :     if (!gequal0(t)) break;
    4704          63 :     P = Q;
    4705             :   }
    4706          63 :   *Z = P; return v;
    4707             : }
    4708             : static GEN
    4709        1043 : mynffactor(GEN NF, GEN P, long dimlim)
    4710             : {
    4711             :   long i, l, v;
    4712             :   GEN R, E;
    4713        1043 :   if (dimlim != 1)
    4714             :   {
    4715         490 :     R = NF? nffactor(NF, P): QX_factor(P);
    4716         490 :     if (!dimlim) return R;
    4717          21 :     E = gel(R,2);
    4718          21 :     R = gel(R,1); l = lg(R);
    4719          98 :     for (i = 1; i < l; i++)
    4720          91 :       if (degpol(gel(R,i)) > dimlim) break;
    4721          21 :     if (i == 1) return NULL;
    4722          21 :     setlg(E,i);
    4723          21 :     setlg(R,i); return mkmat2(R, E);
    4724             :   }
    4725             :   /* dimlim = 1 */
    4726         553 :   R = nfroots(NF, P); l = lg(R);
    4727         553 :   if (l == 1) return NULL;
    4728         490 :   v = varn(P);
    4729         490 :   settyp(R, t_COL);
    4730         490 :   if (degpol(P) == l-1)
    4731         441 :     E = const_col(l-1, gen_1);
    4732             :   else
    4733             :   {
    4734          49 :     E = cgetg(l, t_COL);
    4735          49 :     for (i = 1; i < l; i++) gel(E,i) = utoi(RgX_valrem_root(P, gel(R,i), &P));
    4736             :   }
    4737         490 :   R = deg1_from_roots(R, v);
    4738         490 :   return mkmat2(R, E);
    4739             : }
    4740             : 
    4741             : /* Let K be a number field attached to NF (Q if NF = NULL). A K-vector
    4742             :  * space of dimension d > 0 is given by a t_MAT A (n x d, full column rank)
    4743             :  * giving a K-basis, X a section (d x n: left pseudo-inverse of A). Return a
    4744             :  * pair (T, fa), where T is an element of the Hecke algebra (a sum of Tp taken
    4745             :  * from vector vTp) acting on A (a d x d t_MAT) and fa is the factorization of
    4746             :  * its characteristic polynomial, limited to factors of degree <= dimlim if
    4747             :  * dimlim != 0 (return NULL if there are no factors of degree <= dimlim) */
    4748             : static GEN
    4749        1036 : findbestsplit(GEN NF, GEN vTp, GEN A, GEN X, long dimlim, long vz)
    4750             : {
    4751        1036 :   GEN T = NULL, Tkeep = NULL, fakeep = NULL;
    4752        1036 :   long lmax = 0, i, lT = lg(vTp);
    4753        2240 :   for (i = 1; i < lT; i++)
    4754             :   {
    4755        1120 :     GEN D, P, E, fa, TpA = gel(vTp,i);
    4756             :     long l;
    4757        2093 :     if (typ(TpA) == t_INT) break;
    4758        1043 :     if (lg(TpA) > lg(A)) TpA = RgM_mul(X, RgM_mul(TpA, A)); /* Tp | A */
    4759        1043 :     T = T ? RgM_add(T, TpA) : TpA;
    4760        1043 :     if (!NF) { P = QM_charpoly_ZX(T); setvarn(P, vz); }
    4761             :     else
    4762             :     {
    4763         196 :       P = charpoly(Q_remove_denom(T, &D), vz);
    4764         196 :       if (D) P = gdiv(RgX_unscale(P, D), powiu(D, degpol(P)));
    4765             :     }
    4766        1043 :     fa = mynffactor(NF, P, dimlim);
    4767        1043 :     if (!fa) return NULL;
    4768         980 :     E = gel(fa, 2);
    4769             :     /* characteristic polynomial is separable ? */
    4770         980 :     if (isint1(vecmax(E))) { Tkeep = T; fakeep = fa; break; }
    4771          84 :     l = lg(E);
    4772             :     /* characteristic polynomial has more factors than before ? */
    4773          84 :     if (l > lmax) { lmax = l; Tkeep = T; fakeep = fa; }
    4774             :   }
    4775         973 :   return mkvec2(Tkeep, fakeep);
    4776             : }
    4777             : 
    4778             : static GEN
    4779         161 : nfcontent(GEN nf, GEN v)
    4780             : {
    4781         161 :   long i, l = lg(v);
    4782         161 :   GEN c = gel(v,1);
    4783         161 :   for (i = 2; i < l; i++) c = idealadd(nf, c, gel(v,i));
    4784         161 :   if (typ(c) == t_MAT && gequal1(gcoeff(c,1,1))) c = gen_1;
    4785         161 :   return c;
    4786             : }
    4787             : static GEN
    4788         252 : nf_primpart(GEN nf, GEN B)
    4789             : {
    4790         252 :   switch(typ(B))
    4791             :   {
    4792             :     case t_COL:
    4793             :     {
    4794         161 :       GEN A = matalgtobasis(nf, B), c = nfcontent(nf, A);
    4795         161 :       if (typ(c) == t_INT) return B;
    4796          21 :       c = idealred_elt(nf,c);
    4797          21 :       A = Q_primpart( nfC_nf_mul(nf, A, Q_primpart(nfinv(nf,c))) );
    4798          21 :       A = liftpol_shallow( matbasistoalg(nf, A) );
    4799          21 :       if (gexpo(A) > gexpo(B)) A = B;
    4800          21 :       return A;
    4801             :     }
    4802             :     case t_MAT:
    4803             :     {
    4804             :       long i, l;
    4805          91 :       GEN A = cgetg_copy(B, &l);
    4806          91 :       for (i = 1; i < l; i++) gel(A,i) = nf_primpart(nf, gel(B,i));
    4807          91 :       return A;
    4808             :     }
    4809             :     default:
    4810           0 :       pari_err_TYPE("nf_primpart", B);
    4811             :       return NULL; /*LCOV_EXCL_LINE*/
    4812             :   }
    4813             : }
    4814             : 
    4815             : /* rotate entries of v to accomodate new entry 'x' (push out oldest entry) */
    4816             : static void
    4817        1001 : vecpush(GEN v, GEN x)
    4818             : {
    4819             :   long i;
    4820        1001 :   for (i = lg(v)-1; i > 1; i--) gel(v,i) = gel(v,i-1);
    4821        1001 :   gel(v,1) = x;
    4822        1001 : }
    4823             : 
    4824             : /* sort t_VEC of vector spaces by increasing dimension */
    4825             : static GEN
    4826         938 : sort_by_dim(GEN v)
    4827             : {
    4828         938 :   long i, l = lg(v);
    4829         938 :   GEN D = cgetg(l, t_VECSMALL);
    4830         938 :   for (i = 1; i < l; i++) D[i] = lg(gmael(v,i,2));
    4831         938 :   return vecpermute(v , vecsmall_indexsort(D));
    4832             : }
    4833             : static GEN
    4834         938 : split_starting_space(GEN mf)
    4835             : {
    4836         938 :   long d = MF_get_dim(mf), d2;
    4837         938 :   GEN id = matid(d);
    4838         938 :   switch(MF_get_space(mf))
    4839             :   {
    4840             :     case mf_NEW:
    4841         931 :     case mf_CUSP: return mkvec2(id, id);
    4842             :   }
    4843           7 :   d2 = lg(MF_get_S(mf))-1;
    4844           7 :   return mkvec2(vecslice(id, d-d2+1,d),
    4845             :                 shallowconcat(zeromat(d2,d-d2),matid(d2)));
    4846             : }
    4847             : /* If dimlim > 0, keep only the dimension <= dimlim eigenspaces.
    4848             :  * See mfsplit for the meaning of flag. */
    4849             : static GEN
    4850        1330 : split_ii(GEN mf, long dimlim, long flag, long *pnewd)
    4851             : {
    4852             :   forprime_t iter;
    4853        1330 :   GEN CHI = MF_get_CHI(mf), empty = cgetg(1, t_VEC), mf0 = mf;
    4854             :   GEN NF, POLCYC, todosp, Tpbigvec, simplesp;
    4855        1330 :   long N = MF_get_N(mf), k = MF_get_k(mf);
    4856        1330 :   long ord, FC, NEWT, dimsimple = 0, newd = -1;
    4857        1330 :   const long NBH = 5, vz = 1;
    4858             :   ulong p;
    4859             : 
    4860        1330 :   switch(MF_get_space(mf))
    4861             :   {
    4862        1141 :     case mf_NEW: break;
    4863             :     case mf_CUSP:
    4864             :     case mf_FULL:
    4865         182 :       if (k > 1) { mf0 = mfinittonew(mf); break; }
    4866         175 :       newd = lg(MF_get_S(mf))-1 - mfolddim(N, k, CHI);
    4867         175 :       break;
    4868           7 :     default: pari_err_TYPE("mfsplit [space does not contain newspace]", mf);
    4869             :       return NULL; /*LCOV_EXCL_LINE*/
    4870             :   }
    4871        1323 :   if (newd < 0) newd = mf0? MF_get_dim(mf0): 0;
    4872        1323 :   *pnewd = newd;
    4873        1323 :   if (!newd) return mkvec2(cgetg(1, t_MAT), empty);
    4874             : 
    4875         938 :   NEWT = (k > 1 && MF_get_space(mf0) == mf_NEW);
    4876         938 :   todosp = mkvec( split_starting_space(mf0) );
    4877         938 :   simplesp = empty;
    4878         938 :   FC = mfcharconductor(CHI);
    4879         938 :   ord = mfcharorder_canon(CHI);
    4880         938 :   if (ord == 1) NF = POLCYC = NULL;
    4881             :   else
    4882             :   {
    4883         154 :     POLCYC = mfcharpol(CHI);
    4884         154 :     NF = nfinit(POLCYC,DEFAULTPREC);
    4885             :   }
    4886         938 :   Tpbigvec = zerovec(NBH);
    4887         938 :   u_forprime_init(&iter, 2, ULONG_MAX);
    4888         938 :   while (dimsimple < newd && (p = u_forprime_next(&iter)))
    4889             :   {
    4890             :     GEN nextsp;
    4891             :     long ind;
    4892        1246 :     if (N % (p*p) == 0 && N/p % FC == 0) continue; /* T_p = 0 in this case */
    4893        1001 :     vecpush(Tpbigvec, NEWT? mfnewmathecke_p(mf0,p): mfheckemat_p(mf0,p));
    4894        1001 :     nextsp = empty;
    4895        2037 :     for (ind = 1; ind < lg(todosp); ind++)
    4896             :     {
    4897        1036 :       GEN tmp = gel(todosp, ind), fa, P, E, D, Tp, DTp;
    4898        1036 :       GEN A = gel(tmp, 1);
    4899        1036 :       GEN X = gel(tmp, 2);
    4900             :       long lP, i;
    4901        1036 :       tmp = findbestsplit(NF, Tpbigvec, A, X, dimlim, vz);
    4902        1750 :       if (!tmp) continue; /* nothing there */
    4903         973 :       Tp = gel(tmp, 1);
    4904         973 :       fa = gel(tmp, 2);
    4905         973 :       P = gel(fa, 1);
    4906         973 :       E = gel(fa, 2); lP = lg(P);
    4907             :       /* lP > 1 */
    4908         973 :       if (DEBUGLEVEL) err_printf("Exponents = %Ps\n", E);
    4909         973 :       if (lP == 2)
    4910             :       {
    4911         693 :         GEN P1 = gel(P,1);
    4912         693 :         long e1 = itos(gel(E,1)), d1 = degpol(P1);
    4913         693 :         if (e1 * d1 == lg(Tp)-1)
    4914             :         {
    4915         651 :           if (e1 > 1) nextsp = vec_append(nextsp, mkvec2(A,X));
    4916             :           else
    4917             :           { /* simple module */
    4918         637 :             simplesp = vec_append(simplesp, mkvec3(A,Tp,P1));
    4919         637 :             dimsimple += d1;
    4920             :           }
    4921         651 :           continue;
    4922             :         }
    4923             :       }
    4924             :       /* Found splitting */
    4925         322 :       DTp = Q_remove_denom(Tp, &D);
    4926        1092 :       for (i = 1; i < lP; i++)
    4927             :       {
    4928         770 :         GEN Ai, Xi, dXi, AAi, v, y, Pi = gel(P,i);
    4929         770 :         Ai = RgX_RgM_eval(D? RgX_rescale(Pi,D): Pi, DTp);
    4930         770 :         Ai = QabM_ker(Ai, POLCYC, ord);
    4931         770 :         if (NF) Ai = nf_primpart(NF, Ai);
    4932             : 
    4933         770 :         AAi = RgM_mul(A, Ai);
    4934             :         /* gives section, works on nonsquare matrices */
    4935         770 :         Xi = QabM_pseudoinv(Ai, POLCYC, ord, &v, &dXi);
    4936         770 :         Xi = RgM_Rg_div(Xi, dXi);
    4937         770 :         y = gel(v,1);
    4938         770 :         if (isint1(gel(E,i)))
    4939             :         {
    4940         672 :           GEN Tpi = RgM_mul(Xi, RgM_mul(rowpermute(Tp,y), Ai));
    4941         672 :           simplesp = vec_append(simplesp, mkvec3(AAi, Tpi, Pi));
    4942         672 :           dimsimple += degpol(Pi);
    4943             :         }
    4944             :         else
    4945             :         {
    4946          98 :           Xi = RgM_mul(Xi, rowpermute(X,y));
    4947          98 :           nextsp = vec_append(nextsp, mkvec2(AAi, Xi));
    4948             :         }
    4949             :       }
    4950             :     }
    4951        1001 :     todosp = nextsp; if (lg(todosp) == 1) break;
    4952             :   }
    4953         938 :   if (DEBUGLEVEL) err_printf("end split, need to clean\n");
    4954         938 :   return mfspclean(mf, mf0, NF, ord, sort_by_dim(simplesp), flag);
    4955             : }
    4956             : static GEN
    4957          14 : dim_filter(GEN v, long dim)
    4958             : {
    4959          14 :   GEN P = gel(v,2);
    4960          14 :   long j, l = lg(P);
    4961         112 :   for (j = 1; j < l; j++)
    4962         105 :     if (degpol(gel(P,j)) > dim)
    4963             :     {
    4964           7 :       v = mkvec2(vecslice(gel(v,1),1,j-1), vecslice(P,1,j-1));
    4965           7 :       break;
    4966             :     }
    4967          14 :   return v;
    4968             : }
    4969             : static long
    4970         196 : dim_sum(GEN v)
    4971             : {
    4972         196 :   GEN P = gel(v,2);
    4973         196 :   long j, l = lg(P), d = 0;
    4974         196 :   for (j = 1; j < l; j++) d += degpol(gel(P,j));
    4975         196 :   return d;
    4976             : }
    4977             : static GEN
    4978        1267 : split_i(GEN mf, long dimlim, long flag)
    4979        1267 : { long junk; return split_ii(mf, dimlim, flag, &junk); }
    4980             : /* mf is either already split or output by mfinit. Splitting is done only for
    4981             :  * newspace except in weight 1. If flag = 0 (default) split completely.
    4982             :  * If flag = d > 0, only give the Galois polynomials in degree > d
    4983             :  * Flag is ignored if dimlim = 1. */
    4984             : GEN
    4985          77 : mfsplit(GEN mf0, long dimlim, long flag)
    4986             : {
    4987          77 :   pari_sp av = avma;
    4988          77 :   GEN v, mf = checkMF_i(mf0);
    4989          77 :   if (!mf) pari_err_TYPE("mfsplit", mf0);
    4990          77 :   if ((v = obj_check(mf, MF_SPLIT)))
    4991          14 :   { if (dimlim) v = dim_filter(v, dimlim); }
    4992          63 :   else if (dimlim && (v = obj_check(mf, MF_SPLITN)))
    4993          21 :   { v = (itos(gel(v,1)) >= dimlim)? dim_filter(gel(v,2), dimlim): NULL; }
    4994          77 :   if (!v)
    4995             :   {
    4996             :     long newd;
    4997          63 :     v = split_ii(mf, dimlim, flag, &newd);
    4998          63 :     if (lg(v) == 1) obj_insert(mf, MF_SPLITN, mkvec2(utoi(dimlim), v));
    4999          63 :     else if (!flag)
    5000             :     {
    5001          42 :       if (dim_sum(v) == newd) obj_insert(mf, MF_SPLIT,v);
    5002          21 :       else obj_insert(mf, MF_SPLITN, mkvec2(utoi(dimlim), v));
    5003             :     }
    5004             :   }
    5005          77 :   return gerepilecopy(av, v);
    5006             : }
    5007             : static GEN
    5008         364 : split(GEN mf) { return split_i(mf,0,0); }
    5009             : GEN
    5010         707 : MF_get_newforms(GEN mf) { return gel(obj_checkbuild(mf,MF_SPLIT,&split),1); }
    5011             : GEN
    5012         532 : MF_get_fields(GEN mf) { return gel(obj_checkbuild(mf,MF_SPLIT,&split),2); }
    5013             : 
    5014             : /*************************************************************************/
    5015             : /*                     Modular forms of Weight 1                         */
    5016             : /*************************************************************************/
    5017             : /* S_1(G_0(N)), small N. Return 1 if definitely empty; return 0 if maybe
    5018             :  * non-empty  */
    5019             : static int
    5020       16065 : wt1empty(long N)
    5021             : {
    5022       16065 :   if (N <= 100) switch (N)
    5023             :   { /* non-empty [32/100] */
    5024             :     case 23: case 31: case 39: case 44: case 46:
    5025             :     case 47: case 52: case 55: case 56: case 57:
    5026             :     case 59: case 62: case 63: case 68: case 69:
    5027             :     case 71: case 72: case 76: case 77: case 78:
    5028             :     case 79: case 80: case 83: case 84: case 87:
    5029             :     case 88: case 92: case 93: case 94: case 95:
    5030        5439 :     case 99: case 100: return 0;
    5031        3472 :     default: return 1;
    5032             :   }
    5033        7154 :   if (N <= 600) switch(N)
    5034             :   { /* empty [111/500] */
    5035             :     case 101: case 102: case 105: case 106: case 109:
    5036             :     case 113: case 121: case 122: case 123: case 125:
    5037             :     case 130: case 134: case 137: case 146: case 149:
    5038             :     case 150: case 153: case 157: case 162: case 163:
    5039             :     case 169: case 170: case 173: case 178: case 181:
    5040             :     case 182: case 185: case 187: case 193: case 194:
    5041             :     case 197: case 202: case 205: case 210: case 218:
    5042             :     case 221: case 226: case 233: case 241: case 242:
    5043             :     case 245: case 246: case 250: case 257: case 265:
    5044             :     case 267: case 269: case 274: case 277: case 281:
    5045             :     case 289: case 293: case 298: case 305: case 306:
    5046             :     case 313: case 314: case 317: case 326: case 337:
    5047             :     case 338: case 346: case 349: case 353: case 361:
    5048             :     case 362: case 365: case 369: case 370: case 373:
    5049             :     case 374: case 377: case 386: case 389: case 394:
    5050             :     case 397: case 401: case 409: case 410: case 421:
    5051             :     case 425: case 427: case 433: case 442: case 449:
    5052             :     case 457: case 461: case 466: case 481: case 482:
    5053             :     case 485: case 490: case 493: case 509: case 514:
    5054             :     case 521: case 530: case 533: case 534: case 538:
    5055             :     case 541: case 545: case 554: case 557: case 562:
    5056             :     case 565: case 569: case 577: case 578: case 586:
    5057         336 :     case 593: return 1;
    5058        6804 :     default: return 0;
    5059             :   }
    5060          14 :   return 0;
    5061             : }
    5062             : 
    5063             : static GEN
    5064          28 : initwt1trace(GEN mf)
    5065             : {
    5066          28 :   GEN S = MF_get_S(mf), v, H;
    5067             :   long l, i;
    5068          28 :   if (lg(S) == 1) return mftrivial();
    5069          28 :   H = mfheckemat(mf, Mindex_as_coef(mf));
    5070          28 :   l = lg(H); v = cgetg(l, t_VEC);
    5071          28 :   for (i = 1; i < l; i++) gel(v,i) = gtrace(gel(H,i));
    5072          28 :   v = Minv_RgC_mul(MF_get_Minv(mf), v);
    5073          28 :   return mflineardiv_linear(S, v, 1);
    5074             : }
    5075             : static GEN
    5076          21 : initwt1newtrace(GEN mf)
    5077             : {
    5078          21 :   GEN v, D, S, Mindex, CHI = MF_get_CHI(mf);
    5079          21 :   long FC, lD, i, sb, N1, N2, lM, N = MF_get_N(mf);
    5080          21 :   CHI = mfchartoprimitive(CHI, &FC);
    5081          21 :   if (N % FC || mfcharparity(CHI) == 1) return mftrivial();
    5082          21 :   D = mydivisorsu(N/FC); lD = lg(D);
    5083          21 :   S = MF_get_S(mf);
    5084          21 :   if (lg(S) == 1) return mftrivial();
    5085          21 :   N2 = newd_params2(N);
    5086          21 :   N1 = N / N2;
    5087          21 :   Mindex = MF_get_Mindex(mf);
    5088          21 :   lM = lg(Mindex);
    5089          21 :   sb = Mindex[lM-1];
    5090          21 :   v = zerovec(sb+1);
    5091          42 :   for (i = 1; i < lD; i++)
    5092             :   {
    5093          21 :     long M = FC*D[i], j;
    5094          21 :     GEN tf = initwt1trace(M == N? mf: mfinit_Nkchi(M, 1, CHI, mf_CUSP, 0));
    5095             :     GEN listd, w;
    5096          21 :     if (mf_get_type(tf) == t_MF_CONST) continue;
    5097          21 :     w = mfcoefs_i(tf, sb, 1);
    5098          21 :     if (M == N) { v = gadd(v, w); continue; }
    5099           0 :     listd = mydivisorsu(u_ppo(ugcd(N/M, N1), FC));
    5100           0 :     for (j = 1; j < lg(listd); j++)
    5101             :     {
    5102           0 :       long d = listd[j], d2 = d*d; /* coprime to FC */
    5103           0 :       GEN dk = mfchareval_i(CHI, d);
    5104           0 :       long NMd = N/(M*d), m;
    5105           0 :       for (m = 1; m <= sb/d2; m++)
    5106             :       {
    5107           0 :         long be = mubeta2(NMd, m);
    5108           0 :         if (be)
    5109             :         {
    5110           0 :           GEN c = gmul(dk, gmulsg(be, gel(w, m+1)));
    5111           0 :           long n = m*d2;
    5112           0 :           gel(v, n+1) = gadd(gel(v, n+1), c);
    5113             :         }
    5114             :       }
    5115             :     }
    5116             :   }
    5117          21 :   if (gequal0(gel(v,2))) return mftrivial();
    5118          21 :   v = vecpermute(v,Mindex);
    5119          21 :   v = Minv_RgC_mul(MF_get_Minv(mf), v);
    5120          21 :   return mflineardiv_linear(S, v, 1);
    5121             : }
    5122             : 
    5123             : /* Matrix of T(p), p \nmid N */
    5124             : static GEN
    5125         196 : Tpmat(long p, long lim, GEN CHI)
    5126             : {
    5127         196 :   GEN M = zeromatcopy(lim, p*lim), chip = mfchareval_i(CHI, p); /* != 0 */
    5128             :   long i, j, pi, pj;
    5129         196 :   gcoeff(M, 1, 1) = gaddsg(1, chip);
    5130         196 :   for (i = 1, pi = p; i < lim; i++,  pi += p) gcoeff(M, i+1, pi+1) = gen_1;
    5131         196 :   for (j = 1, pj = p; pj < lim; j++, pj += p) gcoeff(M, pj+1, j+1) = chip;
    5132         196 :   return M;
    5133             : }
    5134             : 
    5135             : /* assume !wt1empty(N), in particular N>25 */
    5136             : /* Returns [[lim,p], mf (weight 2), p*lim x dim matrix] */
    5137             : static GEN
    5138        1799 : mfwt1_pre(long N)
    5139             : {
    5140        1799 :   GEN M, mf = mfinit_Nkchi(N, 2, mfchartrivial(), mf_CUSP, 0);
    5141             :   /*not empty for N>25*/
    5142             :   long p, lim;
    5143        1799 :   if (uisprime(N))
    5144             :   {
    5145         392 :     p = 2; /*N>25 is not 2 */
    5146         392 :     lim = ceilA1(N, 3);
    5147             :   }
    5148             :   else
    5149             :   {
    5150             :     forprime_t S;
    5151        1407 :     u_forprime_init(&S, 2, N);
    5152        1407 :     while ((p = u_forprime_next(&S)))
    5153        2527 :       if (N % p) break;
    5154        1407 :     lim = mfsturm_mf(mf) + 1;
    5155             :   }
    5156             :   /* p = smalllest prime not dividing N */
    5157        1799 :   M = bhnmat_extend_nocache(MF_get_M(mf), N, p*lim-1, 1, MF_get_S(mf));
    5158        1799 :   return mkvec3(mkvecsmall2(lim, p), mf, M);
    5159             : }
    5160             : 
    5161             : /* lg(A) > 1, E a t_POL */
    5162             : static GEN
    5163        1120 : mfmatsermul(GEN A, GEN E)
    5164             : {
    5165        1120 :   long j, l = lg(A), r = nbrows(A);
    5166        1120 :   GEN M = cgetg(l, t_MAT);
    5167        1120 :   E = RgXn_red_shallow(E, r+1);
    5168       12481 :   for (j = 1; j < l; j++)
    5169             :   {
    5170       11361 :     GEN c = RgV_to_RgX(gel(A,j), 0);
    5171       11361 :     gel(M, j) = RgX_to_RgC(RgXn_mul(c, E, r+1), r);
    5172             :   }
    5173        1120 :   return M;
    5174             : }
    5175             : /* lg(Ap) > 1, Ep an Flxn */
    5176             : static GEN
    5177         728 : mfmatsermul_Fl(GEN Ap, GEN Ep, ulong p)
    5178             : {
    5179         728 :   long j, l = lg(Ap), r = nbrows(Ap);
    5180         728 :   GEN M = cgetg(l, t_MAT);
    5181        9590 :   for (j = 1; j < l; j++)
    5182             :   {
    5183        8862 :     GEN c = Flv_to_Flx(gel(Ap,j), 0);
    5184        8862 :     gel(M,j) = Flx_to_Flv(Flxn_mul(c, Ep, r+1, p), r);
    5185             :   }
    5186         728 :   return M;
    5187             : }
    5188             : 
    5189             : /* CHI mod F | N, return mfchar of modulus N.
    5190             :  * FIXME: wasteful, G should be precomputed  */
    5191             : static GEN
    5192       16548 : mfcharinduce(GEN CHI, long N)
    5193             : {
    5194             :   GEN G, chi;
    5195       16548 :   if (mfcharmodulus(CHI) == N) return CHI;
    5196        2940 :   G = znstar0(utoipos(N), 1);
    5197        2940 :   chi = zncharinduce(gel(CHI,1), gel(CHI,2), G);
    5198        2940 :   CHI = leafcopy(CHI);
    5199        2940 :   gel(CHI,1) = G;
    5200        2940 :   gel(CHI,2) = chi; return CHI;
    5201             : }
    5202             : 
    5203             : static GEN
    5204        3983 : gmfcharno(GEN CHI)
    5205             : {
    5206        3983 :   GEN G = gel(CHI,1), chi = gel(CHI,2);
    5207        3983 :   return mkintmod(znconreyexp(G, chi), znstar_get_N(G));
    5208             : }
    5209             : static long
    5210       12726 : mfcharno(GEN CHI)
    5211             : {
    5212       12726 :   GEN n = znconreyexp(gel(CHI,1), gel(CHI,2));
    5213       12726 :   return itou(n);
    5214             : }
    5215             : 
    5216             : /* return k such that minimal mfcharacter in Galois orbit of CHI is CHI^k */
    5217             : static long
    5218       11347 : mfconreyminimize(GEN CHI)
    5219             : {
    5220       11347 :   GEN G = gel(CHI,1), cyc, chi;
    5221       11347 :   cyc = ZV_to_zv(znstar_get_cyc(G));
    5222       11347 :   chi = ZV_to_zv(znconreychar(G, gel(CHI,2)));
    5223       11347 :   return zv_cyc_minimize(cyc, chi, coprimes_zv(mfcharorder(CHI)));
    5224             : }
    5225             : 
    5226             : /* find scalar c such that first non-0 entry of c*v is 1; return c*v
    5227             :  * (set c = NULL for 1) */
    5228             : static GEN
    5229        1701 : RgV_normalize(GEN v, GEN *pc)
    5230             : {
    5231        1701 :   long i, l = lg(v);
    5232        1701 :   *pc = NULL;
    5233        3948 :   for (i = 1; i < l; i++)
    5234             :   {
    5235        3948 :     GEN c = gel(v,i);
    5236        3948 :     if (!gequal0(c))
    5237             :     {
    5238        1701 :       if (gequal1(c)) { *pc = gen_1; return v; }
    5239         595 :       *pc = ginv(c); return RgV_Rg_mul(v, *pc);
    5240             :     }
    5241             :   }
    5242           0 :   return v;
    5243             : }
    5244             : /* ordchi != 2 mod 4 */
    5245             : static GEN
    5246        2282 : mftreatdihedral(GEN DIH, GEN POLCYC, long ordchi, long biglim, GEN *pS)
    5247             : {
    5248             :   GEN M, Minv, C;
    5249             :   long l, i;
    5250        2282 :   l = lg(DIH); if (l == 1) return NULL;
    5251        2282 :   if (!pS) return DIH;
    5252         728 :   C = cgetg(l, t_VEC);
    5253         728 :   M = cgetg(l, t_MAT);
    5254        2044 :   for (i = 1; i < l; i++)
    5255             :   {
    5256        1316 :     GEN c, v = mfcoefs_i(gel(DIH,i), biglim, 1);
    5257        1316 :     gel(M,i) = RgV_normalize(v, &c);
    5258        1316 :     gel(C,i) = Rg_col_ei(c, l-1, i);
    5259             :   }
    5260         728 :   Minv = gel(mfclean(M,POLCYC,ordchi,0),2);
    5261         728 :   M = RgM_Minv_mul(M, Minv);
    5262         728 :   C = RgM_Minv_mul(C, Minv);
    5263         728 :   *pS = vecmflinear(DIH, C);
    5264         728 :   return M;
    5265             : }
    5266             : 
    5267             : static GEN
    5268         189 : mfstabiter(GEN M, GEN A2, GEN E1inv, long lim, GEN P, long ordchi)
    5269             : {
    5270             :   GEN A, VC, con;
    5271         189 :   E1inv = primitive_part(E1inv, &con);
    5272         189 :   VC = con? ginv(con): gen_1;
    5273         189 :   A = mfmatsermul(A2, E1inv);
    5274             :   while(1)
    5275         105 :   {
    5276         294 :     GEN R = shallowconcat(RgM_mul(M,A), rowslice(A,1,lim));
    5277         294 :     GEN B = QabM_ker(R, P, ordchi);
    5278         294 :     long lA = lg(A), lB = lg(B);
    5279         294 :     if (lB == 1) return NULL;
    5280         294 :     if (lB == lA) return mkvec2(A, VC);
    5281         105 :     B = rowslice(B, 1, lA-1);
    5282         105 :     if (ordchi != 1) B = gmodulo(B, P);
    5283         105 :     A = Q_primitive_part(RgM_mul(A,B), &con);
    5284         105 :     VC = gmul(VC,B); /* first VC is a scalar, then a RgM */
    5285         105 :     if (con) VC = RgM_Rg_div(VC, con);
    5286             :   }
    5287             : }
    5288             : static long
    5289         189 : mfstabitermodp(GEN Mp, GEN Ap, long p, long lim)
    5290             : {
    5291         189 :   GEN VC = NULL;
    5292             :   while (1)
    5293          21 :   {
    5294         210 :     GEN Rp = shallowconcat(Flm_mul(Mp,Ap,p), rowslice(Ap,1,lim));
    5295         210 :     GEN Bp = Flm_ker(Rp, p);
    5296         210 :     long lA = lg(Ap), lB = lg(Bp);
    5297         210 :     if (lB == 1) return 0;
    5298         210 :     if (lB == lA) return lA-1;
    5299          21 :     Bp = rowslice(Bp, 1, lA-1);
    5300          21 :     Ap = Flm_mul(Ap, Bp, p);
    5301          21 :     VC = VC? Flm_mul(VC, Bp, p): Bp;
    5302             :   }
    5303             : }
    5304             : 
    5305             : static GEN
    5306         350 : mfintereis(GEN A, GEN M2, GEN y, GEN den, GEN E2, GEN P, long ordchi)
    5307             : {
    5308         350 :   GEN z, M1 = mfmatsermul(A,E2), M1den = is_pm1(den)? M1: RgM_Rg_mul(M1,den);
    5309         350 :   M2 = RgM_mul(M2, rowpermute(M1, y));
    5310         350 :   z = QabM_ker(RgM_sub(M2,M1den), P, ordchi);
    5311         350 :   if (ordchi != 1) z = gmodulo(z, P);
    5312         350 :   return mkvec2(RgM_mul(A,z), z);
    5313             : }
    5314             : static GEN
    5315         357 : mfintereismodp(GEN A, GEN M2, GEN E2, ulong p)
    5316             : {
    5317         357 :   GEN M1 = mfmatsermul_Fl(A, E2, p), z;
    5318         357 :   long j, lx = lg(A);
    5319         357 :   z = Flm_ker(shallowconcat(M1, M2), p);
    5320         357 :   for (j = lg(z) - 1; j; j--) setlg(z[j], lx);
    5321         357 :   return mkvec2(Flm_mul(A,z,p), z);
    5322             : }
    5323             : 
    5324             : static GEN
    5325         196 : mfcharinv_i(GEN CHI)
    5326             : {
    5327         196 :   GEN G = gel(CHI,1);
    5328         196 :   CHI = leafcopy(CHI); gel(CHI,2) =  zncharconj(G, gel(CHI,2)); return CHI;
    5329             : }
    5330             : 
    5331             : /* upper bound dim S_1(Gamma_0(N),chi) performing the linear algebra mod p */
    5332             : static long
    5333         196 : mfwt1dimmodp(GEN A, GEN ES, GEN M, long ordchi, long dih, long lim)
    5334             : {
    5335             :   GEN Ap, ApF, ES1p, VC;
    5336         196 :   ulong p, r = QabM_init(ordchi, &p);
    5337             : 
    5338         196 :   ApF = Ap = QabM_to_Flm(A, r, p);
    5339         196 :   VC = NULL;
    5340         196 :   ES1p = QabX_to_Flx(gel(ES,1), r, p);
    5341         196 :   if (lg(ES) >= 3)
    5342             :   {
    5343         182 :     GEN M2 = mfmatsermul_Fl(ApF, ES1p, p);
    5344         182 :     pari_sp av = avma;
    5345             :     long i;
    5346         532 :     for (i = 2; i < lg(ES); i++)
    5347             :     {
    5348         357 :       GEN ESip = QabX_to_Flx(gel(ES,i), r, p);
    5349         357 :       GEN C, ApC = mfintereismodp(Ap, M2, ESip, p);
    5350         357 :       Ap = gel(ApC,1);
    5351         357 :       if (lg(Ap)-1 == dih) return dih;
    5352         350 :       C = gel(ApC,2); VC = VC? Flm_mul(VC, C, p): C;
    5353         350 :       gerepileall(av, 2, &Ap,&VC);
    5354             :     }
    5355             :   }
    5356             :   /* intersection of Eisenstein series quotients non empty: use Schaeffer */
    5357         189 :   Ap = mfmatsermul_Fl(Ap, Flxn_inv(ES1p,nbrows(Ap),p), p);
    5358         189 :   return mfstabitermodp(QabM_to_Flm(M,r,p), Ap, p, lim);
    5359             : }
    5360             : 
    5361             : /* Compute the full S_1(\G_0(N),\chi). If pS is NULL, only the dimension
    5362             :  * dim, in the form of a vector having dim components. Otherwise output
    5363             :  * a basis: ptvf contains a pointer to the vector of forms, and the
    5364             :  * program returns the corresponding matrix of Fourier expansions.
    5365             :  * ptdimdih gives the dimension of the subspace generated by dihedral forms;
    5366             :  * TMP is from mfwt1_pre or NULL. */
    5367             : static GEN
    5368       10738 : mfwt1basis(long N, GEN CHI, GEN TMP, GEN *pS, long *ptdimdih)
    5369             : {
    5370             :   GEN ES, mf, A, M, Tp, tmp1, tmp2, den;
    5371             :   GEN S, ESA, VC, C, POLCYC, ES1, ES1INV, DIH, a0, a0i;
    5372             :   long plim, lim, biglim, i, p, dA, dimp, ordchi, dih;
    5373             : 
    5374       10738 :   if (ptdimdih) *ptdimdih = 0;
    5375       10738 :   if (pS) *pS = NULL;
    5376       10738 :   if (wt1empty(N) || mfcharparity(CHI) != -1) return NULL;
    5377       10521 :   ordchi = mfcharorder_canon(CHI);
    5378       10521 :   if (uisprime(N) && ordchi > 4) return NULL;
    5379       10493 :   if (!pS)
    5380             :   {
    5381        7035 :     dih = mfdihedralcuspdim(N, CHI);
    5382        7035 :     DIH = zerovec(dih);
    5383             :   }
    5384             :   else
    5385             :   {
    5386        3458 :     DIH = mfdihedralcusp(N, CHI);
    5387        3458 :     dih = lg(DIH) - 1;
    5388             :   }
    5389       10493 :   POLCYC = (ordchi == 1)? NULL: mfcharpol(CHI);
    5390       10493 :   if (ptdimdih) *ptdimdih = dih;
    5391       10493 :   biglim = mfsturmNk(N, 2);
    5392       10493 :   if (N <= 600) switch(N)
    5393             :   {
    5394             :     long m;
    5395             :     case 219: case 273: case 283: case 331: case 333: case 344: case 416:
    5396             :     case 438: case 468: case 491: case 504: case 546: case 553: case 563:
    5397             :     case 566: case 581: case 592:
    5398          14 :       break; /* one chi with both exotic and dihedral forms */
    5399             :     default: /* only dihedral forms */
    5400        9429 :       if (!dih) return NULL;
    5401             :       /* fall through */
    5402             :     case 124: case 133: case 148: case 171: case 201: case 209: case 224:
    5403             :     case 229: case 248: case 261: case 266: case 288: case 296: case 301:
    5404             :     case 309: case 325: case 342: case 371: case 372: case 380: case 399:
    5405             :     case 402: case 403: case 404: case 408: case 418: case 432: case 444:
    5406             :     case 448: case 451: case 453: case 458: case 496: case 497: case 513:
    5407             :     case 522: case 527: case 532: case 576: case 579:
    5408             :       /* no chi with both exotic and dihedral; one chi with exotic forms */
    5409        3185 :       if (dih) return mftreatdihedral(DIH, POLCYC, ordchi, biglim, pS);
    5410         910 :       CHI = mfcharinduce(CHI,N);
    5411         910 :       m = mfcharno(CHI);
    5412         910 :       if (N == 124 && (m != 67 && m != 87)) return NULL;
    5413         784 :       if (N == 133 && (m != 83 && m !=125)) return NULL;
    5414         490 :       if (N == 148 && (m !=105 && m !=117)) return NULL;
    5415         364 :       if (N == 171 && (m != 94 && m !=151)) return NULL;
    5416         364 :       if (N == 201 && (m != 29 && m !=104)) return NULL;
    5417         364 :       if (N == 209 && (m != 87 && m !=197)) return NULL;
    5418         364 :       if (N == 224 && (m != 95 && m !=191)) return NULL;
    5419         364 :       if (N == 229 && (m !=107 && m !=122)) return NULL;
    5420         364 :       if (N == 248 && (m != 87 && m !=191)) return NULL;
    5421         273 :       if (N == 261 && (m != 46 && m !=244)) return NULL;
    5422         273 :       if (N == 266 && (m != 83 && m !=125)) return NULL;
    5423         273 :       if (N == 288 && (m != 31 && m !=223)) return NULL;
    5424         273 :       if (N == 296 && (m !=105 && m !=265)) return NULL;
    5425             :   }
    5426         196 :   if (!TMP) TMP = mfwt1_pre(N);
    5427         196 :   tmp1= gel(TMP,1); lim = tmp1[1]; p = tmp1[2]; plim = p*lim;
    5428         196 :   mf  = gel(TMP,2);
    5429         196 :   A   = gel(TMP,3); /* p*lim x dim matrix */
    5430         196 :   S = MF_get_S(mf);
    5431         196 :   ESA = mfeisensteinbasis(N, 1, mfcharinv_i(CHI));
    5432         196 :   ES = RgM_to_RgXV(mfvectomat(ESA, plim+1, 1), 0);
    5433         196 :   ES1 = gel(ES,1); /* does not vanish at oo */
    5434         196 :   Tp = Tpmat(p, lim, CHI);
    5435         196 :   dimp = mfwt1dimmodp(A, ES, Tp, ordchi, dih, lim);
    5436         196 :   if (!dimp) return NULL;
    5437         196 :   if (dimp == dih) return mftreatdihedral(DIH, POLCYC, ordchi, biglim, pS);
    5438         189 :   VC = gen_1;
    5439         189 :   if (lg(ES) >= 3)
    5440             :   {
    5441             :     pari_sp btop;
    5442         175 :     long lim2 = (3*lim)/2 + 1;
    5443         175 :     GEN Ash = rowslice(A, 1, lim2), M2 = mfmatsermul(Ash, ES1);
    5444             :     GEN v, y, M2M2I, M2I;
    5445         175 :     M2I = QabM_pseudoinv(M2, POLCYC, ordchi, &v, &den);
    5446         175 :     y = gel(v,1);
    5447         175 :     M2M2I = RgM_mul(M2,M2I);
    5448         175 :     btop = avma;
    5449         525 :     for (i = 2; i < lg(ES); i++)
    5450             :     {
    5451         350 :       GEN APC = mfintereis(Ash, M2M2I, y, den, gel(ES,i), POLCYC,ordchi);
    5452         350 :       Ash = gel(APC,1); if (lg(Ash) == 1) return NULL;
    5453         350 :       VC = gmul(VC, gel(APC,2));
    5454         350 :       if (gc_needed(btop, 1))
    5455             :       {
    5456           6 :         if (DEBUGMEM > 1) pari_warn(warnmem,"mfwt1basis i = %ld", i);
    5457           6 :         gerepileall(btop, 2, &Ash, &VC);
    5458             :       }
    5459             :     }
    5460         175 :     A = RgM_mul(A, vecslice(VC,1, lg(Ash)-1));
    5461             :   }
    5462         189 :   a0 = gel(ES1,2); /* non-zero */
    5463         189 :   if (gequal1(a0)) a0 = a0i = NULL;
    5464             :   else
    5465             :   {
    5466         189 :     a0i = ginv(a0);
    5467         189 :     ES1 = RgX_Rg_mul(RgX_unscale(ES1,a0), a0i);
    5468             :   }
    5469         189 :   ES1INV = RgXn_inv(ES1, plim-1);
    5470         189 :   if (a0) ES1INV = RgX_Rg_mul(RgX_unscale(ES1INV, a0i), a0i);
    5471         189 :   tmp2 = mfstabiter(Tp, A, ES1INV, lim, POLCYC, ordchi);
    5472         189 :   if (!tmp2) return NULL;
    5473         189 :   A = gel(tmp2,1); dA = lg(A);
    5474         189 :   VC = gmul(VC, gel(tmp2,2));
    5475         189 :   C = cgetg(dA, t_VEC);
    5476         189 :   M = cgetg(dA, t_MAT);
    5477         574 :   for (i = 1; i < dA; i++)
    5478             :   {
    5479         385 :     GEN c, v = gel(A,i);
    5480         385 :     gel(M,i) = RgV_normalize(v, &c);
    5481         385 :     gel(C,i) = RgC_Rg_mul(gel(VC,i), c);
    5482             :   }
    5483         189 :   if (pS)
    5484             :   {
    5485         140 :     GEN Minv = gel(mfclean(M, POLCYC, ordchi, 0), 2);
    5486         140 :     M = RgM_Minv_mul(M, Minv);
    5487         140 :     C = RgM_Minv_mul(C, Minv);
    5488         140 :     *pS = vecmflineardiv0(S, C, gel(ESA,1));
    5489             :   }
    5490         189 :   return M;
    5491             : }
    5492             : 
    5493             : static void
    5494         322 : MF_set_space(GEN mf, long x) { gmael(mf,1,4) = utoi(x); }
    5495             : static GEN
    5496         168 : mfwt1_cusptonew(GEN mf)
    5497             : {
    5498         168 :   const long vy = 1;
    5499         168 :   GEN vP, F, S, Snew, vF, v = split(mf);
    5500             :   long i, lP, dSnew, ct;
    5501             : 
    5502         168 :   F = gel(v,1);
    5503         168 :   vP= gel(v,2); lP = lg(vP);
    5504         168 :   if (lP == 1) { obj_insert(mf, MF_SPLIT, v); return NULL; }
    5505         154 :   MF_set_space(mf, mf_NEW);
    5506         154 :   S = MF_get_S(mf);
    5507         154 :   dSnew = dim_sum(v);
    5508         154 :   Snew = cgetg(dSnew + 1, t_VEC); ct = 0;
    5509         154 :   vF = cgetg(lP, t_MAT);
    5510         329 :   for (i = 1; i < lP; i++)
    5511             :   {
    5512         175 :     GEN V, P = gel(vP,i), f = liftpol_shallow(gel(F,i));
    5513         175 :     long j, d = degpol(P);
    5514         175 :     gel(vF,i) = V = zerocol(dSnew);
    5515         175 :     if (d == 1)
    5516             :     {
    5517          84 :       gel(Snew, ct+1) = mflineardiv_linear(S, f, 0);
    5518          84 :       gel(V, ct+1) = gen_1;
    5519             :     }
    5520             :     else
    5521             :     {
    5522          91 :       f = RgXV_to_RgM(f,d);
    5523         280 :       for (j = 1; j <= d; j++)
    5524             :       {
    5525         189 :         gel(Snew, ct+j) = mflineardiv_linear(S, row(f,j), 0);
    5526         189 :         gel(V, ct+j) = mkpolmod(pol_xn(j-1,vy), P);
    5527             :       }
    5528             :     }
    5529         175 :     ct += d;
    5530             :   }
    5531         154 :   obj_insert(mf, MF_SPLIT, mkvec2(vF, vP));
    5532         154 :   gel(mf,3) = Snew; return mf;
    5533             : }
    5534             : static GEN
    5535        3542 : mfwt1init(long N, GEN CHI, GEN TMP, long space, long flraw)
    5536             : {
    5537        3542 :   GEN mf, mf1, S, M = mfwt1basis(N, CHI, TMP, &S, NULL);
    5538        3542 :   if (!M) return NULL;
    5539         868 :   mf1 = mkvec4(stoi(N), gen_1, CHI, utoi(mf_CUSP));
    5540         868 :   mf = mkmf(mf1, cgetg(1,t_VEC), S, gen_0, NULL);
    5541         868 :   if (space == mf_NEW)
    5542             :   {
    5543         168 :     gel(mf,5) = mfcleanCHI(M,CHI, 0);
    5544         168 :     mf = mfwt1_cusptonew(mf); if (!mf) return NULL;
    5545         154 :     if (!flraw) M = mfcoefs_mf(mf, mfsturmNk(N,1)+1, 1);
    5546             :   }
    5547         854 :   gel(mf,5) = flraw? zerovec(3): mfcleanCHI(M, CHI, 0);
    5548         854 :   return mf;
    5549             : }
    5550             : 
    5551             : static GEN
    5552         931 : mfEMPTY(GEN mf1)
    5553             : {
    5554         931 :   GEN Minv = mkMinv(cgetg(1,t_MAT), NULL,NULL,NULL);
    5555         931 :   GEN M = mkvec3(cgetg(1,t_VECSMALL), Minv, cgetg(1,t_MAT));
    5556         931 :   return mkmf(mf1, cgetg(1,t_VEC), cgetg(1,t_VEC), cgetg(1,t_VEC), M);
    5557             : }
    5558             : static GEN
    5559         616 : mfEMPTYall(long N, GEN gk, GEN vCHI, long space)
    5560             : {
    5561             :   long i, l;
    5562             :   GEN v, gN, gs;
    5563         616 :   if (!vCHI) return cgetg(1, t_VEC);
    5564          14 :   gN = utoipos(N); gs = utoi(space);
    5565          14 :   l = lg(vCHI); v = cgetg(l, t_VEC);
    5566          14 :   for (i = 1; i < l; i++) gel(v,i) = mfEMPTY(mkvec4(gN,gk,gel(vCHI,i),gs));
    5567          14 :   return v;
    5568             : }
    5569             : 
    5570             : static GEN
    5571        3983 : fmt_dim(GEN CHI, long d, long dih)
    5572        3983 : { return mkvec4(gmfcharorder(CHI), gmfcharno(CHI), utoi(d), stoi(dih)); }
    5573             : /* merge two vector of fmt_dim's for the same vector of characters. If CHI
    5574             :  * is not NULL, remove dim-0 spaces and add character from CHI */
    5575             : static GEN
    5576           7 : merge_dims(GEN V, GEN W, GEN CHI)
    5577             : {
    5578           7 :   long i, j, id, l = lg(V);
    5579           7 :   GEN A = cgetg(l, t_VEC);
    5580           7 :   if (l == 1) return A;
    5581           7 :   id = CHI? 1: 3;
    5582          21 :   for (i = j = 1; i < l; i++)
    5583             :   {
    5584          14 :     GEN v = gel(V,i), w = gel(W,i);
    5585          14 :     long dv = itou(gel(v,id)), dvh = itou(gel(v,id+1)), d;
    5586          14 :     long dw = itou(gel(w,id)), dwh = itou(gel(w,id+1));
    5587          14 :     d = dv + dw;
    5588          14 :     if (d || CHI)
    5589          42 :       gel(A,j++) = CHI? fmt_dim(gel(CHI,i),d, dvh+dwh)
    5590          28 :                       : mkvec2s(d,dvh+dwh);
    5591             :   }
    5592           7 :   setlg(A, j); return A;
    5593             : }
    5594             : static GEN
    5595        3010 : mfdim0all(GEN w)
    5596             : {
    5597        3010 :   if (w) retconst_vec(lg(w)-1, zerovec(2));
    5598        3003 :   return cgetg(1,t_VEC);
    5599             : }
    5600             : static long
    5601        7196 : mfwt1cuspdim_i(long N, GEN CHI, GEN TMP, long *dih)
    5602             : {
    5603        7196 :   pari_sp av = avma;
    5604        7196 :   GEN b = mfwt1basis(N, CHI, TMP, NULL, dih);
    5605        7196 :   return gc_long(av, b? lg(b)-1: 0);
    5606             : }
    5607             : static long
    5608         357 : mfwt1cuspdim(long N, GEN CHI) { return mfwt1cuspdim_i(N, CHI, NULL, NULL); }
    5609             : static GEN
    5610        4144 : mfwt1cuspdimall(long N, GEN vCHI)
    5611             : {
    5612             :   GEN z, TMP, w;
    5613             :   long i, j, l;
    5614        4144 :   if (wt1empty(N)) return mfdim0all(vCHI);
    5615        1141 :   w = mfwt1chars(N,vCHI);
    5616        1141 :   l = lg(w); if (l == 1) return cgetg(1,t_VEC);
    5617        1141 :   z = cgetg(l, t_VEC);
    5618        1141 :   TMP = mfwt1_pre(N);
    5619        7861 :   for (i = j = 1; i < l; i++)
    5620             :   {
    5621        6720 :     GEN CHI = gel(w,i);
    5622        6720 :     long dih, d = mfwt1cuspdim_i(N, CHI, TMP, &dih);
    5623        6720 :     if (vCHI)
    5624          42 :       gel(z,j++) = mkvec2s(d, dih);
    5625        6678 :     else if (d)
    5626        1428 :       gel(z,j++) = fmt_dim(CHI, d, dih);
    5627             :   }
    5628        1141 :   setlg(z,j); return z;
    5629             : }
    5630             : 
    5631             : /* dimension of S_1(Gamma_1(N)) */
    5632             : static long
    5633        4123 : mfwt1cuspdimsum(long N)
    5634             : {
    5635        4123 :   pari_sp av = avma;
    5636        4123 :   GEN v = mfwt1cuspdimall(N, NULL);
    5637        4123 :   long i, ct = 0, l = lg(v);
    5638        5544 :   for (i = 1; i < l; i++)
    5639             :   {
    5640        1421 :     GEN w = gel(v,i); /* [ord(CHI),*,dim,*] */
    5641        1421 :     ct += itou(gel(w,3))*myeulerphiu(itou(gel(w,1)));
    5642             :   }
    5643        4123 :   return gc_long(av,ct);
    5644             : }
    5645             : 
    5646             : static GEN
    5647          56 : mfwt1newdimall(long N, GEN vCHI)
    5648             : {
    5649             :   GEN z, w, vTMP, fa, P, E;
    5650             :   long i, c, l, lw, P1;
    5651          56 :   if (wt1empty(N)) return mfdim0all(vCHI);
    5652          56 :   w = mfwt1chars(N,vCHI);
    5653          56 :   lw = lg(w); if (lw == 1) return cgetg(1,t_VEC);
    5654          56 :   vTMP = const_vec(N, NULL);
    5655          56 :   gel(vTMP,N) = mfwt1_pre(N);
    5656             :   /* if p || N and p \nmid F(CHI), S_1^new(G0(N),chi) = 0 */
    5657          56 :   fa = znstar_get_faN(gmael(w,1,1));
    5658          56 :   P = gel(fa,1); l = lg(P);
    5659          56 :   E = gel(fa,2);
    5660         154 :   for (i = P1 = 1; i < l; i++)
    5661          98 :     if (E[i] == 1) P1 *= itou(gel(P,i));
    5662             :   /* P1 = \prod_{v_p(N) = 1} p */
    5663          56 :   z = cgetg(lw, t_VEC);
    5664         182 :   for (i = c = 1; i < lw; i++)
    5665             :   {
    5666             :     long S, j, l, F, dihnew;
    5667         126 :     GEN D, CHI = gel(w,i), CHIP = mfchartoprimitive(CHI,&F);
    5668             : 
    5669         126 :     S = F % P1? 0: mfwt1cuspdim_i(N, CHI, gel(vTMP,N), &dihnew);
    5670         126 :     if (!S)
    5671             :     {
    5672          56 :       if (vCHI) gel(z, c++) = zerovec(2);
    5673          56 :       continue;
    5674             :     }
    5675          70 :     D = mydivisorsu(N/F); l = lg(D);
    5676          77 :     for (j = l-2; j > 0; j--) /* skip last M = N */
    5677             :     {
    5678           7 :       long M = D[j]*F, m, s, dih;
    5679           7 :       GEN TMP = gel(vTMP,M);
    5680           7 :       if (wt1empty(M) || !(m = mubeta(D[l-j]))) continue; /*m = mubeta(N/M)*/
    5681           7 :       if (!TMP) gel(vTMP,M) = TMP = mfwt1_pre(M);
    5682           7 :       s = mfwt1cuspdim_i(M, CHIP, TMP, &dih);
    5683           7 :       if (s) { S += m * s; dihnew += m * dih; }
    5684             :     }
    5685          70 :     if (vCHI)
    5686          63 :       gel(z,c++) = mkvec2s(S, dihnew);
    5687           7 :     else if (S)
    5688           7 :       gel(z, c++) = fmt_dim(CHI, S, dihnew);
    5689             :   }
    5690          56 :   setlg(z,c); return z;
    5691             : }
    5692             : 
    5693             : static GEN
    5694          28 : mfwt1olddimall(long N, GEN vCHI)
    5695             : {
    5696             :   long i, j, l;
    5697             :   GEN z, w;
    5698          28 :   if (wt1empty(N)) return mfdim0all(vCHI);
    5699          28 :   w = mfwt1chars(N,vCHI);
    5700          28 :   l = lg(w); z = cgetg(l, t_VEC);
    5701          84 :   for (i = j = 1; i < l; i++)
    5702             :   {
    5703          56 :     GEN CHI = gel(w,i);
    5704          56 :     long d = mfolddim(N, 1, CHI);
    5705          56 :     if (vCHI)
    5706          28 :       gel(z,j++) = mkvec2s(d,d?-1:0);
    5707          28 :     else if (d)
    5708           7 :       gel(z, j++) = fmt_dim(CHI, d, -1);
    5709             :   }
    5710          28 :   setlg(z,j); return z;
    5711             : }
    5712             : 
    5713             : static long
    5714         469 : mfwt1olddimsum(long N)
    5715             : {
    5716             :   GEN D;
    5717         469 :   long N2, i, l, S = 0;
    5718         469 :   newd_params(N, &N2); /* will ensure mubeta != 0 */
    5719         469 :   D = mydivisorsu(N/N2); l = lg(D);
    5720        2485 :   for (i = 2; i < l; i++)
    5721             :   {
    5722        2016 :     long M = D[l-i]*N2, d = mfwt1cuspdimsum(M);
    5723        2016 :     if (d) S -= mubeta(D[i]) * d;
    5724             :   }
    5725         469 :   return S;
    5726             : }
    5727             : static long
    5728        1050 : mfwt1newdimsum(long N)
    5729             : {
    5730        1050 :   long S = mfwt1cuspdimsum(N);
    5731        1050 :   return S? S - mfwt1olddimsum(N): 0;
    5732             : }
    5733             : 
    5734             : static long
    5735         210 : mfisdihedral(GEN vF, GEN DIH)
    5736             : {
    5737         210 :   GEN vG = gel(DIH,1), M = gel(DIH,2), v, G, bnr, w, gen, cyc, D, f, nf, con;
    5738             :   GEN f0, f0b, xin;
    5739             :   long i, l, e, j, L, n;
    5740         210 :   if (lg(M) == 1) return 0;
    5741          28 :   v = RgM_RgC_invimage(M, vF);
    5742          28 :   if (!v) return 0;
    5743          28 :   l = lg(v);
    5744          28 :   for (i = 1; i < l; i++)
    5745          28 :     if (!gequal0(gel(v,i))) break;
    5746          28 :   if (i == l) return 0;
    5747          28 :   G = gel(vG,i);
    5748          28 :   bnr = gel(G,2); cyc = bnr_get_cyc(bnr); D = gel(cyc,1);
    5749          28 :   w = gel(G,3);
    5750          28 :   f = bnr_get_mod(bnr);
    5751          28 :   nf = bnr_get_nf(bnr);
    5752          28 :   con = gel(galoisconj(nf,gen_1), 2);
    5753          28 :   f0 = gel(f,1); f0b = galoisapply(nf, con, f0);
    5754          28 :   xin = zv_to_ZV(gel(w,2)); /* xi(bnr.gen[i]) = e(xin[i] / D) */
    5755          28 :   if (!gequal(f0,f0b))
    5756             :   { /* finite part of conductor not ambiguous */
    5757          14 :     GEN a = idealmul(nf, f0, idealdivexact(nf, f0b, idealadd(nf, f0, f0b)));
    5758          14 :     GEN bnr0 = bnr;
    5759          14 :     bnr = bnrinit0(bnr_get_bnf(bnr), mkvec2(a, gel(f,2)), 1);
    5760          14 :     xin = RgV_RgM_mul(xin, bnrsurjection(bnr, bnr0));
    5761             :     /* still xi(gen[i]) = e(xin[i] / D), for the new generators */
    5762             :   }
    5763          28 :   gen = bnr_get_gen(bnr); L = lg(gen);
    5764          42 :   for (j = 1, e = itou(D); j < L; j++)
    5765             :   {
    5766          35 :     GEN Ng = idealnorm(nf, gel(gen,j));
    5767          35 :     GEN a = shifti(gel(xin,j), 1); /* xi(g_j^2) = e(a/D) */
    5768          35 :     GEN b = FpV_dotproduct(xin, isprincipalray(bnr,Ng), D);
    5769          35 :     GEN m = Fp_sub(a, b, D); /* xi(g_j/\bar{g_j}) = e(m/D) */
    5770          35 :     e = ugcd(e, itou(m)); if (e == 1) break;
    5771             :   }
    5772          28 :   n = itou(D) / e;
    5773          28 :   return n == 1? 4: 2*n;
    5774             : }
    5775             : 
    5776             : static ulong
    5777         119 : radical_u(ulong n)
    5778         119 : { return zv_prod(gel(myfactoru(n),1)); }
    5779             : 
    5780             : /* list of fundamental discriminants unramified outside N, with sign s
    5781             :  * [s = 0 => no sign condition] */
    5782             : static GEN
    5783         119 : mfunram(long N, long s)
    5784             : {
    5785         119 :   long cN = radical_u(N >> vals(N)), p = 1, m = 1, l, c, i;
    5786         119 :   GEN D = mydivisorsu(cN), res;
    5787         119 :   l = lg(D);
    5788         119 :   if (s == 1) m = 0; else if (s == -1) p = 0;
    5789         119 :   res = cgetg(6*l - 5, t_VECSMALL);
    5790         119 :   c = 1;
    5791         119 :   if (!odd(N))
    5792             :   { /* d = 1 */
    5793          56 :     if (p) res[c++] = 8;
    5794          56 :     if (m) { res[c++] =-8; res[c++] =-4; }
    5795             :   }
    5796         364 :   for (i = 2; i < l; i++)
    5797             :   { /* skip d = 1, done above */
    5798         245 :     long d = D[i], d4 = d & 3L; /* d odd, squarefree, d4 = 1 or 3 */
    5799         245 :     if (d4 == 1) { if (p) res[c++] = d; }
    5800         182 :     else         { if (m) res[c++] =-d; }
    5801         245 :     if (!odd(N))
    5802             :     {
    5803          56 :       if (p) { res[c++] = 8*d; if (d4 == 3) res[c++] = 4*d; }
    5804          56 :       if (m) { res[c++] =-8*d; if (d4 == 1) res[c++] =-4*d; }
    5805             :     }
    5806             :   }
    5807         119 :   setlg(res, c); return res;
    5808             : }
    5809             : 
    5810             : /* Return 1 if F is definitely not S4 type; return 0 on failure. */
    5811             : static long
    5812         105 : mfisnotS4(long N, GEN w)
    5813             : {
    5814         105 :   GEN D = mfunram(N, 0);
    5815         105 :   long i, lD = lg(D), lw = lg(w);
    5816         616 :   for (i = 1; i < lD; i++)
    5817             :   {
    5818         511 :     long p, d = D[i], ok = 0;
    5819        1442 :     for (p = 2; p < lw; p++)
    5820        1442 :       if (w[p] && kross(d,p) == -1) { ok = 1; break; }
    5821         511 :     if (!ok) return 0;
    5822             :   }
    5823         105 :   return 1;
    5824             : }
    5825             : 
    5826             : /* Return 1 if Q(sqrt(5)) \not\subset Q(F), i.e. F is definitely not A5 type;
    5827             :  * return 0 on failure. */
    5828             : static long
    5829         105 : mfisnotA5(GEN F)
    5830             : {
    5831         105 :   GEN CHI = mf_get_CHI(F), P = mfcharpol(CHI), T, Q;
    5832             : 
    5833         105 :   if (mfcharorder(CHI) % 5 == 0) return 0;
    5834         105 :   T = mf_get_field(F); if (degpol(T) == 1) return 1;
    5835         105 :   if (degpol(P) > 1) T = rnfequation(P,T);
    5836         105 :   Q = gsubgs(pol_xn(2,varn(T)), 5);
    5837         105 :   return (typ(nfisincl(Q, T)) == t_INT);
    5838             : }
    5839             : 
    5840             : /* Given v[p+1]^2 / chi(p) - 2 = z + 1/z with z primitive root of unity of order n,
    5841             :  * return n */
    5842             : static long
    5843        6741 : mffindrootof1(GEN v, long p, GEN CHI)
    5844             : {
    5845        6741 :   GEN ap = gel(v,p+1), u0, u1, u1k, u2;
    5846        6741 :   long c = 1;
    5847        6741 :   if (gequal0(ap)) return 2;
    5848        5033 :   u0 = gen_2; u1k = u1 = gsubgs(gdiv(gsqr(ap), mfchareval_i(CHI, p)), 2);
    5849       19845 :   while (!gequalsg(2, liftpol_shallow(u1))) /* u1 = z^c + z^-c */
    5850             :   {
    5851        9779 :     u2 = gsub(gmul(u1k, u1), u0);
    5852        9779 :     u0 = u1; u1 = u2; c++;
    5853             :   }
    5854        5033 :   return c;
    5855             : }
    5856             : 
    5857             : /* we known that F is not dihedral */
    5858             : static long
    5859         182 : mfgaloistype_i(long N, GEN CHI, GEN F, GEN v)
    5860             : {
    5861             :   forprime_t iter;
    5862         182 :   long lim = lg(v)-2;
    5863         182 :   GEN w = zero_zv(lim);
    5864             :   pari_sp av;
    5865             :   ulong p;
    5866         182 :   u_forprime_init(&iter, 2, lim);
    5867         182 :   av = avma;
    5868        5474 :   while((p = u_forprime_next(&iter))) if (N%p) switch(mffindrootof1(v, p, CHI))
    5869             :   {
    5870        1400 :     case 1: case 2: continue;
    5871        3451 :     case 3: w[p] = 1; break;
    5872          70 :     case 4: return -24; /* S4 */
    5873           0 :     case 5: return -60; /* A5 */
    5874           7 :     default: pari_err_DOMAIN("mfgaloistype", "form", "not a",
    5875             :                              strtoGENstr("cuspidal eigenform"), F);
    5876           0 :     set_avma(av);
    5877             :   }
    5878         105 :   if (mfisnotS4(N,w) && mfisnotA5(F)) return -12; /* A4 */
    5879           0 :   return 0; /* FAILURE */
    5880             : }
    5881             : 
    5882             : static GEN
    5883         210 : mfgaloistype0(long N, GEN CHI, GEN F, GEN DIH, long lim)
    5884             : {
    5885         210 :   pari_sp av = avma;
    5886         210 :   GEN vF = mftocol(F, lim, 1);
    5887         210 :   long t = mfisdihedral(vF, DIH);
    5888         210 :   if (t) { set_avma(av); return stoi(t); }
    5889             :   for(;;)
    5890             :   {
    5891           0 :     t = mfgaloistype_i(N, CHI, F, vF);
    5892         175 :     set_avma(av); if (t) return stoi(t);
    5893           0 :     lim += lim >> 1; vF = mfcoefs_i(F,lim,1);
    5894             :   }
    5895             : }
    5896             : 
    5897             : /* If f is NULL, give all the galoistypes, otherwise just for f */
    5898             : GEN
    5899         217 : mfgaloistype(GEN NK, GEN f)
    5900             : {
    5901         217 :   pari_sp av = avma;
    5902         217 :   GEN CHI, T, F, DIH, mf = checkMF_i(NK);
    5903             :   long N, k, lL, i, lim, SB;
    5904             : 
    5905         217 :   if (f && !checkmf_i(f)) pari_err_TYPE("mfgaloistype", f);
    5906         210 :   if (mf)
    5907             :   {
    5908         175 :     N = MF_get_N(mf);
    5909         175 :     k = MF_get_k(mf);
    5910         175 :     CHI = MF_get_CHI(mf);
    5911             :   }
    5912             :   else
    5913             :   {
    5914          35 :     checkNK(NK, &N, &k, &CHI, 0);
    5915          35 :     mf = f? NULL: mfinit_i(NK, mf_NEW);
    5916             :   }
    5917         210 :   if (k != 1) pari_err_DOMAIN("mfgaloistype", "k", "!=", gen_1, stoi(k));
    5918         210 :   SB = mf? mfsturm_mf(mf): mfsturmNk(N,1);
    5919         210 :   DIH = mfdihedralnew(N,CHI);
    5920         210 :   lim = lg(DIH) == 1? 200: SB;
    5921         210 :   DIH = mkvec2(DIH, mfvectomat(DIH,SB,1));
    5922         210 :   if (f) return gerepileuptoint(av, mfgaloistype0(N,CHI, f, DIH, lim));
    5923         112 :   F = mfeigenbasis(mf); lL = lg(F);
    5924         112 :   T = cgetg(lL, t_VEC);
    5925         112 :   for (i=1; i < lL; i++) gel(T,i) = mfgaloistype0(N, CHI, gel(F,i), DIH, lim);
    5926         112 :   return gerepileupto(av, T);
    5927             : }
    5928             : 
    5929             : /******************************************************************/
    5930             : /*                   Find all dihedral forms.                     */
    5931             : /******************************************************************/
    5932             : /* lim >= 2 */
    5933             : static void
    5934          14 : consttabdihedral(long lim)
    5935          14 : { cache_set(cache_DIH, mfdihedralall(mkvecsmall2(1,lim))); }
    5936             : 
    5937             : /* a ideal coprime to bnr modulus */
    5938             : static long
    5939       77049 : mfdiheval(GEN bnr, GEN w, GEN a)
    5940             : {
    5941       77049 :   GEN L, cycn = gel(w,1), chin = gel(w,2);
    5942       77049 :   long ordmax = cycn[1];
    5943       77049 :   L = ZV_to_Flv(isprincipalray(bnr,a), ordmax);
    5944       77049 :   return Flv_dotproduct(chin, L, ordmax);
    5945             : }
    5946             : 
    5947             : /* A(x^k) mod T */
    5948             : static GEN
    5949       25599 : Galois(GEN A, long k, GEN T)
    5950             : {
    5951       25599 :   if (typ(A) != t_POL) return A;
    5952        9674 :   return gmod(RgX_inflate(A, k), T);
    5953             : }
    5954             : static GEN
    5955         609 : vecGalois(GEN v, long k, GEN T)
    5956             : {
    5957             :   long i, l;
    5958         609 :   GEN w = cgetg_copy(v,&l);
    5959         609 :   for (i = 1; i < l; i++) gel(w,i) = Galois(gel(v,i), k, T);
    5960         609 :   return w;
    5961             : }
    5962             : 
    5963             : static GEN
    5964      153692 : fix_pol(GEN S, GEN Pn, int *trace)
    5965             : {
    5966      153692 :   if (typ(S) != t_POL) return S;
    5967      107310 :   S = RgX_rem(S, Pn);
    5968      107310 :   if (typ(S) == t_POL)
    5969             :   {
    5970      107310 :     switch(lg(S))
    5971             :     {
    5972       37765 :       case 2: return gen_0;
    5973       17080 :       case 3: return gel(S,2);
    5974             :     }
    5975       52465 :     *trace = 1;
    5976             :   }
    5977       52465 :   return S;
    5978             : }
    5979             : 
    5980             : static GEN
    5981       10465 : dihan(GEN bnr, GEN w, GEN k0j, ulong lim)
    5982             : {
    5983       10465 :   GEN nf = bnr_get_nf(bnr), f = bid_get_ideal(bnr_get_bid(bnr));
    5984       10465 :   GEN v = zerovec(lim+1), cycn = gel(w,1), Tinit = gel(w,3);
    5985       10465 :   GEN Pn = gel(Tinit,lg(Tinit)==4? 2: 1);
    5986       10465 :   long j, ordmax = cycn[1], k0 = k0j[1], jdeg = k0j[2];
    5987       10465 :   long D = itos(nf_get_disc(nf)), vt = varn(Pn);
    5988       10465 :   int trace = 0;
    5989             :   ulong p, n;
    5990             :   forprime_t T;
    5991             : 
    5992       10465 :   if (!lim) return v;
    5993       10465 :   gel(v,2) = gen_1;
    5994       10465 :   u_forprime_init(&T, 2, lim);
    5995             :   /* fill in prime powers first */
    5996       10465 :   while ((p = u_forprime_next(&T)))
    5997             :   {
    5998             :     GEN vP, vchiP, S;
    5999             :     long k, lP;
    6000             :     ulong q, qk;
    6001       70616 :     if (kross(D,p) >= 0) q = p;
    6002       29050 :     else if (!(q = umuluu_le(p,p,lim))) continue;
    6003             :     /* q = Norm P */
    6004       47180 :     vP = idealprimedec(nf, utoipos(p));
    6005       47180 :     lP = lg(vP);
    6006       47180 :     vchiP = cgetg(lP, t_VECSMALL);
    6007      128100 :     for (j = k = 1; j < lP; j++)
    6008             :     {
    6009       80920 :       GEN P = gel(vP,j);
    6010       80920 :       if (!idealval(nf, f, P)) vchiP[k++] = mfdiheval(bnr,w,P);
    6011             :     }
    6012       47180 :     if (k == 1) continue;
    6013       45493 :     setlg(vchiP, k); lP = k;
    6014       45493 :     if (lP == 2)
    6015             :     { /* one prime above p not dividing f */
    6016       13937 :       long s, s0 = vchiP[1];
    6017       24668 :       for (qk=q, s = s0;; s = Fl_add(s,s0,ordmax))
    6018             :       {
    6019       35399 :         S = mygmodulo_lift(s, ordmax, gen_1, vt);
    6020       24668 :         gel(v, qk+1) = fix_pol(S, Pn, &trace);
    6021       24668 :         if (!(qk = umuluu_le(qk,q,lim))) break;
    6022             :       }
    6023             :     }
    6024             :     else /* two primes above p not dividing f */
    6025             :     {
    6026       31556 :       long s, s0 = vchiP[1], s1 = vchiP[2];
    6027       46662 :       for (qk=q, k = 1;; k++)
    6028       15106 :       { /* sum over a,b s.t. Norm( P1^a P2^b ) = q^k, i.e. a+b = k */
    6029             :         long a;
    6030       46662 :         GEN S = gen_0;
    6031      162701 :         for (a = 0; a <= k; a++)
    6032             :         {
    6033      116039 :           s = Fl_add(Fl_mul(a, s0, ordmax), Fl_mul(k-a, s1, ordmax), ordmax);
    6034      116039 :           S = gadd(S, mygmodulo_lift(s, ordmax, gen_1, vt));
    6035             :         }
    6036       46662 :         gel(v, qk+1) = fix_pol(S, Pn, &trace);
    6037       46662 :         if (!(qk = umuluu_le(qk,q,lim))) break;
    6038             :       }
    6039             :     }
    6040             :   }
    6041             :   /* complete with non-prime powers */
    6042      199164 :   for (n = 2; n <= lim; n++)
    6043             :   {
    6044      188699 :     GEN S, fa = myfactoru(n), P = gel(fa, 1), E = gel(fa, 2);
    6045             :     long q;
    6046      188699 :     if (lg(P) == 2) continue;
    6047             :     /* not a prime power */
    6048       82362 :     q = upowuu(P[1],E[1]);
    6049       82362 :     S = gmul(gel(v, q + 1), gel(v, n/q + 1));
    6050       82362 :     gel(v, n+1) = fix_pol(S, Pn, &trace);
    6051             :   }
    6052       10465 :   if (trace)
    6053             :   {
    6054        5355 :     if (lg(Tinit) == 4) v = QabV_tracerel(Tinit, jdeg, v);
    6055             :     /* Apply Galois Mod(k0, ordw) */
    6056        5355 :     if (k0 > 1) { GEN Pm = gel(Tinit,1); v = vecGalois(v, k0, Pm); }
    6057             :   }
    6058       10465 :   return v;
    6059             : }
    6060             : 
    6061             : /* as cyc_normalize for t_VECSMALL cyc */
    6062             : static GEN
    6063       26782 : cyc_normalize_zv(GEN cyc)
    6064             : {
    6065       26782 :   long i, o = cyc[1], l = lg(cyc); /* > 1 */
    6066       26782 :   GEN D = cgetg(l, t_VECSMALL);
    6067       26782 :   D[1] = o; for (i = 2; i < l; i++) D[i] = o / cyc[i];
    6068       26782 :   return D;
    6069             : }
    6070             : /* as char_normalize for t_VECSMALLs */
    6071             : static GEN
    6072      117950 : char_normalize_zv(GEN chi, GEN ncyc)
    6073             : {
    6074      117950 :   long i, l = lg(chi);
    6075      117950 :   GEN c = cgetg(l, t_VECSMALL);
    6076      117950 :   if (l > 1) {
    6077      117950 :     c[1] = chi[1];
    6078      117950 :     for (i = 2; i < l; i++) c[i] = chi[i] * ncyc[i];
    6079             :   }
    6080      117950 :   return c;
    6081             : }
    6082             : 
    6083             : static GEN
    6084        8939 : dihan_bnf(long D)
    6085        8939 : { setrand(gen_1); return Buchall(quadpoly(stoi(D)), 0, LOWDEFAULTPREC); }
    6086             : static GEN
    6087       37226 : dihan_bnr(GEN bnf, GEN A)
    6088       37226 : { setrand(gen_1); return bnrinit0(bnf, A, 1); }
    6089             : 
    6090             : /* Hecke xi * (D/.) = Dirichlet chi, return v in Q^r st chi(g_i) = e(v[i]).
    6091             :  * cycn = cyc_normalize_zv(bnr.cyc), chin = char_normalize_zv(chi,cyc) */
    6092             : static GEN
    6093       34412 : bnrchartwist2conrey(GEN chin, GEN cycn, GEN bnrconreyN, GEN kroconreyN)
    6094             : {
    6095       34412 :   long l = lg(bnrconreyN), c1 = cycn[1], i;
    6096       34412 :   GEN v = cgetg(l, t_COL);
    6097      125132 :   for (i = 1; i < l; i++)
    6098             :   {
    6099       90720 :     GEN d = sstoQ(zv_dotproduct(chin, gel(bnrconreyN,i)), c1);
    6100       90720 :     if (kroconreyN[i] < 0) d = gadd(d, ghalf);
    6101       90720 :     gel(v,i) = d;
    6102             :   }
    6103       34412 :   return v;
    6104             : }
    6105             : 
    6106             : /* chi(g_i) = e(v[i]) denormalize wrt Conrey generators orders */
    6107             : static GEN
    6108       34412 : conreydenormalize(GEN znN, GEN v)
    6109             : {
    6110       34412 :   GEN gcyc = znstar_get_conreycyc(znN), w;
    6111       34412 :   long l = lg(v), i;
    6112       34412 :   w = cgetg(l, t_COL);
    6113      125132 :   for (i = 1; i < l; i++)
    6114       90720 :     gel(w,i) = modii(gmul(gel(v,i), gel(gcyc,i)), gel(gcyc,i));
    6115       34412 :   return w;
    6116             : }
    6117             : 
    6118             : static long
    6119       83538 : Miyake(GEN vchi, GEN gb, GEN cycn)
    6120             : {
    6121       83538 :   long i, e = cycn[1], lb = lg(gb);
    6122       83538 :   GEN v = char_normalize_zv(vchi, cycn);
    6123      124264 :   for (i = 1; i < lb; i++)
    6124       99666 :     if ((zv_dotproduct(v, gel(gb,i)) -  v[i]) % e) return 1;
    6125       24598 :   return 0;
    6126             : }
    6127             : 
    6128             : /* list of Hecke characters not induced by a Dirichlet character up to Galois
    6129             :  * conjugation, whose conductor is bnr.cond; cycn = cyc_normalize(bnr.cyc)*/
    6130             : static GEN
    6131       26782 : mklvchi(GEN bnr, GEN con, GEN cycn)
    6132             : {
    6133       26782 :   GEN gb = NULL, cyc = bnr_get_cyc(bnr), cycsmall = ZV_to_zv(cyc);
    6134       26782 :   GEN vchi = cyc2elts(cycsmall);
    6135       26782 :   long ordmax = cycsmall[1], c, i, l;
    6136       26782 :   if (con)
    6137             :   {
    6138        7784 :     GEN g = bnr_get_gen(bnr), nf = bnr_get_nf(bnr);
    6139        7784 :     long lg = lg(g);
    6140        7784 :     gb = cgetg(lg, t_VEC);
    6141       18270 :     for (i = 1; i < lg; i++)
    6142       10486 :       gel(gb,i) = ZV_to_zv(isprincipalray(bnr, galoisapply(nf, con, gel(g,i))));
    6143             :   }
    6144       26782 :   l = lg(vchi);
    6145      303450 :   for (i = c = 1; i < l; i++)
    6146             :   {
    6147      276668 :     GEN chi = gel(vchi,i);
    6148      276668 :     if (!con || Miyake(chi, gb, cycn)) gel(vchi, c++) = Flv_to_ZV(chi);
    6149             :   }
    6150       26782 :   setlg(vchi, c); l = c;
    6151      278852 :   for (i = 1; i < l; i++)
    6152             :   {
    6153      252070 :     GEN chi = gel(vchi,i);
    6154             :     long n;
    6155      252070 :     if (!chi) continue;
    6156     1054578 :     for (n = 2; n < ordmax; n++)
    6157      965496 :       if (ugcd(n, ordmax) == 1)
    6158             :       {
    6159      397194 :         GEN tmp = vecmodii(gmulsg(n, chi), cyc);
    6160             :         long j;
    6161     7618100 :         for (j = i+1; j < l; j++)
    6162     7220906 :           if (gel(vchi,j) && gequal(gel(vchi,j), tmp)) gel(vchi,j) = NULL;
    6163             :       }
    6164             :   }
    6165      278852 :   for (i = c = 1; i < l; i++)
    6166             :   {
    6167      252070 :     GEN chi = gel(vchi,i);
    6168      252070 :     if (chi && bnrisconductor(bnr, chi)) gel(vchi, c++) = chi;
    6169             :   }
    6170       26782 :   setlg(vchi, c); return vchi;
    6171             : }
    6172             : 
    6173             : /* con = NULL if D > 0 or if D < 0 and id != idcon. */
    6174             : static GEN
    6175       33670 : mfdihedralcommon(GEN bnf, GEN id, GEN znN, GEN kroconreyN, long N, long D, GEN con)
    6176             : {
    6177             :   GEN bnr, bnrconreyN, cyc, cycn, cycN, Lvchi, res, g, P;
    6178             :   long i, j, ordmax, l, lc, deghecke, degrel;
    6179             : 
    6180       33670 :   bnr = dihan_bnr(bnf, id);
    6181       33670 :   cyc = ZV_to_zv( bnr_get_cyc(bnr) );
    6182       33670 :   lc = lg(cyc); if (lc == 1) return NULL;
    6183             : 
    6184       26782 :   g = znstar_get_conreygen(znN); l = lg(g);
    6185       26782 :   bnrconreyN = cgetg(l, t_VEC);
    6186      100576 :   for (i = 1; i < l; i++)
    6187       73794 :     gel(bnrconreyN,i) = ZV_to_zv(isprincipalray(bnr,gel(g,i)));
    6188             : 
    6189       26782 :   cycn = cyc_normalize_zv(cyc);
    6190       26782 :   cycN = ZV_to_zv(znstar_get_cyc(znN));
    6191       26782 :   ordmax = cyc[1];
    6192       26782 :   P = polcyclo(ord_canon(ordmax), fetch_user_var("t"));
    6193       26782 :   deghecke = myeulerphiu(ordmax);
    6194       26782 :   Lvchi = mklvchi(bnr, con, cycn); l = lg(Lvchi);
    6195       26782 :   if (l == 1) return NULL;
    6196       15834 :   res = cgetg(l, t_VEC);
    6197       50246 :   for (j = 1; j < l; j++)
    6198             :   {
    6199       34412 :     GEN T, Tinit, v, vchi = ZV_to_zv(gel(Lvchi,j));
    6200       34412 :     GEN chi, chin = char_normalize_zv(vchi, cycn);
    6201             :     long ordw, vnum, k0;
    6202       34412 :     v = bnrchartwist2conrey(chin, cycn, bnrconreyN, kroconreyN);
    6203       34412 :     ordw = itou(Q_denom(v));
    6204       34412 :     Tinit = Qab_trace_init(P, ord_canon(ordmax), ord_canon(ordw));
    6205       34412 :     chi = conreydenormalize(znN, v);
    6206       34412 :     vnum = itou(znconreyexp(znN, chi));
    6207       34412 :     chi = ZV_to_zv(znconreychar(znN,chi));
    6208       34412 :     degrel = deghecke / myeulerphiu(ordw);
    6209       34412 :     k0 = zv_cyc_minimize(cycN, chi, coprimes_zv(ordw));
    6210       34412 :     vnum = Fl_powu(vnum, k0, N);
    6211             :     /* encodes degrel forms: jdeg = 0..degrel-1 */
    6212       34412 :     T = mkvecsmalln(6, N, k0, vnum, D, ordmax, degrel);
    6213       34412 :     gel(res,j) = mkvec3(T, id, mkvec3(cycn,chin,Tinit));
    6214             :   }
    6215       15834 :   return res;
    6216             : }
    6217             : 
    6218             : /* Append to v all dihedral weight 1 forms coming from D, if fundamental.
    6219             :  * level in [l1, l2] */
    6220             : static void
    6221       18578 : append_dihedral(GEN v, long D, long l1, long l2)
    6222             : {
    6223       18578 :   long Da = labs(D), no, N, i, numi, ct, min, max;
    6224             :   GEN bnf, con, LI, resall, varch;
    6225             :   pari_sp av;
    6226             : 
    6227             :   /* min <= Nf <= max */
    6228       18578 :   max = l2 / Da;
    6229       18578 :   if (l1 == l2)
    6230             :   { /* assume Da | l2 */
    6231           0 :     min = max;
    6232           0 :     if (D > 0 && min < 3) return;
    6233             :   }
    6234             :   else /* assume l1 < l2 */
    6235       18578 :     min = (l1 + Da-1)/Da;
    6236       18578 :   if (!sisfundamental(D)) return;
    6237             : 
    6238        5684 :   av = avma;
    6239        5684 :   bnf = dihan_bnf(D);
    6240        5684 :   con = gel(galoisconj(bnf,gen_1), 2);
    6241        5684 :   LI = ideallist(bnf, max);
    6242        5684 :   numi = 0; for (i = min; i <= max; i++) numi += lg(gel(LI, i)) - 1;
    6243        5684 :   if (D > 0)
    6244             :   {
    6245        1414 :     numi <<= 1;
    6246        1414 :     varch = mkvec2(mkvec2(gen_1,gen_0), mkvec2(gen_0,gen_1));
    6247             :   }
    6248             :   else
    6249        4270 :     varch = NULL;
    6250        5684 :   resall = cgetg(numi+1, t_VEC); ct = 1;
    6251       55006 :   for (no = min; no <= max; no++)
    6252             :   {
    6253             :     GEN LIs, znN, conreyN, kroconreyN;
    6254             :     long flcond, lgc, lglis;
    6255       49322 :     if (D < 0)
    6256       30086 :       flcond = (no == 2 || no == 3 || (no == 4 && (D&7L)==1));
    6257             :     else
    6258       19236 :       flcond = (no == 4 && (D&7L) != 1);
    6259       49322 :     if (flcond) continue;
    6260       44604 :     LIs = gel(LI, no);
    6261       44604 :     N = Da*no;
    6262       44604 :     znN = znstar0(utoi(N), 1);
    6263       44604 :     conreyN = znstar_get_conreygen(znN); lgc = lg(conreyN);
    6264       44604 :     kroconreyN = cgetg(lgc, t_VECSMALL);
    6265       44604 :     for (i = 1; i < lgc; i++) kroconreyN[i] = krosi(D, gel(conreyN, i));
    6266       44604 :     lglis = lg(LIs);
    6267       87752 :     for (i = 1; i < lglis; i++)
    6268             :     {
    6269       43148 :       GEN id = gel(LIs, i), idcon, conk;
    6270             :       long j, inf, maxinf;
    6271       43148 :       if (typ(id) == t_INT) continue;
    6272       28154 :       idcon = galoisapply(bnf, con, id);
    6273       28154 :       conk = (D < 0 && gequal(idcon, id)) ? con : NULL;
    6274       51380 :       for (j = i; j < lglis; j++)
    6275       51380 :         if (gequal(idcon, gel(LIs, j))) { gel(LIs, j) = gen_0; break; }
    6276       28154 :       maxinf = (D < 0 || gequal(idcon,id))? 1: 2;
    6277       61824 :       for (inf = 1; inf <= maxinf; inf++)
    6278             :       {
    6279       33670 :         GEN ide = (D > 0)? mkvec2(id, gel(varch,inf)): id;
    6280       33670 :         GEN res = mfdihedralcommon(bnf, ide, znN, kroconreyN, N, D, conk);
    6281       33670 :         if (res) gel(resall, ct++) = res;
    6282             :       }
    6283             :     }
    6284             :   }
    6285        5684 :   if (ct == 1) set_avma(av);
    6286             :   else
    6287             :   {
    6288        4788 :     setlg(resall, ct);
    6289        4788 :     vectrunc_append(v, gerepilecopy(av, shallowconcat1(resall)));
    6290             :   }
    6291             : }
    6292             : 
    6293             : static long
    6294       42042 : di_N(GEN a) { return gel(a,1)[1]; }
    6295             : /* All primitive dihedral wt1 forms: LIM a t_VECSMALL with a single component
    6296             :  * (only level LIM) or 2 components [m,M], m < M (between m and M) */
    6297             : static GEN
    6298          14 : mfdihedralall(GEN LIM)
    6299             : {
    6300             :   GEN res, z;
    6301             :   long limD, ct, i, l1, l2;
    6302             : 
    6303          14 :   if (lg(LIM) == 2) l1 = l2 = LIM[1]; else { l1 = LIM[1]; l2 = LIM[2]; }
    6304          14 :   limD = l2;
    6305          14 :   res = vectrunc_init(2*limD);
    6306          14 :   if (l1 == l2)
    6307             :   {
    6308           0 :     GEN D = mydivisorsu(l1);
    6309           0 :     long l = lg(D), j;
    6310           0 :     for (j = 2; j < l; j++)
    6311             :     { /* skip d = 1 */
    6312           0 :       long d = D[j];
    6313           0 :       if (d == 2) continue;
    6314           0 :       append_dihedral(res, -d, l1,l2);
    6315           0 :       if (d >= 5 && D[l-j] >= 3) append_dihedral(res, d, l1,l2); /* Nf >= 3 */
    6316             :     }
    6317             :   }
    6318             :   else
    6319             :   {
    6320             :     long D;
    6321          14 :     for (D = -3; D >= -limD; D--) append_dihedral(res, D, l1,l2);
    6322          14 :     limD /= 3; /* Nf >= 3 (GTM 193, prop 3.3.18) */
    6323          14 :     for (D = 5; D <= limD;   D++) append_dihedral(res, D, l1,l2);
    6324             :   }
    6325          14 :   ct = lg(res);
    6326          14 :   if (ct > 1) res = shallowconcat1(res);
    6327          14 :   if (l1 == l2) return res; /* single level */
    6328          14 :   if (ct > 1)
    6329             :   { /* sort wrt N */
    6330          14 :     res = vecpermute(res, indexvecsort(res, mkvecsmall(1)));
    6331          14 :     ct = lg(res);
    6332             :   }
    6333          14 :   z = const_vec(l2-l1+1, cgetg(1,t_VEC));
    6334        7672 :   for (i = 1; i < ct;)
    6335             :   { /* regroup result sharing the same N */
    6336        7644 :     long n = di_N(gel(res,i)), j = i+1, k;
    6337             :     GEN v;
    6338        7644 :     while (j < ct && di_N(gel(res,j)) == n) j++;
    6339        7644 :     n -= l1-1;
    6340        7644 :     gel(z, n) = v = cgetg(j-i+1, t_VEC);
    6341        7644 :     for (k = 1; i < j; k++,i++) gel(v,k) = gel(res,i);
    6342             :   }
    6343          14 :   return z;
    6344             : }
    6345             : 
    6346             : /* return [vF, index], where vecpermute(vF,index) generates dihedral forms
    6347             :  * for character CHI */
    6348             : static GEN
    6349       23772 : mfdihedralnew_i(long N, GEN CHI)
    6350             : {
    6351             :   GEN bnf, Tinit, Pm, vf, M, V, NK, SP;
    6352             :   long Dold, d, ordw, i, SB, c, l, k0, k1, chino, chinoorig, lv;
    6353             : 
    6354       23772 :   SP = cache_get(cache_DIH, N);
    6355       23772 :   if (!SP) SP = mfdihedralall(mkvecsmall(N));
    6356       23772 :   lv = lg(SP); if (lv == 1) return NULL;
    6357       11347 :   CHI = mfcharinduce(CHI,N);
    6358       11347 :   ordw = mfcharorder(CHI);
    6359       11347 :   chinoorig = mfcharno(CHI);
    6360       11347 :   k0 = mfconreyminimize(CHI);
    6361       11347 :   chino = Fl_powu(chinoorig, k0, N);
    6362       11347 :   k1 = Fl_inv(k0 % ordw, ordw);
    6363       11347 :   V = cgetg(lv, t_VEC);
    6364       11347 :   d = 0;
    6365       35231 :   for (i = l = 1; i < lv; i++)
    6366             :   {
    6367       23884 :     GEN sp = gel(SP,i), T = gel(sp,1);
    6368       23884 :     if (T[3] != chino) continue;
    6369        3556 :     d += T[6];
    6370        3556 :     if (k1 != 1)
    6371             :     {
    6372          77 :       GEN t = leafcopy(T);
    6373          77 :       t[3] = chinoorig;
    6374          77 :       t[2] = (t[2]*k1)%ordw;
    6375          77 :       sp = mkvec4(t, gel(sp,2), gel(sp,3), gel(sp,4));
    6376             :     }
    6377        3556 :     gel(V, l++) = sp;
    6378             :   }
    6379       11347 :   setlg(V, l); /* dihedral forms of level N and character CHI */
    6380       11347 :   if (l == 1) return NULL;
    6381             : 
    6382        2331 :   SB = myeulerphiu(ordw) * mfsturmNk(N,1) + 1;
    6383        2331 :   M = cgetg(d+1, t_MAT);
    6384        2331 :   vf = cgetg(d+1, t_VEC);
    6385        2331 :   NK = mkNK(N, 1, CHI);
    6386        2331 :   bnf = NULL; Dold = 0;
    6387        5887 :   for (i = c = 1; i < l; i++)
    6388             :   { /* T = [N, k0, conreyno, D, ordmax, degrel] */
    6389        3556 :     GEN bnr, Vi = gel(V,i), T = gel(Vi,1), id = gel(Vi,2), w = gel(Vi,3);
    6390        3556 :     long jdeg, k0i = T[2], D = T[4], degrel = T[6];
    6391             : 
    6392        3556 :     if (D != Dold) { Dold = D; bnf = dihan_bnf(D); }
    6393        3556 :     bnr = dihan_bnr(bnf, id);
    6394       10430 :     for (jdeg = 0; jdeg < degrel; jdeg++,c++)
    6395             :     {
    6396        6874 :       GEN k0j = mkvecsmall2(k0i, jdeg), an = dihan(bnr, w, k0j, SB);
    6397        6874 :       settyp(an, t_COL); gel(M,c) = Q_primpart(an);
    6398        6874 :       gel(vf,c) = tag3(t_MF_DIHEDRAL, NK, bnr, w, k0j);
    6399             :     }
    6400             :   }
    6401        2331 :   Tinit = gmael3(V,1,3,3); Pm = gel(Tinit,1);
    6402        2331 :   V = QabM_indexrank(M, degpol(Pm)==1? NULL: Pm, ord_canon(ordw));
    6403        2331 :   return mkvec2(vf,gel(V,2));
    6404             : }
    6405             : static long
    6406       15813 : mfdihedralnewdim(long N, GEN CHI)
    6407             : {
    6408       15813 :   pari_sp av = avma;
    6409       15813 :   GEN S = mfdihedralnew_i(N, CHI);
    6410       15813 :   return gc_long(av, S? lg(gel(S,2))-1: 0);
    6411             : }
    6412             : static GEN
    6413        7959 : mfdihedralnew(long N, GEN CHI)
    6414             : {
    6415        7959 :   pari_sp av = avma;
    6416        7959 :   GEN S = mfdihedralnew_i(N, CHI);
    6417        7959 :   if (!S) { set_avma(av); return cgetg(1, t_VEC); }
    6418         777 :   return vecpermute(gel(S,1), gel(S,2));
    6419             : }
    6420             : 
    6421             : static long
    6422        7035 : mfdihedralcuspdim(long N, GEN CHI)
    6423             : {
    6424        7035 :   pari_sp av = avma;
    6425             :   GEN D, CHIP;
    6426             :   long F, i, lD, dim;
    6427             : 
    6428        7035 :   CHIP = mfchartoprimitive(CHI, &F);
    6429        7035 :   D = mydivisorsu(N/F); lD = lg(D);
    6430        7035 :   dim = mfdihedralnewdim(N, CHI); /* d = 1 */
    6431       15813 :   for (i = 2; i < lD; i++)
    6432             :   {
    6433        8778 :     long d = D[i], M = N/d, a = mfdihedralnewdim(M, CHIP);
    6434        8778 :     if (a) dim += a * mynumdivu(d);
    6435             :   }
    6436        7035 :   return gc_long(av,dim);
    6437             : }
    6438             : 
    6439             : static GEN
    6440        5642 : mfbdall(GEN E, long N)
    6441             : {
    6442        5642 :   GEN v, D = mydivisorsu(N);
    6443        5642 :   long i, j, nD = lg(D) - 1, nE = lg(E) - 1;
    6444        5642 :   v = cgetg(nD*nE + 1, t_VEC);
    6445        7294 :   for (j = 1; j <= nE; j++)
    6446             :   {
    6447        1652 :     GEN Ej = gel(E, j);
    6448        1652 :     for (i = 0; i < nD; i++) gel(v, i*nE + j) = mfbd_i(Ej, D[i+1]);
    6449             :   }
    6450        5642 :   return v;
    6451             : }
    6452             : static GEN
    6453        3458 : mfdihedralcusp(long N, GEN CHI)
    6454             : {
    6455        3458 :   pari_sp av = avma;
    6456             :   GEN D, CHIP, z;
    6457             :   long F, i, lD;
    6458             : 
    6459        3458 :   CHIP = mfchartoprimitive(CHI, &F);
    6460        3458 :   D = mydivisorsu(N/F); lD = lg(D);
    6461        3458 :   z = cgetg(lD, t_VEC);
    6462        3458 :   gel(z,1) = mfdihedralnew(N, CHI);
    6463        7749 :   for (i = 2; i < lD; i++) /* skip 1 */
    6464             :   {
    6465        4291 :     long d = D[i], M = N / d;
    6466        4291 :     GEN LF = mfdihedralnew(M, mfcharinduce(CHIP, M));
    6467        4291 :     gel(z,i) = mfbdall(LF, d);
    6468             :   }
    6469        3458 :   return gerepilecopy(av, shallowconcat1(z));
    6470             : }
    6471             : 
    6472             : /* used to decide between ratlift and comatrix for ZM_inv; ratlift is better
    6473             :  * when N has many divisors */
    6474             : static int
    6475        2394 : abundant(ulong N) { return mynumdivu(N) >= 8; }
    6476             : 
    6477             : /* CHI an mfchar */
    6478             : static int
    6479         294 : cmp_ord(void *E, GEN a, GEN b)
    6480             : {
    6481         294 :   GEN chia = MF_get_CHI(a), chib = MF_get_CHI(b);
    6482         294 :   (void)E; return cmpii(gmfcharorder(chia), gmfcharorder(chib));
    6483             : }
    6484             : /* mfinit structure.
    6485             : -- mf[1] contains [N,k,CHI,space],
    6486             : -- mf[2] contains vector of closures of Eisenstein series, empty if not
    6487             :    full space.
    6488             : -- mf[3] contains vector of closures, so #mf[3] = dimension of cusp/new space.
    6489             : -- mf[4] contains the corresponding indices: either j for T(j)tf if newspace,
    6490             :    or [M,j,d] for B(d)T(j)tf_M if cuspspace or oldspace.
    6491             : -- mf[5] contains the matrix M of first coefficients of basis, never cleaned.
    6492             :  * NK is either [N,k] or [N,k,CHI].
    6493             :  * mfinit does not do the splitting, only the basis generation. */
    6494             : 
    6495             : /* Set flraw to 1 if do not need mf[5]: no mftobasis etc..., only the
    6496             :    expansions of the basis elements are needed. */
    6497             : 
    6498             : static GEN
    6499        4662 : mfinit_Nkchi(long N, long k, GEN CHI, long space, long flraw)
    6500             : {
    6501        4662 :   GEN M = NULL, mf = NULL, mf1 = mkvec4(utoi(N), stoi(k), CHI, utoi(space));
    6502        4662 :   long sb = mfsturmNk(N, k);
    6503             :   cachenew_t cache;
    6504        4662 :   if (k < 0 || badchar(N, k, CHI)) return mfEMPTY(mf1);
    6505        4627 :   if (k == 0) /*nothing*/;
    6506        4585 :   else if (k == 1)
    6507             :   {
    6508         336 :     switch (space)
    6509             :     {
    6510             :       case mf_NEW:
    6511             :       case mf_FULL:
    6512         308 :       case mf_CUSP: mf = mfwt1init(N, CHI, NULL, space, flraw); break;
    6513          14 :       case mf_EISEN:break;
    6514           7 :       case mf_OLD: pari_err_IMPL("mfinit in weight 1 for old space");
    6515           7 :       default: pari_err_FLAG("mfinit");
    6516             :     }
    6517             :   }
    6518             :   else /* k >= 2 */
    6519             :   {
    6520        4249 :     long ord = mfcharorder_canon(CHI);
    6521        4249 :     GEN z = NULL, P = (ord == 1)? NULL: mfcharpol(CHI);
    6522        4249 :     switch(space)
    6523             :     {
    6524             :       case mf_EISEN:
    6525         105 :         break;
    6526             :       case mf_NEW:
    6527        1176 :         mf = mfnewinit(N, k, CHI, &cache, 1);
    6528        1176 :         if (mf && !flraw) { M = MF_get_M(mf); z = MF_get_Mindex(mf); }
    6529        1176 :         break;
    6530             :       case mf_OLD:
    6531             :       case mf_CUSP:
    6532             :       case mf_FULL:
    6533        2961 :         mf = mfinitcusp(N, k, CHI, &cache, space);
    6534        2961 :         if (mf && !flraw)
    6535             :         {
    6536        2135 :           GEN S = MF_get_S(mf);
    6537        2135 :           M = bhnmat_extend(M, sb+1, 1, S, &cache);
    6538        2135 :           if (space != mf_FULL) gel(mf,5) = mfcleanCHI(M, CHI, abundant(N));
    6539             :         }
    6540        2961 :         dbg_cachenew(&cache);
    6541        2961 :         break;
    6542           7 :       default: pari_err_FLAG("mfinit");
    6543             :     }
    6544        4242 :     if (z) gel(mf,5) = mfclean2(M, z, P, ord);
    6545             :   }
    6546        4606 :   if (!mf) mf = mfEMPTY(mf1);
    6547             :   else
    6548             :   {
    6549        3738 :     gel(mf,1) = mf1;
    6550        3738 :     if (flraw) gel(mf,5) = zerovec(3);
    6551             :   }
    6552        4606 :   if (!space_is_cusp(space))
    6553             :   {
    6554         637 :     GEN E = mfeisensteinbasis(N, k, CHI);
    6555         637 :     gel(mf,2) = E;
    6556         637 :     if (!flraw)
    6557             :     {
    6558         427 :       if (M)
    6559         168 :         M = shallowconcat(mfvectomat(E, sb+1, 1), M);
    6560             :       else
    6561         259 :         M = mfcoefs_mf(mf, sb+1, 1);
    6562         427 :       gel(mf,5) = mfcleanCHI(M, CHI, abundant(N));
    6563             :     }
    6564             :   }
    6565        4606 :   return mf;
    6566             : }
    6567             : 
    6568             : /* mfinit for k = nk/dk */
    6569             : static GEN
    6570        2506 : mfinit_Nndkchi(long N, long nk, long dk, GEN CHI, long space, long flraw)
    6571         210 : { return (dk == 2)? mf2init_Nkchi(N, nk >> 1, CHI, space, flraw)
    6572        2716 :                   : mfinit_Nkchi(N, nk, CHI, space, flraw); }
    6573             : static GEN
    6574        3171 : mfinit_i(GEN NK, long space)
    6575             : {
    6576             :   GEN CHI, mf;
    6577             :   long N, k, dk, joker;
    6578        3171 :   if (checkmf_i(NK))
    6579             :   {
    6580         126 :     N = mf_get_N(NK);
    6581         126 :     Qtoss(mf_get_gk(NK), &k, &dk);
    6582         126 :     CHI = mf_get_CHI(NK);
    6583             :   }
    6584        3045 :   else if ((mf = checkMF_i(NK)))
    6585             :   {
    6586          21 :     long s = MF_get_space(mf);
    6587          21 :     if (s == space) return mf;
    6588          21 :     Qtoss(MF_get_gk(mf), &k, &dk);
    6589          21 :     if (dk == 1 && k > 1 && space == mf_NEW && (s == mf_CUSP || s == mf_FULL))
    6590          21 :       return mfinittonew(mf);
    6591           0 :     N = MF_get_N(mf);
    6592           0 :     CHI = MF_get_CHI(mf);
    6593             :   }
    6594             :   else
    6595        3024 :     checkNK2(NK, &N, &k, &dk, &CHI, 1);
    6596        3129 :   joker = !CHI || typ(CHI) == t_COL;
    6597        3129 :   if (joker)
    6598             :   {
    6599        1141 :     GEN mf, vCHI = CHI;
    6600             :     long i, j, l;
    6601        1141 :     if (CHI && lg(CHI) == 1) return cgetg(1,t_VEC);
    6602        1134 :     if (k < 0) return mfEMPTYall(N, sstoQ(k,dk), CHI, space);
    6603        1120 :     if (k == 1 && dk == 1 && space != mf_EISEN)
    6604         483 :     {
    6605             :       GEN TMP, gN, gs;
    6606        1085 :       if (space != mf_CUSP && space != mf_NEW)
    6607           0 :         pari_err_IMPL("mfinit([N,1,wildcard], space != cusp or new space)");
    6608        1085 :       if (wt1empty(N)) return mfEMPTYall(N, gen_1, CHI, space);
    6609         483 :       vCHI = mfwt1chars(N,vCHI);
    6610         483 :       l = lg(vCHI); mf = cgetg(l, t_VEC); if (l == 1) return mf;
    6611         483 :       TMP = mfwt1_pre(N); gN = utoipos(N); gs = utoi(space);
    6612        3717 :       for (i = j = 1; i < l; i++)
    6613             :       {
    6614        3234 :         pari_sp av = avma;
    6615        3234 :         GEN c = gel(vCHI,i), z = mfwt1init(N, c, TMP, space, 0);
    6616        3234 :         if (!z) {
    6617        2590 :           set_avma(av);
    6618        2590 :           if (CHI) z = mfEMPTY(mkvec4(gN,gen_1,c,gs));
    6619             :         }
    6620        3234 :         if (z) gel(mf, j++) = z;
    6621             :       }
    6622             :     }
    6623             :     else
    6624             :     {
    6625          35 :       vCHI = mfchars(N,k,dk,vCHI);
    6626          35 :       l = lg(vCHI); mf = cgetg(l, t_VEC);
    6627         119 :       for (i = j = 1; i < l; i++)
    6628             :       {
    6629          84 :         pari_sp av = avma;
    6630          84 :         GEN v = mfinit_Nndkchi(N, k, dk, gel(vCHI,i), space, 0);
    6631          84 :         if (MF_get_dim(v) || CHI) gel(mf, j++) = v; else set_avma(av);
    6632             :       }
    6633             :     }
    6634         518 :     setlg(mf,j);
    6635         518 :     if (!CHI) gen_sort_inplace(mf, NULL, &cmp_ord, NULL);
    6636         518 :     return mf;
    6637             :   }
    6638        1988 :   return mfinit_Nndkchi(N, k, dk, CHI, space, 0);
    6639             : }
    6640             : GEN
    6641        2219 : mfinit(GEN NK, long space)
    6642             : {
    6643        2219 :   pari_sp av = avma;
    6644        2219 :   return gerepilecopy(av, mfinit_i(NK, space));
    6645             : }
    6646             : 
    6647             : /* UTILITY FUNCTIONS */
    6648             : static void
    6649         357 : cusp_canon(GEN cusp, long N, long *pA, long *pC)
    6650             : {
    6651         357 :   pari_sp av = avma;
    6652             :   long A, C, tc, cg;
    6653         357 :   if (N <= 0) pari_err_DOMAIN("mfcuspwidth","N","<=",gen_0,stoi(N));
    6654         350 :   if (!cusp || (tc = typ(cusp)) == t_INFINITY) { *pA = 1; *pC = N; return; }
    6655         343 :   if (tc != t_INT && tc != t_FRAC) pari_err_TYPE("checkcusp", cusp);
    6656         343 :   Qtoss(cusp, &A,&C);
    6657         343 :   if (N % C)
    6658             :   {
    6659             :     ulong uC;
    6660          14 :     long u = Fl_invgen((C-1)%N + 1, N, &uC);
    6661          14 :     A = Fl_mul(A, u, N);
    6662          14 :     C = (long)uC;
    6663             :   }
    6664         343 :   cg = ugcd(C, N/C);
    6665         343 :   while (ugcd(A, N) > 1) A += cg;
    6666         343 :   *pA = A % N; *pC = C; set_avma(av);
    6667             : }
    6668             : static long
    6669         812 : mfcuspcanon_width(long N, long C)
    6670         812 : { return (!C || C == N)? 1 : N / ugcd(N, Fl_sqr(umodsu(C,N),N)); }
    6671             : /* v = [a,c] a ZC, width of cusp (a:c) */
    6672             : static long
    6673        7427 : mfZC_width(long N, GEN v)
    6674             : {
    6675        7427 :   ulong C = umodiu(gel(v,2), N);
    6676        7427 :   return (C == 0)? 1: N / ugcd(N, Fl_sqr(C,N));
    6677             : }
    6678             : long
    6679         161 : mfcuspwidth(GEN gN, GEN cusp)
    6680             : {
    6681         161 :   long N = 0, A, C;
    6682             :   GEN mf;
    6683         161 :   if (typ(gN) == t_INT) N = itos(gN);
    6684          42 :   else if ((mf = checkMF_i(gN))) N = MF_get_N(mf);
    6685           0 :   else pari_err_TYPE("mfcuspwidth", gN);
    6686         161 :   cusp_canon(cusp, N, &A, &C);
    6687         154 :   return mfcuspcanon_width(N, C);
    6688             : }
    6689             : 
    6690             : /* Q a t_INT */
    6691             : static GEN
    6692          14 : findq(GEN al, GEN Q)
    6693             : {
    6694             :   long n;
    6695          14 :   if (typ(al) == t_FRAC && cmpii(gel(al,2), Q) <= 0)
    6696           0 :     return mkvec(mkvec2(gel(al,1), gel(al,2)));
    6697          14 :   n = 1 + (long)ceil(2.0781*gtodouble(glog(Q, LOWDEFAULTPREC)));
    6698          14 :   return contfracpnqn(gboundcf(al,n), n);
    6699             : }
    6700             : static GEN
    6701          91 : findqga(long N, GEN z)
    6702             : {
    6703          91 :   GEN Q, LDC, CK = NULL, DK = NULL, ma, x, y = imag_i(z);
    6704             :   long j, l;
    6705          91 :   if (gcmpgs(gmulsg(2*N, y), 1) >= 0) return NULL;
    6706          14 :   x = real_i(z);
    6707          14 :   Q = ground(ginv(gsqrt(gmulsg(N, y), LOWDEFAULTPREC)));
    6708          14 :   LDC = findq(gmulsg(-N,x), Q);
    6709          14 :   ma = gen_1; l = lg(LDC);
    6710          35 :   for (j = 1; j < l; j++)
    6711             :   {
    6712          21 :     GEN D, DC = gel(LDC,j), C1 = gel(DC,2);
    6713          21 :     if (cmpii(C1,Q) > 0) break;
    6714          21 :     D = gel(DC,1);
    6715          21 :     if (ugcdiu(D,N) == 1)
    6716             :     {
    6717           7 :       GEN C = mului(N, C1), den;
    6718           7 :       den = gadd(gsqr(gmul(C,y)), gsqr(gadd(D, gmul(C,x))));
    6719           7 :       if (gcmp(den, ma) < 0) { ma = den; CK = C; DK = D; }
    6720             :     }
    6721             :   }
    6722          14 :   return DK? mkvec2(CK, DK): NULL;
    6723             : }
    6724             : 
    6725             : static long
    6726          98 : valNC2(GEN P, GEN E, long e)
    6727             : {
    6728          98 :   long i, d = 1, l = lg(P);
    6729         252 :   for (i = 1; i < l; i++)
    6730             :   {
    6731         154 :     long v = u_lval(e, P[i]) << 1;
    6732         154 :     if (v == E[i] + 1) v--;
    6733         154 :     d *= upowuu(P[i], v);
    6734             :   }
    6735          98 :   return d;
    6736             : }
    6737             : 
    6738             : static GEN
    6739          35 : findqganew(long N, GEN z)
    6740             : {
    6741          35 :   GEN MI, DI, x = real_i(z), y = imag_i(z), Ck = gen_0, Dk = gen_1, fa, P, E;
    6742             :   long i;
    6743          35 :   MI = ginv(utoi(N));
    6744          35 :   DI = mydivisorsu(mysqrtu(N));
    6745          35 :   fa = myfactoru(N); P = gel(fa,1); E = gel(fa,2);
    6746         133 :   for (i = 1; i < lg(DI); i++)
    6747             :   {
    6748          98 :     long e = DI[i], g;
    6749             :     GEN U, C, D, m;
    6750          98 :     (void)cxredsl2(gmulsg(e, z), &U);
    6751          98 :     C = gcoeff(U,2,1); if (!signe(C)) continue;
    6752          98 :     D = gcoeff(U,2,2);
    6753          98 :     g = ugcdiu(D,e);
    6754          98 :     if (g > 1) { C = muliu(C,e/g); D = diviuexact(D,g); } else C = muliu(C,e);
    6755          98 :     m = gadd(gsqr(gadd(gmul(C, x), D)), gsqr(gmul(C, y)));
    6756          98 :     m = gdivgs(m, valNC2(P, E, e));
    6757          98 :     if (gcmp(m, MI) < 0) { MI = m; Ck = C; Dk = D; }
    6758             :   }
    6759          35 :   return signe(Ck)? mkvec2(Ck, Dk): NULL;
    6760             : }
    6761             : 
    6762             : /* Return z' and U = [a,b;c,d] \in SL_2(Z), z' = U*z,
    6763             :  * Im(z')/width(U.oo) > sqrt(3)/(2N). Set *pczd = c*z+d */
    6764             : static GEN
    6765         161 : cxredga0N(long N, GEN z, GEN *pU, GEN *pczd, long flag)
    6766             : {
    6767         161 :   GEN v = NULL, A, B, C, D;
    6768             :   long e;
    6769         161 :   if (N == 1) return cxredsl2_i(z, pU, pczd);
    6770         126 :   e = gexpo(gel(z,2));
    6771         126 :   if (e < 0) z = gprec_wensure(z, precision(z) + nbits2extraprec(-e));
    6772         126 :   v = flag? findqganew(N,z): findqga(N,z);
    6773         126 :   if (!v) { *pU = matid(2); *pczd = gen_1; return z; }
    6774          42 :   C = gel(v,1);
    6775          42 :   D = gel(v,2);
    6776          42 :   if (!is_pm1(bezout(C,D, &B,&A))) pari_err_BUG("cxredga0N [gcd > 1]");
    6777          42 :   B = negi(B);
    6778          42 :   *pU = mkmat2(mkcol2(A,C), mkcol2(B,D));
    6779          42 :   *pczd = gadd(gmul(C,z), D);
    6780          42 :   return gdiv(gadd(gmul(A,z), B), *pczd);
    6781             : }
    6782             : 
    6783             : static GEN
    6784         147 : lfunthetaall(GEN b, GEN vL, GEN t, long bitprec)
    6785             : {
    6786         147 :   long i, l = lg(vL);
    6787         147 :   GEN v = cgetg(l, t_VEC);
    6788         322 :   for (i = 1; i < l; i++)
    6789             :   {
    6790         175 :     GEN T, L = gel(vL,i), a0 = gel(L,1), ldata = gel(L,2);
    6791         175 :     GEN van = gel(ldata_get_an(ldata),2);
    6792         175 :     if (lg(van) == 1)
    6793             :     {
    6794           0 :       T = gmul(b, a0);
    6795           0 :       if (isexactzero(T)) { GEN z = real_0_bit(-bitprec); T = mkcomplex(z,z); }
    6796             :     }
    6797             :     else
    6798             :     {
    6799         175 :       T = gmul2n(lfuntheta(ldata, t, 0, bitprec), -1);
    6800         175 :       T = gmul(b, gadd(a0, T));
    6801             :     }
    6802         175 :     gel(v,i) = T;
    6803             :   }
    6804         147 :   return l == 2? gel(v,1): v;
    6805             : }
    6806             : 
    6807             : /* P in ZX */
    6808             : static GEN
    6809         168 : ZX_roots(GEN P, long prec)
    6810             : {
    6811         168 :   long d = degpol(P);
    6812         168 :   if (d == 1) return mkvec(gen_0);
    6813         168 :   if (d == 2 && isint1(gel(P,2)) && isintzero(gel(P,3)) && isint1(gel(P,4)))
    6814           7 :     return mkvec2(powIs(3), gen_I()); /* order as polroots */
    6815         161 :   return (ZX_sturm(P) == d)? realroots(P,NULL,prec): QX_complex_roots(P,prec);
    6816             : }
    6817             : /* initializations for RgX_RgV_eval / RgC_embed */
    6818             : static GEN
    6819         203 : rootspowers(GEN v)
    6820             : {
    6821         203 :   long i, l = lg(v);
    6822         203 :   GEN w = cgetg(l, t_VEC);
    6823         203 :   for (i = 1; i < l; i++) gel(w,i) = gpowers(gel(v,i), l-2);
    6824         203 :   return w;
    6825             : }
    6826             : /* mf embeddings attached to Q(chi)/(T), chi attached to cyclotomic P */
    6827             : static GEN
    6828         819 : getembed(GEN P, GEN T, GEN zcyclo, long prec)
    6829             : {
    6830             :   long i, l;
    6831             :   GEN v;
    6832         819 :   if (degpol(P) == 1) P = NULL; /* mfcharpol for quadratic char */
    6833         819 :   if (degpol(T) == 1) T = NULL; /* dim 1 orbit */
    6834         819 :   if (T && P)
    6835          35 :   { /* K(y) / (T(y)), K = Q(t)/(P) cyclotomic */
    6836          35 :     GEN vr = RgX_is_ZX(T)? ZX_roots(T,prec): roots(RgX_embed1(T,zcyclo), prec);
    6837          35 :     v = rootspowers(vr); l = lg(v);
    6838          35 :     for (i = 1; i < l; i++) gel(v,i) = mkcol3(P,zcyclo,gel(v,i));
    6839             :   }
    6840         784 :   else if (T)
    6841             :   { /* Q(y) / (T(y)), T non-cyclotomic */
    6842         168 :     GEN vr = ZX_roots(T, prec);
    6843         168 :     v = rootspowers(vr); l = lg(v);
    6844         168 :     for (i = 1; i < l; i++) gel(v,i) = mkcol2(T, gel(v,i));
    6845             :   }
    6846             :   else /* cyclotomic or rational */
    6847         616 :     v = mkvec(P? mkvec2(P, zcyclo): cgetg(1,t_VEC));
    6848         819 :   return v;
    6849             : }
    6850             : static GEN
    6851         672 : grootsof1_CHI(GEN CHI, long prec)
    6852         672 : { return grootsof1(mfcharorder_canon(CHI), prec); }
    6853             : /* return the [Q(F):Q(chi)] embeddings of F */
    6854             : static GEN
    6855         518 : mfgetembed(GEN F, long prec)
    6856             : {
    6857         518 :   GEN T = mf_get_field(F), CHI = mf_get_CHI(F), P = mfcharpol(CHI);
    6858         518 :   return getembed(P, T, grootsof1_CHI(CHI, prec), prec);
    6859             : }
    6860             : static GEN
    6861           7 : mfchiembed(GEN mf, long prec)
    6862             : {
    6863           7 :   GEN CHI = MF_get_CHI(mf), P = mfcharpol(CHI);
    6864           7 :   return getembed(P, pol_x(0), grootsof1_CHI(CHI, prec), prec);
    6865             : }
    6866             : /* mfgetembed for the successive eigenforms in MF_get_newforms */
    6867             : static GEN
    6868         147 : mfeigenembed(GEN mf, long prec)
    6869             : {
    6870         147 :   GEN vP = MF_get_fields(mf), vF = MF_get_newforms(mf);
    6871         147 :   GEN zcyclo, vE, CHI = MF_get_CHI(mf), P = mfcharpol(CHI);
    6872         147 :   long i, l = lg(vP);
    6873         147 :   vF = Q_remove_denom(liftpol_shallow(vF), NULL);
    6874         147 :   prec += nbits2extraprec(gexpo(vF));
    6875         147 :   zcyclo = grootsof1_CHI(CHI, prec);
    6876         147 :   vE = cgetg(l, t_VEC);
    6877         147 :   for (i = 1; i < l; i++) gel(vE,i) = getembed(P, gel(vP,i), zcyclo, prec);
    6878         147 :   return vE;
    6879             : }
    6880             : 
    6881             : static int
    6882          28 : checkPv(GEN P, GEN v)
    6883          28 : { return typ(P) == t_POL && typ(v) == t_VEC && lg(v)-1 >= degpol(P); }
    6884             : static int
    6885          28 : checkemb_i(GEN E)
    6886             : {
    6887          28 :   long t = typ(E), l = lg(E);
    6888          28 :   if (t == t_VEC) return l == 1 || (l == 3 && checkPv(gel(E,1), gel(E,2)));
    6889          21 :   if (t != t_COL) return 0;
    6890          21 :   if (l == 3) return checkPv(gel(E,1), gel(E,2));
    6891          21 :   return l == 4 && typ(gel(E,2)) == t_VEC && checkPv(gel(E,1), gel(E,3));
    6892             : }
    6893             : static GEN
    6894          28 : anyembed(GEN v, GEN E)
    6895             : {
    6896          28 :   switch(typ(v))
    6897             :   {
    6898          21 :     case t_VEC: case t_COL: return mfvecembed(E, v);
    6899           7 :     case t_MAT: return mfmatembed(E, v);
    6900             :   }
    6901           0 :   return mfembed(E, v);
    6902             : }
    6903             : GEN
    6904          49 : mfembed0(GEN E, GEN v, long prec)
    6905             : {
    6906          49 :   pari_sp av = avma;
    6907          49 :   GEN mf, vE = NULL;
    6908          49 :   if (checkmf_i(E)) vE = mfgetembed(E, prec);
    6909          35 :   else if ((mf = checkMF_i(E))) vE = mfchiembed(mf, prec);
    6910          49 :   if (vE)
    6911             :   {
    6912          21 :     long i, l = lg(vE);
    6913             :     GEN w;
    6914          21 :     if (!v) return gerepilecopy(av, l == 2? gel(vE,1): vE);
    6915           0 :     w = cgetg(l, t_VEC);
    6916           0 :     for (i = 1; i < l; i++) gel(w,i) = anyembed(v, gel(vE,i));
    6917           0 :     return gerepilecopy(av, l == 2? gel(w,1): w);
    6918             :   }
    6919          28 :   if (!checkemb_i(E) || !v) pari_err_TYPE("mfembed", E);
    6920          28 :   return gerepilecopy(av, anyembed(v,E));
    6921             : }
    6922             : 
    6923             : /* dummy lfun create for theta evaluation */
    6924             : static GEN
    6925         840 : mfthetaancreate(GEN van, GEN N, GEN k)
    6926             : {
    6927         840 :   GEN L = zerovec(6);
    6928         840 :   gel(L,1) = lfuntag(t_LFUN_GENERIC, van);
    6929         840 :   gel(L,3) = mkvec2(gen_0, gen_1);
    6930         840 :   gel(L,4) = k;
    6931         840 :   gel(L,5) = N; return L;
    6932             : }
    6933             : /* destroy van and prepare to evaluate theta(sigma(van)), for all sigma in
    6934             :  * embeddings vector vE */
    6935             : static GEN
    6936         301 : van_embedall(GEN van, GEN vE, GEN gN, GEN gk)
    6937             : {
    6938         301 :   GEN a0 = gel(van,1), vL;
    6939         301 :   long i, lE = lg(vE), l = lg(van);
    6940         301 :   van++; van[0] = evaltyp(t_VEC) | evallg(l-1); /* remove a0 */
    6941         301 :   vL = cgetg(lE, t_VEC);
    6942         805 :   for (i = 1; i < lE; i++)
    6943             :   {
    6944         504 :     GEN E = gel(vE,i), v = mfvecembed(E, van);
    6945         504 :     gel(vL,i) = mkvec2(mfembed(E,a0), mfthetaancreate(v, gN, gk));
    6946             :   }
    6947         301 :   return vL;
    6948             : }
    6949             : 
    6950             : static int
    6951        1015 : cusp_AC(GEN cusp, long *A, long *C)
    6952             : {
    6953        1015 :   switch(typ(cusp))
    6954             :   {
    6955         105 :     case t_INFINITY: *A = 1; *C = 0; break;
    6956         273 :     case t_INT:  *A = itos(cusp); *C = 1; break;
    6957         427 :     case t_FRAC: *A = itos(gel(cusp, 1)); *C = itos(gel(cusp, 2)); break;
    6958             :     case t_REAL: case t_COMPLEX:
    6959         210 :       *A = 0; *C = 0;
    6960         210 :       if (gsigne(imag_i(cusp)) <= 0)
    6961           7 :         pari_err_DOMAIN("mfeval","imag(tau)","<=",gen_0,cusp);
    6962         203 :       return 0;
    6963           0 :     default: pari_err_TYPE("cusp_AC", cusp);
    6964             :   }
    6965         805 :   return 1;
    6966             : }
    6967             : static GEN
    6968         511 : cusp2mat(long A, long C)
    6969             : { long B, D;
    6970         511 :   cbezout(A, C, &D, &B);
    6971         511 :   return mkmat22s(A, -B, C, D);
    6972             : }
    6973             : static GEN
    6974           7 : mkS(void) { return mkmat22s(0,-1,1,0); }
    6975             : 
    6976             : /* if t is a cusp, return F(t), else NULL */
    6977             : static GEN
    6978         343 : evalcusp(GEN mf, GEN F, GEN t, long prec)
    6979             : {
    6980             :   long A, C;
    6981             :   GEN R;
    6982         343 :   if (!cusp_AC(t, &A,&C)) return NULL;
    6983         189 :   if (C % mf_get_N(F) == 0) return gel(mfcoefs_i(F, 0, 1), 1);
    6984         175 :   R = mfgaexpansion(mf, F, cusp2mat(A,C), 0, prec);
    6985         175 :   return gequal0(gel(R,1))? gmael(R,3,1): gen_0;
    6986             : }
    6987             : /* Evaluate an mf closure numerically, i.e., in the usual sense, either for a
    6988             :  * single tau or a vector of tau; for each, return a vector of results
    6989             :  * corresponding to all complex embeddings of F. If flag is non-zero, allow
    6990             :  * replacing F by F | gamma to increase imag(gamma^(-1).tau) [ expensive if
    6991             :  * MF_EISENSPACE not present ] */
    6992             : static GEN
    6993         154 : mfeval_i(GEN mf, GEN F, GEN vtau, long flag, long bitprec)
    6994             : {
    6995             :   GEN L0, vL, vb, sqN, vczd, vTAU, vs, van, vE;
    6996         154 :   long N = mf_get_N(F), N0, ta, lv, i, prec = nbits2prec(bitprec);
    6997         154 :   GEN gN = utoipos(N), gk = mf_get_gk(F), gk1 = gsubgs(gk,1), vgk;
    6998         154 :   long flscal = 0;
    6999             : 
    7000             :   /* gen_0 is ignored, second component assumes Ramanujan-Petersson in
    7001             :    * 1/2-integer weight */
    7002         154 :   vgk = mkvec2(gen_0, mfiscuspidal(mf,F)? gmul2n(gk1,-1): gk1);
    7003         154 :   ta = typ(vtau);
    7004         154 :   if (!is_vec_t(ta)) { flscal = 1; vtau = mkvec(vtau); ta = t_VEC; }
    7005         154 :   lv = lg(vtau);
    7006         154 :   sqN = sqrtr_abs(utor(N, prec));
    7007         154 :   vs = const_vec(lv-1, NULL);
    7008         154 :   vb = const_vec(lv-1, NULL);
    7009         154 :   vL = cgetg(lv, t_VEC);
    7010         154 :   vTAU = cgetg(lv, t_VEC);
    7011         154 :   vczd = cgetg(lv, t_VEC);
    7012         154 :   L0 = mfthetaancreate(NULL, gN, vgk); /* only for thetacost */
    7013         154 :   vE = mfgetembed(F, prec);
    7014         154 :   N0 = 0;
    7015         329 :   for (i = 1; i < lv; i++)
    7016             :   {
    7017         182 :     GEN z = gel(vtau,i), tau, U;
    7018             :     long w, n;
    7019             : 
    7020         182 :     gel(vs,i) = evalcusp(mf, F, z, prec);
    7021         175 :     if (gel(vs,i)) continue;
    7022         147 :     tau = cxredga0N(N, z, &U, &gel(vczd,i), flag);
    7023         147 :     if (!flag) w = 0; else { w = mfZC_width(N, gel(U,1)); tau = gdivgs(tau,w); }
    7024         147 :     gel(vTAU,i) = mulcxmI(gmul(tau, sqN));
    7025         147 :     n = lfunthetacost(L0, real_i(gel(vTAU,i)), 0, bitprec);
    7026         147 :     if (N0 < n) N0 = n;
    7027         147 :     if (flag)
    7028             :     {
    7029          35 :       GEN A, al, v = mfslashexpansion(mf, F, ZM_inv(U,NULL), N0, 0, &A, prec);
    7030          35 :       gel(vL,i) = van_embedall(v, vE, gN, vgk);
    7031          35 :       al = gel(A,1);
    7032          35 :       if (!gequal0(al))
    7033           7 :         gel(vb,i) = gexp(gmul(gmul(gmulsg(w,al),PiI2(prec)), tau), prec);
    7034             :     }
    7035             :   }
    7036         147 :   if (!flag)
    7037             :   {
    7038         112 :     van = mfcoefs_i(F, N0, 1);
    7039         112 :     vL = const_vec(lv-1, van_embedall(van, vE, gN, vgk));
    7040             :   }
    7041         322 :   for (i = 1; i < lv; i++)
    7042             :   {
    7043             :     GEN T;
    7044         175 :     if (gel(vs,i)) continue;
    7045         147 :     T = gpow(gel(vczd,i), gneg(gk), prec);
    7046         147 :     if (flag && gel(vb,i)) T = gmul(T, gel(vb,i));
    7047         147 :     gel(vs,i) = lfunthetaall(T, gel(vL,i), gel(vTAU,i), bitprec);
    7048             :   }
    7049         147 :   return flscal? gel(vs,1): vs;
    7050             : }
    7051             : 
    7052             : static long
    7053        1078 : mfistrivial(GEN F)
    7054             : {
    7055        1078 :   switch(mf_get_type(F))
    7056             :   {
    7057           7 :     case t_MF_CONST: return lg(gel(F,2)) == 1;
    7058         224 :     case t_MF_LINEAR: case t_MF_LINEAR_BHN: return gequal0(gel(F,3));
    7059         847 :     default: return 0;
    7060             :   }
    7061             : }
    7062             : 
    7063             : static long
    7064         896 : mf_same_k(GEN mf, GEN f) { return gequal(MF_get_gk(mf), mf_get_gk(f)); }
    7065             : static long
    7066         854 : mf_same_CHI(GEN mf, GEN f)
    7067             : {
    7068         854 :   GEN F1, F2, chi1, chi2, CHI1 = MF_get_CHI(mf), CHI2 = mf_get_CHI(f);
    7069             :   /* are the primitive chars attached to CHI1 and CHI2 equal ? */
    7070         854 :   F1 = znconreyconductor(gel(CHI1,1), gel(CHI1,2), &chi1);
    7071         854 :   if (typ(F1) == t_VEC) F1 = gel(F1,1);
    7072         854 :   F2 = znconreyconductor(gel(CHI2,1), gel(CHI2,2), &chi2);
    7073         854 :   if (typ(F2) == t_VEC) F2 = gel(F2,1);
    7074         854 :   return equalii(F1,F2) && ZV_equal(chi1,chi2);
    7075             : }
    7076             : /* check k and CHI rigorously, but not coefficients nor N */
    7077             : static long
    7078         189 : mfisinspace_i(GEN mf, GEN F)
    7079             : {
    7080         189 :   return mfistrivial(F) || (mf_same_k(mf,F) && mf_same_CHI(mf,F));
    7081             : }
    7082             : static void
    7083           7 : err_space(GEN F)
    7084           7 : { pari_err_DOMAIN("mftobasis", "form", "does not belong to",
    7085           0 :                   strtoGENstr("space"), F); }
    7086             : 
    7087             : static long
    7088         140 : mfcheapeisen(GEN mf)
    7089             : {
    7090         140 :   long k, L, N = MF_get_N(mf);
    7091             :   GEN P;
    7092         140 :   if (N <= 70) return 1;
    7093          84 :   k = itos(gceil(MF_get_gk(mf)));
    7094          84 :   if (odd(k)) k--;
    7095          84 :   switch (k)
    7096             :   {
    7097           0 :     case 2:  L = 190; break;
    7098          14 :     case 4:  L = 162; break;
    7099             :     case 6:
    7100          70 :     case 8:  L = 88; break;
    7101           0 :     case 10: L = 78; break;
    7102           0 :     default: L = 66; break;
    7103             :   }
    7104          84 :   P = gel(myfactoru(N), 1);
    7105          84 :   return P[lg(P)-1] <= L;
    7106             : }
    7107             : 
    7108             : static GEN
    7109         175 : myimag_i(GEN tau)
    7110             : {
    7111         175 :   long tc = typ(tau);
    7112         175 :   if (tc == t_INFINITY || tc == t_INT || tc == t_FRAC)
    7113          28 :     return gen_1;
    7114         147 :   if (tc == t_VEC)
    7115             :   {
    7116             :     long ltau, i;
    7117           7 :     GEN z = cgetg_copy(tau, &ltau);
    7118           7 :     for (i=1; i<ltau; i++) gel(z,i) = myimag_i(gel(tau,i));
    7119           7 :     return z;
    7120             :   }
    7121         140 :   return imag_i(tau);
    7122             : }
    7123             : 
    7124             : static GEN
    7125         140 : mintau(GEN vtau)
    7126             : {
    7127         140 :   if (!is_vec_t(typ(vtau))) return myimag_i(vtau);
    7128           7 :   return (lg(vtau) == 1)? gen_1: vecmin(myimag_i(vtau));
    7129             : }
    7130             : 
    7131             : /* initialization for mfgaexpansion: what does not depend on cusp */
    7132             : static GEN
    7133         826 : mf_eisendec(GEN mf, GEN F, long prec)
    7134             : {
    7135         826 :   GEN B = liftpol_shallow(mfeisensteindec(mf, F)), v = variables_vecsmall(B);
    7136         826 :   GEN Mvecj = obj_check(mf, MF_EISENSPACE);
    7137         826 :   long l = lg(v), i, ord;
    7138         826 :   if (lg(Mvecj) < 5) Mvecj = gel(Mvecj,1);
    7139         826 :   ord = itou(gel(Mvecj,4));
    7140         882 :   for (i = 1; i < l; i++)
    7141         637 :     if (v[i] != 1) { B = gsubst(B, v[i], rootsof1u_cx(ord, prec)); break; }
    7142         826 :   return B;
    7143             : }
    7144             : 
    7145             : GEN
    7146         154 : mfeval(GEN mf0, GEN F, GEN vtau, long bitprec)
    7147             : {
    7148         154 :   pari_sp av = avma;
    7149         154 :   long flnew = 1;
    7150         154 :   GEN mf = checkMF_i(mf0);
    7151         154 :   if (!mf) pari_err_TYPE("mfeval", mf0);
    7152         154 :   if (!checkmf_i(F)) pari_err_TYPE("mfeval", F);
    7153         154 :   if (!mfisinspace_i(mf, F)) err_space(F);
    7154         154 :   if (!obj_check(mf, MF_EISENSPACE)) flnew = mfcheapeisen(mf);
    7155         154 :   if (flnew && gcmpgs(gmulsg(2*MF_get_N(mf), mintau(vtau)), 1) >= 0) flnew = 0;
    7156         154 :   return gerepilecopy(av, mfeval_i(mf, F, vtau, flnew, bitprec));
    7157             : }
    7158             : 
    7159             : static long
    7160         182 : val(GEN v, long bit)
    7161             : {
    7162         182 :   long c, l = lg(v);
    7163         399 :   for (c = 1; c < l; c++)
    7164         385 :     if (gexpo(gel(v,c)) > -bit) return c-1;
    7165          14 :   return -1;
    7166             : }
    7167             : GEN
    7168         196 : mfcuspval(GEN mf, GEN F, GEN cusp, long bitprec)
    7169             : {
    7170         196 :   pari_sp av = avma;
    7171         196 :   long lvE, w, N, sb, n, A, C, prec = nbits2prec(bitprec);
    7172             :   GEN ga, gk, vE;
    7173         196 :   mf = checkMF(mf);
    7174         196 :   if (!checkmf_i(F)) pari_err_TYPE("mfcuspval",F);
    7175         196 :   N = MF_get_N(mf);
    7176         196 :   cusp_canon(cusp, N, &A, &C);
    7177         196 :   gk = mf_get_gk(F);
    7178         196 :   if (typ(gk) != t_INT)
    7179             :   {
    7180          42 :     GEN FT = mfmultheta(F), mf2 = obj_checkbuild(mf, MF_MF2INIT, &mf2init);
    7181          42 :     GEN r = mfcuspval(mf2, FT, cusp, bitprec);
    7182          42 :     if ((C & 3L) == 2)
    7183             :     {
    7184          14 :       GEN z = sstoQ(1,4);
    7185          14 :       r = gsub(r, typ(r) == t_VEC? const_vec(lg(r)-1, z): z);
    7186             :     }
    7187          42 :     return gerepileupto(av, r);
    7188             :   }
    7189         154 :   vE = mfgetembed(F, prec);
    7190         154 :   lvE = lg(vE);
    7191         154 :   w = mfcuspcanon_width(N, C);
    7192         154 :   sb = w * mfsturmNk(N, itos(gk));
    7193         154 :   ga = cusp2mat(A,C);
    7194         161 :   for (n = 8;; n = minss(sb, n << 1))
    7195           7 :   {
    7196         161 :     GEN R = mfgaexpansion(mf, F, ga, n, prec), res = liftpol_shallow(gel(R,3));
    7197         161 :     GEN v = cgetg(lvE-1, t_VECSMALL);
    7198         161 :     long j, ok = 1;
    7199         161 :     res = RgC_embedall(res, vE);
    7200         343 :     for (j = 1; j < lvE; j++)
    7201             :     {
    7202         182 :       v[j] = val(gel(res,j), bitprec/2);
    7203         182 :       if (v[j] < 0) ok = 0;
    7204             :     }
    7205         161 :     if (ok)
    7206             :     {
    7207         147 :       res = cgetg(lvE, t_VEC);
    7208         147 :       for (j = 1; j < lvE; j++) gel(res,j) = gadd(gel(R,1), sstoQ(v[j], w));
    7209         147 :       return gerepilecopy(av, lvE==2? gel(res,1): res);
    7210             :     }
    7211          14 :     if (n == sb) return lvE==2? mkoo(): const_vec(lvE-1, mkoo()); /* 0 */
    7212             :   }
    7213             : }
    7214             : 
    7215             : long
    7216         196 : mfiscuspidal(GEN mf, GEN F)
    7217             : {
    7218         196 :   pari_sp av = avma;
    7219             :   GEN mf2;
    7220         196 :   if (space_is_cusp(MF_get_space(mf))) return 1;
    7221          77 :   if (typ(mf_get_gk(F)) == t_INT)
    7222             :   {
    7223          49 :     GEN v = mftobasis(mf,F,0), vE = vecslice(v, 1, lg(MF_get_E(mf))-1);
    7224          49 :     return gc_long(av, gequal0(vE));
    7225             :   }
    7226          28 :   if (!gequal0(mfak_i(F, 0))) return 0;
    7227          14 :   mf2 = obj_checkbuild(mf, MF_MF2INIT, &mf2init);
    7228          14 :   return mfiscuspidal(mf2, mfmultheta(F));
    7229             : }
    7230             : 
    7231             : /* F = vector of newforms in mftobasis format */
    7232             : static GEN
    7233          77 : mffrickeeigen_i(GEN mf, GEN F, GEN vE, long prec)
    7234             : {
    7235          77 :   GEN M, Z, L0, gN = MF_get_gN(mf), gk = MF_get_gk(mf);
    7236          77 :   long N0, i, lM, bit = prec2nbits(prec), k = itou(gk);
    7237          77 :   long LIM = 5; /* Sturm bound is enough */
    7238             : 
    7239          77 :   L0 = mfthetaancreate(NULL, gN, gk); /* only for thetacost */
    7240             : START:
    7241          77 :   N0 = lfunthetacost(L0, gen_1, LIM, bit);
    7242          77 :   M = mfcoefs_mf(mf, N0, 1);
    7243          77 :   lM = lg(F);
    7244          77 :   Z = cgetg(lM, t_VEC);
    7245         231 :   for (i = 1; i < lM; i++)
    7246             :   { /* expansion of D * F[i] */
    7247         154 :     GEN D, z, van = RgM_RgC_mul(M, Q_remove_denom(gel(F,i), &D));
    7248         154 :     GEN L = van_embedall(van, gel(vE,i), gN, gk);
    7249         154 :     long l = lg(L), j, bit_add = D? expi(D): 0;
    7250         154 :     gel(Z,i) = z = cgetg(l, t_VEC);
    7251         483 :     for (j = 1; j < l; j++)
    7252             :     {
    7253             :       GEN v, C, C0;
    7254             :       long m, e;
    7255         462 :       for (m = 0; m <= LIM; m++)
    7256             :       {
    7257         462 :         v = lfuntheta(gmael(L,j,2), gen_1, m, bit);
    7258         462 :         if (gexpo(v) > bit_add - bit/2) break;
    7259             :       }
    7260         329 :       if (m > LIM) { LIM <<= 1; goto START; }
    7261         329 :       C = mulcxpowIs(gdiv(v,conj_i(v)), 2*m - k);
    7262         329 :       C0 = grndtoi(C, &e); if (e < 5-bit_accuracy(precision(C))) C = C0;
    7263         329 :       gel(z,j) = C;
    7264             :     }
    7265             :   }
    7266          77 :   return Z;
    7267             : }
    7268             : static GEN
    7269          70 : mffrickeeigen(GEN mf, GEN vE, long prec)
    7270             : {
    7271          70 :   GEN D = obj_check(mf, MF_FRICKE);
    7272          70 :   if (D) { long p = gprecision(D); if (!p || p >= prec) return D; }
    7273          63 :   D = mffrickeeigen_i(mf, MF_get_newforms(mf), vE, prec);
    7274          63 :   return obj_insert(mf, MF_FRICKE, D);
    7275             : }
    7276             : 
    7277             : /* integral weight, new space for primitive quadratic character CHIP;
    7278             :  * MF = vector of embedded eigenforms coefs on mfbasis, by orbit.
    7279             :  * Assume N > Q > 1 and (Q,f(CHIP)) = 1 */
    7280             : static GEN
    7281          56 : mfatkineigenquad(GEN mf, GEN CHIP, long Q, GEN MF, long bitprec)
    7282             : {
    7283             :   GEN L0, la2, S, F, vP, tau, wtau, Z, va, vb, den, coe, sqrtQ, sqrtN;
    7284          56 :   GEN M, gN, gk = MF_get_gk(mf);
    7285          56 :   long N0, x, yq, i, j, lF, dim, muQ, prec = nbits2prec(bitprec);
    7286          56 :   long N = MF_get_N(mf), k = itos(gk), NQ = N / Q;
    7287             : 
    7288             :   /* Q coprime to FC */
    7289          56 :   F = MF_get_newforms(mf);
    7290          56 :   vP = MF_get_fields(mf);
    7291          56 :   lF = lg(F);
    7292          56 :   Z = cgetg(lF, t_VEC);
    7293          56 :   S = MF_get_S(mf); dim = lg(S) - 1;
    7294          56 :   muQ = mymoebiusu(Q);
    7295          56 :   if (muQ)
    7296             :   {
    7297          42 :     GEN SQ = cgetg(dim+1,t_VEC), Qk = gpow(stoi(Q), sstoQ(k-2, 2), prec);
    7298          42 :     long i, bit2 = bitprec >> 1;
    7299          42 :     for (j = 1; j <= dim; j++) gel(SQ,j) = mfak_i(gel(S,j), Q);
    7300          84 :     for (i = 1; i < lF; i++)
    7301             :     {
    7302          42 :       GEN S = RgV_dotproduct(gel(F,i), SQ), T = gel(vP,i);
    7303             :       long e;
    7304          42 :       if (degpol(T) > 1 && typ(S) != t_POLMOD) S = gmodulo(S, T);
    7305          42 :       S = grndtoi(gdiv(conjvec(S, prec), Qk), &e);
    7306          42 :       if (e > -bit2) pari_err_PREC("mfatkineigenquad");
    7307          42 :       if (muQ == -1) S = gneg(S);
    7308          42 :       gel(Z,i) = S;
    7309             :     }
    7310          42 :     return Z;
    7311             :   }
    7312          14 :   la2 = mfchareval_i(CHIP, Q); /* 1 or -1 */
    7313          14 :   (void)cbezout(Q, NQ, &x, &yq);
    7314          14 :   sqrtQ = sqrtr_abs(utor(Q,prec));
    7315          14 :   tau = mkcomplex(gadd(sstoQ(-1, NQ), ginv(utoi(1000))),
    7316             :                   divru(sqrtQ, N));
    7317          14 :   den = gaddgs(gmulsg(NQ, tau), 1);
    7318          14 :   wtau = gdiv(gsub(gmulsg(x, tau), sstoQ(yq, Q)), den);
    7319          14 :   coe = gpowgs(gmul(sqrtQ, den), k);
    7320             : 
    7321          14 :   sqrtN = sqrtr_abs(utor(N,prec));
    7322          14 :   tau  = mulcxmI(gmul(tau,  sqrtN));
    7323          14 :   wtau = mulcxmI(gmul(wtau, sqrtN));
    7324          14 :   gN = utoipos(N);
    7325          14 :   L0 = mfthetaancreate(NULL, gN, gk); /* only for thetacost */
    7326          14 :   N0 = maxss(lfunthetacost(L0,real_i(tau), 0,bitprec),
    7327             :              lfunthetacost(L0,real_i(wtau),0,bitprec));
    7328          14 :   M = mfcoefs_mf(mf, N0, 1);
    7329          14 :   va = cgetg(dim+1, t_VEC);
    7330          14 :   vb = cgetg(dim+1, t_VEC);
    7331         105 :   for (j = 1; j <= dim; j++)
    7332             :   {
    7333          91 :     GEN L, v = vecslice(gel(M,j), 2, N0+1); /* remove a0 */
    7334          91 :     settyp(v, t_VEC); L = mfthetaancreate(v, gN, gk);
    7335          91 :     gel(va,j) = lfuntheta(L, tau,0,bitprec);
    7336          91 :     gel(vb,j) = lfuntheta(L,wtau,0,bitprec);
    7337             :   }
    7338          84 :   for (i = 1; i < lF; i++)
    7339             :   {
    7340          70 :     GEN z, FE = gel(MF,i);
    7341          70 :     long l = lg(FE);
    7342          70 :     z = cgetg(l, t_VEC);
    7343          70 :     for (j = 1; j < l; j++)
    7344             :     {
    7345          70 :       GEN f = gel(FE,j), a = RgV_dotproduct(va,f), b = RgV_dotproduct(vb,f);
    7346          70 :       GEN la = ground( gdiv(b, gmul(a,coe)) );
    7347          70 :       if (!gequal(gsqr(la), la2)) pari_err_PREC("mfatkineigenquad");
    7348          70 :       if (typ(la) == t_INT)
    7349             :       {
    7350          70 :         if (j != 1) pari_err_BUG("mfatkineigenquad");
    7351          70 :         z = const_vec(l-1, la); break;
    7352             :       }
    7353           0 :       gel(z,j) = la;
    7354             :     }
    7355          70 :     gel(Z,i) = z;
    7356             :   }
    7357          14 :   return Z;
    7358             : }
    7359             : 
    7360             : static GEN
    7361          70 : myusqrt(ulong a, long prec)
    7362             : {
    7363          70 :   if (a == 1UL) return gen_1;
    7364          56 :   if (uissquareall(a, &a)) return utoipos(a);
    7365          42 :   return sqrtr_abs(utor(a, prec));
    7366             : }
    7367             : /* Assume mf is a non-trivial new space, rational primitive character CHIP
    7368             :  * and (Q,FC) = 1 */
    7369             : static GEN
    7370          98 : mfatkinmatnewquad(GEN mf, GEN CHIP, long Q, long flag, long PREC)
    7371             : {
    7372          98 :   GEN cM, M, D, MF, den, vE, F = MF_get_newforms(mf);
    7373          98 :   long i, c, e, prec, bitprec, lF = lg(F), N = MF_get_N(mf), k = MF_get_k(mf);
    7374             : 
    7375          98 :   if (Q == 1) return mkvec4(gen_0, matid(MF_get_dim(mf)), gen_1, mf);
    7376          98 :   den = gel(MF_get_Minv(mf), 2);
    7377          98 :   bitprec = expi(den) + 64;
    7378          98 :   if (!flag) bitprec = maxss(bitprec, prec2nbits(PREC));
    7379             : 
    7380             : START:
    7381          98 :   prec = nbits2prec(bitprec);
    7382          98 :   vE = mfeigenembed(mf, prec);
    7383          98 :   M = cgetg(lF, t_VEC);
    7384          98 :   for (i = 1; i < lF; i++) gel(M,i) = RgC_embedall(gel(F,i), gel(vE,i));
    7385          98 :   if (Q != N)
    7386             :   {
    7387          56 :     D = mfatkineigenquad(mf, CHIP, Q, M, bitprec);
    7388          56 :     c = odd(k)? Q: 1;
    7389             :   }
    7390             :   else
    7391             :   {
    7392          42 :     D = mffrickeeigen(mf, vE, DEFAULTPREC);
    7393          42 :     c = mfcharmodulus(CHIP); if (odd(k)) c = -Q/c;
    7394             :   }
    7395          98 :   D = shallowconcat1(D);
    7396          98 :   if (vec_isconst(D)) { MF = diagonal_shallow(D); flag = 0; }
    7397             :   else
    7398             :   {
    7399          63 :     M = shallowconcat1(M);
    7400          63 :     MF = RgM_mul(matmuldiagonal(M,D), ginv(M));
    7401             :   }
    7402          98 :   if (!flag) return mkvec4(gen_0, MF, gen_1, mf);
    7403             : 
    7404          21 :   if (c > 0)
    7405          21 :     cM = myusqrt(c, PREC);
    7406             :   else
    7407             :   {
    7408           0 :     MF = imag_i(MF); c = -c;
    7409           0 :     cM = mkcomplex(gen_0, myusqrt(c,PREC));
    7410             :   }
    7411          21 :   if (c != 1) MF = RgM_Rg_mul(MF, myusqrt(c,prec));
    7412          21 :   MF = grndtoi(RgM_Rg_mul(MF,den), &e);
    7413          21 :   if (e > -32) { bitprec <<= 1; goto START; }
    7414          21 :   MF = RgM_Rg_div(MF, den);
    7415          21 :   if (is_rational_t(typ(cM)) && !isint1(cM))
    7416           0 :   { MF = RgM_Rg_div(MF, cM); cM = gen_1; }
    7417          21 :   return mkvec4(gen_0, MF, cM, mf);
    7418             : }
    7419             : 
    7420             : /* let CHI mod N, Q || N, return \bar{CHI_Q} * CHI_{N/Q} */
    7421             : static GEN
    7422          70 : mfcharAL(GEN CHI, long Q)
    7423             : {
    7424          70 :   GEN G = gel(CHI,1), c = gel(CHI,2), cycc, d, P, E, F;
    7425          70 :   long l = lg(c), N = mfcharmodulus(CHI), i;
    7426          70 :   if (N == Q) return mfcharconj(CHI);
    7427          42 :   if (N == 1) return CHI;
    7428          42 :   CHI = leafcopy(CHI);
    7429          42 :   gel(CHI,2) = d = leafcopy(c);
    7430          42 :   F = znstar_get_faN(G);
    7431          42 :   P = gel(F,1);
    7432          42 :   E = gel(F,2);
    7433          42 :   cycc = znstar_get_conreycyc(G);
    7434          42 :   if (!odd(Q) && equaliu(gel(P,1), 2) && E[1] >= 3)
    7435          14 :     gel(d,2) = Fp_neg(gel(d,2), gel(cycc,2));
    7436          56 :   else for (i = 1; i < l; i++)
    7437          28 :     if (!umodui(Q, gel(P,i))) gel(d,i) = Fp_neg(gel(d,i), gel(cycc,i));
    7438          42 :   return CHI;
    7439             : }
    7440             : static long
    7441         189 : atkin_get_NQ(long N, long Q, const char *f)
    7442             : {
    7443         189 :   long NQ = N / Q;
    7444         189 :   if (N % Q) pari_err_DOMAIN(f,"N % Q","!=",gen_0,utoi(Q));
    7445         189 :   if (ugcd(NQ, Q) > 1) pari_err_DOMAIN(f,"gcd(Q,N/Q)","!=",gen_1,utoi(Q));
    7446         189 :   return NQ;
    7447             : }
    7448             : 
    7449             : /* transform mf to new_NEW if possible */
    7450             : static GEN
    7451        1127 : MF_set_new(GEN mf)
    7452             : {
    7453        1127 :   GEN vMjd, vj, gk = MF_get_gk(mf);
    7454             :   long l, j;
    7455        1127 :   if (MF_get_space(mf) != mf_CUSP
    7456         182 :       || typ(gk) != t_INT || itou(gk) == 1) return mf;
    7457         168 :   vMjd = MFcusp_get_vMjd(mf); l = lg(vMjd);
    7458         168 :   if (l > 1 && gel(vMjd,1)[1] != MF_get_N(mf)) return mf; /* oldspace != 0 */
    7459         168 :   mf = shallowcopy(mf);
    7460         168 :   gel(mf,1) = shallowcopy(gel(mf,1));
    7461         168 :   MF_set_space(mf, mf_NEW);
    7462         168 :   vj = cgetg(l, t_VECSMALL);
    7463         168 :   for (j = 1; j < l; j++) vj[j] = gel(vMjd, j)[2];
    7464         168 :   gel(mf,4) = vj; return mf;
    7465             : }
    7466             : 
    7467             : /* if flag = 1, rationalize, else don't */
    7468             : static GEN
    7469         168 : mfatkininit_i(GEN mf, long Q, long flag, long prec)
    7470             : {
    7471             :   GEN M, B, C, CHI, CHIAL, G, chi, P, z, g, mfB, s, Mindex, Minv;
    7472         168 :   long j, l, lim, ord, FC, NQ, cQ, nk, dk, N = MF_get_N(mf);
    7473             : 
    7474         168 :   B = MF_get_basis(mf); l = lg(B);
    7475         168 :   M = cgetg(l, t_MAT); if (l == 1) return mkvec4(gen_0,M,gen_1,mf);
    7476         168 :   Qtoss(MF_get_gk(mf), &nk,&dk);
    7477         168 :   Q = labs(Q);
    7478         168 :   NQ = atkin_get_NQ(N, Q, "mfatkininit");
    7479         168 :   CHI = MF_get_CHI(mf);
    7480         168 :   CHI = mfchartoprimitive(CHI, &FC);
    7481         168 :   ord = mfcharorder_canon(CHI);
    7482         168 :   mf = MF_set_new(mf);
    7483         168 :   if (MF_get_space(mf) == mf_NEW && ord == 1 && NQ % FC == 0 && dk == 1)
    7484          98 :     return mfatkinmatnewquad(mf, CHI, Q, flag, prec);
    7485             :   /* now flag != 0 */
    7486          70 :   G   = gel(CHI,1);
    7487          70 :   chi = gel(CHI,2);
    7488          70 :   if (Q == N) { g = mkmat22s(0, -1, N, 0); cQ = NQ; } /* Fricke */
    7489             :   else
    7490             :   {
    7491          28 :     GEN F, gQP = utoi(ugcd(Q, FC));
    7492             :     long t, v;
    7493          28 :     chi = znchardecompose(G, chi, gQP);
    7494          28 :     F = znconreyconductor(G, chi, &chi);
    7495          28 :     G = znstar0(F,1);
    7496          28 :     (void)cbezout(Q, NQ, &t, &v);
    7497          28 :     g = mkmat22s(Q*t, 1, -N*v, Q);
    7498          28 :     cQ = -NQ*v;
    7499             :   }
    7500          70 :   C = s = gen_1;
    7501             :   /* N.B. G,chi are G_Q,chi_Q [primitive] at this point */
    7502          70 :   if (lg(chi) != 1) C = ginv( znchargauss(G, chi, gen_1, prec2nbits(prec)) );
    7503          70 :   if (dk == 1)
    7504          63 :   { if (odd(nk)) s = myusqrt(Q,prec); }
    7505             :   else
    7506             :   {
    7507           7 :     long r = nk >> 1; /* k-1/2 */
    7508           7 :     s = gpow(utoipos(Q), mkfracss(odd(r)? 1: 3, 4), prec);
    7509           7 :     if (odd(cQ))
    7510             :     {
    7511           7 :       long t = r + ((cQ-1) >> 1);
    7512           7 :       s = mkcomplex(s, odd(t)? gneg(s): s);
    7513             :     }
    7514             :   }
    7515          70 :   if (!isint1(s)) C = gmul(C, s);
    7516          70 :   CHIAL = mfcharAL(CHI, Q);
    7517          70 :   if (dk == 2)
    7518           7 :     CHIAL = mfcharmul(CHIAL, induce(gel(CHIAL,1), utoipos(odd(Q) ? Q<<2 : Q)));
    7519          70 :   CHIAL = mfchartoprimitive(CHIAL,NULL);
    7520          70 :   mfB = gequal(CHIAL,CHI)? mf: mfinit_Nndkchi(N,nk,dk,CHIAL,MF_get_space(mf),0);
    7521          70 :   Mindex = MF_get_Mindex(mfB);
    7522          70 :   Minv = MF_get_Minv(mfB);
    7523          70 :   P = z = NULL;
    7524          70 :   if (ord != 1) { P = mfcharpol(CHI); z = rootsof1u_cx(ord, prec); }
    7525          70 :   lim = maxss(mfsturm(mfB), mfsturm(mf)) + 1;
    7526         217 :   for (j = 1; j < l; j++)
    7527             :   {
    7528         147 :     GEN v = mfslashexpansion(mf, gel(B,j), g, lim, 0, NULL, prec+1);
    7529             :     long junk;
    7530         147 :     if (!isint1(C)) v = RgV_Rg_mul(v, C);
    7531         147 :     v = bestapprnf(v, P, z, prec);
    7532         147 :     v = vecpermute_partial(v, Mindex, &junk);
    7533         147 :     v = Minv_RgC_mul(Minv, v); /* cf mftobasis_i */
    7534         147 :     gel(M, j) = v;
    7535             :   }
    7536          70 :   if (is_rational_t(typ(C)) && !gequal1(C)) { M = gdiv(M, C); C = gen_1; }
    7537          70 :   if (mfB == mf) mfB = gen_0;
    7538          70 :   return mkvec4(mfB, M, C, mf);
    7539             : }
    7540             : GEN
    7541          77 : mfatkininit(GEN mf, long Q, long prec)
    7542             : {
    7543          77 :   pari_sp av = avma;
    7544          77 :   mf = checkMF(mf); return gerepilecopy(av, mfatkininit_i(mf, Q, 1, prec));
    7545             : }
    7546             : static void
    7547          21 : checkmfa(GEN z)
    7548             : {
    7549          21 :   if (typ(z) != t_VEC || lg(z) != 5 || typ(gel(z,2)) != t_MAT
    7550          21 :       || !checkMF_i(gel(z,4))
    7551          21 :       || (!isintzero(gel(z,1)) && !checkMF_i(gel(z,1))))
    7552           0 :     pari_err_TYPE("mfatkin [please apply mfatkininit()]",z);
    7553          21 : }
    7554             : 
    7555             : /* Apply atkin Q to closure F */
    7556             : GEN
    7557          21 : mfatkin(GEN mfa, GEN F)
    7558             : {
    7559          21 :   pari_sp av = avma;
    7560             :   GEN z, mfB, MQ, mf;
    7561          21 :   checkmfa(mfa);
    7562          21 :   mfB= gel(mfa,1);
    7563          21 :   MQ = gel(mfa,2);
    7564          21 :   mf = gel(mfa,4);
    7565          21 :   if (typ(mfB) == t_INT) mfB = mf;
    7566          21 :   z = RgM_RgC_mul(MQ, mftobasis_i(mf,F));
    7567          21 :   return gerepileupto(av, mflinear(mfB, z));
    7568             : }
    7569             : 
    7570             : GEN
    7571          49 : mfatkineigenvalues(GEN mf, long Q, long prec)
    7572             : {
    7573          49 :   pari_sp av = avma;
    7574             :   GEN vF, L, CHI, M, mfatk, C, MQ, vE, mfB;
    7575             :   long N, NQ, l, i;
    7576             : 
    7577          49 :   mf = checkMF(mf); N = MF_get_N(mf);
    7578          49 :   vF = MF_get_newforms(mf); l = lg(vF);
    7579             :   /* N.B. k is integral */
    7580          49 :   if (l == 1) { set_avma(av); return cgetg(1, t_VEC); }
    7581          49 :   L = cgetg(l, t_VEC);
    7582          49 :   if (Q == 1)
    7583             :   {
    7584           7 :     GEN vP = MF_get_fields(mf);
    7585           7 :     for (i = 1; i < l; i++) gel(L,i) = const_vec(degpol(gel(vP,i)), gen_1);
    7586           7 :     return L;
    7587             :   }
    7588          42 :   vE = mfeigenembed(mf,prec);
    7589          42 :   if (Q == N) return gerepileupto(av, mffrickeeigen(mf, vE, prec));
    7590          21 :   Q = labs(Q);
    7591          21 :   NQ = atkin_get_NQ(N, Q, "mfatkineigenvalues"); /* != 1 */
    7592          21 :   mfatk = mfatkininit(mf, Q, prec);
    7593          21 :   mfB= gel(mfatk,1); if (typ(mfB) != t_VEC) mfB = mf;
    7594          21 :   MQ = gel(mfatk,2);
    7595          21 :   C  = gel(mfatk,3);
    7596          21 :   M = row(mfcoefs_mf(mfB,1,1), 2); /* vec of a_1(b_i) for mfbasis functions */
    7597          56 :   for (i = 1; i < l; i++)
    7598             :   {
    7599          35 :     GEN c = RgV_dotproduct(RgM_RgC_mul(MQ,gel(vF,i)), M); /* C * eigen_i */
    7600          35 :     gel(L,i) = Rg_embedall_i(c, gel(vE,i));
    7601             :   }
    7602          21 :   if (!gequal1(C)) L = gdiv(L, C);
    7603          21 :   CHI = MF_get_CHI(mf);
    7604          21 :   if (mfcharorder(CHI) <= 2 && NQ % mfcharconductor(CHI) == 0) L = ground(L);
    7605          21 :   return gerepilecopy(av, L);
    7606             : }
    7607             : 
    7608             : /* expand B_d V, keeping same length */
    7609             : static GEN
    7610        4788 : bdexpand(GEN V, long d)
    7611             : {
    7612             :   GEN W;
    7613             :   long N, n;
    7614        4788 :   if (d == 1) return V;
    7615        1624 :   N = lg(V)-1; W = zerovec(N);
    7616        1624 :   for (n = 0; n <= (N-1)/d; n++) gel(W, n*d+1) = gel(V, n+1);
    7617        1624 :   return W;
    7618             : }
    7619             : /* expand B_d V, increasing length up to lim */
    7620             : static GEN
    7621         266 : bdexpandall(GEN V, long d, long lim)
    7622             : {
    7623             :   GEN W;
    7624             :   long N, n;
    7625         266 :   if (d == 1) return V;
    7626          35 :   N = lg(V)-1; W = zerovec(lim);
    7627          35 :   for (n = 0; n <= N-1 && n*d <= lim; n++) gel(W, n*d+1) = gel(V, n+1);
    7628          35 :   return W;
    7629             : }
    7630             : 
    7631             : static void
    7632        7798 : parse_vecj(GEN T, GEN *E1, GEN *E2)
    7633             : {
    7634        7798 :   if (lg(T)==3) { *E1 = gel(T,1); *E2 = gel(T,2); }
    7635        4165 :   else { *E1 = T; *E2 = NULL; }
    7636        7798 : }
    7637             : 
    7638             : /* g in M_2(Z) ? */
    7639             : static int
    7640        2401 : check_M2Z(GEN g)
    7641        2401 : {  return typ(g) == t_MAT && lg(g) == 3 && lgcols(g) == 3 && RgM_is_ZM(g); }
    7642             : /* g in SL_2(Z) ? */
    7643             : static int
    7644        1470 : check_SL2Z(GEN g) { return check_M2Z(g) && equali1(ZM_det(g)); }
    7645             : 
    7646             : static GEN
    7647        7686 : mfcharcxeval(GEN CHI, long n, long prec)
    7648             : {
    7649             :   GEN ordg;
    7650             :   ulong ord;
    7651        7686 :   if (ugcd(mfcharmodulus(CHI), labs(n)) > 1) return gen_0;
    7652        7686 :   ordg = gmfcharorder(CHI);
    7653        7686 :   ord = itou(ordg);
    7654        7686 :   return rootsof1q_cx(znchareval_i(CHI,n,ordg), ord, prec);
    7655             : }
    7656             : 
    7657             : static GEN
    7658        4403 : RgV_shift(GEN V, GEN gn)
    7659             : {
    7660             :   long i, n, l;
    7661             :   GEN W;
    7662        4403 :   if (typ(gn) != t_INT) pari_err_BUG("RgV_shift [n not integral]");
    7663        4403 :   n = itos(gn);
    7664        4403 :   if (n < 0) pari_err_BUG("RgV_shift [n negative]");
    7665        4403 :   if (!n) return V;
    7666          98 :   W = cgetg_copy(V, &l); if (n > l-1) n = l-1;
    7667          98 :   for (i=1; i <= n; i++) gel(W,i) = gen_0;
    7668          98 :   for (    ; i < l; i++) gel(W,i) = gel(V, i-n);
    7669          98 :   return W;
    7670             : }
    7671             : static GEN
    7672        6776 : hash_eisengacx(hashtable *H, void *E, long w, GEN ga, long n, long prec)
    7673             : {
    7674        6776 :   ulong h = H->hash(E);
    7675        6776 :   hashentry *e = hash_search2(H, E, h);
    7676             :   GEN v;
    7677        6776 :   if (e) v = (GEN)e->val;
    7678             :   else
    7679             :   {
    7680        4410 :     v = mfeisensteingacx((GEN)E, w, ga, n, prec);
    7681        4410 :     hash_insert2(H, E, (void*)v, h);
    7682             :   }
    7683        6776 :   return v;
    7684             : }
    7685             : static GEN
    7686        4403 : vecj_expand(GEN B, hashtable *H, long w, GEN ga, long n, long prec)
    7687             : {
    7688             :   GEN E1, E2, v;
    7689        4403 :   parse_vecj(B, &E1, &E2);
    7690        4403 :   v = hash_eisengacx(H, (void*)E1, w, ga, n, prec);
    7691        4403 :   if (E2)
    7692             :   {
    7693        2352 :     GEN u = hash_eisengacx(H, (void*)E2, w, ga, n, prec);
    7694        2352 :     GEN a = gadd(gel(v,1), gel(u,1));
    7695        2352 :     GEN b = RgV_mul_RgXn(gel(v,2), gel(u,2));
    7696        2352 :     v = mkvec2(a,b);
    7697             :   }
    7698        4403 :   return v;
    7699             : }
    7700             : static GEN
    7701         889 : shift_M(GEN M, GEN Valpha, long w)
    7702             : {
    7703         889 :   long i, l = lg(Valpha);
    7704         889 :   GEN almin = vecmin(Valpha);
    7705        5292 :   for (i = 1; i < l; i++)
    7706             :   {
    7707        4403 :     GEN alpha = gel(Valpha, i), gsh = gmulsg(w, gsub(alpha,almin));
    7708        4403 :     gel(M,i) = RgV_shift(gel(M,i), gsh);
    7709             :   }
    7710         889 :   return almin;
    7711             : }
    7712             : static GEN mfeisensteinspaceinit(GEN NK);
    7713             : #if 0
    7714             : /* ga in M_2^+(Z)), n >= 0 */
    7715             : static GEN
    7716             : mfgaexpansion_init(GEN mf, GEN ga, long n, long prec)
    7717             : {
    7718             :   GEN M, Mvecj, vecj, almin, Valpha;
    7719             :   long i, w, l, N = MF_get_N(mf), c = itos(gcoeff(ga,2,1));
    7720             :   hashtable *H;
    7721             : 
    7722             :   if (c % N == 0)
    7723             :   { /* ga in G_0(N), trivial case; w = 1 */
    7724             :     GEN chid = mfcharcxeval(MF_get_CHI(mf), itos(gcoeff(ga,2,2)), prec);
    7725             :     return mkvec2(chid, utoi(n));
    7726             :   }
    7727             : 
    7728             :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    7729             :   if (lg(Mvecj) < 5) pari_err_IMPL("mfgaexpansion_init in this case");
    7730             :   w = mfcuspcanon_width(N, c);
    7731             :   vecj = gel(Mvecj, 3);
    7732             :   l = lg(vecj);
    7733             :   M = cgetg(l, t_VEC);
    7734             :   Valpha = cgetg(l, t_VEC);
    7735             :   H = hash_create(l, (ulong(*)(void*))&hash_GEN,
    7736             :                      (int(*)(void*,void*))&gidentical, 1);
    7737             :   for (i = 1; i < l; i++)
    7738             :   {
    7739             :     GEN v = vecj_expand(gel(vecj,i), H, w, ga, n, prec);
    7740             :     gel(Valpha,i) = gel(v,1);
    7741             :     gel(M,i) = gel(v,2);
    7742             :   }
    7743             :   almin = shift_M(M, Valpha, w);
    7744             :   return mkvec3(almin, utoi(w), M);
    7745             : }
    7746             : /* half-integer weight not supported; vF = [F,eisendec(F)].
    7747             :  * Minit = mfgaexpansion_init(mf, ga, n, prec) */
    7748             : static GEN
    7749             : mfgaexpansion_with_init(GEN Minit, GEN vF)
    7750             : {
    7751             :   GEN v;
    7752             :   if (lg(Minit) == 3)
    7753             :   { /* ga in G_0(N) */
    7754             :     GEN chid = gel(Minit,1), gn = gel(Minit,2);
    7755             :     v = mfcoefs_i(gel(vF,1), itou(gn), 1);
    7756             :     v = mkvec3(gen_0, gen_1, RgV_Rg_mul(v,chid));
    7757             :   }
    7758             :   else
    7759             :   {
    7760             :     GEN V = RgM_RgC_mul(gel(Minit,3), gel(vF,2));
    7761             :     v = mkvec3(gel(Minit,1), gel(Minit,2), V);
    7762             :   }
    7763             :   return v;
    7764             : }
    7765             : #endif
    7766             : 
    7767             : /* B = mfeisensteindec(F) already embedded, ga in M_2^+(Z)), n >= 0 */
    7768             : static GEN
    7769         889 : mfgaexpansion_i(GEN mf, GEN B0, GEN ga, long n, long prec)
    7770             : {
    7771         889 :   GEN M, Mvecj, vecj, almin, Valpha, B, E = NULL;
    7772         889 :   long i, j, w, nw, l, N = MF_get_N(mf), bit = prec2nbits(prec) / 2;
    7773             :   hashtable *H;
    7774             : 
    7775         889 :   Mvecj = obj_check(mf, MF_EISENSPACE);
    7776         889 :   if (lg(Mvecj) < 5) { E = gel(Mvecj, 2); Mvecj = gel(Mvecj, 1); }
    7777         889 :   vecj = gel(Mvecj, 3);
    7778         889 :   l = lg(vecj);
    7779         889 :   B = cgetg(l, t_COL);
    7780         889 :   M = cgetg(l, t_VEC);
    7781         889 :   Valpha = cgetg(l, t_VEC);
    7782         889 :   w = mfZC_width(N, gel(ga,1));
    7783         889 :   nw = E ? n + w : n;
    7784         889 :   H = hash_create(l, (ulong(*)(void*))&hash_GEN,
    7785             :                      (int(*)(void*,void*))&gidentical, 1);
    7786        7882 :   for (i = j = 1; i < l; i++)
    7787             :   {
    7788             :     GEN v;
    7789        6993 :     if (gequal0(gel(B0,i))) continue;
    7790        4403 :     v = vecj_expand(gel(vecj,i), H, w, ga, nw, prec);
    7791        4403 :     gel(B,j) = gel(B0,i);
    7792        4403 :     gel(Valpha,j) = gel(v,1);
    7793        4403 :     gel(M,j) = gel(v,2); j++;
    7794             :   }
    7795         889 :   setlg(Valpha, j);
    7796         889 :   setlg(B, j);
    7797         889 :   setlg(M, j); l = j;
    7798         889 :   if (l == 1) return mkvec3(gen_0, utoi(w), zerovec(n+1));
    7799         889 :   almin = shift_M(M, Valpha, w);
    7800         889 :   B = RgM_RgC_mul(M, B); l = lg(B);
    7801      138110 :   for (i = 1; i < l; i++)
    7802      137221 :     if (gexpo(gel(B,i)) < -bit) gel(B,i) = gen_0;
    7803         889 :   settyp(B, t_VEC);
    7804         889 :   if (E)
    7805             :   {
    7806          21 :     GEN v = hash_eisengacx(H, (void*)E, w, ga, n, prec);
    7807          21 :     long ell = 0;
    7808          21 :     almin = gsub(almin, gel(v,1));
    7809          21 :     if (gsigne(almin) < 0)
    7810             :     {
    7811           0 :       GEN gell = gceil(gmulsg(-w, almin));
    7812           0 :       ell = itos(gell);
    7813           0 :       almin = gadd(almin, gdivgs(gell, w));
    7814           0 :       if (nw < ell) pari_err_IMPL("alpha < 0 in mfgaexpansion");
    7815             :     }
    7816          21 :     B = vecslice(B, ell + 1, n + ell + 1);
    7817          21 :     B = RgV_div_RgXn(B, gel(v,2));
    7818             :   }
    7819         889 :   return mkvec3(almin, utoi(w), B);
    7820             : }
    7821             : 
    7822             : /* Theta multiplier: assume 4 | C, (C,D)=1 */
    7823             : static GEN
    7824         238 : mfthetamultiplier(long C, long D)
    7825             : {
    7826         238 :   long s = kross(C, D);
    7827         238 :   if ((D&3L) == 1) return stoi(s);
    7828          49 :   return s > 0 ? powIs(3) : gen_I();
    7829             : }
    7830             : static GEN
    7831         238 : mfthetaexpansion(GEN M, long n)
    7832             : {
    7833         238 :   GEN s, al, sla, V = zerovec(n + 1);
    7834         238 :   long w, lim, la, f, C = itos(gcoeff(M, 2, 1)), D = itos(gcoeff(M, 2, 2));
    7835         238 :   switch (C & 3L)
    7836             :   {
    7837          56 :     case 0: al = gen_0; w = 1;
    7838          56 :       s = mfthetamultiplier(C,D);
    7839          56 :       lim = usqrt(n); gel(V, 1) = s;
    7840          56 :       s = gmul2n(s, 1);
    7841          56 :       for (f = 1; f <= lim; f++) gel(V, f*f + 1) = s;
    7842          56 :       break;
    7843          84 :     case 2: al = sstoQ(1,4); w = 1;
    7844          84 :       s = gmul2n(mfthetamultiplier(C - 2*D, D), 1);
    7845          84 :       lim = (usqrt(n << 2) - 1) >> 1;
    7846          84 :       for (f = 0; f <= lim; f++) gel(V, f*(f+1) + 1) = s;
    7847          84 :       break;
    7848          98 :     default: al = gen_0; w = 4; la = (-D*C) & 3L;
    7849          98 :       s = mfthetamultiplier(-(D + la*C), C);
    7850          98 :       s = gsub(s, mulcxI(s));
    7851          98 :       sla = gmul(s, powIs(-la));
    7852          98 :       lim = usqrt(n); gel(V, 1) = gmul2n(s, -1);
    7853          98 :       for (f = 1; f <= lim; f++) gel(V, f*f + 1) = odd(f) ? sla : s;
    7854          98 :       break;
    7855             :   }
    7856         238 :   return mkvec3(al, stoi(w), V);
    7857             : }
    7858             : 
    7859             : /* F 1/2 integral weight */
    7860             : static GEN
    7861         238 : mf2gaexpansion(GEN mf2, GEN F, GEN ga, long n, long prec)
    7862             : {
    7863         238 :   GEN FT = mfmultheta(F), mf = obj_checkbuild(mf2, MF_MF2INIT, &mf2init);
    7864             :   GEN res, V1, Tres, V2, al, V, gsh;
    7865         238 :   long w2, C = itos(gcoeff(ga,2,1)), w = mfcuspcanon_width(MF_get_N(mf), C);
    7866         238 :   long ext = ((C & 3L) != 2)? 0: (w+3) >> 2;
    7867         238 :   long prec2 = prec + nbits2extraprec((long)M_PI/(2*M_LN2)*sqrt(n + ext));
    7868         238 :   res = mfgaexpansion(mf, FT, ga, n + ext, prec2);
    7869         238 :   Tres = mfthetaexpansion(ga, n + ext);
    7870         238 :   V1 = gel(res,3);
    7871         238 :   V2 = gel(Tres,3);
    7872         238 :   al = gsub(gel(res,1), gel(Tres,1));
    7873         238 :   w2 = itos(gel(Tres,2));
    7874         238 :   if (w != itos(gel(res,2)) || w % w2)
    7875           0 :     pari_err_BUG("mf2gaexpansion [incorrect w2 or w]");
    7876         238 :   if (w2 != w) V2 = bdexpand(V2, w/w2);
    7877         238 :   V = RgV_div_RgXn(V1, V2);
    7878         238 :   gsh = gfloor(gmulsg(w, al));
    7879         238 :   if (!gequal0(gsh))
    7880             :   {
    7881          28 :     al = gsub(al, gdivgs(gsh, w));
    7882          28 :     if (gsigne(gsh) > 0)
    7883             :     {
    7884           0 :       V = RgV_shift(V, gsh);
    7885           0 :       V = vecslice(V, 1, n + 1);
    7886             :     }
    7887             :     else
    7888             :     {
    7889          28 :       long sh = -itos(gsh), i;
    7890          28 :       if (sh > ext) pari_err_BUG("mf2gaexpansion [incorrect sh]");
    7891         119 :       for (i = 1; i <= sh; i++)
    7892          91 :         if (!gequal0(gel(V,i))) pari_err_BUG("mf2gaexpansion [sh too large]");
    7893          28 :       V = vecslice(V, sh+1, n + sh+1);
    7894             :     }
    7895             :   }
    7896         238 :   obj_free(mf); return mkvec3(al, stoi(w), gprec_wtrunc(V, prec));
    7897             : }
    7898             : 
    7899             : static GEN
    7900          70 : mfgaexpansionatkin(GEN mf, GEN F, GEN C, GEN D, long Q, long n, long prec)
    7901             : {
    7902          70 :   GEN mfa = mfatkininit_i(mf, Q, 0, prec), MQ = gel(mfa,2);
    7903          70 :   long i, FC, k = MF_get_k(mf);
    7904          70 :   GEN x, v, V, z, s, CHI = mfchartoprimitive(MF_get_CHI(mf), &FC);
    7905             : 
    7906             :   /* V = mfcoefs(F | w_Q, n), can't use mfatkin because MQ non-rational */
    7907          70 :   V = RgM_RgC_mul(mfcoefs_mf(mf,n,1), RgM_RgC_mul(MQ, mftobasis_i(mf,F)));
    7908          70 :   (void)bezout(utoipos(Q), C, &x, &v);
    7909          70 :   s = mfchareval_i(CHI, (umodiu(x, FC) * umodiu(D, FC)) % FC);
    7910          70 :   s = gdiv(s, gpow(utoipos(Q), sstoQ(k,2), prec));
    7911          70 :   V = RgV_Rg_mul(V, s);
    7912          70 :   z = rootsof1powinit(umodiu(D,Q)*umodiu(v,Q) % Q, Q, prec);
    7913          70 :   for (i = 1; i <= n+1; i++) gel(V,i) = gmul(gel(V,i), rootsof1pow(z, i-1));
    7914          70 :   return mkvec3(gen_0, utoipos(Q), V);
    7915             : }
    7916             : 
    7917             : /* allow F of the form [F, mf_eisendec(F)]~ */
    7918             : static GEN
    7919        1463 : mfgaexpansion(GEN mf, GEN F, GEN ga, long n, long prec)
    7920             : {
    7921        1463 :   GEN v, EF = NULL, res, Mvecj, c, d;
    7922             :   long precnew, N;
    7923             : 
    7924        1463 :   if (n < 0) pari_err_DOMAIN("mfgaexpansion", "n", "<", gen_0, stoi(n));
    7925        1463 :   if (typ(F) == t_COL && lg(F) == 3) { EF = gel(F,2); F = gel(F,1); }
    7926        1463 :   if (!checkmf_i(F)) pari_err_TYPE("mfgaexpansion", F);
    7927        1463 :   if (!check_SL2Z(ga)) pari_err_TYPE("mfgaexpansion",ga);
    7928        1463 :   if (typ(mf_get_gk(F)) != t_INT) return mf2gaexpansion(mf, F, ga, n, prec);
    7929        1225 :   c = gcoeff(ga,2,1);
    7930        1225 :   d = gcoeff(ga,2,2);
    7931        1225 :   N = MF_get_N(mf);
    7932        1225 :   if (!umodiu(c, mf_get_N(F)))
    7933             :   { /* trivial case: ga in Gamma_0(N) */
    7934         266 :     long w = mfcuspcanon_width(N, umodiu(c,N));
    7935         266 :     GEN CHI = mf_get_CHI(F);
    7936         266 :     GEN chid = mfcharcxeval(CHI, umodiu(d,mfcharmodulus(CHI)), prec);
    7937         266 :     v = mfcoefs_i(F, n/w, 1); if (!isint1(chid)) v = RgV_Rg_mul(v,chid);
    7938         266 :     return mkvec3(gen_0, stoi(w), bdexpandall(v,w,n+1));
    7939             :   }
    7940         959 :   mf = MF_set_new(mf);
    7941         959 :   if (MF_get_space(mf) == mf_NEW)
    7942             :   {
    7943         441 :     long cN = umodiu(c,N), g = ugcd(cN,N), Q = N/g;
    7944         441 :     GEN CHI = MF_get_CHI(mf);
    7945         441 :     if (ugcd(cN, Q)==1 && mfcharorder(CHI) <= 2
    7946         217 :                        && g % mfcharconductor(CHI) == 0
    7947         112 :                        && degpol(mf_get_field(F)) == 1)
    7948          70 :       return mfgaexpansionatkin(mf, F, c, d, Q, n, prec);
    7949             :   }
    7950         889 :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    7951         889 :   precnew = prec;
    7952         889 :   if (lg(Mvecj) < 5)
    7953             :   {
    7954          21 :     long e, w = mfZC_width(N, gel(ga,1));
    7955          21 :     GEN v, E = gel(Mvecj,2);
    7956          21 :     v = mfeisensteingacx(E, w, ga, n, LOWDEFAULTPREC);
    7957          21 :     v = gel(v,2);
    7958          21 :     e = gexpo(RgXn_inv(RgV_to_RgX(v,0), n+1));
    7959          21 :     if (e > 0) precnew += nbits2extraprec(e);
    7960             :   }
    7961         889 :   if (!EF) EF = mf_eisendec(mf, F, precnew);
    7962         889 :   res = mfgaexpansion_i(mf, EF, ga, n, precnew);
    7963         889 :   return precnew == prec ? res : gprec_wtrunc(res, prec);
    7964             : }
    7965             : 
    7966             : /* parity = -1 or +1 */
    7967             : static GEN
    7968         217 : findd(long N, long parity)
    7969             : {
    7970         217 :   GEN L, D = mydivisorsu(N);
    7971         217 :   long i, j, l = lg(D);
    7972         217 :   L = cgetg(l, t_VEC);
    7973        1218 :   for (i = j = 1; i < l; i++)
    7974             :   {
    7975        1001 :     long d = D[i];
    7976        1001 :     if (parity == -1) d = -d;
    7977        1001 :     if (sisfundamental(d)) gel(L,j++) = stoi(d);
    7978             :   }
    7979         217 :   setlg(L,j); return L;
    7980             : }
    7981             : /* does ND contain a divisor of N ? */
    7982             : static int
    7983         413 : seenD(long N, GEN ND)
    7984             : {
    7985         413 :   long j, l = lg(ND);
    7986         427 :   for (j = 1; j < l; j++)
    7987          14 :     if (N % ND[j] == 0) return 1;
    7988         413 :   return 0;
    7989             : }
    7990             : static GEN
    7991          42 : search_levels(GEN vN, const char *f)
    7992             : {
    7993          42 :   switch(typ(vN))
    7994             :   {
    7995           7 :     case t_INT: vN = mkvecsmall(itos(vN)); break;
    7996          35 :     case t_VEC: case t_COL: vN = ZV_to_zv(vN); break;
    7997           0 :     case t_VECSMALL: vN = leafcopy(vN); break;
    7998           0 :     default: pari_err_TYPE(f, vN);
    7999             :   }
    8000          42 :   vecsmall_sort(vN); return vN;
    8001             : }
    8002             : GEN
    8003          14 : mfsearch(GEN NK, GEN V, long space)
    8004             : {
    8005          14 :   pari_sp av = avma;
    8006             :   GEN F, gk, NbyD, vN;
    8007             :   long n, nk, dk, parity, nV, i, lvN;
    8008             : 
    8009          14 :   if (typ(NK) != t_VEC || lg(NK) != 3) pari_err_TYPE("mfsearch", NK);
    8010          14 :   gk = gel(NK,2);
    8011          14 :   if (typ(gmul2n(gk, 1)) != t_INT) pari_err_TYPE("mfsearch [k]", gk);
    8012          14 :   switch(typ(V))
    8013             :   {
    8014          14 :     case t_VEC: V = shallowtrans(V);
    8015          14 :     case t_COL: break;
    8016           0 :     default: pari_err_TYPE("mfsearch [V]", V);
    8017             :   }
    8018          14 :   vN = search_levels(gel(NK,1), "mfsearch [N]");
    8019          14 :   lvN = lg(vN);
    8020             : 
    8021          14 :   Qtoss(gk, &nk,&dk);
    8022          14 :   parity = (dk == 1 && odd(nk)) ? -1 : 1;
    8023          14 :   nV = lg(V)-2;
    8024          14 :   F = cgetg(1, t_VEC);
    8025          14 :   NbyD = const_vec(vN[lvN-1], cgetg(1,t_VECSMALL));
    8026         231 :   for (n = 1; n < lvN; n++)
    8027             :   {
    8028         217 :     long N = vN[n];
    8029             :     GEN L;
    8030         217 :     if (N <= 0 || (dk == 2 && (N & 3))) continue;
    8031         217 :     L = findd(N, parity);
    8032         630 :     for (i = 1; i < lg(L); i++)
    8033             :     {
    8034         413 :       GEN mf, M, CO, gD = gel(L,i);
    8035         413 :       GEN *ND = (GEN*)NbyD + itou(gD); /* points to NbyD[|D|] */
    8036             : 
    8037         413 :       if (seenD(N, *ND)) continue;
    8038         413 :       mf = mfinit_Nndkchi(N, nk, dk, get_mfchar(gD), space, 1);
    8039         413 :       M = mfcoefs_mf(mf, nV, 1);
    8040         413 :       CO = inverseimage(M, V); if (lg(CO) == 1) continue;
    8041             : 
    8042          42 :       F = vec_append(F, mflinear(mf,CO));
    8043          42 :       *ND = vecsmall_append(*ND, N); /* add to NbyD[|D|] */
    8044             :     }
    8045             :   }
    8046          14 :   return gerepilecopy(av, F);
    8047             : }
    8048             : 
    8049             : static GEN
    8050         882 : search_from_split(GEN mf, GEN vap, GEN vlp)
    8051             : {
    8052         882 :   pari_sp av = avma;
    8053         882 :   long lvlp = lg(vlp), j, jv, l1;
    8054         882 :   GEN v, NK, S1, S, M = NULL;
    8055             : 
    8056         882 :   S1 = gel(split_i(mf, 1, 0), 1); /* rational newforms */
    8057         882 :   l1 = lg(S1);
    8058         882 :   if (l1 == 1) return gc_NULL(av);
    8059         448 :   v = cgetg(l1, t_VEC);
    8060         448 :   S = MF_get_S(mf);
    8061         448 :   NK = mf_get_NK(gel(S,1));
    8062         448 :   if (lvlp > 1) M = rowpermute(mfcoefs_mf(mf, vlp[lvlp-1], 1), vlp);
    8063         966 :   for (j = jv = 1; j < l1; j++)
    8064             :   {
    8065         518 :     GEN vF = gel(S1,j);
    8066             :     long t;
    8067         651 :     for (t = lvlp-1; t > 0; t--)
    8068             :     { /* lhs = vlp[j]-th coefficient of eigenform */
    8069         595 :       GEN rhs = gel(vap,t), lhs = RgMrow_RgC_mul(M, vF, t);
    8070         595 :       if (!gequal(lhs, rhs)) break;
    8071             :     }
    8072         518 :     if (!t) gel(v,jv++) = mflinear_i(NK,S,vF);
    8073             :   }
    8074         448 :   if (jv == 1) return gc_NULL(av);
    8075          56 :   setlg(v,jv); return v;
    8076             : }
    8077             : GEN
    8078          28 : mfeigensearch(GEN NK, GEN AP)
    8079             : {
    8080          28 :   pari_sp av = avma;
    8081          28 :   GEN k, vN, vap, vlp, vres = cgetg(1, t_VEC), D;
    8082             :   long n, lvN, i, l, even;
    8083             : 
    8084          28 :   if (!AP) l = 1;
    8085             :   else
    8086             :   {
    8087          28 :     l = lg(AP);
    8088          28 :     if (typ(AP) != t_VEC) pari_err_TYPE("mfeigensearch",AP);
    8089             :   }
    8090          28 :   vap = cgetg(l, t_VEC);
    8091          28 :   vlp = cgetg(l, t_VECSMALL);
    8092          28 :   if (l > 1)
    8093             :   {
    8094          28 :     GEN perm = indexvecsort(AP, mkvecsmall(1));
    8095          77 :     for (i = 1; i < l; i++)
    8096             :     {
    8097          49 :       GEN v = gel(AP,perm[i]), gp, ap;
    8098          49 :       if (typ(v) != t_VEC || lg(v) != 3) pari_err_TYPE("mfeigensearch", AP);
    8099          49 :       gp = gel(v,1);
    8100          49 :       ap = gel(v,2);
    8101          49 :       if (typ(gp) != t_INT || (typ(ap) != t_INT && typ(ap) != t_INTMOD))
    8102           0 :         pari_err_TYPE("mfeigensearch", AP);
    8103          49 :       gel(vap,i) = ap;
    8104          49 :       vlp[i] = itos(gp)+1; if (vlp[i] < 0) pari_err_TYPE("mfeigensearch", AP);
    8105             :     }
    8106             :   }
    8107          28 :   l = lg(NK);
    8108          28 :   if (typ(NK) != t_VEC || l != 3) pari_err_TYPE("mfeigensearch",NK);
    8109          28 :   k = gel(NK,2);
    8110          28 :   vN = search_levels(gel(NK,1), "mfeigensearch [N]");
    8111          28 :   lvN = lg(vN);
    8112          28 :   vecsmall_sort(vlp);
    8113          28 :   even = !mpodd(k);
    8114         966 :   for (n = 1; n < lvN; n++)
    8115             :   {
    8116         938 :     pari_sp av2 = avma;
    8117             :     GEN mf, L;
    8118         938 :     long N = vN[n];
    8119         938 :     if (even) D = gen_1;
    8120             :     else
    8121             :     {
    8122         112 :       long r = (N&3L);
    8123         112 :       if (r == 1 || r == 2) continue;
    8124          56 :       D = stoi( corediscs(-N, NULL) ); /* < 0 */
    8125             :     }
    8126         882 :     mf = mfinit_i(mkvec3(utoipos(N), k, D), mf_NEW);
    8127         882 :     L = search_from_split(mf, vap, vlp);
    8128         882 :     if (L) vres = shallowconcat(vres, L); else set_avma(av2);
    8129             :   }
    8130          28 :   return gerepilecopy(av, vres);
    8131             : }
    8132             : 
    8133             : /* tf_{N,k}(n) */
    8134             : static GEN
    8135     3144414 : mfnewtracecache(long N, long k, long n, cachenew_t *cache)
    8136             : {
    8137     3144414 :   GEN C = NULL, S;
    8138             :   long lcache;
    8139     3144414 :   if (!n) return gen_0;
    8140     3042816 :   S = gel(cache->vnew,N);
    8141     3042816 :   lcache = lg(S);
    8142     3042816 :   if (n < lcache) C = gel(S, n);
    8143     3042816 :   if (C) cache->newHIT++;
    8144     1860593 :   else C = mfnewtrace_i(N,k,n,cache);
    8145     3042816 :   cache->newTOTAL++;
    8146     3042816 :   if (n < lcache) gel(S,n) = C;
    8147     3042816 :   return C;
    8148             : }
    8149             : 
    8150             : static long
    8151        1386 : mfdim_Nkchi(long N, long k, GEN CHI, long space)
    8152             : {
    8153        1386 :   if (k < 0 || badchar(N,k,CHI)) return 0;
    8154        1085 :   if (k == 0)
    8155          35 :     return mfcharistrivial(CHI) && !space_is_cusp(space)? 1: 0;
    8156        1050 :   switch(space)
    8157             :   {
    8158         238 :     case mf_NEW: return mfnewdim(N,k,CHI);
    8159         196 :     case mf_CUSP:return mfcuspdim(N,k,CHI);
    8160         168 :     case mf_OLD: return mfolddim(N,k,CHI);
    8161         217 :     case mf_FULL:return mffulldim(N,k,CHI);
    8162         231 :     case mf_EISEN: return mfeisensteindim(N,k,CHI);
    8163           0 :     default: pari_err_FLAG("mfdim");
    8164             :   }
    8165             :   return 0;/*LCOV_EXCL_LINE*/
    8166             : }
    8167             : static long
    8168        2114 : mfwt1dimsum(long N, long space)
    8169             : {
    8170        2114 :   switch(space)
    8171             :   {
    8172        1050 :     case mf_NEW:  return mfwt1newdimsum(N);
    8173        1057 :     case mf_CUSP: return mfwt1cuspdimsum(N);
    8174           7 :     case mf_OLD:  return mfwt1olddimsum(N);
    8175             :   }
    8176           0 :   pari_err_FLAG("mfdim");
    8177             :   return 0; /*LCOV_EXCL_LINE*/
    8178             : }
    8179             : /* mfdim for k = nk/dk */
    8180             : static long
    8181       44744 : mfdim_Nndkchi(long N, long nk, long dk, GEN CHI, long space)
    8182       43463 : { return (dk == 2)? mf2dim_Nkchi(N, nk >> 1, CHI, space)
    8183       88207 :                   : mfdim_Nkchi(N, nk, CHI, space); }
    8184             : /* FIXME: use direct dim Gamma1(N) formula, don't compute individual spaces */
    8185             : static long
    8186         252 : mfwtkdimsum(long N, long k, long dk, long space)
    8187             : {
    8188         252 :   GEN w = mfchars(N, k, dk, NULL);
    8189         252 :   long i, j, D = 0, l = lg(w);
    8190        1239 :   for (i = j = 1; i < l; i++)
    8191             :   {
    8192         987 :     GEN CHI = gel(w,i);
    8193         987 :     long d = mfdim_Nndkchi(N,k,dk,CHI,space);
    8194         987 :     if (d) D += d * myeulerphiu(mfcharorder(CHI));
    8195             :   }
    8196         252 :   return D;
    8197             : }
    8198             : static GEN
    8199         105 : mfwt1dims(long N, GEN vCHI, long space)
    8200             : {
    8201         105 :   GEN D = NULL;
    8202         105 :   switch(space)
    8203             :   {
    8204          56 :     case mf_NEW: D = mfwt1newdimall(N, vCHI); break;
    8205          21 :     case mf_CUSP:D = mfwt1cuspdimall(N, vCHI); break;
    8206          28 :     case mf_OLD: D = mfwt1olddimall(N, vCHI); break;
    8207           0 :     default: pari_err_FLAG("mfdim");
    8208             :   }
    8209         105 :   return D;
    8210             : }
    8211             : static GEN
    8212        2961 : mfwtkdims(long N, long k, long dk, GEN vCHI, long space)
    8213             : {
    8214        2961 :   GEN D, w = mfchars(N, k, dk, vCHI);
    8215        2961 :   long i, j, l = lg(w);
    8216        2961 :   D = cgetg(l, t_VEC);
    8217       46592 :   for (i = j = 1; i < l; i++)
    8218             :   {
    8219       43631 :     GEN CHI = gel(w,i);
    8220       43631 :     long d = mfdim_Nndkchi(N,k,dk,CHI,space);
    8221       43631 :     if (vCHI)
    8222         574 :       gel(D, j++) = mkvec2s(d, 0);
    8223       43057 :     else if (d)
    8224        2520 :       gel(D, j++) = fmt_dim(CHI, d, 0);
    8225             :   }
    8226        2961 :   setlg(D,j); return D;
    8227             : }
    8228             : GEN
    8229        5719 : mfdim(GEN NK, long space)
    8230             : {
    8231        5719 :   pari_sp av = avma;
    8232             :   long N, k, dk, joker;
    8233             :   GEN CHI, mf;
    8234        5719 :   if ((mf = checkMF_i(NK))) return utoi(MF_get_dim(mf));
    8235        5586 :   checkNK2(NK, &N, &k, &dk, &CHI, 2);
    8236        5586 :   if (!CHI) joker = 1;
    8237             :   else
    8238        2611 :     switch(typ(CHI))
    8239             :     {
    8240        2373 :       case t_INT: joker = 2; break;
    8241         112 :       case t_COL: joker = 3; break;
    8242         126 :       default: joker = 0; break;
    8243             :     }
    8244        5586 :   if (joker)
    8245             :   {
    8246             :     long d;
    8247             :     GEN D;
    8248        5460 :     if (k < 0) switch(joker)
    8249             :     {
    8250           0 :       case 1: return cgetg(1,t_VEC);
    8251           7 :       case 2: return gen_0;
    8252           0 :       case 3: return mfdim0all(CHI);
    8253             :     }
    8254        5453 :     if (k == 0)
    8255             :     {
    8256          28 :       if (space_is_cusp(space)) switch(joker)
    8257             :       {
    8258           7 :         case 1: return cgetg(1,t_VEC);
    8259           0 :         case 2: return gen_0;
    8260           7 :         case 3: return mfdim0all(CHI);
    8261             :       }
    8262          14 :       switch(joker)
    8263             :       {
    8264             :         long i, l;
    8265           7 :         case 1: retmkvec(fmt_dim(mfchartrivial(),0,0));
    8266           0 :         case 2: return gen_1;
    8267           7 :         case 3: l = lg(CHI); D = cgetg(l,t_VEC);
    8268          35 :                 for (i = 1; i < l; i++)
    8269             :                 {
    8270          28 :                   long t = mfcharistrivial(gel(CHI,i));
    8271          28 :                   gel(D,i) = mkvec2(t? gen_1: gen_0, gen_0);
    8272             :                 }
    8273           7 :                 return D;
    8274             :       }
    8275             :     }
    8276        5425 :     if (dk == 1 && k == 1 && space != mf_EISEN)
    8277         105 :     {
    8278        2219 :       long fix = 0, space0 = space;
    8279        2219 :       if (space == mf_FULL) space = mf_CUSP; /* remove Eisenstein part */
    8280        2219 :       if (joker == 2)
    8281             :       {
    8282        2114 :         d = mfwt1dimsum(N, space);
    8283        2114 :         if (space0 == mf_FULL) d += mfwtkdimsum(N,k,dk,mf_EISEN);/*add it back*/
    8284        2114 :         set_avma(av); return utoi(d);
    8285             :       }
    8286             :       /* must initialize explicitly: trivial spaces for E_k/S_k differ */
    8287         105 :       if (space0 == mf_FULL)
    8288             :       {
    8289           7 :         if (!CHI) fix = 1; /* must remove 0 spaces */
    8290           7 :         CHI = mfchars(N, k, dk, CHI);
    8291             :       }
    8292         105 :       D = mfwt1dims(N, CHI, space);
    8293         105 :       if (space0 == mf_FULL)
    8294             :       {
    8295           7 :         GEN D2 = mfwtkdims(N, k, dk, CHI, mf_EISEN);
    8296           7 :         D = merge_dims(D, D2, fix? CHI: NULL);
    8297             :       }
    8298             :     }
    8299             :     else
    8300             :     {
    8301        3206 :       if (joker==2) { d = mfwtkdimsum(N,k,dk,space); set_avma(av); return utoi(d); }
    8302        2954 :       D = mfwtkdims(N, k, dk, CHI, space);
    8303             :     }
    8304        3059 :     if (!CHI) return gerepileupto(av, vecsort(D, mkvecsmall(1)));
    8305         105 :     return gerepilecopy(av, D);
    8306             :   }
    8307         126 :   return utoi( mfdim_Nndkchi(N, k, dk, CHI, space) );
    8308             : }
    8309             : 
    8310             : GEN
    8311         308 : mfbasis(GEN NK, long space)
    8312             : {
    8313         308 :   pari_sp av = avma;
    8314             :   long N, k, dk;
    8315             :   GEN mf, CHI;
    8316         308 :   if ((mf = checkMF_i(NK))) return concat(gel(mf,2), gel(mf,3));
    8317           7 :   checkNK2(NK, &N, &k, &dk, &CHI, 0);
    8318           7 :   if (dk == 2) return gerepilecopy(av, mf2basis(N, k>>1, CHI, space));
    8319           7 :   mf = mfinit_Nkchi(N, k, CHI, space, 1);
    8320           7 :   return gerepilecopy(av, MF_get_basis(mf));
    8321             : }
    8322             : 
    8323             : static GEN
    8324          28 : deg1ser_shallow(GEN a1, GEN a0, long v, long e)
    8325          28 : { return RgX_to_ser(deg1pol_shallow(a1, a0, v), e+2); }
    8326             : /* r / x + O(1) */
    8327             : static GEN
    8328          28 : simple_pole(GEN r)
    8329             : {
    8330          28 :   GEN S = deg1ser_shallow(gen_0, r, 0, 1);
    8331          28 :   setvalp(S, -1); return S;
    8332             : }
    8333             : 
    8334             : /* F form, E embedding; mfa = mfatkininit or root number (eigenform case) */
    8335             : static GEN
    8336         105 : mflfuncreate(GEN mfa, GEN F, GEN E, GEN N, GEN gk)
    8337             : {
    8338         105 :   GEN LF = cgetg(8,t_VEC), polar = cgetg(1,t_COL), eps;
    8339         105 :   long k = itou(gk);
    8340         105 :   gel(LF,1) = lfuntag(t_LFUN_MFCLOS, mkvec3(F,E,gen_1));
    8341         105 :   if (typ(mfa) != t_VEC)
    8342          84 :     eps = mfa; /* cuspidal eigenform: root number; no poles */
    8343             :   else
    8344             :   { /* mfatkininit */
    8345          21 :     GEN a0, b0, vF, vG, G = NULL, M = gdiv(gel(mfa,2), gel(mfa,3)), mf = gel(mfa,4);
    8346          21 :     vF = mftobasis_i(mf, F);
    8347          21 :     vG = RgM_RgC_mul(M, vF);
    8348          21 :     if (gequal(vF,vG)) eps = gen_1;
    8349           7 :     else if (gequal(vF,gneg(vG))) eps = gen_m1;
    8350             :     else
    8351             :     { /* not self-dual */
    8352           7 :       eps = NULL;
    8353           7 :       G = mfatkin(mfa, F);
    8354           7 :       gel(LF,2) = lfuntag(t_LFUN_MFCLOS, mkvec3(G,E,ginv(gel(mfa,3))));
    8355           7 :       gel(LF,6) = powIs(k);
    8356             :     }
    8357             :     /* polar part */
    8358          21 :     a0 = mfcoef(F,0);
    8359          21 :     b0 = eps? gmul(eps,a0): mfcoef(G,0);
    8360          21 :     if (!gequal0(b0))
    8361             :     {
    8362          14 :       b0 = mulcxpowIs(gmul2n(b0,1), k);
    8363          14 :       polar = vec_append(polar, mkvec2(gk, simple_pole(b0)));
    8364             :     }
    8365          21 :     if (!gequal0(a0))
    8366             :     {
    8367          14 :       a0 = gneg(gmul2n(a0,1));
    8368          14 :       polar = vec_append(polar, mkvec2(gen_0, simple_pole(a0)));
    8369             :     }
    8370             :   }
    8371         105 :   if (eps) /* self-dual */
    8372             :   {
    8373          98 :     gel(LF,2) = mfcharorder(mf_get_CHI(F)) <= 2? gen_0: gen_1;
    8374          98 :     gel(LF,6) = mulcxpowIs(eps,k);
    8375             :   }
    8376         105 :   gel(LF,3) = mkvec2(gen_0, gen_1);
    8377         105 :   gel(LF,4) = gk;
    8378         105 :   gel(LF,5) = N;
    8379         105 :   if (lg(polar) == 1) setlg(LF,7); else gel(LF,7) = polar;
    8380         105 :   return LF;
    8381             : }
    8382             : static GEN
    8383          91 : mflfuncreateall(long sd, GEN mfa, GEN F, GEN vE, GEN gN, GEN gk)
    8384             : {
    8385          91 :   long i, l = lg(vE);
    8386          91 :   GEN L = cgetg(l, t_VEC);
    8387         196 :   for (i = 1; i < l; i++)
    8388         105 :     gel(L,i) = mflfuncreate(sd? gel(mfa,i): mfa, F, gel(vE,i), gN, gk);
    8389          91 :   return L;
    8390             : }
    8391             : GEN
    8392          42 : lfunmf(GEN mf, GEN F, long bitprec)
    8393             : {
    8394          42 :   pari_sp av = avma;
    8395          42 :   long i, l, prec = nbits2prec(bitprec);
    8396             :   GEN L, gk, gN;
    8397          42 :   mf = checkMF(mf);
    8398          42 :   gk = MF_get_gk(mf);
    8399          42 :   gN = MF_get_gN(mf);
    8400          42 :   if (typ(gk)!=t_INT) pari_err_IMPL("half-integral weight");
    8401          42 :   if (F)
    8402             :   {
    8403             :     GEN v;
    8404          35 :     long s = MF_get_space(mf);
    8405          35 :     if (!checkmf_i(F)) pari_err_TYPE("lfunmf", F);
    8406          35 :     if (!mfisinspace_i(mf, F)) err_space(F);
    8407          35 :     L = NULL;
    8408          35 :     if ((s == mf_NEW || s == mf_CUSP || s == mf_FULL)
    8409          21 :         && gequal(mfcoefs_i(F,1,1), mkvec2(gen_0,gen_1)))
    8410             :     { /* check if eigenform */
    8411          14 :       GEN vP, vF, b = mftobasis_i(mf, F);
    8412          14 :       long lF, d = degpol(mf_get_field(F));
    8413          14 :       v = mfsplit(mf, d, 0);
    8414          14 :       vF = gel(v,1);
    8415          14 :       vP = gel(v,2); lF = lg(vF);
    8416          14 :       for (i = 1; i < lF; i++)
    8417          14 :         if (degpol(gel(vP,i)) == d && gequal(gel(vF,i), b))
    8418             :         {
    8419          14 :           GEN vE = mfgetembed(F, prec);
    8420          14 :           GEN Z = mffrickeeigen_i(mf, mkvec(b), mkvec(vE), prec);
    8421          14 :           L = mflfuncreateall(1, gel(Z,1), F, vE, gN, gk);
    8422          14 :           break;
    8423             :         }
    8424             :     }
    8425          35 :     if (!L)
    8426             :     { /* not an eigenform: costly general case */
    8427          21 :       GEN mfa = mfatkininit_i(mf, itou(gN), 1, prec);
    8428          21 :       L = mflfuncreateall(0,mfa, F, mfgetembed(F,prec), gN, gk);
    8429             :     }
    8430          35 :     if (lg(L) == 2) L = gel(L,1);
    8431             :   }
    8432             :   else
    8433             :   {
    8434           7 :     GEN M = mfeigenbasis(mf), vE = mfeigenembed(mf, prec);
    8435           7 :     GEN v = mffrickeeigen(mf, vE, prec);
    8436           7 :     l = lg(vE); L = cgetg(l, t_VEC);
    8437          63 :     for (i = 1; i < l; i++)
    8438          56 :       gel(L,i) = mflfuncreateall(1,gel(v,i), gel(M,i), gel(vE,i), gN, gk);
    8439             :   }
    8440          42 :   return gerepilecopy(av, L);
    8441             : }
    8442             : 
    8443             : GEN
    8444          21 : mffromell(GEN E)
    8445             : {
    8446          21 :   pari_sp av = avma;
    8447             :   GEN mf, F, z, v, S;
    8448             :   long N, i, l;
    8449             : 
    8450          21 :   checkell(E);
    8451          21 :   if (ell_get_type(E) != t_ELL_Q) pari_err_TYPE("mfffromell [E not over Q]", E);
    8452          21 :   N = itos(ellQ_get_N(E));
    8453          21 :   mf = mfinit_i(mkvec2(utoi(N), gen_2), mf_NEW);
    8454          21 :   v = split_i(mf, 1, 0);
    8455          21 :   S = gel(v,1); l = lg(S); /* rational newforms */
    8456          21 :   F = tag(t_MF_ELL, mkNK(N,2,mfchartrivial()), E);
    8457          21 :   z = mftobasis_i(mf, F);
    8458          21 :   for(i = 1; i < l; i++)
    8459          21 :     if (gequal(z, gel(S,i))) break;
    8460          21 :   if (i == l) pari_err_BUG("mffromell [E is not modular]");
    8461          21 :   return gerepilecopy(av, mkvec3(mf, F, z));
    8462             : }
    8463             : 
    8464             : /* returns -1 if not, degree otherwise */
    8465             : long
    8466          98 : polishomogeneous(GEN P)
    8467             : {
    8468             :   long i, D, l;
    8469          98 :   if (typ(P) != t_POL) return 0;
    8470          49 :   D = -1; l = lg(P);
    8471         231 :   for (i = 2; i < l; i++)
    8472             :   {
    8473         182 :     GEN c = gel(P,i);
    8474             :     long d;
    8475         182 :     if (gequal0(c)) continue;
    8476          84 :     d = polishomogeneous(c);
    8477          84 :     if (d < 0) return -1;
    8478          84 :     if (D < 0) D = d + i-2; else if (D != d + i-2) return -1;
    8479             :   }
    8480          49 :   return D;
    8481             : }
    8482             : 
    8483             : /* P a t_POL, 1 if spherical, 0 otherwise */
    8484             : static int
    8485          14 : RgX_isspherical(GEN Qi, GEN P)
    8486             : {
    8487          14 :   pari_sp av = avma;
    8488             :   GEN va, S;
    8489             :   long lva, i, j;
    8490          14 :   if (degpol(P) <= 1) return 1;
    8491          14 :   va = variables_vecsmall(P); lva = lg(va);
    8492          14 :   if (lva > lg(Qi)) pari_err(e_MISC, "too many variables in mffromqf");
    8493          14 :   S = gen_0;
    8494          42 :   for (j = 1; j < lva; j++)
    8495             :   {
    8496          28 :     GEN col = gel(Qi, j), Pj = deriv(P, va[j]);
    8497          70 :     for (i = 1; i <= j; i++)
    8498             :     {
    8499          42 :       GEN coe = gel(col, i);
    8500          42 :       if (i != j) coe = gmul2n(coe, 1);
    8501          42 :       if (!gequal0(coe)) S = gadd(S, gmul(coe, deriv(Pj, va[i])));
    8502             :     }
    8503             :   }
    8504          14 :   return gc_bool(av, gequal0(S));
    8505             : }
    8506             : 
    8507             : static GEN
    8508          28 : c_QFsimple_i(long n, GEN Q, GEN P)
    8509             : {
    8510          28 :   pari_sp av = avma;
    8511          28 :   GEN V, v = qfrep0(Q, utoi(n), 1);
    8512          28 :   long i, l = lg(v);
    8513          28 :   V = cgetg(l+1, t_VEC);
    8514          49 :   if (!P || equali1(P))
    8515             :   {
    8516          21 :     gel(V,1) = gen_1;
    8517          21 :     for (i = 2; i <= l; i++) gel(V,i) = utoi(v[i-1] << 1);
    8518             :   }
    8519             :   else
    8520             :   {
    8521           7 :     gel(V,1) = gcopy(P);
    8522           7 :     for (i = 2; i <= l; i++) gel(V,i) = gmulgs(P, v[i-1] << 1);
    8523             :   }
    8524          28 :   return gerepileupto(av, V);
    8525             : }
    8526             : static GEN
    8527          35 : c_QF_i(long n, GEN Q, GEN P)
    8528             : {
    8529          35 :   pari_sp av = avma;
    8530             :   GEN V, v, va;
    8531             :   long i, lva, lq, l;
    8532          35 :   if (!P || typ(P) != t_POL) return c_QFsimple_i(n, Q, P);
    8533           7 :   v = gel(minim(Q, utoi(2*n), NULL), 3);
    8534           7 :   va = variables_vec(P); lq = lg(Q) - 1; lva = lg(va) - 1;
    8535           7 :   V = zerovec(n + 1); l = lg(v);
    8536          35 :   for (i = 1; i < l; i++)
    8537             :   {
    8538          28 :     GEN X = gel(v,i);
    8539          28 :     long ind = (itos(qfeval0(Q, X, NULL)) >> 1) + 1;
    8540          28 :     if (lq > lva) X = vecslice(X, 1, lva);
    8541          28 :     gel(V, ind) = gadd(gel(V, ind), gsubstvec(P, va, X));
    8542             :   }
    8543           7 :   return gerepilecopy(av, gmul2n(V, 1));
    8544             : }
    8545             : 
    8546             : GEN
    8547          42 : mffromqf(GEN Q, GEN P)
    8548             : {
    8549          42 :   pari_sp av = avma;
    8550             :   GEN G, Qi, F, D, N, mf, v, gk, gwt, chi;
    8551             :   long m, d, space;
    8552          42 :   if (typ(Q) != t_MAT) pari_err_TYPE("mffromqf", Q);
    8553          42 :   if (!RgM_is_ZM(Q) || !qfiseven(Q))
    8554           0 :     pari_err_TYPE("mffromqf [not integral or even]", Q);
    8555          42 :   m = lg(Q)-1;
    8556          42 :   gk = sstoQ(m, 2);
    8557          42 :   Qi = ZM_inv(Q, &N);
    8558          42 :   if (!qfiseven(Qi)) N = shifti(N, 1);
    8559          42 :   d = 0;
    8560          42 :   if (!P || gequal1(P)) P = NULL;
    8561             :   else
    8562             :   {
    8563          21 :     P = simplify_shallow(P);
    8564          21 :     if (typ(P) == t_POL)
    8565             :     {
    8566          14 :       d = polishomogeneous(P);
    8567          14 :       if (d < 0) pari_err_TYPE("mffromqf [not homogeneous t_POL]", P);
    8568          14 :       if (!RgX_isspherical(Qi, P))
    8569           0 :         pari_err_TYPE("mffromqf [not a spherical t_POL]", P);
    8570             :     }
    8571             :   }
    8572          42 :   D = ZM_det(Q);
    8573          42 :   if (typ(gk) == t_INT) { if (mpodd(gk)) D = negi(D); } else D = shifti(D, 1);
    8574          42 :   space = d > 0 ? mf_CUSP : mf_FULL;
    8575          42 :   G = znstar0(N,1);
    8576          42 :   chi = mkvec2(G, znchar_quad(G,D));
    8577          42 :   gwt = gaddgs(gk, d);
    8578          42 :   mf = mfinit(mkvec3(N, gwt, chi), space);
    8579          42 :   if (odd(d))
    8580             :   {
    8581           7 :     F = mftrivial();
    8582           7 :     v = zerocol(MF_get_dim(mf));
    8583             :   }
    8584             :   else
    8585             :   {
    8586          35 :     F = c_QF_i(mfsturm(mf), Q, P);
    8587          35 :     v = mftobasis_i(mf, F);
    8588          35 :     F = mflinear(mf, v);
    8589             :   }
    8590          42 :   return gerepilecopy(av, mkvec3(mf, F, v));
    8591             : }
    8592             : 
    8593             : /***********************************************************************/
    8594             : /*                          Eisenstein Series                          */
    8595             : /***********************************************************************/
    8596             : /* \sigma_{k-1}(\chi,n) */
    8597             : static GEN
    8598       22001 : sigchi(long k, GEN CHI, long n)
    8599             : {
    8600       22001 :   pari_sp av = avma;
    8601       22001 :   GEN S = gen_1, D = mydivisorsu(u_ppo(n,mfcharmodulus(CHI)));
    8602       22001 :   long i, l = lg(D), ord = mfcharorder(CHI), vt = varn(mfcharpol(CHI));
    8603       77574 :   for (i = 2; i < l; i++) /* skip D[1] = 1 */
    8604             :   {
    8605       55573 :     long d = D[i], a = mfcharevalord(CHI, d, ord);
    8606       55573 :     S = gadd(S, mygmodulo_lift(a, ord, powuu(d, k-1), vt));
    8607             :   }
    8608       22001 :   return gerepileupto(av,S);
    8609             : }
    8610             : 
    8611             : /* write n = n0*n1*n2, (n0,N1*N2) = 1, n1 | N1^oo, n2 | N2^oo;
    8612             :  * return NULL if (n,N1,N2) > 1, else return factoru(n0) */
    8613             : static GEN
    8614      260071 : sigchi2_dec(long n, long N1, long N2, long *pn1, long *pn2)
    8615             : {
    8616      260071 :   GEN P0, E0, P, E, fa = myfactoru(n);
    8617             :   long i, j, l;
    8618      260071 :   *pn1 = 1;
    8619      260071 :   *pn2 = 1;
    8620      260071 :   if (N1 == 1 && N2 == 1) return fa;
    8621      246575 :   P = gel(fa,1); l = lg(P);
    8622      246575 :   E = gel(fa,2);
    8623      246575 :   P0 = cgetg(l, t_VECSMALL);
    8624      246575 :   E0 = cgetg(l, t_VECSMALL);
    8625      570745 :   for (i = j = 1; i < l; i++)
    8626             :   {
    8627      346248 :     long p = P[i], e = E[i];
    8628      346248 :     if (N1 % p == 0)
    8629             :     {
    8630       37653 :       if (N2 % p == 0) return NULL;
    8631       15575 :       *pn1 *= upowuu(p,e);
    8632             :     }
    8633      308595 :     else if (N2 % p == 0)
    8634       58835 :       *pn2 *= upowuu(p,e);
    8635      249760 :     else { P0[j] = p; E0[j] = e; j++; }
    8636             :   }
    8637      224497 :   setlg(P0, j);
    8638      224497 :   setlg(E0, j); return mkvec2(P0,E0);
    8639             : }
    8640             : 
    8641             : /* sigma_{k-1}(\chi_1,\chi_2,n), ord multiple of lcm(ord(CHI1),ord(CHI2)) */
    8642             : static GEN
    8643      212114 : sigchi2(long k, GEN CHI1, GEN CHI2, long n, long ord)
    8644             : {
    8645      212114 :   pari_sp av = avma;
    8646      212114 :   GEN S = gen_0, D;
    8647      212114 :   long i, l, n1, n2, vt, N1 = mfcharmodulus(CHI1), N2 = mfcharmodulus(CHI2);
    8648      212114 :   D = sigchi2_dec(n, N1, N2, &n1, &n2); if (!D) { set_avma(av); return S; }
    8649      194404 :   D = divisorsu_fact(D); l = lg(D);
    8650      194404 :   vt = varn(mfcharpol(CHI1));
    8651      770329 :   for (i = 1; i < l; i++)
    8652             :   { /* S += d^(k-1)*chi1(d)*chi2(n/d) */
    8653      575925 :     long a, d = n2*D[i], nd = n1*D[l-i]; /* (d,N1)=1; (n/d,N2) = 1 */
    8654      575925 :     a = mfcharevalord(CHI1, d, ord) + mfcharevalord(CHI2, nd, ord);
    8655      575925 :     if (a >= ord) a -= ord;
    8656      575925 :     S = gadd(S, mygmodulo_lift(a, ord, powuu(d, k-1), vt));
    8657             :   }
    8658      194404 :   return gerepileupto(av, S);
    8659             : }
    8660             : 
    8661             : /**************************************************************************/
    8662             : /**           Dirichlet characters with precomputed values               **/
    8663             : /**************************************************************************/
    8664             : /* CHI mfchar */
    8665             : static GEN
    8666       11375 : mfcharcxinit(GEN CHI, long prec)
    8667             : {
    8668       11375 :   GEN G = gel(CHI,1), chi = gel(CHI,2), z, V;
    8669       11375 :   GEN v = ncharvecexpo(G, znconrey_normalized(G,chi));
    8670       11375 :   long n, l = lg(v), o = mfcharorder(CHI);
    8671       11375 :   V = cgetg(l, t_VEC);
    8672       11375 :   z = grootsof1(o, prec); /* Mod(t, Phi_o(t)) -> e(1/o) */
    8673       11375 :   for (n = 1; n < l; n++) gel(V,n) = v[n] < 0? gen_0: gel(z, v[n]+1);
    8674       11375 :   return mkvecn(6, G, chi, gmfcharorder(CHI), v, V, mfcharpol(CHI));
    8675             : }
    8676             : /* v a "CHIvec" */
    8677             : static long
    8678    20373346 : CHIvec_N(GEN v) { return itou(znstar_get_N(gel(v,1))); }
    8679             : static GEN
    8680       11606 : CHIvec_CHI(GEN v)
    8681       11606 : { return mkvec4(gel(v,1), gel(v,2), gel(v,3), gel(v,6)); }
    8682             : /* character order */
    8683             : static long
    8684       32487 : CHIvec_ord(GEN v) { return itou(gel(v,3)); }
    8685             : /* character exponents, i.e. t such that chi(n) = e(t) */
    8686             : static GEN
    8687      333494 : CHIvec_expo(GEN v) { return gel(v,4); }
    8688             : /* character values chi(n) */
    8689             : static GEN
    8690    19870921 : CHIvec_val(GEN v) { return gel(v,5); }
    8691             : /* CHI(n) */
    8692             : static GEN
    8693    19857817 : mychareval(GEN v, long n)
    8694             : {
    8695    19857817 :   long N = CHIvec_N(v), ind = n%N;
    8696    19857817 :   if (ind <= 0) ind += N;
    8697    19857817 :   return gel(CHIvec_val(v), ind);
    8698             : }
    8699             : /* return c such that CHI(n) = e(c / ordz) or -1 if (n,N) > 1 */
    8700             : static long
    8701      333494 : mycharexpo(GEN v, long n)
    8702             : {
    8703      333494 :   long N = CHIvec_N(v), ind = n%N;
    8704      333494 :   if (ind <= 0) ind += N;
    8705      333494 :   return CHIvec_expo(v)[ind];
    8706             : }
    8707             : /* faster than mfcharparity */
    8708             : static long
    8709       37338 : CHIvec_parity(GEN v) { return mycharexpo(v,-1) ? -1: 1; }
    8710             : /**************************************************************************/
    8711             : 
    8712             : static ulong
    8713       47957 : sigchi2_Fl(long k, GEN CHI1vec, GEN CHI2vec, long n, GEN vz, ulong p)
    8714             : {
    8715       47957 :   pari_sp av = avma;
    8716       47957 :   long ordz = lg(vz)-2, i, l, n1, n2;
    8717       47957 :   ulong S = 0;
    8718       47957 :   GEN D = sigchi2_dec(n, CHIvec_N(CHI1vec), CHIvec_N(CHI2vec), &n1, &n2);
    8719       47957 :   if (!D) return gc_ulong(av,S);
    8720       43589 :   D = divisorsu_fact(D);
    8721       43589 :   l = lg(D);
    8722      147294 :   for (i = 1; i < l; i++)
    8723             :   { /* S += d^(k-1)*chi1(d)*chi2(n/d) */
    8724      103705 :     long a, d = n2*D[i], nd = n1*D[l-i]; /* (d,N1)=1, (n/d,N2)=1 */
    8725      103705 :     a = mycharexpo(CHI2vec, nd) + mycharexpo(CHI1vec, d);
    8726      103705 :     if (a >= ordz) a -= ordz;
    8727      103705 :     S = Fl_add(S, mygmodulo_Fl(a, vz, Fl_powu(d,k-1,p), p), p);
    8728             :   }
    8729       43589 :   return gc_ulong(av,S);
    8730             : }
    8731             : 
    8732             : /**********************************************************************/
    8733             : /* Fourier expansions of Eisenstein series                            */
    8734             : /**********************************************************************/
    8735             : /* L(CHI,0) / 2, order(CHI) | ord != 0 */
    8736             : static GEN
    8737        1701 : charLFwt1(GEN CHI, long ord)
    8738             : {
    8739             :   GEN S;
    8740        1701 :   long r, vt, m = mfcharmodulus(CHI);
    8741             : 
    8742        1701 :   if (m == 1) return mkfrac(gen_m1,stoi(4));
    8743        1701 :   S = gen_0; vt = varn(mfcharpol(CHI));
    8744       51723 :   for (r = 1; r < m; r++)
    8745             :   { /* S += r*chi(r) */
    8746             :     long a;
    8747       50022 :     if (ugcd(m,r) != 1) continue;
    8748       38318 :     a = mfcharevalord(CHI,r,ord);
    8749       38318 :     S = gadd(S, mygmodulo_lift(a, ord, utoi(r), vt));
    8750             :   }
    8751        1701 :   return gdivgs(S, -2*m);
    8752             : }
    8753             : /* L(CHI,0) / 2, mod p */
    8754             : static ulong
    8755        1323 : charLFwt1_Fl(GEN CHIvec, GEN vz, ulong p)
    8756             : {
    8757        1323 :   long r, m = CHIvec_N(CHIvec);
    8758             :   ulong S;
    8759        1323 :   if (m == 1) return Rg_to_Fl(mkfrac(gen_m1,stoi(4)), p);
    8760        1323 :   S = 0;
    8761       64659 :   for (r = 1; r < m; r++)
    8762             :   { /* S += r*chi(r) */
    8763       63336 :     long a = mycharexpo(CHIvec,r);
    8764       63336 :     if (a < 0) continue;
    8765       61166 :     S = Fl_add(S, mygmodulo_Fl(a, vz, r, p), p);
    8766             :   }
    8767        1323 :   return Fl_div(Fl_neg(S,p), 2*m, p);
    8768             : }
    8769             : /* L(CHI,1-k) / 2, order(CHI) | ord != 0 */
    8770             : static GEN
    8771        1386 : charLFwtk(long k, GEN CHI, long ord)
    8772             : {
    8773             :   GEN S, P, dS;
    8774             :   long r, m, vt;
    8775             : 
    8776        1386 :   if (k == 1) return charLFwt1(CHI, ord);
    8777        1386 :   m = mfcharmodulus(CHI);
    8778        1386 :   if (m == 1) return gdivgs(bernfrac(k),-2*k);
    8779         791 :   S = gen_0; vt = varn(mfcharpol(CHI));
    8780         791 :   P = ZX_rescale(Q_remove_denom(bernpol(k,0), &dS), utoi(m));
    8781         791 :   dS = mul_denom(dS, stoi(-2*m*k));
    8782       10171 :   for (r = 1; r < m; r++)
    8783             :   { /* S += P(r)*chi(r) */
    8784             :     long a;
    8785        9380 :     if (ugcd(r,m) != 1) continue;
    8786        7602 :     a = mfcharevalord(CHI,r,ord);
    8787        7602 :     S = gadd(S, mygmodulo_lift(a, ord, poleval(P, utoi(r)), vt));
    8788             :   }
    8789         791 :   return gdiv(S, dS);
    8790             : }
    8791             : /* L(CHI,1-k) / 2, mod p */
    8792             : static ulong
    8793        1988 : charLFwtk_Fl(long k, GEN CHIvec, GEN vz, ulong p)
    8794             : {
    8795             :   GEN P;
    8796             :   long r, m;
    8797             :   ulong S;
    8798        1988 :   if (k == 1) return charLFwt1_Fl(CHIvec, vz, p);
    8799         665 :   m = CHIvec_N(CHIvec);
    8800         665 :   if (m == 1) return Rg_to_Fl(gdivgs(bernfrac(k),-2*k), p);
    8801         399 :   S = 0;
    8802         399 :   P = RgX_to_Flx(RgX_rescale(bernpol(k,0), utoi(m)), p);
    8803        8085 :   for (r = 1; r < m; r++)
    8804             :   { /* S += P(r)*chi(r) */
    8805        7686 :     long a = mycharexpo(CHIvec,r);
    8806        7686 :     if (a < 0) continue;
    8807        6566 :     S = Fl_add(S, mygmodulo_Fl(a, vz, Flx_eval(P,r,p), p), p);
    8808             :   }
    8809         399 :   return Fl_div(Fl_neg(S,p), 2*k*m, p);
    8810             : }
    8811             : 
    8812             : static GEN
    8813        5754 : mfeisenstein2_0(long k, GEN CHI1, GEN CHI2, long ord)
    8814             : {
    8815        5754 :   if (k == 1 && mfcharistrivial(CHI1))
    8816        1701 :     return charLFwt1(CHI2, ord);
    8817        4053 :   else if (mfcharistrivial(CHI2))
    8818        1239 :     return charLFwtk(k, CHI1, ord);
    8819        2814 :   else return gen_0;
    8820             : }
    8821             : static ulong
    8822        3290 : mfeisenstein2_0_Fl(long k, GEN CHI1vec, GEN CHI2vec, GEN vz, ulong p)
    8823             : {
    8824        3290 :   if (k == 1 && CHIvec_ord(CHI1vec) == 1)
    8825        1323 :     return charLFwtk_Fl(k, CHI2vec, vz, p);
    8826        1967 :   else if (CHIvec_ord(CHI2vec) == 1)
    8827         665 :     return charLFwtk_Fl(k, CHI1vec, vz, p);
    8828        1302 :   else return 0;
    8829             : }
    8830             : static GEN
    8831          84 : NK_eisen2(long k, GEN CHI1, GEN CHI2)
    8832             : {
    8833          84 :   long N = mfcharmodulus(CHI1)*mfcharmodulus(CHI2);
    8834          84 :   return mkNK(N, k, mfcharmul(CHI1,CHI2));
    8835             : }
    8836             : static GEN
    8837         259 : mfeisenstein_i(long k, GEN CHI1, GEN CHI2)
    8838             : {
    8839         259 :   long s = 1, ord, vt;
    8840             :   GEN E0, NK, vchi, T;
    8841         259 :   if (CHI2) { CHI2 = get_mfchar(CHI2); if (mfcharparity(CHI2) < 0) s = -s; }
    8842         259 :   if (CHI1) { CHI1 = get_mfchar(CHI1); if (mfcharparity(CHI1) < 0) s = -s; }
    8843         245 :   if (s != m1pk(k)) return mftrivial();
    8844         224 :   if (!CHI1) CHI1 = mfchartrivial();
    8845         224 :   if (!CHI2)
    8846             :   { /* E_k(chi1) */
    8847         140 :     vt = varn(mfcharpol(CHI1));
    8848         140 :     ord = mfcharorder(CHI1);
    8849         140 :     NK = mkNK(mfcharmodulus(CHI1), k, CHI1);
    8850         140 :     E0 = charLFwtk(k, CHI1, ord);
    8851         140 :     vchi = mkvec3(E0, mkvec(mfcharpol(CHI1)), CHI1);
    8852         140 :     return tag(t_MF_EISEN, NK, vchi);
    8853             :   }
    8854             :   /* E_k(chi1,chi2) */
    8855          84 :   vt = varn(mfcharpol(CHI1));
    8856          84 :   NK = NK_eisen2(k, CHI1, CHI2);
    8857          84 :   ord = ulcm(mfcharorder(CHI1), mfcharorder(CHI2));
    8858          84 :   E0 = mfeisenstein2_0(k, CHI1, CHI2, ord);
    8859          84 :   T = mkvec(polcyclo(ord_canon(ord), vt));
    8860          84 :   vchi = mkvec4(E0, T, CHI1, CHI2);
    8861          84 :   return tag2(t_MF_EISEN, NK, vchi, mkvecsmall2(ord,0));
    8862             : }
    8863             : GEN
    8864         259 : mfeisenstein(long k, GEN CHI1, GEN CHI2)
    8865             : {
    8866         259 :   pari_sp av = avma;
    8867         259 :   if (k < 1) pari_err_DOMAIN("mfeisenstein", "k", "<", gen_1, stoi(k));
    8868         259 :   return gerepilecopy(av, mfeisenstein_i(k, CHI1, CHI2));
    8869             : }
    8870             : 
    8871             : static GEN
    8872        1351 : mfeisenstein2all(long N0, GEN NK, long k, GEN CHI1, GEN CHI2, GEN T, long o)
    8873             : {
    8874        1351 :   GEN E, E0 = mfeisenstein2_0(k, CHI1,CHI2, o), vchi = mkvec4(E0, T, CHI1,CHI2);
    8875        1351 :   long j, d = (lg(T)==4)? itou(gmael(T,3,1)): 1;
    8876        1351 :   E = cgetg(d+1, t_VEC);
    8877        1351 :   for (j=1; j<=d; j++) gel(E,j) = tag2(t_MF_EISEN, NK,vchi,mkvecsmall2(o,j-1));
    8878        1351 :   return mfbdall(E, N0 / mf_get_N(gel(E,1)));
    8879             : }
    8880             : 
    8881             : static GEN
    8882         609 : zncharsG(GEN G)
    8883             : {
    8884         609 :   long i, l, N = itou(znstar_get_N(G));
    8885             :   GEN vCHI, V;
    8886         609 :   if (N == 1) return mkvec2(gen_1,cgetg(1,t_COL));
    8887         609 :   vCHI = const_vec(N,NULL);
    8888         609 :   V = cyc2elts(znstar_get_conreycyc(G));
    8889         609 :   l = lg(V);
    8890       23037 :   for (i = 1; i < l; i++)
    8891             :   {
    8892       22428 :     GEN chi0, chi = zc_to_ZC(gel(V,i)), n, F;
    8893       22428 :     F = znconreyconductor(G, chi, &chi0);
    8894       22428 :     if (typ(F) != t_INT) F = gel(F,1);
    8895       22428 :     n = znconreyexp(G, chi);
    8896       22428 :     gel(vCHI, itos(n)) = mkvec2(F, chi0);
    8897             :   }
    8898         609 :   return vCHI;
    8899             : }
    8900             : 
    8901             : /* CHI primitive, f(CHI) | N. Return pairs (CHI1,CHI2) both primitive
    8902             :  * such that f(CHI1)*f(CHI2) | N and CHI1 * CHI2 = CHI;
    8903             :  * if k = 1, CHI1 is even; if k = 2, omit (1,1) if CHI = 1 */
    8904             : static GEN
    8905         805 : mfeisensteinbasis_i(long N0, long k, GEN CHI)
    8906             : {
    8907         805 :   GEN G = gel(CHI,1), chi = gel(CHI,2), vT = const_vec(myeulerphiu(N0), NULL);
    8908             :   GEN CHI0, GN, chiN, Lchi, LG, V, RES, NK, T;
    8909         805 :   long i, j, l, n, n1, N, ord = mfcharorder(CHI), OC = ord_canon(ord);
    8910         805 :   long F = mfcharmodulus(CHI), vt = varn(mfcharpol(CHI));
    8911             : 
    8912         805 :   CHI0 = (F == 1)? CHI: mfchartrivial();
    8913         805 :   j = 1; RES = cgetg(N0+1, t_VEC);
    8914         805 :   T = gel(vT,OC) = Qab_trace_init(polcyclo(OC,vt), OC, OC);
    8915         805 :   if (F != 1 || k != 2)
    8916             :   { /* N1 = 1 */
    8917         679 :     NK = mkNK(F, k, CHI);
    8918         679 :     gel(RES, j++) = mfeisenstein2all(N0, NK, k, CHI0, CHI, T, ord);
    8919         679 :     if (F != 1 && k != 1)
    8920         203 :       gel(RES, j++) = mfeisenstein2all(N0, NK, k, CHI, CHI0, T, ord);
    8921             :   }
    8922         805 :   if (N0 == 1) { setlg(RES,j); return RES; }
    8923         735 :   GN = G; chiN = chi;
    8924         735 :   if (F == N0) N = N0;
    8925             :   else
    8926             :   {
    8927         441 :     GEN faN = myfactoru(N0), P = gel(faN,1), E = gel(faN,2);
    8928         441 :     long lP = lg(P);
    8929        1134 :     for (i = N = 1; i < lP; i++)
    8930             :     {
    8931         693 :       long p = P[i];
    8932         693 :       N *= upowuu(p, maxuu(E[i]/2, z_lval(F,p)));
    8933             :     }
    8934         441 :     if ((N & 3) == 2) N >>= 1;
    8935         441 :     if (N == 1) { setlg(RES,j); return RES; }
    8936         315 :     if (F != N)
    8937             :     {
    8938          98 :       GN = znstar0(utoipos(N),1);
    8939          98 :       chiN = zncharinduce(G, chi, GN);
    8940             :     }
    8941             :   }
    8942         609 :   LG = const_vec(N, NULL); /* LG[d] = znstar(d,1) or NULL */
    8943         609 :   gel(LG,1) = gel(CHI0,1);
    8944         609 :   gel(LG,F) = G;
    8945         609 :   gel(LG,N) = GN;
    8946         609 :   Lchi = coprimes_zv(N);
    8947         609 :   n = itou(znconreyexp(GN,chiN));
    8948         609 :   V = zncharsG(GN); l = lg(V);
    8949       30002 :   for (n1 = 2; n1 < l; n1++) /* skip 1 (trivial char) */
    8950             :   {
    8951       29393 :     GEN v = gel(V,n1), w, chi1, chi2, G1, G2, CHI1, CHI2;
    8952             :     long N12, N1, N2, no, oc, o12, t, m;
    8953       29393 :     if (!Lchi[n1]) continue;
    8954       21770 :     chi1 = gel(v,2); N1 = itou(gel(v,1)); /* conductor of chi1 */
    8955       21770 :     w = gel(V, Fl_div(n,n1,N));
    8956       21770 :     chi2 = gel(w,2); N2 = itou(gel(w,1)); /* conductor of chi2 */
    8957       21770 :     N12 = N1 * N2;
    8958       21770 :     if (N2 == 1 || N0 % N12) continue;
    8959             : 
    8960         658 :     G1 = gel(LG,N1); if (!G1) gel(LG,N1) = G1 = znstar0(utoipos(N1), 1);
    8961         658 :     if (k == 1 && zncharisodd(G1,chi1)) continue;
    8962         469 :     G2 = gel(LG,N2); if (!G2) gel(LG,N2) = G2 = znstar0(utoipos(N2), 1);
    8963         469 :     CHI1 = mfcharGL(G1, chi1);
    8964         469 :     CHI2 = mfcharGL(G2, chi2);
    8965         469 :     o12 = ulcm(mfcharorder(CHI1), mfcharorder(CHI2));
    8966             :     /* remove Galois orbit: same trace */
    8967         469 :     no = Fl_powu(n1, ord, N);
    8968         763 :     for (t = 1+ord, m = n1; t <= o12; t += ord)
    8969             :     { /* m <-> CHI1^t, if t in Gal(Q(chi1,chi2)/Q), omit (CHI1^t,CHI2^t) */
    8970         294 :       m = Fl_mul(m, no, N); if (!m) break;
    8971         294 :       if (ugcd(t, o12) == 1) Lchi[m] = 0;
    8972             :     }
    8973         469 :     oc = ord_canon(o12); T = gel(vT,oc);
    8974         469 :     if (!T) T = gel(vT,oc) = Qab_trace_init(polcyclo(oc,vt), oc, OC);
    8975         469 :     NK = mkNK(N12, k, CHI);
    8976         469 :     gel(RES, j++) = mfeisenstein2all(N0, NK, k, CHI1, CHI2, T, o12);
    8977             :   }
    8978         609 :   setlg(RES,j); return RES;
    8979             : }
    8980             : 
    8981             : static GEN
    8982         616 : mfbd_E2(GEN E2, long d, GEN CHI)
    8983             : {
    8984         616 :   GEN E2d = mfbd_i(E2, d);
    8985         616 :   GEN F = mkvec2(E2, E2d), L = mkvec2(gen_1, utoineg(d));
    8986             :   /* cannot use mflinear_i: E2 and E2d do not have the same level */
    8987         616 :   return tag3(t_MF_LINEAR, mkNK(d,2,CHI), F, L, gen_1);
    8988             : }
    8989             : /* C-basis of E_k(Gamma_0(N),chi). If k = 1, the first basis element must not
    8990             :  * vanish at oo [used in mfwt1basis]. Here E_1(CHI), whose q^0 coefficient
    8991             :  * does not vanish (since L(CHI,0) does not) *if* CHI is not trivial; which
    8992             :  * must be the case in weight 1.
    8993             :  *
    8994             :  * (k>=3): In weight k >= 3, basis is B(d) E(CHI1,(CHI/CHI1)_prim), where
    8995             :  * CHI1 is primitive modulo N1, and if N2 is the conductor of CHI/CHI1
    8996             :  * then d*N1*N2 | N.
    8997             :  * (k=2): In weight k=2, same if CHI is nontrivial. If CHI is trivial, must
    8998             :  * not take CHI1 trivial, and must add E_2(tau)-dE_2(d tau)), where
    8999             :  * d|N, d > 1.
    9000             :  * (k=1): In weight k=1, same as k >= 3 except that we restrict to CHI1 even */
    9001             : static GEN
    9002         833 : mfeisensteinbasis(long N, long k, GEN CHI)
    9003             : {
    9004             :   long i, F;
    9005             :   GEN L;
    9006         833 :   if (badchar(N, k, CHI)) return cgetg(1, t_VEC);
    9007         833 :   if (k == 0) return mfcharistrivial(CHI)? mkvec(mf1()): cgetg(1, t_VEC);
    9008         805 :   CHI = mfchartoprimitive(CHI, &F);
    9009         805 :   L = mfeisensteinbasis_i(N, k, CHI);
    9010         805 :   if (F == 1 && k == 2)
    9011             :   {
    9012         126 :     GEN v, E2 = mfeisenstein(2, NULL, NULL), D = mydivisorsu(N);
    9013         126 :     long nD = lg(D)-1;
    9014         126 :     v = cgetg(nD, t_VEC); L = vec_append(L,v);
    9015         126 :     for (i = 1; i < nD; i++) gel(v,i) = mfbd_E2(E2, D[i+1], CHI);
    9016             :   }
    9017         805 :   return lg(L) == 1? L: shallowconcat1(L);
    9018             : }
    9019             : 
    9020             : static GEN
    9021          70 : not_in_space(GEN F, long flag)
    9022             : {
    9023          70 :   if (!flag) err_space(F);
    9024          63 :   return cgetg(1, t_COL);
    9025             : }
    9026             : /* when flag set, no error */
    9027             : GEN
    9028         805 : mftobasis(GEN mf, GEN F, long flag)
    9029             : {
    9030         805 :   pari_sp av2, av = avma;
    9031             :   GEN G, v, y, gk;
    9032         805 :   long N, B, ismf = checkmf_i(F);
    9033             : 
    9034         805 :   mf = checkMF(mf);
    9035         805 :   if (ismf)
    9036             :   {
    9037         714 :     if (mfistrivial(F)) return zerocol(MF_get_dim(mf));
    9038         707 :     if (!mf_same_k(mf, F) || !mf_same_CHI(mf, F)) return not_in_space(F, flag);
    9039             :   }
    9040         756 :   N = MF_get_N(mf);
    9041         756 :   gk = MF_get_gk(mf);
    9042         756 :   if (ismf)
    9043             :   {
    9044         665 :     long NF = mf_get_N(F);
    9045         665 :     B = maxuu(mfsturmNgk(NF,gk), mfsturmNgk(N,gk)) + 1;
    9046         665 :     v = mfcoefs_i(F,B,1);
    9047             :   }
    9048             :   else
    9049             :   {
    9050          91 :     B = mfsturmNgk(N, gk) + 1;
    9051          91 :     switch(typ(F))
    9052             :     { /* F(0),...,F(lg(v)-2) */
    9053          63 :       case t_SER: v = sertocol(F); settyp(v,t_VEC); break;
    9054          14 :       case t_VEC: v = F; break;
    9055           7 :       case t_COL: v = shallowtrans(F); break;
    9056           7 :       default: pari_err_TYPE("mftobasis",F);
    9057             :                v = NULL;/*LCOV_EXCL_LINE*/
    9058             :     }
    9059          84 :     if (flag) B = minss(B, lg(v)-2);
    9060             :   }
    9061         749 :   y = mftobasis_i(mf, v);
    9062         749 :   if (typ(y) == t_VEC)
    9063             :   {
    9064          21 :     if (flag) return gerepilecopy(av, y);
    9065           0 :     pari_err(e_MISC, "not enough coefficients in mftobasis");
    9066             :   }
    9067         728 :   av2 = avma;
    9068         728 :   if (MF_get_space(mf) == mf_FULL || mfsturm(mf)+1 == B) return y;
    9069         210 :   G = mflinear(mf, y);
    9070         210 :   if (!gequal(v, mfcoefs_i(G, lg(v)-2,1))) y = NULL;
    9071         210 :   if (!y) { set_avma(av); return not_in_space(F, flag); }
    9072         182 :   set_avma(av2); return gerepileupto(av, y);
    9073             : }
    9074             : 
    9075             : /* assume N > 0; first cusp is always 0 */
    9076             : static GEN
    9077          49 : mfcusps_i(long N)
    9078             : {
    9079             :   long i, c, l;
    9080             :   GEN D, v;
    9081             : 
    9082          49 :   if (N == 1) return mkvec(gen_0);
    9083          49 :   D = mydivisorsu(N); l = lg(D); /* left on stack */
    9084          49 :   c = mfnumcuspsu_fact(myfactoru(N));
    9085          49 :   v = cgetg(c + 1, t_VEC);
    9086         350 :   for (i = c = 1; i < l; i++)
    9087             :   {
    9088         301 :     long C = D[i], NC = D[l-i], lima = ugcd(C, NC), A0, A;
    9089         889 :     for (A0 = 0; A0 < lima; A0++)
    9090         588 :       if (ugcd(A0, lima) == 1)
    9091             :       {
    9092         392 :         A = A0; while (ugcd(A,C) > 1) A += lima;
    9093         392 :         gel(v, c++) = sstoQ(A, C);
    9094             :       }
    9095             :   }
    9096          49 :   return v;
    9097             : }
    9098             : /* List of cusps of Gamma_0(N) */
    9099             : GEN
    9100          28 : mfcusps(GEN gN)
    9101             : {
    9102             :   long N;
    9103             :   GEN mf;
    9104          28 :   if (typ(gN) == t_INT) N = itos(gN);
    9105          14 :   else if ((mf = checkMF_i(gN))) N = MF_get_N(mf);
    9106           0 :   else { pari_err_TYPE("mfcusps", gN); N = 0; }
    9107          28 :   if (N <= 0) pari_err_DOMAIN("mfcusps", "N", "<=", gen_0, stoi(N));
    9108          28 :   return mfcusps_i(N);
    9109             : }
    9110             : 
    9111             : long
    9112         315 : mfcuspisregular(GEN NK, GEN cusp)
    9113             : {
    9114             :   long v, N, dk, nk, t, o;
    9115             :   GEN mf, CHI, go, A, C, g, c, d;
    9116         315 :   if ((mf = checkMF_i(NK)))
    9117             :   {
    9118          49 :     GEN gk = MF_get_gk(mf);
    9119          49 :     N = MF_get_N(mf);
    9120          49 :     CHI = MF_get_CHI(mf);
    9121          49 :     Qtoss(gk, &nk, &dk);
    9122             :   }
    9123             :   else
    9124         266 :     checkNK2(NK, &N, &nk, &dk, &CHI, 0);
    9125         315 :   if (typ(cusp) == t_INFINITY) return 1;
    9126         315 :   if (typ(cusp) == t_FRAC) { A = gel(cusp,1); C = gel(cusp,2); }
    9127          28 :   else { A = cusp; C = gen_1; }
    9128         315 :   g = diviuexact(mului(N,C), ugcd(N, Fl_sqr(umodiu(C,N), N)));
    9129         315 :   c = mulii(negi(C),g);
    9130         315 :   d = addiu(mulii(A,g), 1);
    9131         315 :   if (!CHI) return 1;
    9132         315 :   go = gmfcharorder(CHI);
    9133         315 :   v = vali(go); if (v < 2) go = shifti(go, 2-v);
    9134         315 :   t = itou( znchareval(gel(CHI,1), gel(CHI,2), d, go) );
    9135         315 :   if (dk == 1) return t == 0;
    9136         154 :   o = itou(go);
    9137         154 :   if (kronecker(c,d) < 0) t = Fl_add(t, o/2, o);
    9138         154 :   if (Mod4(d) == 1) return t == 0;
    9139          14 :   t = Fl_sub(t, Fl_mul(o/4, nk, o), o);
    9140          14 :   return t == 0;
    9141             : }
    9142             : 
    9143             : /* Some useful closures */
    9144             : 
    9145             : /* sum_{d|n} d^k */
    9146             : static GEN
    9147       16464 : mysumdivku(ulong n, ulong k)
    9148             : {
    9149       16464 :   GEN fa = myfactoru(n);
    9150       16464 :   return k == 1? usumdiv_fact(fa): usumdivk_fact(fa,k);
    9151             : }
    9152             : static GEN
    9153         658 : c_Ek(long n, long d, GEN F)
    9154             : {
    9155         658 :   GEN E = cgetg(n + 2, t_VEC), C = gel(F,2);
    9156         658 :   long i, k = mf_get_k(F);
    9157         658 :   gel (E, 1) = gen_1;
    9158        8260 :   for (i = 1; i <= n; i++)
    9159             :   {
    9160        7602 :     pari_sp av = avma;
    9161        7602 :     gel(E, i+1) = gerepileupto(av, gmul(C, mysumdivku(i*d, k-1)));
    9162             :   }
    9163         658 :   return E;
    9164             : }
    9165             : 
    9166             : GEN
    9167         189 : mfEk(long k)
    9168             : {
    9169         189 :   pari_sp av = avma;
    9170             :   GEN E0, NK;
    9171         189 :   if (k < 0 || odd(k)) pari_err_TYPE("mfEk [incorrect k]", stoi(k));
    9172         189 :   if (!k) return mf1();
    9173         182 :   E0 = gdivsg(-2*k, bernfrac(k));
    9174         182 :   NK = mkNK(1,k,mfchartrivial());
    9175         182 :   return gerepilecopy(av, tag(t_MF_Ek, NK, E0));
    9176             : }
    9177             : 
    9178             : GEN
    9179          49 : mfDelta(void)
    9180             : {
    9181          49 :   pari_sp av = avma;
    9182          49 :   return gerepilecopy(av, tag0(t_MF_DELTA, mkNK(1,12,mfchartrivial())));
    9183             : }
    9184             : 
    9185             : GEN
    9186         504 : mfTheta(GEN psi)
    9187             : {
    9188         504 :   pari_sp av = avma;
    9189             :   GEN N, gk, psi2;
    9190             :   long par;
    9191         504 :   if (!psi) { psi = mfchartrivial(); N = utoipos(4); par = 1; }
    9192             :   else
    9193             :   {
    9194             :     long FC;
    9195          21 :     psi = get_mfchar(psi);
    9196          21 :     FC = mfcharconductor(psi);
    9197          21 :     if (mfcharmodulus(psi) != FC)
    9198           0 :       pari_err_TYPE("mfTheta [nonprimitive character]", psi);
    9199          21 :     par = mfcharparity(psi);
    9200          21 :     N = shifti(sqru(FC),2);
    9201             :   }
    9202         504 :   if (par > 0) { gk = ghalf; psi2 = psi; }
    9203           7 :   else { gk = gsubsg(2, ghalf); psi2 = mfcharmul(psi, get_mfchar(stoi(-4))); }
    9204         504 :   return gerepilecopy(av, tag(t_MF_THETA, mkgNK(N, gk, psi2, pol_x(1)), psi));
    9205             : }
    9206             : 
    9207             : /* Output 0 if not desired eta product: if flag=0 (default) require
    9208             :  * holomorphic at cusps. If flag set, accept meromorphic, but sill in some
    9209             :  * modular function space */
    9210             : GEN
    9211         140 : mffrometaquo(GEN eta, long flag)
    9212             : {
    9213         140 :   pari_sp av = avma;
    9214             :   GEN NK, N, k, BR, P;
    9215         140 :   long v, cusp = 0;
    9216         140 :   if (!etaquotype(&eta, &N,&k,&P, &v, NULL, flag? NULL: &cusp) || cusp < 0)
    9217             :   {
    9218          14 :     set_avma(av); return gen_0;
    9219             :   }
    9220         126 :   if (lg(gel(eta,1)) == 1) { set_avma(av); return mf1(); }
    9221         119 :   BR = mkvec2(ZV_to_zv(gel(eta,1)), ZV_to_zv(gel(eta,2)));
    9222         119 :   if (v < 0) v = 0;
    9223         119 :   NK = mkgNK(N, k, get_mfchar(P), pol_x(1));
    9224         119 :   return gerepilecopy(av, tag2(t_MF_ETAQUO, NK, BR, utoi(v)));
    9225             : }
    9226             : 
    9227             : #if 0
    9228             : /* number of primitive characters modulo N */
    9229             : static ulong
    9230             : numprimchars(ulong N)
    9231             : {
    9232             :   GEN fa, P, E;
    9233             :   long i, l;
    9234             :   ulong n;
    9235             :   if ((N & 3) == 2) return 0;
    9236             :   fa = myfactoru(N);
    9237             :   P = gel(fa,1); l = lg(P);
    9238             :   E = gel(fa,2);
    9239             :   for (i = n = 1; i < l; i++)
    9240             :   {
    9241             :     ulong p = P[i], e = E[i];
    9242             :     if (e == 2) n *= p-2; else n *= (p-1)*(p-1)*upowuu(p,e-2);
    9243             :   }
    9244             :   return n;
    9245             : }
    9246             : #endif
    9247             : 
    9248             : /* Space generated by products of two Eisenstein series */
    9249             : 
    9250             : INLINE int
    9251      112483 : cmp_small(long a, long b) { return a>b? 1: (a<b? -1: 0); }
    9252             : static int
    9253       62657 : cmp_small_priority(void *E, GEN a, GEN b)
    9254             : {
    9255       62657 :   GEN prio = (GEN)E;
    9256       62657 :   return cmp_small(prio[(long)a], prio[(long)b]);
    9257             : }
    9258             : static long
    9259         938 : znstar_get_expo(GEN G)
    9260             : {
    9261         938 :   GEN cyc = znstar_get_cyc(G);
    9262         938 :   return (lg(cyc) == 1)? 1: itou(gel(cyc,1));
    9263             : }
    9264             : 
    9265             : /* Return [vchi, bymod, vG]:
    9266             :  * vG[f] = znstar(f,1) for f a conductor of (at least) a char mod N; else NULL
    9267             :  * bymod[f] = vecsmall of conrey indexes of chars modulo f | N; else NULL
    9268             :  * vchi[n] = a list of CHIvec [G0,chi0,o,ncharvecexpo(G0,nchi0),...]:
    9269             :  *   chi0 = primitive char attached to Conrey Mod(n,N)
    9270             :  * (resp. NULL if (n,N) > 1) */
    9271             : static GEN
    9272         469 : charsmodN(long N)
    9273             : {
    9274         469 :   GEN D, G, prio, phio, dummy = cgetg(1,t_VEC);
    9275         469 :   GEN vP, vG = const_vec(N,NULL), vCHI  = const_vec(N,NULL);
    9276         469 :   GEN bymod = const_vec(N,NULL);
    9277         469 :   long pn, i, l, vt = fetch_user_var("t");
    9278         469 :   D = mydivisorsu(N); l = lg(D);
    9279        3059 :   for (i = 1; i < l; i++)
    9280        2590 :     gel(bymod, D[i]) = vecsmalltrunc_init(myeulerphiu(D[i])+1);
    9281         469 :   gel(vG,N) = G = znstar0(utoipos(N),1);
    9282         469 :   pn = znstar_get_expo(G);  /* exponent(Z/NZ)^* */
    9283         469 :   vP = const_vec(pn,NULL);
    9284       22456 :   for (i = 1; i <= N; i++)
    9285             :   {
    9286             :     GEN P, gF, G0, chi0, nchi0, chi, v, go;
    9287             :     long j, F, o;
    9288       21987 :     if (ugcd(i,N) != 1) continue;
    9289       11067 :     chi = znconreylog(G, utoipos(i));
    9290       11067 :     gF = znconreyconductor(G, chi, &chi0);
    9291       11067 :     F = (typ(gF) == t_INT)? itou(gF): itou(gel(gF,1));
    9292       11067 :     G0 = gel(vG, F); if (!G0) G0 = gel(vG,F) = znstar0(gF, 1);
    9293       11067 :     nchi0 = znconreylog_normalize(G0,chi0);
    9294       11067 :     go = gel(nchi0,1); o = itou(go); /* order(chi0) */
    9295       11067 :     v = ncharvecexpo(G0, nchi0);
    9296       11067 :     if (!equaliu(go, pn)) v = zv_z_mul(v, pn / o);
    9297       11067 :     P = gel(vP, o); if (!P) P = gel(vP,o) = polcyclo(o,vt);
    9298             :     /* mfcharcxinit with dummy complex powers */
    9299       11067 :     gel(vCHI,i) = mkvecn(6, G0, chi0, go, v, dummy, P);
    9300       11067 :     D = mydivisorsu(N / F); l = lg(D);
    9301       11067 :     for (j = 1; j < l; j++) vecsmalltrunc_append(gel(bymod, F*D[j]), i);
    9302             :   }
    9303         469 :   phio = zero_zv(pn); l = lg(vCHI); prio = cgetg(l, t_VEC);
    9304       22456 :   for (i = 1; i < l; i++)
    9305             :   {
    9306       21987 :     GEN CHI = gel(vCHI,i);
    9307             :     long o;
    9308       21987 :     if (!CHI) continue;
    9309       11067 :     o = CHIvec_ord(CHI);
    9310       11067 :     if (!phio[o]) phio[o] = myeulerphiu(o);
    9311       11067 :     prio[i] = phio[o];
    9312             :   }
    9313         469 :   l = lg(bymod);
    9314             :   /* sort characters by increasing value of phi(order) */
    9315       22456 :   for (i = 1; i < l; i++)
    9316             :   {
    9317       21987 :     GEN z = gel(bymod,i);
    9318       21987 :     if (z) gen_sort_inplace(z, (void*)prio, &cmp_small_priority, NULL);
    9319             :   }
    9320         469 :   return mkvec3(vCHI, bymod, vG);
    9321             : }
    9322             : 
    9323             : static GEN
    9324        4319 : mfeisenstein2pure(long k, GEN CHI1, GEN CHI2, long ord, GEN P, long lim)
    9325             : {
    9326        4319 :   GEN c, V = cgetg(lim+2, t_COL);
    9327             :   long n;
    9328        4319 :   c = mfeisenstein2_0(k, CHI1, CHI2, ord);
    9329        4319 :   if (P) c = grem(c, P);
    9330        4319 :   gel(V,1) = c;
    9331       92512 :   for (n=1; n <= lim; n++)
    9332             :   {
    9333       88193 :     c = sigchi2(k, CHI1, CHI2, n, ord);
    9334       88193 :     if (P) c = grem(c, P);
    9335       88193 :     gel(V,n+1) = c;
    9336             :   }
    9337        4319 :   return V;
    9338             : }
    9339             : static GEN
    9340        3290 : mfeisenstein2pure_Fl(long k, GEN CHI1vec, GEN CHI2vec, GEN vz, ulong p, long lim)
    9341             : {
    9342        3290 :   GEN V = cgetg(lim+2, t_VECSMALL);
    9343             :   long n;
    9344        3290 :   V[1] = mfeisenstein2_0_Fl(k, CHI1vec, CHI2vec, vz, p);
    9345        3290 :   for (n=1; n <= lim; n++) V[n+1] = sigchi2_Fl(k, CHI1vec, CHI2vec, n, vz, p);
    9346        3290 :   return V;
    9347             : }
    9348             : 
    9349             : static GEN
    9350         175 : getcolswt2(GEN M, GEN D, ulong p)
    9351             : {
    9352         175 :   GEN R, v = gel(M,1);
    9353         175 :   long i, l = lg(M) - 1;
    9354         175 :   R = cgetg(l, t_MAT); /* skip D[1] = 1 */
    9355         616 :   for (i = 1; i < l; i++)
    9356             :   {
    9357         441 :     GEN w = Flv_Fl_mul(gel(M,i+1), D[i+1], p);
    9358         441 :     gel(R,i) = Flv_sub(v, w, p);
    9359             :   }
    9360         175 :   return R;
    9361             : }
    9362             : static GEN
    9363        4319 : expandbd(GEN V, long d)
    9364             : {
    9365             :   long L, n, nd;
    9366             :   GEN W;
    9367        4319 :   if (d == 1) return V;
    9368        1575 :   L = lg(V)-1; W = zerocol(L); /* nd = n/d */
    9369        1575 :   for (n = nd = 0; n < L; n += d, nd++) gel(W, n+1) = gel(V, nd+1);
    9370        1575 :   return W;
    9371             : }
    9372             : static GEN
    9373        5222 : expandbd_Fl(GEN V, long d)
    9374             : {
    9375             :   long L, n, nd;
    9376             :   GEN W;
    9377        5222 :   if (d == 1) return V;
    9378        1932 :   L = lg(V)-1; W = zero_Flv(L); /* nd = n/d */
    9379        1932 :   for (n = nd = 0; n < L; n += d, nd++) W[n+1] = V[nd+1];
    9380        1932 :   return W;
    9381             : }
    9382             : static void
    9383        3290 : getcols_i(GEN *pM, GEN *pvj, GEN gk, GEN CHI1vec, GEN CHI2vec, long NN1, GEN vz,
    9384             :           ulong p, long lim)
    9385             : {
    9386        3290 :   GEN CHI1 = CHIvec_CHI(CHI1vec), CHI2 = CHIvec_CHI(CHI2vec);
    9387        3290 :   long N2 = CHIvec_N(CHI2vec);
    9388        3290 :   GEN vj, M, D = mydivisorsu(NN1/N2);
    9389        3290 :   long i, l = lg(D), k = gk[2];
    9390        3290 :   GEN V = mfeisenstein2pure_Fl(k, CHI1vec, CHI2vec, vz, p, lim);
    9391        3290 :   M = cgetg(l, t_MAT);
    9392        3290 :   for (i = 1; i < l; i++) gel(M,i) = expandbd_Fl(V, D[i]);
    9393        3290 :   if (k == 2 && N2 == 1 && CHIvec_N(CHI1vec) == 1)
    9394             :   {
    9395         175 :     M = getcolswt2(M, D, p); l--;
    9396         175 :     D = vecslice(D, 2, l);
    9397             :   }
    9398        3290 :   *pM = M;
    9399        3290 :   *pvj = vj = cgetg(l, t_VEC);
    9400        3290 :   for (i = 1; i < l; i++) gel(vj,i) = mkvec4(gk, CHI1, CHI2, utoipos(D[i]));
    9401        3290 : }
    9402             : 
    9403             : /* find all CHI1, CHI2 mod N such that CHI1*CHI2 = CHI, f(CHI1)*f(CHI2) | N.
    9404             :  * set M = mfcoefs(B_e E(CHI1,CHI2), lim), vj = [e,i1,i2] */
    9405             : static void
    9406        1267 : getcols(GEN *pM, GEN *pv, long k, long nCHI, GEN allN, GEN vz, ulong p,
    9407             :         long lim)
    9408             : {
    9409        1267 :   GEN vCHI = gel(allN,1), gk = utoi(k);
    9410        1267 :   GEN M = cgetg(1,t_MAT), v = cgetg(1,t_VEC);
    9411        1267 :   long i1, N = lg(vCHI)-1;
    9412       63322 :   for (i1 = 1; i1 <= N; i1++)
    9413             :   {
    9414       62055 :     GEN CHI1vec = gel(vCHI, i1), CHI2vec, M1, v1;
    9415             :     long NN1, i2;
    9416      121618 :     if (!CHI1vec) continue;
    9417       46718 :     if (k == 1 && CHIvec_parity(CHI1vec) == -1) continue;
    9418       29582 :     NN1 = N/CHIvec_N(CHI1vec); /* N/f(chi1) */;
    9419       29582 :     i2 = Fl_div(nCHI,i1, N);
    9420       29582 :     if (!i2) i2 = 1;
    9421       29582 :     CHI2vec = gel(vCHI,i2);
    9422       29582 :     if (NN1 % CHIvec_N(CHI2vec)) continue; /* f(chi1)f(chi2) | N ? */
    9423        2492 :     getcols_i(&M1, &v1, gk, CHI1vec, CHI2vec, NN1, vz, p, lim);
    9424        2492 :     M = shallowconcat(M, M1);
    9425        2492 :     v = shallowconcat(v, v1);
    9426             :   }
    9427        1267 :   *pM = M;
    9428        1267 :   *pv = v;
    9429        1267 : }
    9430             : 
    9431             : static void
    9432         833 : update_Mj(GEN *M, GEN *vecj, GEN *pz, ulong p)
    9433             : {
    9434             :   GEN perm;
    9435         833 :   *pz = Flm_indexrank(*M, p); perm = gel(*pz,2);
    9436         833 :   *M = vecpermute(*M, perm);
    9437         833 :   *vecj = vecpermute(*vecj, perm);
    9438         833 : }
    9439             : static int
    9440         301 : getcolsgen(long dim, GEN *pM, GEN *pvj, GEN *pz, long k, long ell, long nCHI,
    9441             :            GEN allN, GEN vz, ulong p, long lim)
    9442             : {
    9443         301 :   GEN vCHI = gel(allN,1), bymod = gel(allN,2), gell = utoi(ell);
    9444         301 :   long i1, N = lg(vCHI)-1;
    9445         301 :   long L = lim+1;
    9446         301 :   if (lg(*pvj)-1 >= dim) update_Mj(pM, pvj, pz, p);
    9447         301 :   if (lg(*pvj)-1 == dim) return 1;
    9448        1099 :   for (i1 = 1; i1 <= N; i1++)
    9449             :   {
    9450        1085 :     GEN CHI1vec = gel(vCHI, i1), T;
    9451             :     long par1, j, l, N1, NN1;
    9452             : 
    9453        1085 :     if (!CHI1vec) continue;
    9454        1071 :     par1 = CHIvec_parity(CHI1vec);
    9455        1071 :     if (ell == 1 && par1 == -1) continue;
    9456         672 :     if (odd(ell)) par1 = -par1;
    9457         672 :     N1 = CHIvec_N(CHI1vec);
    9458         672 :     NN1 = N/N1;
    9459         672 :     T = gel(bymod, NN1); l = lg(T);
    9460        2394 :     for (j = 1; j < l; j++)
    9461             :     {
    9462        1995 :       long i2 = T[j], l1, l2, j1, s, nC;
    9463        1995 :       GEN M, M1, M2, vj, vj1, vj2, CHI2vec = gel(vCHI, i2);
    9464        3192 :       if (CHIvec_parity(CHI2vec) != par1) continue;
    9465         798 :       nC = Fl_div(nCHI, Fl_mul(i1,i2,N), N);
    9466         798 :       getcols(&M2, &vj2, k-ell, nC, allN, vz, p, lim);
    9467         798 :       l2 = lg(M2); if (l2 == 1) continue;
    9468         798 :       getcols_i(&M1, &vj1, gell, CHI1vec, CHI2vec, NN1, vz, p, lim);
    9469         798 :       l1 = lg(M1);
    9470         798 :       M1 = Flm_to_FlxV(M1, 0);
    9471         798 :       M2 = Flm_to_FlxV(M2, 0);
    9472         798 :       M  = cgetg((l1-1)*(l2-1) + 1, t_MAT);
    9473         798 :       vj = cgetg((l1-1)*(l2-1) + 1, t_VEC);
    9474        1995 :       for (j1 = s = 1; j1 < l1; j1++)
    9475             :       {
    9476        1197 :         GEN E = gel(M1,j1), v = gel(vj1,j1);
    9477             :         long j2;
    9478        5166 :         for (j2 = 1; j2 < l2; j2++, s++)
    9479             :         {
    9480        3969 :           GEN c = Flx_to_Flv(Flxn_mul(E, gel(M2,j2), L, p), L);
    9481        3969 :           gel(M,s) = c;
    9482        3969 :           gel(vj,s) = mkvec2(v, gel(vj2,j2));
    9483             :         }
    9484             :       }
    9485         798 :       *pM = shallowconcat(*pM, M);
    9486         798 :       *pvj = shallowconcat(*pvj, vj);
    9487         798 :       if (lg(*pvj)-1 >= dim) update_Mj(pM, pvj, pz, p);
    9488         798 :       if (lg(*pvj)-1 == dim) return 1;
    9489             :     }
    9490             :   }
    9491          14 :   if (ell == 1)
    9492             :   {
    9493          14 :     update_Mj(pM, pvj, pz, p);
    9494          14 :     return (lg(*pvj)-1 == dim);
    9495             :   }
    9496           0 :   return 0;
    9497             : }
    9498             : 
    9499             : static GEN
    9500         931 : mkF2bd(long d, long lim)
    9501             : {
    9502         931 :   GEN V = zerovec(lim + 1);
    9503             :   long n;
    9504         931 :   gel(V, 1) = ginv(stoi(-24));
    9505         931 :   for (n = 1; n <= lim/d; n++) gel(V, n*d + 1) = mysumdivku(n, 1);
    9506         931 :   return V;
    9507             : }
    9508             : 
    9509             : static GEN
    9510        4676 : mkeisen(GEN E, long ord, GEN P, long lim)
    9511             : {
    9512        4676 :   long k = itou(gel(E,1)), e = itou(gel(E,4));
    9513        4676 :   GEN CHI1 = gel(E,2), CHI2 = gel(E,3);
    9514        4676 :   if (k == 2 && mfcharistrivial(CHI1) && mfcharistrivial(CHI2))
    9515         357 :     return gsub(mkF2bd(1,lim), gmulgs(mkF2bd(e,lim), e));
    9516             :   else
    9517             :   {
    9518        4319 :     GEN V = mfeisenstein2pure(k, CHI1, CHI2, ord, P, lim);
    9519        4319 :     return expandbd(V, e);
    9520             :   }
    9521             : }
    9522             : static GEN
    9523         441 : mkM(GEN vj, long pn, GEN P, long lim)
    9524             : {
    9525         441 :   long j, l = lg(vj), L = lim+1;
    9526         441 :   GEN M = cgetg(l, t_MAT);
    9527        3836 :   for (j = 1; j < l; j++)
    9528             :   {
    9529             :     GEN E1, E2;
    9530        3395 :     parse_vecj(gel(vj,j), &E1,&E2);
    9531        3395 :     E1 = RgV_to_RgX(mkeisen(E1, pn, P, lim), 0);
    9532        3395 :     if (E2)
    9533             :     {
    9534        1281 :       E2 = RgV_to_RgX(mkeisen(E2, pn, P, lim), 0);
    9535        1281 :       E1 = RgXn_mul(E1, E2, L);
    9536             :     }
    9537        3395 :     E1 = RgX_to_RgC(E1, L);
    9538        3395 :     if (P && E2) E1 = RgXQV_red(E1, P);
    9539        3395 :     gel(M,j) = E1;
    9540             :   }
    9541         441 :   return M;
    9542             : }
    9543             : 
    9544             : /* assume N > 2 */
    9545             : static GEN
    9546          21 : mffindeisen1(long N)
    9547             : {
    9548          21 :   GEN G = znstar0(utoipos(N), 1), L = chargalois(G, NULL), chi0 = NULL;
    9549          21 :   long j, m = N, l = lg(L);
    9550         154 :   for (j = 1; j < l; j++)
    9551             :   {
    9552         147 :     GEN chi = gel(L,j);
    9553         147 :     long r = myeulerphiu(itou(zncharorder(G,chi)));
    9554         147 :     if (r >= m) continue;
    9555         105 :     chi = znconreyfromchar(G, chi);
    9556         105 :     if (zncharisodd(G,chi)) { m = r; chi0 = chi; if (r == 1) break; }
    9557             :   }
    9558          21 :   if (!chi0) pari_err_BUG("mffindeisen1 [no Eisenstein series found]");
    9559          21 :   chi0 = znchartoprimitive(G,chi0);
    9560          21 :   return mfcharGL(gel(chi0,1), gel(chi0,2));
    9561             : }
    9562             : 
    9563             : static GEN
    9564         469 : mfeisensteinspaceinit_i(long N, long k, GEN CHI)
    9565             : {
    9566         469 :   GEN M, Minv, vj, vG, GN, allN, P, vz, z = NULL;
    9567         469 :   long nCHI, lim, ell, ord, pn, dim = mffulldim(N, k, CHI);
    9568             :   ulong r, p;
    9569             : 
    9570         469 :   if (!dim) retmkvec3(cgetg(1,t_VECSMALL),
    9571             :                       mkvec2(cgetg(1,t_MAT),gen_1),cgetg(1,t_VEC));
    9572         469 :   lim = mfsturmNk(N, k) + 1;
    9573         469 :   allN = charsmodN(N);
    9574         469 :   vG = gel(allN,3);
    9575         469 :   GN = gel(vG,N);
    9576         469 :   pn = znstar_get_expo(GN);
    9577         469 :   ord = ord_canon(pn);
    9578         469 :   P = ord == 1? NULL: polcyclo(ord, varn(mfcharpol(CHI)));
    9579         469 :   CHI = induce(GN, CHI); /* lift CHI mod N before mfcharno*/
    9580         469 :   nCHI = mfcharno(CHI);
    9581         469 :   r = QabM_init(ord, &p);
    9582         469 :   vz = Fl_powers(r, pn, p);
    9583         469 :   getcols(&M, &vj, k, nCHI, allN, vz, p, lim);
    9584         483 :   for (ell = k>>1; ell >= 1; ell--)
    9585         301 :     if (getcolsgen(dim, &M, &vj, &z, k, ell, nCHI, allN, vz, p, lim)) break;
    9586         469 :   if (!z) update_Mj(&M, &vj, &z, p);
    9587         469 :   if (lg(vj) - 1 < dim) return NULL;
    9588         441 :   M = mkM(vj, pn, P, lim);
    9589         441 :   Minv = QabM_Minv(rowpermute(M, gel(z,1)), P, ord);
    9590         441 :   return mkvec4(gel(z,1), Minv, vj, utoi(ord));
    9591             : }
    9592             : /* true mf */
    9593             : static GEN
    9594         441 : mfeisensteinspaceinit(GEN mf)
    9595             : {
    9596         441 :   pari_sp av = avma;
    9597         441 :   GEN z, CHI = MF_get_CHI(mf);
    9598         441 :   long N = MF_get_N(mf), k = MF_get_k(mf);
    9599         441 :   if (!CHI) CHI = mfchartrivial();
    9600         441 :   z = mfeisensteinspaceinit_i(N, k, CHI);
    9601         441 :   if (!z)
    9602             :   {
    9603          21 :     GEN E, CHIN = mffindeisen1(N), CHI0 = mfchartrivial();
    9604          21 :     z = mfeisensteinspaceinit_i(N, k+1, mfcharmul(CHI, CHIN));
    9605          21 :     if (z) E = mkvec4(gen_1, CHI0, CHIN, gen_1);
    9606             :     else
    9607             :     {
    9608           7 :       z = mfeisensteinspaceinit_i(N, k+2, CHI);
    9609           7 :       E = mkvec4(gen_2, CHI0, CHI0, utoipos(N));
    9610             :     }
    9611          21 :     z = mkvec2(z, E);
    9612             :   }
    9613         441 :   return gerepilecopy(av, z);
    9614             : }
    9615             : 
    9616             : /* decomposition of modular form on eisenspace */
    9617             : static GEN
    9618         826 : mfeisensteindec(GEN mf, GEN F)
    9619             : {
    9620         826 :   pari_sp av = avma;
    9621             :   GEN M, Mindex, Mvecj, V, B, CHI;
    9622             :   long o, ord;
    9623             : 
    9624         826 :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    9625         826 :   if (lg(Mvecj) < 5)
    9626             :   {
    9627          21 :     GEN E, e = gel(Mvecj,2), gkE = gel(e,1);
    9628          21 :     long dE = itou(gel(e,4));
    9629          21 :     Mvecj = gel(Mvecj,1);
    9630          21 :     E = mfeisenstein(itou(gkE), NULL, gel(e,3));
    9631          21 :     if (dE != 1) E = mfbd_E2(E, dE, gel(e,2)); /* here k = 2 */
    9632          21 :     F = mfmul(F, E);
    9633             :   }
    9634         826 :   M = gel(Mvecj, 2);
    9635         826 :   if (lg(M) == 1) return cgetg(1, t_VEC);
    9636         826 :   Mindex = gel(Mvecj, 1);
    9637         826 :   ord = itou(gel(Mvecj,4));
    9638         826 :   V = mfcoefs(F, Mindex[lg(Mindex)-1]-1, 1); settyp(V, t_COL);
    9639         826 :   CHI = mf_get_CHI(F);
    9640         826 :   o = mfcharorder_canon(CHI);
    9641         826 :   if (o > 1 && o != ord)
    9642             :   { /* convert Mod(.,polcyclo(o)) to Mod(., polcyclo(N)) for o | N,
    9643             :      * o and N both != 2 (mod 4) */
    9644          49 :     GEN z, P = gel(M,4); /* polcyclo(ord) */
    9645          49 :     long vt = varn(P);
    9646          49 :     z = gmodulo(pol_xn(ord/o, vt), P);
    9647          49 :     if (ord % o) pari_err_TYPE("mfeisensteindec", V);
    9648          49 :     V = gsubst(liftpol_shallow(V), vt, z);
    9649             :   }
    9650         826 :   B = Minv_RgC_mul(M, vecpermute(V, Mindex));
    9651         826 :   return gerepileupto(av, B);
    9652             : }
    9653             : 
    9654             : /*********************************************************************/
    9655             : /*                        END EISENSPACE                             */
    9656             : /*********************************************************************/
    9657             : 
    9658             : static GEN
    9659          70 : sertocol2(GEN S, long l)
    9660             : {
    9661          70 :   GEN C = cgetg(l + 2, t_COL);
    9662             :   long i;
    9663          70 :   for (i = 0; i <= l; i++) gel(C, i+1) = polcoef_i(S, i, -1);
    9664          70 :   return C;
    9665             : }
    9666             : 
    9667             : /* Compute polynomial P0 such that F=E4^(k/4)P0(E6/E4^(3/2)). */
    9668             : static GEN
    9669          14 : mfcanfindp0(GEN F, long k)
    9670             : {
    9671          14 :   pari_sp ltop = avma;
    9672             :   GEN E4, E6, V, V1, Q, W, res, M, B;
    9673             :   long l, j;
    9674          14 :   l = k/6 + 2;
    9675          14 :   V = mfcoefsser(F,l);
    9676          14 :   E4 = mfcoefsser(mfEk(4),l);
    9677          14 :   E6 = mfcoefsser(mfEk(6),l);
    9678          14 :   V1 = gdiv(V, gpow(E4, sstoQ(k,4), 0));
    9679          14 :   Q = gdiv(E6, gpow(E4, sstoQ(3,2), 0));
    9680          14 :   W = gpowers(Q, l - 1);
    9681          14 :   M = cgetg(l + 1, t_MAT);
    9682          14 :   for (j = 1; j <= l; j++) gel(M,j) = sertocol2(gel(W,j), l);
    9683          14 :   B = sertocol2(V1, l);
    9684          14 :   res = inverseimage(M, B);
    9685          14 :   if (lg(res) == 1) err_space(F);
    9686          14 :   return gerepilecopy(ltop, gtopolyrev(res, 0));
    9687             : }
    9688             : 
    9689             : /* Compute the first n+1 Taylor coeffs at tau=I of a modular form
    9690             :  * on SL_2(Z). */
    9691             : GEN
    9692          14 : mftaylor(GEN F, long n, long flreal, long prec)
    9693             : {
    9694          14 :   pari_sp ltop = avma;
    9695          14 :   GEN P0, Pm1 = gen_0, v;
    9696          14 :   GEN X2 = mkpoln(3, ghalf,gen_0,gneg(ghalf)); /* (x^2-1) / 2 */
    9697             :   long k, m;
    9698          14 :   if (!checkmf_i(F)) pari_err_TYPE("mftaylor",F);
    9699          14 :   k = mf_get_k(F);
    9700          14 :   if (mf_get_N(F) != 1 || k < 0) pari_err_IMPL("mftaylor for this form");
    9701          14 :   P0 = mfcanfindp0(F, k);
    9702          14 :   v = cgetg(n+2, t_VEC); gel(v, 1) = RgX_coeff(P0,0);
    9703         154 :   for (m = 0; m < n; m++)
    9704             :   {
    9705         140 :     GEN P1 = gdivgs(gmulsg(-(k + 2*m), RgX_shift(P0,1)), 12);
    9706         140 :     P1 = gadd(P1, gmul(X2, RgX_deriv(P0)));
    9707         140 :     if (m) P1 = gsub(P1, gdivgs(gmulsg(m*(m+k-1), Pm1), 144));
    9708         140 :     Pm1 = P0; P0 = P1;
    9709         140 :     gel(v, m+2) = RgX_coeff(P0, 0);
    9710             :   }
    9711          14 :   if (flreal)
    9712             :   {
    9713           7 :     GEN pi2 = Pi2n(1, prec), pim4 = gmulsg(-2, pi2), VPC;
    9714           7 :     GEN C = gmulsg(3, gdiv(gpowgs(ggamma(ginv(utoi(4)), prec), 8), gpowgs(pi2, 6)));
    9715             :     /* E_4(i): */
    9716           7 :     GEN facn = gen_1;
    9717           7 :     VPC = gpowers(gmul(pim4, gsqrt(C, prec)), n);
    9718           7 :     C = gpow(C, sstoQ(k,4), prec);
    9719          84 :     for (m = 0; m <= n; m++)
    9720             :     {
    9721          77 :       gel(v, m+1) = gdiv(gmul(C, gmul(gel(v, m+1), gel(VPC, m+1))), facn);
    9722          77 :       facn = gmulgs(facn, m+1);
    9723             :     }
    9724             :   }
    9725          14 :   return gerepilecopy(ltop, v);
    9726             : }
    9727             : 
    9728             : #if 0
    9729             : /* To be used in mfeigensearch() */
    9730             : GEN
    9731             : mfreadratfile()
    9732             : {
    9733             :   GEN eqn;
    9734             :   pariFILE *F = pari_fopengz("rateigen300.gp");
    9735             :   eqn = gp_readvec_stream(F->file);
    9736             :   pari_fclose(F);
    9737             :   return eqn;
    9738             : }
    9739             : #endif
    9740             :  /*****************************************************************/
    9741             : /*           EISENSTEIN CUSPS: COMPLEX DIRECTLY: one F_k         */
    9742             : /*****************************************************************/
    9743             : 
    9744             : /* CHIvec = charinit(CHI); data = [N1g/g1,N2g/g2,g1/g,g2/g,C/g1,C/g2,
    9745             :  * (N1g/g1)^{-1},(N2g/g2)^{-1}] */
    9746             : 
    9747             : /* nm = n/m;
    9748             :  * z1 = powers of \z_{C/g}^{(Ae/g)^{-1}},
    9749             :  * z2 = powers of \z_N^{A^{-1}(g1g2/C)}]
    9750             :  * N.B. : we compute value and conjugate at the end, so it is (Ae/g)^{-1}
    9751             :  * and not -(Ae/g)^{-1} */
    9752             : static GEN
    9753     5903170 : eiscnm(long nm, long m, GEN CHI1vec, GEN CHI2vec, GEN data, GEN z1)
    9754             : {
    9755     5903170 :   long Cg1 = data[5], s10 = (nm*data[7]) % Cg1, r10 = (nm - data[1]*s10) / Cg1;
    9756     5903170 :   long Cg2 = data[6], s20 = (m *data[8]) % Cg2, r20 = (m  - data[2]*s20) / Cg2;
    9757             :   long j1, r1, s1;
    9758     5903170 :   GEN T = gen_0;
    9759    14927038 :   for (j1 = 0, r1 = r10, s1 = s10; j1 < data[3]; j1++, r1 -= data[1], s1 += Cg1)
    9760             :   {
    9761     9023868 :     GEN c1 = mychareval(CHI1vec, r1);
    9762     9023868 :     if (!gequal0(c1))
    9763             :     {
    9764             :       long j2, r2, s2;
    9765     6173426 :       GEN S = gen_0;
    9766    17004736 :       for (j2 = 0, r2 = r20, s2 = s20; j2 < data[4]; j2++, r2 -= data[2], s2 += Cg2)
    9767             :       {
    9768    10831310 :         GEN c2 = mychareval(CHI2vec, r2);
    9769    10831310 :         if (!gequal0(c2)) S = gadd(S, gmul(c2, rootsof1pow(z1, s1*s2)));
    9770             :       }
    9771     6173426 :       T = gadd(T, gmul(c1, S));
    9772             :     }
    9773             :   }
    9774     5903170 :   return conj_i(T);
    9775             : }
    9776             : 
    9777             : static GEN
    9778      461986 : fg1g2n(long n, long k, GEN CHI1vec, GEN CHI2vec, GEN data, GEN z1, GEN z2)
    9779             : {
    9780      461986 :   pari_sp av = avma;
    9781      461986 :   GEN S = gen_0, D = mydivisorsu(n);
    9782      461986 :   long i, l = lg(D);
    9783     3413571 :   for (i = 1; i < l; i++)
    9784             :   {
    9785     2951585 :     long m = D[i], nm = D[l-i]; /* n/m */
    9786     2951585 :     GEN u = eiscnm( nm,  m, CHI1vec, CHI2vec, data, z1);
    9787     2951585 :     GEN v = eiscnm(-nm, -m, CHI1vec, CHI2vec, data, z1);
    9788     2951585 :     GEN w = odd(k) ? gsub(u, v) : gadd(u, v);
    9789     2951585 :     S = gadd(S, gmul(powuu(m, k-1), w));
    9790             :   }
    9791      461986 :   return gerepileupto(av, gmul(S, rootsof1pow(z2, n)));
    9792             : }
    9793             : 
    9794             : static GEN
    9795       11375 : gausssumcx(GEN CHIvec, long prec)
    9796             : {
    9797       11375 :   GEN z, S, V = CHIvec_val(CHIvec);
    9798       11375 :   long m, N = CHIvec_N(CHIvec);
    9799       11375 :   z = rootsof1u_cx(N, prec);
    9800       11375 :   S = gmul(z, gel(V, N));
    9801       11375 :   for (m = N-1; m >= 1; m--) S = gmul(z, gadd(gel(V, m), S));
    9802       11375 :   return S;
    9803             : }
    9804             : 
    9805             : /* Computation of Q_k(\z_N^s) as a polynomial in \z_N^s. FIXME: explicit
    9806             :  * formula ? */
    9807             : static GEN
    9808        1729 : mfqk(long k, long N)
    9809             : {
    9810        1729 :   GEN X = pol_x(0), P = gsubgs(gpowgs(X,N), 1), ZI, Q, Xm1, invden;
    9811             :   long i;
    9812        1729 :   ZI = cgetg(N, t_VEC);
    9813        1729 :   for (i = 1; i < N; i++) gel(ZI, i) = utoi(i);
    9814        1729 :   ZI = gdivgs(gmul(X, gtopolyrev(ZI, 0)), N);
    9815        1729 :   if (k == 1) return ZI;
    9816        1071 :   invden = RgXQ_powu(ZI, k, P);
    9817        1071 :   Q = gneg(X); Xm1 = gsubgs(X, 1);
    9818        2716 :   for (i = 2; i < k; i++)
    9819        1645 :     Q = gmul(X, ZX_add(gmul(Xm1, ZX_deriv(Q)), gmulsg(-i, Q)));
    9820        1071 :   return RgXQ_mul(Q, invden, P);
    9821             : }
    9822             : /* CHI mfchar */
    9823             : /* Warning: M is a multiple of the conductor of CHI, but is NOT
    9824             :    necessarily its modulus */
    9825             : 
    9826             : static GEN
    9827        2513 : mfskcx(long k, GEN CHI, long M, long prec)
    9828             : {
    9829             :   GEN S, CHIvec, P;
    9830             :   long F, m, i, l;
    9831        2513 :   CHI = mfchartoprimitive(CHI, &F);
    9832        2513 :   CHIvec = mfcharcxinit(CHI, prec);
    9833        2513 :   if (F == 1) S = gdivgs(bernfrac(k), k);
    9834             :   else
    9835             :   {
    9836        1729 :     GEN Q = mfqk(k, F), V = CHIvec_val(CHIvec);
    9837        1729 :     S = gmul(gel(V, F), RgX_coeff(Q, 0));
    9838        1729 :     for (m = 1; m < F; m++) S = gadd(S, gmul(gel(V, m), RgX_coeff(Q, m)));
    9839        1729 :     S = conj_i(S);
    9840             :   }
    9841             :   /* prime divisors of M not dividing f(chi) */
    9842        2513 :   P = gel(myfactoru(u_ppo(M/F,F)), 1); l = lg(P);
    9843        2639 :   for (i = 1; i < l; i++)
    9844             :   {
    9845         126 :     long p = P[i];
    9846         126 :     S = gmul(S, gsubsg(1, gdiv(mychareval(CHIvec, p), powuu(p, k))));
    9847             :   }
    9848        2513 :   return gmul(gmul(gausssumcx(CHIvec, prec), S), powuu(M/F, k));
    9849             : }
    9850             : 
    9851             : static GEN
    9852        4634 : f00_i(long k, GEN CHI1vec, GEN CHI2vec, GEN G2, GEN S, long prec)
    9853             : {
    9854             :   GEN c, a;
    9855        4634 :   long N1 = CHIvec_N(CHI1vec), N2 = CHIvec_N(CHI2vec);
    9856        4634 :   if (S[2] != N1) return gen_0;
    9857        2513 :   c = mychareval(CHI1vec, S[3]);
    9858        2513 :   if (isintzero(c)) return gen_0;
    9859        2513 :   a = mfskcx(k, mfchardiv(CHIvec_CHI(CHI2vec), CHIvec_CHI(CHI1vec)), N1*N2, prec);
    9860        2513 :   a = gmul(a, conj_i(gmul(c,G2)));
    9861        2513 :   return gdiv(a, mulsi(-N2, powuu(S[1], k-1)));
    9862             : }
    9863             : 
    9864             : static GEN
    9865        3857 : f00(long k, GEN CHI1vec,GEN CHI2vec, GEN G1,GEN G2, GEN data, long prec)
    9866             : {
    9867             :   GEN T1, T2;
    9868        3857 :   T2 = f00_i(k, CHI1vec, CHI2vec, G2, data, prec);
    9869        3857 :   if (k > 1) return T2;
    9870         777 :   T1 = f00_i(k, CHI2vec, CHI1vec, G1, data, prec);
    9871         777 :   return gadd(T1, T2);
    9872             : }
    9873             : 
    9874             : /* ga in SL_2(Z), find beta [a,b;c,d] in Gamma_0(N) and mu in Z such that
    9875             :  * beta * ga * T^u = [A',B';C',D'] with C' | N and N | B', C' > 0 */
    9876             : static void
    9877        4431 : mfgatogap(GEN ga, long N, long *pA, long *pC, long *pD, long *pd, long *pmu)
    9878             : {
    9879        4431 :   GEN A = gcoeff(ga,1,1), B = gcoeff(ga,1,2);
    9880        4431 :   GEN C = gcoeff(ga,2,1), D = gcoeff(ga,2,2), a, b, c, d;
    9881             :   long t, Ap, Cp, B1, D1, mu;
    9882        4431 :   Cp = itou(bezout(muliu(A,N), C, &c, &d)); /* divides N */
    9883        4431 :   t = 0;
    9884        4431 :   if (Cp > 1)
    9885             :   { /* (d, N/Cp) = 1, find t such that (d - t*(A*N/Cp), N) = 1 */
    9886        2345 :     long dN = umodiu(d,Cp), Q = (N/Cp * umodiu(A,Cp)) % Cp;
    9887        2345 :     while (ugcd(dN, Cp) > 1) { t++; dN = Fl_sub(dN, Q, Cp); }
    9888             :   }
    9889        4431 :   if (t)
    9890             :   {
    9891         371 :     c = addii(c, mului(t, diviuexact(C,Cp)));
    9892         371 :     d = subii(d, mului(t, muliu(A, N/Cp))); /* (d,N) = 1 */
    9893             :   }
    9894        4431 :   D1 = umodiu(mulii(d,D), N);
    9895        4431 :   (void)bezout(d, mulis(c,-N), &a, &b); /* = 1 */
    9896        4431 :   t = 0; Ap = umodiu(addii(mulii(a,A), mulii(b,C)), N); /* (Ap,Cp) = 1 */
    9897        4431 :   while (ugcd(Ap, N) > 1) { t++; Ap = Fl_add(Ap, Cp, N); }
    9898        4431 :   B1 = umodiu(a,N)*umodiu(B,N) + umodiu(b,N)*umodiu(D,N) + t*D1;
    9899        4431 :   B1 %= N;
    9900        4431 :   *pmu = mu = Fl_neg(Fl_div(B1, Ap, N), N);
    9901             :   /* A', D' and d only needed modulo N */
    9902        4431 :   *pd = umodiu(d, N);
    9903        4431 :   *pA = Ap;
    9904        4431 :   *pC = Cp; *pD = (D1 + Cp*mu) % N;
    9905        4431 : }
    9906             : 
    9907             : #if 0
    9908             : /* CHI is a mfchar, return alpha(CHI) */
    9909             : static long
    9910             : mfalchi(GEN CHI, long AN, long cg)
    9911             : {
    9912             :   GEN G = gel(CHI,1), chi = gel(CHI,2), go = gmfcharorder(CHI);
    9913             :   long o = itou(go), a = itos( znchareval(G, chi, stoi(1 + AN/cg), go) );
    9914             :   if (a < 0 || (cg * a) % o) pari_err_BUG("mfalchi");
    9915             :   return (cg * a) / o;
    9916             : }
    9917             : #endif
    9918             : /* return A such that CHI1(t) * CHI2(t) = e(A) or NULL if (t,N1*N2) > 1 */
    9919             : static GEN
    9920        8862 : mfcharmuleval(GEN CHI1vec, GEN CHI2vec, long t)
    9921             : {
    9922        8862 :   long a1 = mycharexpo(CHI1vec, t), o1 = CHIvec_ord(CHI1vec);
    9923        8862 :   long a2 = mycharexpo(CHI2vec, t), o2 = CHIvec_ord(CHI2vec);;
    9924        8862 :   if (a1 < 0 || a2 < 0) return NULL;
    9925        8862 :   return sstoQ(a1*o2 + a2*o1, o1*o2);
    9926             : }
    9927             : static GEN
    9928        4431 : mfcharmulcxeval(GEN CHI1vec, GEN CHI2vec, long t, long prec)
    9929             : {
    9930        4431 :   GEN A = mfcharmuleval(CHI1vec, CHI2vec, t);
    9931             :   long n, d;
    9932        4431 :   if (!A) return gen_0;
    9933        4431 :   Qtoss(A, &n,&d); return rootsof1q_cx(n, d, prec);
    9934             : }
    9935             : /* alpha(CHI1 * CHI2) */
    9936             : static long
    9937        4431 : mfalchi2(GEN CHI1vec, GEN CHI2vec, long AN, long cg)
    9938             : {
    9939        4431 :   GEN A = mfcharmuleval(CHI1vec, CHI2vec, 1 + AN/cg);
    9940             :   long a;
    9941        4431 :   if (!A) pari_err_BUG("mfalchi2");
    9942        4431 :   A = gmulsg(cg, A);
    9943        4431 :   if (typ(A) != t_INT) pari_err_BUG("mfalchi2");
    9944        4431 :   a = itos(A) % cg; if (a < 0) a += cg;
    9945        4431 :   return a;
    9946             : }
    9947             : 
    9948             : /* return g = (a,b), set u >= 0 s.t. g = a * u (mod b) */
    9949             : static long
    9950       17724 : mybezout(long a, long b, long *pu)
    9951             : {
    9952       17724 :   long junk, g = cbezout(a, b, pu, &junk);
    9953       17724 :   if (*pu < 0) *pu += b/g;
    9954       17724 :   return g;
    9955             : }
    9956             : 
    9957             : /* E = [k, CHI1,CHI2, e], CHI1 and CHI2 primitive mfchars such that,
    9958             :  * CHI1(-1)*CHI2(-1) = (-1)^k; expansion of (B_e (E_k(CHI1,CHI2))) | ga.
    9959             :  * w is the width for the space of the calling function. */
    9960             : static GEN
    9961        4431 : mfeisensteingacx(GEN E, long w, GEN ga, long lim, long prec)
    9962             : {
    9963        4431 :   GEN CHI1vec, CHI2vec, CHI1 = gel(E,2), CHI2 = gel(E,3), v, S, ALPHA;
    9964             :   GEN G1, G2, z1, z2, data;
    9965        4431 :   long k = itou(gel(E,1)), e = itou(gel(E,4));
    9966        4431 :   long N1 = mfcharmodulus(CHI1);
    9967        4431 :   long N2 = mfcharmodulus(CHI2), N = e * N1 * N2;
    9968             :   long NsurC, cg, wN, A, C, Ai, d, mu, alchi, na, da;
    9969             :   long eg, g, gH, U, u0, u1, u2, Aig, H, m, n, t, Cg, NC1, NC2;
    9970             : 
    9971        4431 :   mfgatogap(ga, N, &A, &C, &Ai, &d, &mu);
    9972        4431 :   CHI1vec = mfcharcxinit(CHI1, prec);
    9973        4431 :   CHI2vec = mfcharcxinit(CHI2, prec);
    9974        4431 :   NsurC = N/C; cg  = ugcd(C, NsurC); wN = NsurC / cg;
    9975        4431 :   if (w%wN) pari_err_BUG("mfeisensteingacx [wN does not divide w]");
    9976        4431 :   alchi = mfalchi2(CHI1vec, CHI2vec, A*N, cg);
    9977        4431 :   ALPHA = sstoQ(alchi, NsurC);
    9978             : 
    9979        4431 :   g = mybezout(A*e, C, &u0); Cg = C/g; eg = e/g;
    9980        4431 :   NC1 = mybezout(N1, Cg, &u1);
    9981        4431 :   NC2 = mybezout(N2, Cg, &u2);
    9982        4431 :   H = (NC1*NC2*g)/Cg;
    9983        4431 :   Aig = (Ai*H)%N; if (Aig < 0) Aig += N;
    9984        4431 :   z1 = rootsof1powinit(u0, Cg, prec);
    9985        4431 :   z2 = rootsof1powinit(Aig, N, prec);
    9986        4431 :   data = mkvecsmalln(8, N1/NC1, N2/NC2, NC1, NC2, Cg/NC1, Cg/NC2, u1, u2);
    9987        4431 :   v = zerovec(lim + 1);
    9988             :   /* need n*H = alchi (mod cg) */
    9989        4431 :   gH = mybezout(H, cg, &U);
    9990        4431 :   if (gH > 1)
    9991             :   {
    9992         357 :     if (alchi % gH) return mkvec2(gen_0, v);
    9993         357 :     alchi /= gH; cg /= gH; H /= gH;
    9994             :   }
    9995        4431 :   G1 = gausssumcx(CHI1vec, prec);
    9996        4431 :   G2 = gausssumcx(CHI2vec, prec);
    9997        4431 :   if (!alchi)
    9998        3857 :     gel(v,1) = f00(k, CHI1vec,CHI2vec,G1,G2, mkvecsmall3(NC2,Cg,A*eg), prec);
    9999        4431 :   n = Fl_mul(alchi,U,cg); if (!n) n = cg;
   10000        4431 :   m = (n*H - alchi) / cg; /* positive, exact division */
   10001      466417 :   for (; m <= lim; n+=cg, m+=H)
   10002      461986 :     gel(v, m+1) = fg1g2n(n, k, CHI1vec, CHI2vec, data, z1,z2);
   10003        4431 :   t = (2*e)/g; if (odd(k)) t = -t;
   10004        4431 :   v = gdiv(v, gmul(conj_i(gmul(G1,G2)), mulsi(t, powuu(eg*N2/NC2, k-1))));
   10005        4431 :   if (k == 2 && N1 == 1 && N2 == 1) v = gsub(mkF2bd(wN,lim), gmulsg(e,v));
   10006             : 
   10007        4431 :   Qtoss(ALPHA, &na,&da);
   10008        4431 :   S = conj_i( mfcharmulcxeval(CHI1vec,CHI2vec,d,prec) ); /* CHI(1/d) */
   10009        4431 :   if (wN > 1)
   10010             :   {
   10011        3290 :     GEN z = rootsof1powinit(-mu, wN, prec);
   10012        3290 :     long i, l = lg(v);
   10013        3290 :     for (i = 1; i < l; i++) gel(v,i) = gmul(gel(v,i), rootsof1pow(z,i-1));
   10014             :   }
   10015        4431 :   v = RgV_Rg_mul(v, gmul(S, rootsof1q_cx(-mu*na, da, prec)));
   10016        4431 :   return mkvec2(ALPHA, bdexpand(v, w/wN));
   10017             : }
   10018             : 
   10019             : /*****************************************************************/
   10020             : /*                       END EISENSTEIN CUSPS                    */
   10021             : /*****************************************************************/
   10022             : 
   10023             : static GEN
   10024        1582 : mfchisimpl(GEN CHI)
   10025             : {
   10026             :   GEN G, chi;
   10027        1582 :   if (typ(CHI) == t_INT) return CHI;
   10028        1582 :   G = gel(CHI, 1); chi = gel(CHI, 2);
   10029        1582 :   switch(mfcharorder(CHI))
   10030             :   {
   10031        1134 :     case 1: chi = gen_1; break;
   10032         427 :     case 2: chi = znchartokronecker(G,chi,1); break;
   10033          21 :     default:chi = mkintmod(znconreyexp(G,chi), znstar_get_N(G)); break;
   10034             :   }
   10035        1582 :   return chi;
   10036             : }
   10037             : 
   10038             : GEN
   10039         700 : mfparams(GEN F)
   10040             : {
   10041         700 :   pari_sp av = avma;
   10042             :   GEN z, mf;
   10043         700 :   if ((mf = checkMF_i(F)))
   10044             :   {
   10045          14 :     long N = MF_get_N(mf);
   10046          14 :     GEN gk = MF_get_gk(mf);
   10047          14 :     z = mkvec4(utoi(N), gk, MF_get_CHI(mf), utoi(MF_get_space(mf)));
   10048             :   }
   10049             :   else
   10050             :   {
   10051         686 :     if (!checkmf_i(F)) pari_err_TYPE("mfparams", F);
   10052         686 :     z = shallowcopy( mf_get_NK(F) );
   10053             :   }
   10054         700 :   gel(z,3) = mfchisimpl(gel(z,3));
   10055         700 :   return gerepilecopy(av, z);
   10056             : }
   10057             : 
   10058             : GEN
   10059          14 : mfisCM(GEN F)
   10060             : {
   10061          14 :   pari_sp av = avma;
   10062             :   forprime_t S;
   10063             :   GEN D, v;
   10064             :   long N, k, lD, sb, p, i;
   10065          14 :   if (!checkmf_i(F)) pari_err_TYPE("mfisCM", F);
   10066          14 :   N = mf_get_N(F);
   10067          14 :   k = mf_get_k(F); if (N < 0 || k < 0) pari_err_IMPL("mfisCM for this F");
   10068          14 :   D = mfunram(N, -1);
   10069          14 :   lD = lg(D);
   10070          14 :   sb = maxss(mfsturmNk(N, k), 4*N);
   10071          14 :   v = mfcoefs_i(F, sb, 1);
   10072          14 :   u_forprime_init(&S, 2, sb);
   10073         518 :   while ((p = u_forprime_next(&S)))
   10074             :   {
   10075         490 :     GEN ap = gel(v, p+1);
   10076         490 :     if (!gequal0(ap))
   10077         406 :       for (i = 1; i < lD; i++)
   10078         245 :         if (kross(D[i], p) == -1) { D = vecsplice(D, i); lD--; }
   10079             :   }
   10080          14 :   if (lD == 1) { set_avma(av); return gen_0; }
   10081          14 :   if (lD == 2) { set_avma(av); return stoi(D[1]); }
   10082           7 :   if (k > 1) pari_err_BUG("mfisCM");
   10083           7 :   return gerepileupto(av, zv_to_ZV(D));
   10084             : }
   10085             : 
   10086             : static long
   10087         287 : mfspace_i(GEN mf, GEN F)
   10088             : {
   10089             :   GEN v, vF, gk;
   10090             :   long n, nE, i, l, s, N;
   10091             : 
   10092         287 :   mf = checkMF(mf); s = MF_get_space(mf);
   10093         287 :   if (!F) return s;
   10094         287 :   if (!checkmf_i(F)) pari_err_TYPE("mfspace",F);
   10095         287 :   v = mftobasis(mf, F, 1);
   10096         287 :   n = lg(v)-1; if (!n) return -1;
   10097         231 :   nE = lg(MF_get_E(mf))-1;
   10098         231 :   switch(s)
   10099             :   {
   10100          56 :     case mf_NEW: case mf_OLD: case mf_EISEN: return s;
   10101             :     case mf_FULL:
   10102         140 :       if (mf_get_type(F) == t_MF_THETA) return mf_EISEN;
   10103         133 :       if (!gequal0(vecslice(v,1,nE)))
   10104          63 :         return gequal0(vecslice(v,nE+1,n))? mf_EISEN: mf_FULL;
   10105             :   }
   10106             :   /* mf is mf_CUSP or mf_FULL, F a cusp form */
   10107         105 :   gk = mf_get_gk(F);
   10108         105 :   if (typ(gk) == t_FRAC || equali1(gk)) return mf_CUSP;
   10109          91 :   vF = mftonew_i(mf, vecslice(v, nE+1, n), &N);
   10110          91 :   if (N != MF_get_N(mf)) return mf_OLD;
   10111          63 :   l = lg(vF);
   10112         105 :   for (i = 1; i < l; i++)
   10113          63 :     if (itos(gmael(vF,i,1)) != N) return mf_CUSP;
   10114          42 :   return mf_NEW;
   10115             : }
   10116             : long
   10117         287 : mfspace(GEN mf, GEN F)
   10118         287 : { pari_sp av = avma; return gc_long(av, mfspace_i(mf,F)); }
   10119             : static GEN
   10120           7 : lfunfindchi(GEN ldata, GEN van, long prec)
   10121             : {
   10122           7 :   GEN gN = ldata_get_conductor(ldata), G = znstar0(gN,1), L, go, vz;
   10123           7 :   long k = ldata_get_k(ldata), N = itou(gN), bit = 10 - prec2nbits(prec);
   10124           7 :   long i, j, o, l, odd = k & 1, B0 = 2, B = lg(van)-1;
   10125             : 
   10126           7 :   van = shallowcopy(van);
   10127           7 :   L = cyc2elts(znstar_get_conreycyc(G));
   10128           7 :   l = lg(L);
   10129          21 :   for (i = j = 1; i < l; i++)
   10130             :   {
   10131          14 :     GEN chi = zc_to_ZC(gel(L,i));
   10132          14 :     if (zncharisodd(G,chi) == odd) gel(L,j++) = mfcharGL(G,chi);
   10133             :   }
   10134           7 :   setlg(L,j); l = j;
   10135           7 :   if (l <= 2) return gel(L,1);
   10136           0 :   o = znstar_get_expo(G); go = utoi(o);
   10137           0 :   vz = grootsof1(o, prec);
   10138             :   for (;;)
   10139           0 :   {
   10140             :     long n;
   10141           0 :     for (n = B0; n <= B; n++)
   10142             :     {
   10143           0 :       GEN an = gel(van,n), r;
   10144             :       long j;
   10145           0 :       if (ugcd(n, N) != 1 || gexpo(an) < bit) continue;
   10146           0 :       r = gdiv(an, conj_i(an));
   10147           0 :       for (i = 1; i < l; i++)
   10148             :       {
   10149           0 :         GEN CHI = gel(L,i);
   10150           0 :         if (gexpo(gsub(r, gel(vz, znchareval_i(CHI,n,go)+1))) > bit)
   10151           0 :           gel(L,i) = NULL;
   10152             :       }
   10153           0 :       for (i = j = 1; i < l; i++)
   10154           0 :         if (gel(L,i)) gel(L,j++) = gel(L,i);
   10155           0 :       l = j; setlg(L,l);
   10156           0 :       if (l == 2) return gel(L,1);
   10157             :     }
   10158           0 :     B0 = B+1; B <<= 1;
   10159           0 :     van = ldata_vecan(ldata_get_an(ldata), B, prec);
   10160             :   }
   10161             : }
   10162             : 
   10163             : GEN
   10164           7 : mffromlfun(GEN L, long prec)
   10165             : {
   10166           7 :   pari_sp av = avma;
   10167           7 :   GEN ldata = lfunmisc_to_ldata_shallow(L), Vga = ldata_get_gammavec(ldata);
   10168             :   GEN van, a0, CHI, NK;
   10169             :   long k, N, space;
   10170           7 :   if (!gequal(Vga, mkvec2(gen_0, gen_1))) pari_err_TYPE("mffromlfun", L);
   10171           7 :   k = ldata_get_k(ldata);
   10172           7 :   N = itou(ldata_get_conductor(ldata));
   10173           7 :   van = ldata_vecan(ldata_get_an(ldata), mfsturmNk(N,k) + 2, prec);
   10174           7 :   CHI = lfunfindchi(ldata, van, prec);
   10175           7 :   space = (lg(ldata) == 7)? mf_CUSP: mf_FULL;
   10176           7 :   a0 = (space == mf_CUSP)? gen_0: gneg(lfun(L, gen_0, prec2nbits(prec)));
   10177           7 :   NK = mkvec3(utoi(N), utoi(k), mfchisimpl(CHI));
   10178           7 :   return gerepilecopy(av, mkvec3(NK, utoi(space), shallowconcat(a0, van)));
   10179             : }
   10180             : /*******************************************************************/
   10181             : /*                                                                 */
   10182             : /*                       HALF-INTEGRAL WEIGHT                      */
   10183             : /*                                                                 */
   10184             : /*******************************************************************/
   10185             : /* We use the prefix mf2; k represents the weight -1/2, so e.g.
   10186             :    k = 2 is weight 5/2. N is the level, so 4\mid N, and CHI is the
   10187             :    character, always even. */
   10188             : 
   10189             : static long
   10190        3360 : lamCO(long r, long s, long p)
   10191             : {
   10192        3360 :   if ((s << 1) <= r)
   10193             :   {
   10194        1232 :     long rp = r >> 1;
   10195        1232 :     if (odd(r)) return upowuu(p, rp) << 1;
   10196         336 :     else return (p + 1)*upowuu(p, rp - 1);
   10197             :   }
   10198        2128 :   else return upowuu(p, r - s) << 1;
   10199             : }
   10200             : 
   10201             : static int
   10202        1568 : condC(GEN faN, GEN valF)
   10203             : {
   10204        1568 :   GEN P = gel(faN, 1), E = gel(faN, 2);
   10205        1568 :   long l = lg(P), i;
   10206        3696 :   for (i = 1; i < l; i++)
   10207        3024 :     if ((P[i] & 3L) == 3)
   10208             :     {
   10209        1120 :       long r = E[i];
   10210        1120 :       if (odd(r) || r < (valF[i] << 1)) return 1;
   10211             :     }
   10212         672 :   return 0;
   10213             : }
   10214             : 
   10215             : /* returns 2*zetaCO; weight is k + 1/2 */
   10216             : static long
   10217        3696 : zeta2CO(GEN faN, GEN valF, long r2, long s2, long k)
   10218             : {
   10219        3696 :   if (r2 >= 4) return lamCO(r2, s2, 2) << 1;
   10220        2912 :   if (r2 == 3) return 6;
   10221        1568 :   if (condC(faN, valF)) return 4;
   10222         672 :   if (odd(k)) return s2 ? 3 : 5; else return s2 ? 5: 3;
   10223             : }
   10224             : 
   10225             : /* returns 4 times last term in formula */
   10226             : static long
   10227        3696 : dim22(long N, long F, long k)
   10228             : {
   10229        3696 :   pari_sp av = avma;
   10230        3696 :   GEN vF, faN = myfactoru(N), P = gel(faN, 1), E = gel(faN, 2);
   10231        3696 :   long i, D, l = lg(P);
   10232        3696 :   vF = cgetg(l, t_VECSMALL);
   10233        3696 :   for (i = 1; i < l; i++) vF[i] = u_lval(F, P[i]);
   10234        3696 :   D = zeta2CO(faN, vF, E[1], vF[1], k);
   10235        3696 :   for (i = 2; i < l; i++) D *= lamCO(E[i], vF[i], P[i]);
   10236        3696 :   return gc_long(av,D);
   10237             : }
   10238             : 
   10239             : /* PSI not necessarily primitive, of conductor F */
   10240             : static int
   10241       13846 : charistotallyeven(GEN PSI, long F)
   10242             : {
   10243       13846 :   pari_sp av = avma;
   10244       13846 :   GEN P = gel(myfactoru(F), 1);
   10245       13846 :   GEN G = gel(PSI,1), psi = gel(PSI,2);
   10246             :   long i;
   10247       14350 :   for (i = 1; i < lg(P); i++)
   10248             :   {
   10249         532 :     GEN psip = znchardecompose(G, psi, utoipos(P[i]));
   10250         532 :     if (zncharisodd(G, psip)) return gc_bool(av,0);
   10251             :   }
   10252       13818 :   return gc_bool(av,1);
   10253             : }
   10254             : 
   10255             : static GEN
   10256      299775 : get_PSI(GEN CHI, long t)
   10257             : {
   10258      299775 :   long r = t & 3L, t2 = (r == 2 || r == 3) ? t << 2 : t;
   10259      299775 :   return mfcharmul_i(CHI, induce(gel(CHI,1), utoipos(t2)));
   10260             : }
   10261             : /* space = mf_CUSP, mf_EISEN or mf_FULL, weight k + 1/2 */
   10262             : static long
   10263       41363 : mf2dimwt12(long N, GEN CHI, long space)
   10264             : {
   10265       41363 :   pari_sp av = avma;
   10266       41363 :   GEN D = mydivisorsu(N >> 2);
   10267       41363 :   long i, l = lg(D), dim3 = 0, dim4 = 0;
   10268             : 
   10269       41363 :   CHI = induceN(N, CHI);
   10270      341138 :   for (i = 1; i < l; i++)
   10271             :   {
   10272      299775 :     long rp, t = D[i], Mt = D[l-i];
   10273      299775 :     GEN PSI = get_PSI(CHI,t);
   10274      299775 :     rp = mfcharconductor(PSI);
   10275      299775 :     if (Mt % (rp*rp) == 0) { dim4++; if (charistotallyeven(PSI,rp)) dim3++; }
   10276             :   }
   10277       41363 :   set_avma(av);
   10278       41363 :   switch (space)
   10279             :   {
   10280       40439 :     case mf_CUSP: return dim4 - dim3;
   10281         462 :     case mf_EISEN:return dim3;
   10282         462 :     case mf_FULL: return dim4;
   10283             :   }
   10284             :   return 0; /*LCOV_EXCL_LINE*/
   10285             : }
   10286             : 
   10287             : static long
   10288         693 : mf2dimwt32(long N, GEN CHI, long F, long space)
   10289             : {
   10290             :   long D;
   10291         693 :   switch(space)
   10292             :   {
   10293         231 :     case mf_CUSP: D = mypsiu(N) - 6*dim22(N, F, 1);
   10294         231 :       if (D%24) pari_err_BUG("mfdim");
   10295         231 :       return D/24 + mf2dimwt12(N, CHI, 4);
   10296         231 :     case mf_FULL: D = mypsiu(N) + 6*dim22(N, F, 0);
   10297         231 :       if (D%24) pari_err_BUG("mfdim");
   10298         231 :       return D/24 + mf2dimwt12(N, CHI, 1);
   10299         231 :     case mf_EISEN: D = dim22(N, F, 0) + dim22(N, F, 1);
   10300         231 :       if (D & 3L) pari_err_BUG("mfdim");
   10301         231 :       return (D >> 2) - mf2dimwt12(N, CHI, 3);
   10302             :   }
   10303             :   return 0; /*LCOV_EXCL_LINE*/
   10304             : }
   10305             : 
   10306             : /* F = conductor(CHI), weight k = r+1/2 */
   10307             : static long
   10308       43673 : checkmf2(long N, long r, GEN CHI, long F, long space)
   10309             : {
   10310       43673 :   switch(space)
   10311             :   {
   10312       43652 :     case mf_FULL: case mf_CUSP: case mf_EISEN: break;
   10313             :     case mf_NEW: case mf_OLD:
   10314          14 :       pari_err_TYPE("half-integral weight [new/old spaces]", utoi(space));
   10315             :     default:
   10316           7 :       pari_err_TYPE("half-integral weight [incorrect space]",utoi(space));
   10317             :   }
   10318       43652 :   if (N & 3L)
   10319           0 :     pari_err_DOMAIN("half-integral weight", "N % 4", "!=", gen_0, stoi(N));
   10320       43652 :   return r >= 0 && mfcharparity(CHI) == 1 && N % F == 0;
   10321             : }
   10322             : 
   10323             : /* weight k = r + 1/2 */
   10324             : static long
   10325       43463 : mf2dim_Nkchi(long N, long r, GEN CHI, ulong space)
   10326             : {
   10327       43463 :   long D, D2, F = mfcharconductor(CHI);
   10328       43463 :   if (!checkmf2(N, r, CHI, F, space)) return 0;
   10329       43442 :   if (r == 0) return mf2dimwt12(N, CHI, space);
   10330        2772 :   if (r == 1) return mf2dimwt32(N, CHI, F, space);
   10331        2079 :   if (space == mf_EISEN)
   10332             :   {
   10333         693 :     D = dim22(N, F, r) + dim22(N, F, 1-r);
   10334         693 :     if (D & 3L) pari_err_BUG("mfdim");
   10335         693 :     return D >> 2;
   10336             :   }
   10337        1386 :   D2 = space == mf_FULL? dim22(N, F, 1-r): -dim22(N, F, r);
   10338        1386 :   D = (2*r-1)*mypsiu(N) + 6*D2;
   10339        1386 :   if (D%24) pari_err_BUG("mfdim");
   10340        1386 :   return D/24;
   10341             : }
   10342             : 
   10343             : /* weight k=r+1/2 */
   10344             : static GEN
   10345         210 : mf2init_Nkchi(long N, long r, GEN CHI, long space, long flraw)
   10346             : {
   10347         210 :   GEN Minv, Minvmat, B, M, gk = gaddsg(r,ghalf);
   10348         210 :   GEN mf1 = mkvec4(utoi(N),gk,CHI,utoi(space));
   10349             :   long L;
   10350         210 :   if (!checkmf2(N, r, CHI, mfcharconductor(CHI), space)) return mfEMPTY(mf1);
   10351         210 :   if (space==mf_EISEN) pari_err_IMPL("half-integral weight Eisenstein space");
   10352         210 :   L = mfsturmNgk(N, gk) + 1;
   10353         210 :   B = mf2basis(N, r, CHI, space);
   10354         210 :   M = mflineardivtomat(N,B,L);
   10355         210 :   if (flraw) M = mkvec3(gen_0,gen_0,M);
   10356             :   else
   10357             :   {
   10358         210 :     M = mfcleanCHI(M, CHI, 0);
   10359         210 :     Minv = gel(M,2);
   10360         210 :     Minvmat = RgM_Minv_mul(NULL, Minv);
   10361         210 :     B = vecmflineardiv_linear(B, Minvmat);
   10362         210 :     gel(M,3) = RgM_Minv_mul(gel(M,3), Minv);
   10363         210 :     gel(M,2) = mkMinv(matid(lg(B)-1), NULL,NULL,NULL);
   10364             :   }
   10365         210 :   return mkmf(mf1, cgetg(1,t_VEC), B, gen_0, M);
   10366             : }
   10367             : 
   10368             : /**************************************************************************/
   10369             : /*                          Kohnen + space                                */
   10370             : /**************************************************************************/
   10371             : 
   10372             : static GEN
   10373          21 : mfkohnenbasis_i(GEN mf, GEN CHIP, long eps, long sb)
   10374             : {
   10375          21 :   GEN M = shallowtrans(mfcoefs_mf(mf, sb, 1)), ME;
   10376             :   long c, i, n;
   10377          21 :   ME = cgetg(sb + 2, t_MAT);
   10378         784 :   for (i = 0, c = 1; i <= sb; i++)
   10379             :   {
   10380         763 :     long j = i & 3L;
   10381         763 :     if (j == 2 || j == 2 + eps) gel(ME, c++) = gel(M, i+1);
   10382             :   }
   10383          21 :   setlg(ME, c); ME = shallowtrans(Q_primpart(ME));
   10384          21 :   n = mfcharorder_canon(CHIP);
   10385          21 :   return n == 1? ZM_ker(ME): ZabM_ker(liftpol_shallow(ME), mfcharpol(CHIP), n);
   10386             : }
   10387             : GEN
   10388          21 : mfkohnenbasis(GEN mf)
   10389             : {
   10390          21 :   pari_sp av = avma;
   10391             :   GEN gk, CHI, CHIP, K;
   10392             :   long N4, r, eps, sb;
   10393          21 :   mf = checkMF(mf);
   10394          21 :   if (MF_get_space(mf) != mf_CUSP)
   10395           0 :     pari_err_TYPE("mfkohnenbasis [not a cuspidal space", mf);
   10396          21 :   if (!MF_get_dim(mf)) return cgetg(1, t_MAT);
   10397          21 :   N4 = MF_get_N(mf) >> 2; gk = MF_get_gk(mf); CHI = MF_get_CHI(mf);
   10398          21 :   if (typ(gk) == t_INT) pari_err_TYPE("mfkohnenbasis", gk);
   10399          21 :   r = MF_get_r(mf);
   10400          21 :   CHIP = mfcharchiliftprim(CHI, N4);
   10401          21 :   eps = CHIP==CHI? 1: -1;
   10402          21 :   if (!CHIP) pari_err_TYPE("mfkohnenbasis [incorrect CHI]", CHI);
   10403          21 :   if (odd(r)) eps = -eps;
   10404          21 :   if (uissquarefree(N4))
   10405             :   {
   10406          14 :     long d = mfdim_Nkchi(N4, 2*r, mfcharpow(CHI, gen_2), mf_CUSP);
   10407          14 :     sb = mfsturmNgk(N4 << 2, gk) + 1;
   10408          14 :     K = mfkohnenbasis_i(mf, CHIP, eps, sb);
   10409          14 :     if (lg(K) - 1 == d) return gerepilecopy(av, K);
   10410             :   }
   10411           7 :   sb = mfsturmNgk(N4 << 4, gk) + 1;
   10412           7 :   K = mfkohnenbasis_i(mf, CHIP, eps, sb);
   10413           7 :   return gerepilecopy(av, K);
   10414             : }
   10415             : 
   10416             : /* return [mf3, bijection, mfkohnenbasis, codeshi] */
   10417             : static GEN
   10418          14 : mfkohnenbijection_i(GEN mf)
   10419             : {
   10420          14 :   GEN vB, mf3, K, SHI, P, CHI = MF_get_CHI(mf);
   10421          14 :   long n, lK, i, dim, m, lw, sb3, N4 = MF_get_N(mf)>>2, r = MF_get_r(mf);
   10422          14 :   long Dp[] = {1, 5, 8, 12, 13, 17, 21, 24};
   10423          14 :   long Dm[] = {-3, -4, -7, -8, -11, -15, -19, -20}, *D = odd(r)? Dm: Dp;
   10424          14 :   const long nbD = 8, MAXm = 6560; /* #D, 3^#D - 1 */
   10425             : 
   10426          14 :   K = mfkohnenbasis(mf); lK = lg(K);
   10427          14 :   mf3 = mfinit_Nkchi(N4, r<<1, mfcharpow(CHI,gen_2), mf_CUSP, 0);
   10428          14 :   if (MF_get_dim(mf3) != lK - 1)
   10429           0 :     pari_err_BUG("mfkohnenbijection [different dimensions]");
   10430          14 :   if (lK == 1) return mkvec4(mf3, cgetg(1, t_MAT), K, cgetg(1, t_VEC));
   10431          14 :   CHI = mfcharchiliftprim(CHI, N4);
   10432          14 :   if (!CHI) pari_err_TYPE("mfkohnenbijection [incorrect CHI]", CHI);
   10433          14 :   n = mfcharorder_canon(CHI);
   10434          14 :   P = n==1? NULL: mfcharpol(CHI);
   10435          14 :   SHI = cgetg(nbD+1, t_VEC);
   10436          14 :   sb3 = mfsturm(mf3);
   10437          14 :   vB = RgM_mul(mfcoefs_mf(mf, labs(D[nbD-1])*sb3*sb3, 1), K);
   10438          14 :   dim = MF_get_dim(mf3);
   10439          35 :   for (m = 1, lw = 0; m <= MAXm; m += (m%3)? 2: 1)
   10440             :   {
   10441             :     pari_sp av;
   10442          35 :     ulong m1, y, v = u_lvalrem(m, 3, &y);
   10443             :     GEN z, M;
   10444             :     long j;
   10445          35 :     if (y == 1)
   10446             :     {
   10447          28 :       long d = D[v];
   10448          28 :       GEN a = cgetg(lK, t_MAT);
   10449          98 :       for (i = 1; i < lK; i++)
   10450             :       {
   10451          70 :         pari_sp av2 = avma;
   10452          70 :         GEN f = c_deflate(sb3*sb3, labs(d), gel(vB,i));
   10453          70 :         f = mftobasis_i(mf3, RgV_shimura(f, sb3, d, N4, r, CHI));
   10454          70 :         gel(a,i) = gerepileupto(av2, f);
   10455             :       }
   10456          28 :       lw++; gel(SHI,v+1) = a;
   10457             :     }
   10458          35 :     av = avma; M = NULL;
   10459          91 :     for (j = 1, m1 = m; j <= lw; j++, m1/=3)
   10460             :     {
   10461          56 :       long s = m1%3;
   10462          56 :       if (s)
   10463             :       {
   10464          42 :         GEN t = gel(SHI,j);
   10465          42 :         if (M) M = (s == 2)? RgM_sub(M, t): RgM_add(M, t);
   10466          35 :         else   M = (s == 2)? RgM_neg(t): t;
   10467             :       }
   10468             :     }
   10469          35 :     z = QabM_indexrank(M,P,n);
   10470          35 :     if (lg(gel(z,2)) > dim)
   10471             :     {
   10472          14 :       GEN d = ZV_to_zv( digits(utoipos(m), utoipos(3)) );
   10473          14 :       GEN mres, dMi, Mi = QabM_pseudoinv(M,P,n, NULL,&dMi);
   10474          14 :       long ld = lg(d), c = 1;
   10475          14 :       if (DEBUGLEVEL>1)
   10476           0 :         err_printf("mfkohnenbijection: used %ld discriminants\n",lw);
   10477          14 :       mres = cgetg(ld, t_VEC);
   10478          42 :       for (j = ld-1; j >= 1; j--)
   10479          28 :         if (d[j]) gel(mres,c++) = mkvec2s(D[ld-j-1], d[j]);
   10480          14 :       setlg(mres,c); return mkvec4(mf3, RgM_Rg_div(Mi,dMi), K, mres);
   10481             :     }
   10482          21 :     set_avma(av);
   10483             :   }
   10484           0 :   pari_err_BUG("mfkohnenbijection");
   10485             :   return NULL; /*LCOV_EXCL_LINE*/
   10486             : }
   10487             : GEN
   10488          14 : mfkohnenbijection(GEN mf)
   10489             : {
   10490          14 :   pari_sp av = avma;
   10491             :   long N;
   10492          14 :   mf = checkMF(mf); N = MF_get_N(mf);
   10493          14 :   if (!uissquarefree(N >> 2))
   10494           0 :     pari_err_TYPE("mfkohnenbijection [N/4 not squarefree]", utoi(N));
   10495          14 :   if (MF_get_space(mf) != mf_CUSP || MF_get_r(mf) == 0 || !mfshimura_space_cusp(mf))
   10496           0 :     pari_err_TYPE("mfkohnenbijection [incorrect mf for Kohnen]", mf);
   10497          14 :   return gerepilecopy(av, mfkohnenbijection_i(mf));
   10498             : }
   10499             : 
   10500             : static int
   10501           7 : checkbij_i(GEN b)
   10502             : {
   10503          14 :   return typ(b) == t_VEC && lg(b) == 5 && checkMF_i(gel(b,1))
   10504           7 :          && typ(gel(b,2)) == t_MAT
   10505           7 :          && typ(gel(b,3)) == t_MAT
   10506          14 :          && typ(gel(b,4)) == t_VEC;
   10507             : }
   10508             : 
   10509             : /* bij is the output of mfkohnenbijection */
   10510             : GEN
   10511           7 : mfkohneneigenbasis(GEN mf, GEN bij)
   10512             : {
   10513           7 :   pari_sp av = avma;
   10514             :   GEN mf3, mf30, B, KM, M, k;
   10515             :   long r, i, l, N4;
   10516           7 :   mf = checkMF(mf);
   10517           7 :   if (!checkbij_i(bij))
   10518           0 :     pari_err_TYPE("mfkohneneigenbasis [bijection]", bij);
   10519           7 :   if (MF_get_space(mf) != mf_CUSP)
   10520           0 :     pari_err_TYPE("mfkohneneigenbasis [not a cuspidal space]", mf);
   10521           7 :   if (!MF_get_dim(mf))
   10522           0 :     retmkvec3(cgetg(1, t_VEC), cgetg(1, t_VEC), cgetg(1, t_VEC));
   10523           7 :   N4 = MF_get_N(mf) >> 2; k = MF_get_gk(mf);
   10524           7 :   if (typ(k) == t_INT) pari_err_TYPE("mfkohneneigenbasis", k);
   10525           7 :   if (!uissquarefree(N4))
   10526           0 :     pari_err_TYPE("mfkohneneigenbasis [N not squarefree]", utoipos(N4));
   10527           7 :   r = MF_get_r(mf);
   10528           7 :   KM = RgM_mul(gel(bij,3), gel(bij,2));
   10529           7 :   mf3 = gel(bij,1);
   10530           7 :   mf30 = mfinit_Nkchi(N4, 2*r, MF_get_CHI(mf3), mf_NEW, 0);
   10531           7 :   B = mfcoefs_mf(mf30, mfsturm_mf(mf3), 1); l = lg(B);
   10532           7 :   M = cgetg(l, t_MAT);
   10533           7 :   for (i=1; i<l; i++) gel(M,i) = RgM_RgC_mul(KM, mftobasis_i(mf3, gel(B,i)));
   10534           7 :   return gerepilecopy(av, mkvec3(mf30, M, RgM_mul(M, MF_get_newforms(mf30))));
   10535             : }
   10536             : /*************************** End Kohnen ************************************/
   10537             : /***************************************************************************/
   10538             : 
   10539             : static GEN desc(GEN F);
   10540             : static GEN
   10541         504 : desc_mfeisen(GEN F)
   10542             : {
   10543         504 :   GEN R, gk = mf_get_gk(F);
   10544         504 :   if (typ(gk) == t_FRAC)
   10545           7 :     R = gsprintf("H_{%Ps}", gk);
   10546             :   else
   10547             :   {
   10548         497 :     GEN vchi = gel(F, 2), CHI = mfchisimpl(gel(vchi, 3));
   10549         497 :     long k = itou(gk);
   10550         497 :     if (lg(vchi) < 5) R = gsprintf("F_%ld(%Ps)", k, CHI);
   10551             :     else
   10552             :     {
   10553         294 :       GEN CHI2 = mfchisimpl(gel(vchi, 4));
   10554         294 :       R = gsprintf("F_%ld(%Ps, %Ps)", k, CHI, CHI2);
   10555             :     }
   10556             :   }
   10557         504 :   return R;
   10558             : }
   10559             : static GEN
   10560          35 : desc_hecke(GEN F)
   10561             : {
   10562             :   long n, N;
   10563          35 :   GEN D = gel(F,2);
   10564          35 :   if (typ(D) == t_VECSMALL) { N = D[3]; n = D[1]; }
   10565          14 :   else { GEN nN = gel(D,2); n = nN[1]; N = nN[2]; } /* half integer */
   10566          35 :   return gsprintf("T_%ld(%ld)(%Ps)", N, n, desc(gel(F,3)));
   10567             : }
   10568             : static GEN
   10569          98 : desc_linear(GEN FLD, GEN dL)
   10570             : {
   10571          98 :   GEN F = gel(FLD,2), L = gel(FLD,3), R = strtoGENstr("LIN([");
   10572          98 :   long n = lg(F) - 1, i;
   10573         168 :   for (i = 1; i <= n; i++)
   10574             :   {
   10575         168 :     R = shallowconcat(R, desc(gel(F,i))); if (i == n) break;
   10576          70 :     R = shallowconcat(R, strtoGENstr(", "));
   10577             :   }
   10578          98 :   return shallowconcat(R, gsprintf("], %Ps)", gdiv(L, dL)));
   10579             : }
   10580             : static GEN
   10581          21 : desc_dihedral(GEN F)
   10582             : {
   10583          21 :   GEN bnr = gel(F,2), D = nf_get_disc(bnr_get_nf(bnr)), f = bnr_get_mod(bnr);
   10584          21 :   GEN cyc = bnr_get_cyc(bnr);
   10585          21 :   GEN w = gel(F,3), chin = zv_to_ZV(gel(w,2)), o = utoi(gel(w,1)[1]);
   10586          21 :   GEN chi = char_denormalize(cyc, o, chin);
   10587          21 :   if (lg(gel(f,2)) == 1) f = gel(f,1);
   10588          21 :   return gsprintf("DIH(%Ps, %Ps, %Ps, %Ps)",D,f,cyc,chi);
   10589             : }
   10590             : 
   10591             : static void
   10592        1043 : unpack0