
// Make newform 21.30.g.a in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_21_g();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_21_g_Hecke();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_21_30_g_a();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose lines below.
// The precision argument determines an initial guess on how many Fourier coefficients to use.
// This guess is increased enough to uniquely determine the newform.
// To make the Hecke irreducible modular symbols subspace (type ModSym)
// containing the newform, type "MakeNewformModSym_21_30_g_a();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose line below.
// The default sign is -1.  You can change this with the optional parameter "sign".
function ConvertToHeckeField(input: pass_field := false, Kf := [])
    if not pass_field then
        poly := [1, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_21_g();"
function MakeCharacter_21_g()
    N := 21;
    order := 6;
    char_gens := [8, 10];
    v := [3, 5];
    // chi(gens[i]) = zeta^v[i]
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    F := CyclotomicField(order);
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),[F|F.1^e:e in v]);
    return MinimalBaseRingCharacter(chi);
end function;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_21_g_Hecke();"
function MakeCharacter_21_g_Hecke(Kf)
    N := 21;
    order := 6;
    char_gens := [8, 10];
    char_values := [[-1, 0], [0, 1]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := true, Kf := Kf); // the value of chi on the gens as elements in the Hecke field
    F := Universe(values);// the Hecke field
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),values);
    return chi;
end function;


function ExtendMultiplicatively(weight, aps, character)
    prec := NextPrime(NthPrime(#aps)) - 1; // we will able to figure out a_0 ... a_prec
    primes := PrimesUpTo(prec);
    prime_powers := primes;
    assert #primes eq #aps;
    log_prec := Floor(Log(prec)/Log(2)); // prec < 2^(log_prec+1)
    F := Universe(aps);
    FXY<X, Y> := PolynomialRing(F, 2);
    // 1/(1 - a_p T + p^(weight - 1) * char(p) T^2) = 1 + a_p T + a_{p^2} T^2 + ...
    R<T> := PowerSeriesRing(FXY : Precision := log_prec + 1);
    recursion := Coefficients(1/(1 - X*T + Y*T^2));
    coeffs := [F!0: i in [1..(prec+1)]];
    coeffs[1] := 1; //a_1
    for i := 1 to #primes do
        p := primes[i];
        coeffs[p] := aps[i];
        b := p^(weight - 1) * F!character(p);
        r := 2;
        p_power := p * p;
        //deals with powers of p
        while p_power le prec do
            Append(~prime_powers, p_power);
            coeffs[p_power] := Evaluate(recursion[r + 1], [aps[i], b]);
            p_power *:= p;
            r +:= 1;
        end while;    
    end for;
    Sort(~prime_powers);
    for pp in prime_powers do
        for k := 1 to Floor(prec/pp) do
            if GCD(k, pp) eq 1 then
                coeffs[pp*k] := coeffs[pp]*coeffs[k];
            end if;
        end for;
    end for;
    return coeffs;
end function;


function qexpCoeffs()
    // To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
    weight := 30;
    raw_aps := [[0, 0], [4782969, 4782969], [0, 0], [-1992649800898, 1488174302247], [0, 0], [11187825231754739, -22375650463509478], [0, 0], [-2616406980667028826, 1308203490333514413], [0, 0], [0, 0], [-3160519116279760931545, -3160519116279760931545], [0, -75141779735052686735461], [0, 0], [911042252840926977868645, 0], [0, 0], [0, 0], [0, 0], [-122754623854626450374476072, 61377311927313225187238036], [-351122832321908282397243581, 351122832321908282397243581], [0, 0], [1140449571604062559016808831, 1140449571604062559016808831], [0, -6390640410224343342500234297], [0, 0], [0, 0], [-44094116396974892046660986648, 88188232793949784093321973296], [0, 0], [250535793908186744202678279682, -125267896954093372101339139841], [0, 0], [442847331401621056509900333437, -442847331401621056509900333437], [0, 0], [4623573848372648622949761608471, 0], [0, 0], [0, 0], [10605847573749260358468928226683, -21211695147498520716937856453366], [0, 0], [78707687705223565244069535305996, -78707687705223565244069535305996], [7063285491916685015078195033412, 7063285491916685015078195033412], [0, -43507403624790537359621034149288], [0, 0], [0, 0], [0, 0], [138387169732939108790749732206885, -276774339465878217581499464413770], [0, 0], [-2386984309056564829663469955918025, 2386984309056564829663469955918025], [0, 0], [-1032821477589328192132025921610302, -1032821477589328192132025921610302], [-9410866156003929722395838615444144, 0], [10009018945203128672489887862993946, -20018037890406257344979775725987892], [0, 0], [33713488208931256513582777495028246, -16856744104465628256791388747514123], [0, 0], [0, 0], [21616148735259881546787512405814224, 21616148735259881546787512405814224], [0, 0], [0, 0], [0, 0], [0, 0], [-153237073724616919821916882672575020, 76618536862308459910958441336287510], [-95286206619781100462499533000933429, 95286206619781100462499533000933429], [0, 0], [-401939723396792206143097916436491051, -401939723396792206143097916436491051], [0, 0], [1324027595197852891299414315895106249, -2648055190395705782598828631790212498], [0, 0], [-3204672879281816442762355974056660166, 1602336439640908221381177987028330083], [0, 0], [0, -4932200609819832683914284680830720471], [6541623572862145729415318527580244595, 0], [0, 0], [-1219159494463644029660560694876868580, 2438318988927288059321121389753737160], [0, 0], [0, 0], [-7173708489371501674014965788541508939, -7173708489371501674014965788541508939], [0, -19694649609053488114558551202623787367], [-47696912411527330093483437127458384053, 0], [0, 0], [0, 0], [-105541625198653231771428677241777867218, 52770812599326615885714338620888933609], [0, 0], [27887619854549167115785858480305349297, 27887619854549167115785858480305349297], [0, 0], [-221956205944135543827266014837565388439, 0], [0, 0], [149637328072810697037528158675573523973, -299274656145621394075056317351147047946], [343017267927605173167249500113690029276, -171508633963802586583624750056845014638], [0, 0], [0, 0], [0, -344902551867121237232121238998272406559], [0, 0], [-537446176392047453459363724826430694547, 0], [0, 0], [0, 0], [1831476048247259962286326838201407146725, -1831476048247259962286326838201407146725], [0, 0], [0, 306879063681167218767612935917810242707], [0, 0], [0, 0], [0, 0], [5993206255861357018107915135830806461506, -2996603127930678509053957567915403230753], [0, -5314649433412880714608884267333097285169], [9995951922484701098314477375286138411960, 0], [0, 0], [0, 0], [0, 0], [18695916614073519593175032456023359039961, -18695916614073519593175032456023359039961], [-1346492967076622951364860307135306816821, -1346492967076622951364860307135306816821], [0, 0], [0, 0], [0, 0], [19403570165746135756312144610783146422599, -38807140331492271512624289221566292845198], [4074783322803499635272365436151750015294, -2037391661401749817636182718075875007647], [-19728708249040367229310145053126876693190, 19728708249040367229310145053126876693190], [0, 0], [32591234123792922772998048782874592387955, 32591234123792922772998048782874592387955], [-36089386452879449519184505939682348883076, 0], [0, 0], [-38806609650248725349089073158510194332579, 77613219300497450698178146317020388665158], [0, 0], [0, 0], [0, 0], [42672529856640346824454600809698430466131, 42672529856640346824454600809698430466131], [-174263911544191545392614305409756455337283, 0], [0, 0], [0, 0], [333554143809265785172211895516458919764842, -166777071904632892586105947758229459882421], [0, 0], [0, 92623712987403392561081007114827550306458], [0, 0], [26787045191351970154583623635556204810183, -53574090382703940309167247271112409620366], [-658735339023928188765556513113048377472902, 329367669511964094382778256556524188736451], [-765394192046947824482169097541810519157923, 765394192046947824482169097541810519157923], [0, 0], [0, -581384951712962893940193820022947603408909], [-1106612549947882874796066382536728721241226, 0], [0, 0], [809843275943842300454909352835340826386385, -1619686551887684600909818705670681652772770], [0, 0], [1105163046933396582409193960372961971635378, 1105163046933396582409193960372961971635378], [0, 0], [0, 0], [1436352495299807066005406164592454416371146, -2872704990599614132010812329184908832742292], [0, 0], [3738173010369741653560205370996646951986532, -3738173010369741653560205370996646951986532], [0, 0], [2224238180431856786354332760876569399082973, 2224238180431856786354332760876569399082973], [0, 0], [-3555354775205257517482643166647724968573313, 7110709550410515034965286333295449937146626], [0, 0], [5350953934178764043626611036236510019875380, -2675476967089382021813305518118255009937690], [0, 0], [0, -8478813305245622526029571039746142706737806], [0, 0], [-10085552230073112718301018862823725352175537, 0], [0, 0], [14112093398108954115434523044475644259981509, -14112093398108954115434523044475644259981509], [0, 0], [0, -8553530951964118288309247734244828798542771], [0, 0], [5139172037053642287174947118305073584944531, -10278344074107284574349894236610147169889062], [0, 0], [0, 0], [0, 0], [-32234468734627966084658946278837172636020431, 0], [0, 0], [0, 0], [0, 0], [55320133358125344711467201165305056768840587, -55320133358125344711467201165305056768840587], [28362491276812545655752611901377238018281437, 28362491276812545655752611901377238018281437]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_21_g_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_21_30_g_a();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose lines below.
// The precision argument determines an initial guess on how many Fourier coefficients to use.
// This guess is increased enough to uniquely determine the newform.
function MakeNewformModFrm_21_30_g_a(:prec:=2)
    chi := MakeCharacter_21_g();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 30));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(997) - 1;
    while true do
        trunc_vec := Vector(Kf, [0] cat [f_vec[i]: i in [1..prec]]);
        B := Basis(S, prec + 1);
        S_basismat := Matrix([AbsEltseq(g): g in B]);
        if Rank(S_basismat) eq Min(NumberOfRows(S_basismat), NumberOfColumns(S_basismat)) then
            S_basismat := ChangeRing(S_basismat,Kf);
            f_lincom := Solution(S_basismat,trunc_vec);
            f := &+[f_lincom[i]*Basis(S)[i] : i in [1..#Basis(S)]];
            return f;
        end if;
        error if prec eq maxprec, "Unable to distinguish newform within newspace";
        prec := Min(Ceiling(1.25 * prec), maxprec);
    end while;
end function;


// To make the Hecke irreducible modular symbols subspace (type ModSym)
// containing the newform, type "MakeNewformModSym_21_30_g_a();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose line below.
// The default sign is -1.  You can change this with the optional parameter "sign".
function MakeNewformModSym_21_30_g_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_21_g();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,30,sign)));
    Vf := Kernel([<2,R![0, 1]>],Snew);
    return Vf;
end function;