
// Make newform 9.18.a.b in Magma, downloaded from the LMFDB on 07 September 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_9_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_9_18_a_b();".
// 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_9_18_a_b();".
// 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_9_a();"
function MakeCharacter_9_a()
    N := 9;
    order := 1;
    char_gens := [2];
    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_9_a_Hecke( : Kf := Kf)
    return MakeCharacter_9_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]];
    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 := 18;
    raw_aps := [[528], [0], [1025850], [3225992], [753618228], [2541064526], [5429742318], [1487499860], [317091823464], [-2433410602590], [-8849722053088], [12691652946662], [-48864151002282], [-91019974317844], [49304994276048], [-22940453195766], [-32695090729980], [-1308285854869378], [5196143861984132], [3709489877412408], [3402372968272586], [2366533941308240], [29766750443172204], [-29167184100574170], [-63769879140957598], [160611451805102298], [-90713576977116184], [-195549453377774892], [213755725457651630], [281382909919711374], [-870305059081414528], [-131783402773788612], [-1871283274137719562], [1587062594616703820], [477239652427291050], [-3929641697032521448], [-2294533625364666658], [8010439791098248676], [8614773616496938968], [-2314298560280392686], [7903074249987272460], [14072859473096249462], [28250056704631311168], [49175488372560218306], [-12945762789989287302], [-55175476546424361400], [17350987521680017172], [94841481906128896736], [183781743854956721628], [-91490806306339102810], [-98749681248765635946], [-189336916815368475120], [-138762012936381922318], [334507667142851495748], [-49138214992960426242], [-17884459880036272776], [-115237132981956844110], [-147067104186007937008], [-178744304449573755178], [-280546008737900331162], [339876639409010699996], [764887057924736109594], [1384717113938486712692], [2347334666067782701128], [1584244383306594352826], [-898378316948672000382], [4004688355059819008012], [-1067532990477549662638], [1618407086246806533348], [-5600778231592951780450], [-5107740654588957108066], [-5849646150515287835880], [429534675949689694832], [-6547343306405273959114], [1518498474580113486140], [8045971998736996619904], [-6840403015095275072070], [6234580227124717073102], [2320475993929343865198], [23859040192122913461530], [26183873269354137542940], [11190291442195614141542], [-17429616774732221441712], [-45141263472119455403854], [38526562295180825585720], [3849658152391736013444], [2776139877731031705150], [-13413933814780880811958], [43272778781434817038578], [38572148086890114208976], [-49672422571695751923732], [42651283338417541270560], [-92812581655380263707288], [-1298768054727774133932], [-76678815414755957207500], [-66771244239038847108216], [115659056252100192799170], [179855143005181283152758], [93721058817507961337036], [-152396205092267358605218], [204978565536918064068452], [407359071384692843111058], [-221297011647026280041076], [-488685688747618423214010], [342217679946152388092], [-195910554940856667564478], [480676459929276985286388], [640803912479945537925294], [-360949848389335750147800], [653955034973087113930202], [408714909856448329911392], [180349262969745306233126], [-442851039023330128271082], [17109820895085667919660], [-282293119722601667674888], [377613197198070166747518], [-465535579479129734301244], [-1004115821043804976313352], [-126097968575809092924366], [1061050394008258728338220], [1575110657353559631476822], [291330459065178544769186], [-1338613730896911805912422], [1052954153732516111891604], [-57131090188152974822668], [318403448622788566564098], [2129965696288822312087430], [2342343870326118418709040], [3581853665345551458006872], [-3290125098345465523235554], [1741295714787442650819620], [4615441012904041337205144], [-81960658760876191583248], [-2516075900596800985213258], [527745046053123669017478], [-751718600857887036379390], [-2819136984810492489401286], [-3207324918299312882428588], [-2363283632908174339428702], [-9607213326730342456610730], [-6646164547259439360324628], [5094578017599003967163658], [5781978271536170124497336], [-8389068804767359260587772], [5027694796964745846020990], [-938327987125078323742920], [2331879916677175242636566], [10900835014363810390232358], [-14783787161334517448315620], [-5860871150063640239873376], [-10836378806680182976701778], [-5000536452902812944757362], [-1041083496983276703971404], [-14765133772179500888952312], [-299746871313495060713908], [1714306641421081714754928], [-24253856056624732047422440], [-14013758571322555974736290], [-24321734664731258383713238], [-598393654997457068793582], [-36247682041916308194776052], [18815658765322070204315334], [-1812116692416003354115768], [-20368467949393342030346892], [25259045550982007772760878], [1518395194029642475067304], [-6696496233346891897473568], [-34532574735545687248561498]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_9_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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