
// Make newform 6.20.a.c in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_6_a();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_6_20_a_c();".
// 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_6_20_a_c();".
// 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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_6_a();"
function MakeCharacter_6_a()
    N := 6;
    order := 1;
    char_gens := [5];
    v := [1];
    // 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;

function MakeCharacter_6_a_Hecke(Kf)
    return MakeCharacter_6_a();
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 := 20;
    raw_aps := [[512], [19683], [1953390], [40488776], [1916860452], [3132480182], [607659965586], [2507511106460], [-13588841327928], [-95129927516010], [-128131172993488], [-306931332936994], [1928507793837402], [-6036474006185788], [-2205402168106704], [30561502311272862], [51122948758448340], [-62140463356892698], [-137732367675340084], [-138268877451263208], [-331152017279915878], [1695082134983531840], [336651125543852892], [-2059907328938598390], [-2808994919565485854], [19531903731693852462], [-3674287816041089848], [23141591912259351396], [-39897930375880620010], [-20957704094346078318], [90547816212385552496], [171492709664478044172], [59941505890776027066], [392230890302705485700], [-821882363533783113570], [-210572041067477526088], [81905558171033533766], [791414037195956734412], [414346283896803868056], [298194411854743861542], [1700518489601167489500], [-2615702089046859998098], [-3890959059010748063808], [8917130713923092256962], [2910670657973018027406], [3126826693981862229800], [-8080912390182544528708], [-39534988354537861047088], [27705286064616523031436], [35696094880293116084030], [52410290796496322559162], [31425089174523636569040], [-32472727676384219278318], [-63852182683834079927628], [-74124440098612382134014], [18473449254545541997752], [-33562294209554737636890], [50853048944464083310112], [-20754374802473258256754], [-128770528821321387186678], [315394485630487642772852], [-258168364996002346429458], [-456109900831058575274404], [692775483507754167054792], [-151885727344146354936598], [-333119185964885738333514], [-180525791494985453220988], [-939972076555462187575054], [-2047290386979040332478764], [2125214653276460679466310], [934148944517861613244002], [2352882857248109665138680], [-757751785272330327439264], [139078095354219546050702], [1643835491801884055060660], [-1854253526657579171864448], [-1137348243356054900155410], [-8475611761077370863323434], [-8262814897621863265436718], [8407198043859830880141770], [3892146336901657941069900], [8635717195308839081416382], [14916467530166889311058672], [6100457146398549506330642], [3384973235843993843312120], [9740886135738487280290932], [30558213496592021515229730], [258039540749808148052186], [-31374242930565259518104058], [25641702665115515813304032], [28370115309766736746636956], [23782851411756721313357760], [-31967491718219990328789784], [-8385221705321107792371228], [14514663342212861147779100], [81011605380430572726544872], [-21028704659627892763717770], [106276352936500923649001082], [-21554345924832971880767068], [67730862798832895577391142], [-61865168218143574448592244], [-201945320291250873607305114], [-125429666565749348400270468], [215170874643597982682368650], [27832754953196021436494612], [91471370188025668486106306], [162458002804552219676502276], [363543915334744841352040242], [-125043904832638488426108600], [16623158268687453312880202], [-154786004403894449352243184], [-504580149659893465595850658], [175529278278621686225918586], [49014212878154005752460580], [322239541136087579484429272], [-65609215879014217965984798], [115304173338899164383143852], [-192052794835643624584271784], [535602663412618300672598022], [-330621194006930562213559620], [-736961711144925742087593298], [-862818386503082212312102558], [583017190504301979412383246], [-1237527087910074514158843708], [-414902810126488434955963588], [-1421973303569902595402456778], [1035446480382881478315174110], [-2019043109536643816268839280], [-1231319686266104046476606344], [-1890763671505922177845099738], [-1412308468650552728344603540], [603755075988515317655660952], [2521516499889606893313469472], [3732615935997455941765497326], [328160580090264639013325322], [4267867187099690761559132930], [-2846752829800625621920304178], [1969410597705972734162404316], [2619050659325268539936454966], [-2284092248196008281150549350], [1582878652663566137250989252], [3431095751433069561518461662], [-5856225416570204398894555048], [3986112449678772608261132436], [432633124080721347444379910], [9812798554705453503784575960], [7515126820351675229496341102], [-11539768112711854599620022294], [-759446170071372678951107500], [5042383175085968391312202272], [2272759420387819647112711286], [-14162371639247441700640581678], [7164192158170021193187227132], [-18072085261703171778291088824], [-9387815588781711799345085404], [11637984421758136172457419952], [24596768589052859078400218360], [1301868775391582193062435970], [-15177275004509428042252019014], [-16392533094677428881338329818], [-14435932016263649304589724964], [7490063501553865614341535882], [-13149597494766884385381867544], [-4500907081795313618330052828], [2029480310893314342604574226], [-16791005165064977087091441048], [500531252070376067548004912], [-29762810306838787691528192674]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_6_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_6_20_a_c();".
// 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_6_20_a_c(:prec:=1)
    chi := MakeCharacter_6_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 20));
    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_6_20_a_c();".
// 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_6_20_a_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_6_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,20,sign)));
    Vf := Kernel([<5,R![-1953390, 1]>],Snew);
    return Vf;
end function;