
// Make newform 9.12.a.a in Magma, downloaded from the LMFDB on 25 April 2024.
// 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_12_a_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_9_12_a_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
        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)
    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+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 := 12;
    raw_aps := [[-78], [0], [5370], [-27760], [-637836], [766214], [-3084354], [-19511404], [-15312360], [-10751262], [-50937400], [664740830], [-898833450], [-957947188], [1555741344], [-3792417030], [-555306924], [4950420998], [5292399284], [14831086248], [13971005210], [3720542360], [-8768454036], [25472769174], [-39092494846], [-9310781526], [48575052512], [-225595713276], [-69451179514], [359664575694], [250273055864], [97891780812], [155014706262], [-1126265370436], [1384580812362], [-698601450208], [2131269853958], [-163564486972], [8809425240], [730851575394], [-3923714421540], [2279308973150], [-3387088726656], [-4929207776254], [-6357850272342], [-3785535410272], [179493837380], [2225684379704], [415847834940], [18124772488334], [18764059878], [17625242631984], [-890116982734], [24228041356548], [-7804917351426], [-16595574775368], [-28523590257006], [23379994087976], [-30364120269058], [-15974859435594], [28704507156044], [-48175396168278], [25735029040676], [-34604265524376], [12606588910058], [81824296913634], [-34011469184548], [-59943948292558], [-97868505351324], [-142789606547098], [-144245920772994], [124349284943496], [-160109534089768], [-34725822875218], [-146500203674500], [-64341851639808], [39890033609466], [106551979242806], [-34144491481698], [53334901088234], [101287654153404], [-157928472303826], [-513171359936640], [-74924767954222], [-368335241090320], [111247830230916], [88331413841262], [-9420937722310], [697133548616898], [-187941271733848], [-320006635626996], [150382370364240], [176546273555504], [860958181676292], [601209457412516], [-1092030351829704], [-876371101334814], [371989239852150], [-973507481712628], [1746062781765590], [174624425985380], [-1583649005842494], [975797917848204], [-1924274391870426], [1611320399461196], [-2532503285394046], [3115075841122404], [2207230435400238], [-3411585709398408], [-1575452477398774], [-505592119502056], [-1983747166907074], [1930986428345862], [947688701475164], [1005926522765312], [-1131443807632530], [-868305558659068], [1376364875369256], [-2552758540391838], [-926792107209060], [-1903318977724498], [4929900506581730], [-430293314013702], [3812440532297652], [4037287750902356], [4712672943685602], [866706096098606], [-26266614058368], [-5680181884175392], [-1623115878038218], [3647940323190644], [-6760448268703752], [26483336560376], [7373643624794558], [-6209832596186874], [-8861954082034942], [2950474409673642], [13554522166415156], [15992249482082994], [-9458590804847370], [-10799627434960948], [11041089876780186], [12610352980858976], [786172343593716], [-5925892982962570], [-21020885793462840], [-19837829468018002], [-7966388437568826], [-15974892909578980], [4029257485765728], [9769575093688790], [-16258292298256914], [16606117429647572], [19131414356441544], [-10725166653853684], [28224866626031568], [18355082826013760], [11012509124864814], [8363567050915322], [-22294170304617630], [37803832630940892], [-4795681030490394], [33741958118885504], [-35858687142537900], [12002334917621406], [4426868496055080], [-37916708594080408], [57600280458878]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_9_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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